{% from "templates/initial/sros.j2" import if_name, declare_router with context %}
{% set pid = ospf.process|default(0) %}

{% macro ospf_config(af) %}
{% set ospfv = 'ospf3' if af=='ipv6' else 'ospf' %}

{% macro ospf_interface(l) %}
{{ declare_router(l,sub_path="/"+ospfv+"[ospf-instance="+pid|string + "]") }}
  val:
{% if ospf.reference_bandwidth is defined %}
   reference-bandwidth: {{ ospf.reference_bandwidth * 1000 }} # in kbps
{% endif %}
   admin-state: enable
   area:
   - area-id: "{{ l.ospf.area }}"
     interface:
     - interface-name: "{{ if_name(l.ifname) }}"
       passive: {{ l.ospf.passive }}
{%  if l.ospf.bfd|default(False) %}
       bfd-liveness: { }
{%  endif %}
       # mtu: 1500 # SRL defaults to 1500, now supporting global mtu settings
{# TODO: Add support for multi-area adjacencies and 'secondary' interface-type #}
{%  if l.ospf.network_type|default("") in ["broadcast","non-broadcast","point-to-point"] %}
       interface-type: "{{ l.ospf.network_type }}"
{%  endif %}
{%  if l.ospf.cost is defined %}
       metric: {{ l.ospf.cost }}
{%  endif %}
{% endmacro %}

{% set system_if = { 'ifname': "system", 'ospf': { 'passive': True, 'area': ospf.area } } %}
{% for l in interfaces|default([])+([system_if] if af in loopback else []) if 'ospf' in l %}
{{ ospf_interface(l)}}
{% endfor %}
{% endmacro %}

updates:
{% if ospf.af.ipv4 is defined %}
{{ ospf_config('ipv4') }}
{% endif %}
{% if ospf.af.ipv6 is defined %}
{{ ospf_config('ipv6') }}
{% endif %}
