#!/bin/sh

set -e

. ./helpers

ME=$(basename $0)

setup_sandbox

FRR=${SANDBOX}/out/leaf2/etc/frr/frr.conf
FRR_LEAF1=${SANDBOX}/out/leaf1/etc/frr/frr.conf

# The VLAN-enabled leaf must accept the BGP speaker sessions of both
# clusters: cl1's two network nodes on the legacy speaker VLAN
# (192.168.113), and cl3's three controllers on its transport VLAN
# (192.168.118). Only leaf2, the VLAN-enabled leaf, carries them.
run_setup start
assert_eq "ikvswitch-setup start rc" "${SETUP_RC}" "0"
for peer in 192.168.113.200 192.168.113.201 192.168.118.200 192.168.118.201 192.168.118.202 ; do
	assert_file_contains "leaf2 neighbor ${peer}" "${FRR}" "neighbor ${peer} remote-as external"
	assert_file_contains "leaf2 neighbor ${peer} activate" "${FRR}" "neighbor ${peer} activate"
	assert_file_contains "leaf2 neighbor ${peer} vm-in" "${FRR}" "neighbor ${peer} route-map vm-in in"
	assert_file_contains "leaf2 neighbor ${peer} vm-out" "${FRR}" "neighbor ${peer} route-map vm-out out"
done
assert_file_not_contains "leaf1 no speaker peer" "${FRR_LEAF1}" "neighbor 192.168.118.200 remote-as external"
assert_file_not_contains "leaf1 no legacy peer" "${FRR_LEAF1}" "neighbor 192.168.113.200 remote-as external"

# A configuration file predating the cl3 transport VLAN (the
# VLAN_SBUNET6_* to VLAN_SBUNET8_* variables missing) must keep the
# cl1 speaker peers, and must not emit malformed "neighbor ." peer
# statements for the cl3 controllers.
sed -i -e '/^VLAN_SBUNET6_PREFIX=/d' -e '/^VLAN_SBUNET7_PREFIX=/d' -e '/^VLAN_SBUNET8_PREFIX=/d' ${SANDBOX}/etc/ikvswitch.conf
run_setup start
assert_eq "start with an old conf rc" "${SETUP_RC}" "0"
assert_file_contains "old conf keeps cl1 peer 200" "${FRR}" "neighbor 192.168.113.200 remote-as external"
assert_file_contains "old conf keeps cl1 peer 201" "${FRR}" "neighbor 192.168.113.201 remote-as external"
for bad in "neighbor .200 remote-as external" "neighbor .201 remote-as external" "neighbor .202 remote-as external" "neighbor .200 activate" "neighbor .201 activate" "neighbor .202 activate" ; do
	assert_file_not_contains "old conf no [${bad}]" "${FRR}" "${bad}"
done

exit 0
