#!/bin/sh

set -e

. ../helpers

ME=$(basename $0)

setup_sandbox

FRR_INET=${SANDBOX}/out/internet/etc/frr/frr.conf
FRR_SPINE=${SANDBOX}/out/spine1/etc/frr/frr.conf
FRR_LEAF=${SANDBOX}/out/leaf1/etc/frr/frr.conf

# The internet switch holds the static route for the whole delegated
# block, pointing back at the host bridge's gateway address, anchored
# on NET1_V6 which carries the block's trailing nibble (fd00:1:1 + "0"
# = fd00:1:10: a /59 built from the bare base fd00:1:1 would not be
# canonical). The prefix-list and the map-redistribute permit are in
# the shared SPINE/INTERNET template: inert on the spines, which hold
# no matching static, exactly like the ::/0 handling.
run_setup start
assert_eq "ikvswitch-setup start rc" "${SETUP_RC}" "0"

# The v6 address family must redistribute the kernel table as well:
# the internet switch's only ::/0 is its kernel route via the host
# server, and without this redistribute the guest machines never
# receive a usable default route over BGP (the v4 family already
# redistributes the kernel table).
assert_eq "internet frr v6 redistribute kernel count" \
	"$(grep -c 'redistribute kernel route-map map-redistribute' ${FRR_INET})" "2"

assert_file_contains "internet frr static route" "${FRR_INET}" "ipv6 route fd00:1:10::/59 fd00:1:10::1"
assert_file_contains "internet frr prefix-list" "${FRR_INET}" "ipv6 prefix-list pl-delegated6 seq 5 permit fd00:1:10::/59"
assert_file_contains "internet frr permit 40" "${FRR_INET}" "route-map map-redistribute permit 40"
assert_file_contains "internet frr permit 40 match" "${FRR_INET}" "match ipv6 address prefix-list pl-delegated6"

# The spines inherit the prefix-list and the permit from the shared
# template, but never the static route itself.
assert_file_contains "spine frr prefix-list" "${FRR_SPINE}" "ipv6 prefix-list pl-delegated6 seq 5 permit fd00:1:10::/59"
assert_file_contains "spine frr permit 40" "${FRR_SPINE}" "route-map map-redistribute permit 40"
assert_file_not_contains "spine frr no static route" "${FRR_SPINE}" "ipv6 route fd00:1:10::/59"

# The leafs' template still holds no static route for the block: they
# learn it through BGP. They now carry the pl-delegated6 prefix-list
# as the deny clause of their machine-facing import guard, but never
# originate the block themselves.
assert_file_not_contains "leaf frr no static route" "${FRR_LEAF}" "ipv6 route fd00:1:10::/59"
assert_file_contains "leaf frr guard prefix-list" "${FRR_LEAF}" "ipv6 prefix-list pl-delegated6 seq 5 permit fd00:1:10::/59"

# A user-owned base override must flow through to the static route
# and the prefix-list unchanged in shape. The base is user-owned and
# defined nowhere in the stock file, so the appended value wins.
cat >>${SANDBOX}/etc/ikvswitch-local.conf <<EOF
HOST_VIRTUAL_SUBNET_START_PREFIX_V6=fd00:2:1
EOF
run_setup start
assert_eq "start with overridden base rc" "${SETUP_RC}" "0"
assert_file_contains "internet frr static route overridden" "${FRR_INET}" "ipv6 route fd00:2:10::/59 fd00:2:10::1"
assert_file_contains "internet frr prefix-list overridden" "${FRR_INET}" "ipv6 prefix-list pl-delegated6 seq 5 permit fd00:2:10::/59"

exit 0
