#!/bin/bash

# test for FDP-1454: run server.sh on host1 and client.sh on host2
# then,  on host1 try to ping from vm1 to vm2
# Successful ping in fixed versions

SERVER_CHASSIS="hv1"   #"ovn-chassis-1"
CLIENT_CHASSIS="hv2"   #"ovn-gw-1"
ROUTER="gw-router"
PHYS_NIC="ens7f0np0"   # physical nic connected the other node

systemctl start openvswitch                                                                                     
systemctl start ovn-northd                                                                                      
ovn-nbctl set-connection ptcp:6641                                                                              
ovn-sbctl set-connection ptcp:6642                                                                              
ovs-vsctl set open . external_ids:system-id=$SERVER_CHASSIS \
        external_ids:ovn-remote=tcp:20.0.86.25:6642 \
        external_ids:ovn-encap-type=geneve \
        external_ids:ovn-encap-ip=20.0.86.25
systemctl restart ovn-controller

# Create public bridge with IP and physical NIC
ovs-vsctl add-br br-public
ip addr add 20.0.86.25/24 dev br-public
ip link set br-public up
ovs-vsctl add-port br-public $PHYS_NIC -- set interface $PHYS_NIC type=system

# Create logical internal switch
ovn-nbctl ls-add ls-local
ovn-nbctl lsp-add ls-local vm1
ovn-nbctl lsp-set-addresses vm1 "00:00:00:00:01:02 10.0.0.2 10::2"
ovn-nbctl lsp-set-port-security vm1 "00:00:00:00:01:02 10.0.0.2"

#Create logical public switch
ovn-nbctl ls-add ls-public
ovn-nbctl lsp-add ls-public vm2 
ovn-nbctl lsp-set-addresses vm2 "00:00:00:00:02:02 20.0.0.2 20::2"
ovn-nbctl lsp-set-port-security vm2 "00:00:00:00:02:02 20.0.0.2"

# Create local net port
ovn-nbctl lsp-add ls-public ln-public
ovn-nbctl lsp-set-type ln-public localnet
ovn-nbctl lsp-set-addresses ln-public unknown
ovn-nbctl lsp-set-options ln-public network_name=public

# Create router with reside_on_redirect_chassis=false (triggers FDP-1454 bug)
# Gateway router port will be on hv0 (CLIENT_CHASSIS)
# VMs will be on hv1 (SERVER_CHASSIS)
ovn-nbctl lr-add $ROUTER
ovn-nbctl set logical_router $ROUTER options:reside_on_redirect_chassis=false
#ovn-nbctl set logical_router $ROUTER options:chassis=$CLIENT_CHASSIS

ovn-nbctl lrp-add $ROUTER ro-local 00:00:00:00:01:01 10.0.0.1/24 10::1/64
ovn-nbctl lsp-add ls-local local-ro
ovn-nbctl lsp-set-type local-ro router
ovn-nbctl lsp-set-addresses local-ro router
ovn-nbctl lsp-set-options local-ro router-port=ro-local

ovn-nbctl lrp-add $ROUTER ro-public 00:00:00:00:02:01 20.0.0.1/24 20::1/64
ovn-nbctl lsp-add ls-public public-ro
ovn-nbctl lsp-set-type public-ro router
ovn-nbctl lsp-set-addresses public-ro router
ovn-nbctl lsp-set-options public-ro router-port=ro-public

# Set gateway chassis to hv0 (CLIENT_CHASSIS)
ovn-nbctl lrp-set-gateway-chassis ro-public $CLIENT_CHASSIS
ovn-nbctl lrp-set-options ro-public redirect-type=bridged

# Setup bridge mappings for public network
ovs-vsctl set open . external-ids:ovn-chassis-mac-mappings="public:aa:bb:cc:dd:01:01"
ovs-vsctl set open . external_ids:ovn-bridge-mappings=public:br-public

# Bind VMs to server chassis (hv1)
ovn-nbctl lsp-set-options vm1 requested-chassis=$SERVER_CHASSIS
ovn-nbctl lsp-set-options vm2 requested-chassis=$SERVER_CHASSIS

# Create namespace for VM1
ip netns del vm1 2>/dev/null || true
ip netns add vm1
ovs-vsctl --may-exist add-port br-int vm1 -- set interface vm1 type=internal
ip link set vm1 netns vm1
ip netns exec vm1 ip link set vm1 address 00:00:00:00:01:02
ip netns exec vm1 ip addr add 10.0.0.2/24 dev vm1
ip netns exec vm1 ip link set vm1 up
ip netns exec vm1 ip route add default via 10.0.0.1
ovs-vsctl set interface vm1 external-ids:iface-id=vm1

# Create namespace for VM2
ip netns del vm2 2>/dev/null || true
ip netns add vm2
ovs-vsctl --may-exist add-port br-int vm2 -- set interface vm2 type=internal
ip link set vm2 netns vm2
ip netns exec vm2 ip link set vm2 address 00:00:00:00:02:02
ip netns exec vm2 ip addr add 20.0.0.2/24 dev vm2
ip netns exec vm2 ip link set vm2 up
ip netns exec vm2 ip route add default via 20.0.0.1
ovs-vsctl set interface vm2 external-ids:iface-id=vm2

ovn-nb --wait=hv
