#!/bin/bash
#
# Exit immediately when any command fails
set -e 
#
{% if vrfs is defined %}
cat >/etc/network/interfaces.d/65-evpn-vxlan.intf <<CONFIG
{% for vname,vdata in vrfs.items() if vdata.evpn.transit_vni is defined %}
{%   set vni=vdata.evpn.transit_vni %}
{%   set vlan=evpn._start_transit_vlan + vdata.vrfidx %}
auto tvni-{{ vni }}
iface tvni-{{ vni }}
    vxlan-id {{ vni }}
    vxlan-learning no
    bridge-access {{ vlan }}
#
{% endfor %}
{% for vname,vdata in vrfs.items() if vdata.evpn.transit_vni is defined %}
{%   set vni=vdata.evpn.transit_vni %}
{%   set vlan=evpn._start_transit_vlan + vdata.vrfidx %}
{%   if loop.first %}
iface bridge
{%   endif %}
    bridge-vids {{ vlan }}
    bridge-ports tvni-{{ vni }}
{% endfor %}
#
{% for vname,vdata in vrfs.items() if vdata.evpn.transit_vni is defined %}
{%   set vlan=evpn._start_transit_vlan + vdata.vrfidx %}
auto vlan{{ vlan }}
iface vlan{{ vlan }}
    vrf {{ vname }}
    vlan-id {{ vlan }}
    vlan-raw-device bridge
#
{% endfor %}
CONFIG
ifreload -a
#
{% endif %}
cat >/tmp/evpn_config <<CONFIG
{% include "frr.evpn-config.j2" %}
CONFIG
vtysh -f /tmp/evpn_config
exit $?
