{% macro bgp_config(vrf_bgp,vrf=None) %}
{% set path = "router[router-name=Base]" if not vrf else "service/vprn[service-name="+vrf+"]" %}
- path: configure/{{ path }}
  val:
   router-id: "{{ vrf_bgp.router_id|default(bgp.router_id) }}"
   autonomous-system: {{ bgp.as }}
   bgp:
    ebgp-default-reject-policy:
     import: False
     export: False
{% if not vrf %}
    vpn-apply-export: True
    vpn-apply-import: True
{% endif %}
    multipath:
     ibgp: {{ 16 if 'ixr' in clab.type else 64 }}
     ebgp: {{ 16 if 'ixr' in clab.type else 64 }}
    min-route-advertisement: 1 # Be aggressive about sending updates
    connect-retry: 5           # Retry frequently, suitable in DC context
    client-reflect: {{ (not vrf) and bgp.rr|default('')|bool }}
    # family: cannot disable this
    # ipv4: False # Enabled by default, disable globally and set per group

{% macro bgp_families(neighbor,ipv4=True,ipv6=True) %}
   family:
{% set activate = neighbor.activate|default( {'ipv4': True,'ipv6': True } ) %}
    ipv4: {{ activate.ipv4|default(False) and ipv4 }}
    ipv6: {{ activate.ipv6|default(False) and ipv6 }}
{%  if 'evpn' in neighbor and neighbor.evpn %}
    evpn: True # Must have at least 1 address family enabled
{%  endif %}
{% endmacro %}

{% macro bgp_peer_group(name,type,neighbor,transport_ip) %}
- path: configure/{{ path }}/bgp/group[group-name={{name}}]
  val:
   admin-state: enable
{{ bgp_families(neighbor) }}
   import:
    policy: ["accept_all"]
   export:
    policy: [ "{{ 'next-hop-self-ebgp-routes-only' if 'ibgp' in type and bgp.next_hop_self|default(False) else 'accept_all' }}" ]

{% if bgp.community[ type ]|default([])|length < 2 %}
   send-communities:
{%  for c,vals in {'standard': ['standard','large'],'extended':['extended'] }.items() %}
{%   if c not in bgp.community[ type ] %}
{%    for knob in vals %}
    {{ knob }}: False  # True value not allowed, on by default
{%    endfor %}
{%   endif %}
{%  endfor %}
{% endif %}
{% if transport_ip %}
   local-address: "{{ transport_ip }}"
{%  if bgp.rr|default('')|bool %}
   cluster:
    cluster-id: "{{ bgp.rr_cluster_id|default(False) or vrf_bgp.router_id|default(bgp.router_id) }}"
{%  endif %}
{% endif %}
{% endmacro %}

{% for n in vrf_bgp.neighbors %}
{%  for af in ['ipv4','ipv6'] if n[af] is defined %}

{%   if n[af] is string %}
{# (Re)create peer group #}
{%    set peer_group = 'ebgp' if n.type=='ebgp' else 'ibgp-local-as' if n.type=='localas_ibgp' else ('ibgp-'+af) %}
{%    set transport_ip = loopback[af]|ipaddr('address') if af in loopback and n.type=='ibgp' else None %}
{{    bgp_peer_group(peer_group,'ibgp' if 'ibgp' in n.type else 'ebgp',n,transport_ip) }}

- path: configure/{{ path }}/bgp
  val:
   neighbor:
   - ip-address: "{{ n[af] }}"
     description: "{{ n.name }}"
     peer-as: {{ n.as }}
     group: "{{ peer_group }}"
  {{ bgp_families(n,ipv4=(af=='ipv4' or 'ipv4' not in n),ipv6=(af=='ipv6')) | indent(2) }}

{%    if n.local_as is defined %}
     local-as:
      as-number: {{ n.local_as }}
      prepend-global-as: {{ not n.replace_global_as|default(True) }} # Don't include iBGP global AS in eBGP advertisements
{%    endif %}

{%   else %}
{# TODO BGP unnumbered #}
{%   endif %}
{%  endfor %}
{% endfor %}

{% endmacro %}
