{% macro bfd_params(enable) %}
   admin-state: "{{ 'enable' if enable else 'disable' }}"
{% if enable %}
   receive: {{ bfd.min_rx }}
   transmit-interval: {{ bfd.min_tx }}
   multiplier: {{ bfd.multiplier }} # range 1..20
{% if bfd.min_echo_rx != 0 %}
   echo-receive: {{ bfd.min_echo_rx }}
{% else %}
   # TODO delete this setting, default is 100ms
{% endif %}
{% endif %}
{% endmacro %}

{% macro bfd_decl(ifname,l) %}
{% set bfd_v4 = l.isis.bfd.ipv4|default(False) if 'isis' in l and 'bfd' in l.isis else
               (l.ospf.bfd if 'ospf' in l and 'bfd' in l.ospf else False) %}
{% set isis_bfd_v6 = l.isis.bfd.ipv6|default(False) if 'isis' in l and 'bfd' in l.isis else False %}
{% if l.ipv4|default('')|ipv4 and bfd_v4 %}
- path: configure/router[router-name=Base]/interface[interface-name={{ifname}}]/ipv4/bfd
  val:
{{ bfd_params( 'bfd' not in l or l.bfd ) }}
{% endif %}
{% if (l.ipv6|default('')|ipv6 or l.unnumbered|default(0)) and isis_bfd_v6 %}
- path: configure/router[router-name=Base]/interface[interface-name={{ifname}}]/ipv6/bfd
  val:
{{ bfd_params( 'bfd' not in l or l.bfd ) }}
{% endif %}
{% endmacro %}

replace:
{# { bfd_decl('system',loopback) } #}
{% for l in interfaces|default([]) if bfd is defined or l.bfd|default(False) %}
{{ bfd_decl('i'+l.ifname,l) }}
{% endfor %}
