name: {{ name }}

topology:
  nodes:
{% for name,n in nodes.items() %}
    {{ name }}:
      kind: {{ n.kind | default(n.device) }}
{%    if 'type' in n %}
      type: {{ n.type }}
{%    endif %}
      image: {{ n.clab['image'] if 'image' in n.clab|default({}) else n.box }}
{%    if groups is defined %}
      group: {% for g in groups if n.name in groups[g].members %}{{'' if loop.first else ','}}{{g}}{% endfor %}
{%    endif %}
{% if 'srl-agents' in n.clab %}
      extras:
        srl-agents: {{ n.clab['srl-agents'] }}
{% endif %}

{% if n.binds %}
      binds:
{% for b in n.binds %}
      - {{ b }}
{% endfor %}
{% endif %}
{% if 'runtime' in n %}
      runtime: {{ n.runtime }}
{% endif %}
{% if 'license' in n %}
      license: {{ n.license }}
{% endif %}
{% endfor %}

{% if links %}
{# Define bridges for links with less (stub) or more than 2 nodes #}
{% set bridge_type = defaults.providers.clab.bridge_type|default('bridge') %}
{% for b in links|rejectattr('node_count','eq',2)|map(attribute='bridge')|unique %}
    {{ b }}:
      kind: {{ bridge_type }}
{% endfor %}

  links:
{% for l in links %}
{% if not l.bridge or l.node_count==2 %}
  - endpoints:
{%   for n in nodes.values() %}
{%     for nl in n.interfaces if nl.linkindex == l.linkindex %}
    - "{{ n.name }}:{{ nl.provider_ifname if 'provider_ifname' in nl else nl.ifname }}"
{%     endfor %}
{%   endfor %}
{% else %}
{%   for n in nodes.values() %}
{%     for nl in n.interfaces if nl.linkindex == l.linkindex %}
  - endpoints:
    - "{{ n.name }}:{{ nl.provider_ifname if 'provider_ifname' in nl else nl.ifname }}"
    - "{{ l.bridge }}:{{ n.name }}_{{ nl.provider_ifname if 'provider_ifname' in nl else nl.ifname }}"
{%     endfor %}
{%   endfor %}
{% endif %}
{% endfor %}
{% endif %}
