#!/bin/bash -xe

test -t 1 && USE_TTY="t"

function container_exec() {
    container=$1
    args=$2
    podman exec "-i$USE_TTY" "$container" /bin/bash -c "$args"
}

# create_fake_vm ovn-chassis-1 vm0 00:00:00:20:00:00
# create_fake_vm ovn-chassis-1 vm0 00:00:00:20:00:00 br-ex
function create_fake_vm() {
    container=$1
    name=$2
    mac=$3
    bridge=${4:-"br-int"}

    container_exec $container "ovs-vsctl \
          -- add-port $bridge $name \
          -- set interface $name type=internal"

    container_exec $container "ip netns add $name"
    container_exec $container "ip netns exec $name ip link set lo up"

    container_exec $container "ip link set $name netns $name"
    container_exec $container "ip netns exec $name ip link set $name address $mac"
    container_exec $container "ip netns exec $name ip link set $name up"

    if [ "$bridge" == "br-int" ]; then
        container_exec $container "ovs-vsctl set interface $name external_ids:iface-id=$name"
    fi
}

# set_vm_ip ovn-chassis-1 vm0 192.168.100.10/24 192.168.100.1
# set_vm_ip ovn-chassis-1 vm0 1000::10/64 1000::1
function set_vm_ip() {
    container=$1
    name=$2
    ip=$3
    gw=$4

    container_exec $container "ip netns exec $name ip addr add $ip dev $name"
    container_exec $container "ip netns exec $name ip route add default via $gw dev $name"
}

CENTRAL="ovn-central-az1-1"
CHASSIS1="ovn-chassis-1"
CHASSIS2="ovn-chassis-2"

container_exec $CHASSIS1 "ovs-vsctl set open . external_ids:ovn-bridge-mappings=physnet1:br-ex"
container_exec $CHASSIS2 "ovs-vsctl set open . external_ids:ovn-bridge-mappings=physnet1:br-ex"

container_exec $CENTRAL "ovn-nbctl ls-add ls0"
container_exec $CENTRAL "ovn-nbctl lsp-add ls0 ln_port"
container_exec $CENTRAL "ovn-nbctl lsp-set-addresses ln_port unknown"
container_exec $CENTRAL "ovn-nbctl lsp-set-type ln_port localnet"
container_exec $CENTRAL "ovn-nbctl lsp-set-options ln_port network_name=physnet1"

container_exec $CENTRAL "ovn-nbctl lsp-add ls0 vm0"
container_exec $CENTRAL "ovn-nbctl lsp-set-addresses vm0 \"00:00:00:00:20:10 192.168.20.10\""
container_exec $CENTRAL "ovn-nbctl lsp-set-options vm0 requested-chassis=$CHASSIS1,$CHASSIS2"

container_exec $CENTRAL "ovn-nbctl lsp-add ls0 vm1"
container_exec $CENTRAL "ovn-nbctl lsp-set-addresses vm1 \"00:00:00:00:20:11 192.168.20.11\""

create_fake_vm $CHASSIS1 vm0 00:00:00:00:20:10
set_vm_ip $CHASSIS1 vm0  192.168.20.10/24 192.168.20.1

create_fake_vm $CHASSIS2 vm0 00:00:00:00:20:10
set_vm_ip $CHASSIS2 vm0  192.168.20.10/24 192.168.20.1

create_fake_vm $CHASSIS2 vm1 00:00:00:00:20:11
set_vm_ip $CHASSIS2 vm1  192.168.20.11/24 192.168.20.1

create_fake_vm $CHASSIS1 ext 00:00:00:00:20:13 br-ex
set_vm_ip $CHASSIS1 ext  192.168.20.13/24 192.168.20.1

container_exec $CHASSIS2 "tcpdump -i any -vnnne icmp > tcpdump.log 2>&1" &
sleep 1
container_exec $CHASSIS2 "ip netns exec vm1 ping -c 5 -i 0.5 -s 1500 -M want 192.168.20.10"
container_exec $CHASSIS2 "kill \$(pidof -s tcpdump)"
container_exec $CHASSIS2 "cat tcpdump.log"
