{#
  Define a BGP neighbor
#}
{% macro neighbor(n,ip,bgp,af) %}
{%   set peer = ip if ip is string else 'interface ' + n.local_if|default('?') %}
!
  neighbor {{ peer }}
{%   if peer!=n[af] %}
! This is an unnumbered eBGP session
! WTF Remote-AS configuration not supported for unnumbered peer
    inherit template unnumbered_ebgp inherit-type ebgp
{%   else %}
    remote-as {{ n.as }}
{%   endif %}
    
    description "{{ n.name }}"

{%     if n.local_as is defined %}
    local-as {{ n.local_as }} no-prepend replace-as
{%     endif %}

{%     if n.type == 'ibgp' %}
    update-source loopback0
{%       if bgp.next_hop_self is defined and bgp.next_hop_self %}
{#
  In Dell OS10, next-hop-self is configured under AF
#}
{%         for af in ['ipv4','ipv6'] if bgp[af] is defined and n.activate[af] is defined and n.activate[af] %}
!
 address-family {{ af }} unicast
   next-hop-self
 exit
{%         endfor %}
{%       endif %}

{%       if bgp.rr|default('') and not n.rr|default('') %}
    route-reflector-client
{%       endif %}
{%       if bgp.community.ibgp|default([]) %}
{%         if "extended" in bgp.community.ibgp|default([]) %}
    send-community extended
{%         else %}
    send-community standard
{%         endif %}
{%       endif %}
{%   else %}
{%       if bgp.community.ebgp|default([]) %}
{%         if "extended" in bgp.community.ebgp|default([]) %}
    send-community extended
{%         else %}
    send-community standard
{%         endif %}
{%       endif %}
{%     endif %}

{% for af in ['ipv4','ipv6'] if bgp[af] is defined and n.activate[af] is defined and n.activate[af] %}
!
 address-family {{ af }} unicast
   soft-reconfiguration inbound
   activate
 exit
{% endfor %}

{#
  And now, **WTF**, since Dell OS10 cannot disable ipv4 AF by default, we need to:
#}
{% if n.activate['ipv4'] is not defined or (n.activate['ipv4'] is defined and not n.activate['ipv4']) %}
!
 address-family ipv4 unicast
   no activate
 exit
{% endif %}
  
  no shutdown
  exit
{%- endmacro %}
