{% macro community(list) -%}
{{ "both" if list|length >= 2 else list[0] }}
{%- endmacro %}
!
router bgp {{ bgp.as }}
  no bgp ebgp-requires-policy
  no bgp default ipv4-unicast
  bgp default show-hostname
  bgp default show-nexthop-hostname
{% if bgp.router_id|ipv4 %}
  bgp router-id {{ bgp.router_id }}
{% endif %}
{% if bgp.rr|default(False) and bgp.rr_cluster_id|default(False) %}
  bgp cluster-id {{ bgp.rr_cluster_id }}
{% endif %}
!
{% for af in ['ipv4','ipv6'] %}
{%   for n in bgp.neighbors if n[af] is defined %}
  neighbor {{ n[af] }} remote-as {{ n.as }}
  neighbor {{ n[af] }} description {{ n.name }}
{%     if n.type == 'ibgp' %}
  neighbor {{ n[af] }} update-source lo0
{%     elif n.local_as is defined %}
  neighbor {{ n[af] }} local-as {{ n.local_as }}
{%     endif %}
!
{%   endfor %}
{% endfor %}
{% for af in ['ipv4','ipv6'] if bgp[af] is defined %}
 address-family {{ af }} unicast
!
{%   if loopback[af] is defined and bgp.advertise_loopback %}
  network {{ loopback[af]|ipaddr(0) }}
{%   endif %}
!
{%   for l in interfaces|default([]) if l.bgp.advertise|default("") and l[af] is defined and not 'vrf' in l %}
  network {{ l[af]|ipaddr(0) }}
{%   endfor %}
!
{%   for pfx in bgp.originate|default([]) if af == 'ipv4' %}
  network {{ pfx|ipaddr('0') }}
{%   endfor %}
!
{%   for n in bgp.neighbors if n[af] is defined %}
  neighbor {{ n[af] }} activate
{%     if n.type == 'ibgp' %}
{%       if bgp.next_hop_self is defined and bgp.next_hop_self %}
  neighbor {{ n[af] }} next-hop-self
{%       endif %}
{%       if bgp.rr|default('') and not n.rr|default('') %}
  neighbor {{ n[af] }} route-reflector-client
{%       endif %}
{%       if bgp.community.ibgp|default([]) %}
  neighbor {{ n[af] }} send-community {{ community(bgp.community.ibgp) }}
{%       endif %}
{%     else %}
{%       if bgp.community.ebgp|default([]) %}
  neighbor {{ n[af] }} send-community {{ community(bgp.community.ebgp) }}
{%       endif %}
{%     endif %}
!
{%   endfor %}
{% endfor %}
!
{% for pfx in bgp.originate|default([]) %}
ip route {{ pfx|ipaddr('0') }} Null0
{% endfor %}
