#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2025 Christophe Fontaine

#                                                     .--------------------.
#                                                     |  netns "ospf-peer" |
#  .--------..------------.                           |        .-------.   |
#  | zebra  ||   grout    |                           |        | ospfd |   |
#  '--------'|            |                           |        '-------'   |
#  .-------. |      .------------.             .------------. .-------.    |
#  | ospfd | |      |     p0     |   net_tap   |    x-p0    | | zebra |    |
#  '-------' |      |            +-------------+            | '-------'    |
#         .------.  | 172.16.0.1 |             | 172.16.0.2 |.----------.  |
#         | main |  '------------'             '------------'|    lo    |  |
#         '------'       |                            |      |          |  |
#            |  ping <-------------------------------------> | 16.0.0.1 |  |
#            |           |                            |      '----------'  |
#            '-----------'                            '--------------------'

. $(dirname $0)/_init_frr.sh

create_interface p0
set_ip_address p0 172.16.0.1/24

start_frr ospf-peer 0
move_to_netns x-p0 ospf-peer

# Configure Grout FRR instance
vtysh <<-EOF
configure terminal
ip router-id 172.16.0.1
!
debug ospf event
debug ospf packet all
!
interface lo
	ip address 17.0.0.1/24
exit
!
interface p0
	ip ospf hello-interval 1
	ip ospf network point-to-point
exit
!
!
router ospf
	ospf router-id 172.16.0.1
	network 172.16.0.0/24 area 10.0.0.1
	network 17.0.0.0/24 area 10.0.0.1
exit
!
EOF

vtysh -N ospf-peer <<-EOF
configure terminal
ip router-id 172.16.0.2
!
debug ospf event
debug ospf packet all
!
interface lo
	ip address 16.0.0.1/24
exit
!
interface x-p0
	ip address 172.16.0.2/24
	ip ospf hello-interval 1
	ip ospf network point-to-point
exit
!
router ospf
	ospf router-id 172.16.0.2
	network 172.16.0.0/24 area 10.0.0.1
	network 16.0.0.0/24 area 10.0.0.1
exit
end
!
EOF

attempts=60
while ! vtysh -c 'show ip ospf neighbor json' | jq '.neighbors."172.16.0.2"[0].converged == "Full"' -e ; do
	sleep 1
	if [ "$attempts" -le 0 ]; then
		fail "OSPF failed to connect to neighbor."
	fi
	attempts=$((attempts - 1))
done

wait_event -t 60 'route4 add: vrf=main 16.0.0.1/32 origin=ospf'

grcli ping 16.0.0.1 count 1
