#!/bin/sh

set -e

. ../helpers

ME=$(basename $0)

setup_sandbox

# The chassis VLAN (default 20) must be emitted on every leaf switch,
# next to the management VLAN: one subinterface per server-facing
# port, and one unnumbered peer-group neighbor, so that OVN-mode
# computes can peer their per-chassis router VRF (eg: ovnvrf42)
# through one OVS bridge per NIC (br-bgp0/br-bgp1).
run_setup start
assert_eq "ikvswitch-setup start rc" "${SETUP_RC}" "0"
for LEAF in 1 2 3 4 5 6 ; do
	IFACE=${SANDBOX}/out/leaf${LEAF}/etc/network/interfaces
	FRR=${SANDBOX}/out/leaf${LEAF}/etc/frr/frr.conf
	assert_file_contains "leaf${LEAF} chassis subif auto" "${IFACE}" "auto enp0s6.20"
	assert_file_contains "leaf${LEAF} chassis subif iface" "${IFACE}" "iface enp0s6.20 inet manual"
	assert_file_contains "leaf${LEAF} chassis neighbor" "${FRR}" "neighbor enp0s6.20 interface peer-group leaf"
	assert_file_contains "leaf${LEAF} mgmt subif kept" "${IFACE}" "iface enp0s6.10 inet manual"
done

# A configuration file predating the knob must not emit malformed
# subinterface or neighbor statements (eg: "enp0s6." with an empty
# VLAN ID), and must keep the management VLAN.
sed -i '/^LEAF_TO_VM_CHASSIS_VLAN_NUM=/d' ${SANDBOX}/etc/ikvswitch.conf
run_setup start
assert_eq "start with an old conf rc" "${SETUP_RC}" "0"
IFACE=${SANDBOX}/out/leaf1/etc/network/interfaces
FRR=${SANDBOX}/out/leaf1/etc/frr/frr.conf
assert_file_not_contains "old conf no malformed subif" "${IFACE}" "iface enp0s6. inet manual"
assert_file_not_contains "old conf no malformed neighbor" "${FRR}" "neighbor enp0s6. interface"
assert_file_contains "old conf keeps mgmt subif" "${IFACE}" "iface enp0s6.10 inet manual"
assert_file_contains "old conf keeps mgmt neighbor" "${FRR}" "neighbor enp0s6.10 interface peer-group leaf"

# A deployment choosing another VLAN ID gets exactly that ID, and not
# the default.
reset_conf
sed -i 's/^LEAF_TO_VM_CHASSIS_VLAN_NUM=.*/LEAF_TO_VM_CHASSIS_VLAN_NUM=42/' ${SANDBOX}/etc/ikvswitch.conf
run_setup start
assert_eq "start with a custom VID rc" "${SETUP_RC}" "0"
assert_file_contains "custom VID subif" "${IFACE}" "iface enp0s6.42 inet manual"
assert_file_contains "custom VID neighbor" "${FRR}" "neighbor enp0s6.42 interface peer-group leaf"
assert_file_not_contains "custom VID no default 20" "${IFACE}" "enp0s6.20"

exit 0
