{#
   BGP neighbor definition
#}
{% macro neighbor(n,ip,bgp,instance) %}

{%   set neigh_id = ip|replace('.', '_')|replace(':', '_') %}
{%   set afi_list = [] %}
{%   if n['ipv4'] is defined %}
{{ afi_list.append('ip') }}
{%  endif %}
{%   if n['ipv6'] is defined %}
{{ afi_list.append('ipv6') }}
{%   endif %}


/routing/bgp/connection add template={{instance}} name={{ neigh_id }} remote.address={{ ip }} remote.as={{ n.as }} comment="{{ n.name }}" address-families={{ afi_list|join(',') }} local.role=ebgp

{%   if n.type == 'ibgp' %}
/routing/bgp/connection set [/routing/bgp/connection find name={{ neigh_id }}] local.role=ibgp local.address=loopback

{%     if n.rr|default('') %}
/routing/bgp/connection set [/routing/bgp/connection find name={{ neigh_id }}] local.role=ibgp-rr-client
{%     endif %}

{%     if bgp.rr|default('') and not n.rr|default('') %}
/routing/bgp/connection set [/routing/bgp/connection find name={{ neigh_id }}] local.role=ibgp-rr
{%     endif %}

{%   endif %}

{%    if bgp.next_hop_self is defined and bgp.next_hop_self %}
/routing/bgp/connection set [/routing/bgp/connection find name={{ neigh_id }}] nexthop-choice=force-self
{%    endif %}

{%- endmacro %}

{#
   BGP network statement
#}
{% macro bgp_network(af,pfx) %}
# BGP Network now is controlled by simple ACL
/ip/firewall/address-list/ add list=bgp-networks address={{ pfx|ipaddr('0') }}
{%- endmacro %}
