#!/bin/bash
set -x

# You are expected to have 3 chassis
# Lets' call it ovn-c1, ovn-c2 and ovn-gw

ovn-nbctl ls-add sw0
ovn-nbctl lsp-add sw0 sw0-port1
ovn-nbctl lsp-set-addresses sw0-port1 "50:54:00:00:00:03 10.0.0.3"

ovn-nbctl lsp-add sw0 sw0-port2
ovn-nbctl lsp-set-addresses sw0-port2 "50:54:00:00:00:04 10.0.0.4"

# Create the second logical switch with one port
ovn-nbctl ls-add sw1
ovn-nbctl lsp-add sw1 sw1-port1
ovn-nbctl lsp-set-addresses sw1-port1 "40:54:00:00:00:03 20.0.0.3"

ovn-nbctl lsp-add sw1 sw1-port2
ovn-nbctl lsp-set-addresses sw1-port2 "40:54:00:00:00:04 20.0.0.4"

# Create a logical router and attach both logical switches
ovn-nbctl lr-add lr0
ovn-nbctl lrp-add lr0 lr0-sw0 00:00:00:00:ff:01 10.0.0.1/24
ovn-nbctl lsp-add sw0 sw0-lr0
ovn-nbctl lsp-set-type sw0-lr0 router
ovn-nbctl lsp-set-addresses sw0-lr0 00:00:00:00:ff:01
ovn-nbctl lsp-set-options sw0-lr0 router-port=lr0-sw0

ovn-nbctl lrp-add lr0 lr0-sw1 00:00:00:00:ff:02 20.0.0.1/24
ovn-nbctl lsp-add sw1 sw1-lr0
ovn-nbctl lsp-set-type sw1-lr0 router
ovn-nbctl lsp-set-addresses sw1-lr0 00:00:00:00:ff:02
ovn-nbctl lsp-set-options sw1-lr0 router-port=lr0-sw1


# I am assuimg 172.168.0.100 here.. you can change it if you want.
ovn-nbctl ls-add public
ovn-nbctl lrp-add lr0 lr0-public 00:00:20:20:12:13 172.168.0.100/24
ovn-nbctl lsp-add public public-lr0
ovn-nbctl lsp-set-type public-lr0 router
ovn-nbctl lsp-set-addresses public-lr0 router
ovn-nbctl lsp-set-options public-lr0 router-port=lr0-public

# localnet port
ovn-nbctl lsp-add 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

# schedule the gw router port to a chassis. Change the name of the chassis
ovn-nbctl lrp-set-gateway-chassis lr0-public chassis-1 20


# Create NAT entries for the ports

# sw0-port1
ovn-nbctl lr-nat-add lr0 dnat_and_snat 172.168.0.110 10.0.0.3 sw0-port1 30:54:00:00:00:03
ovn-nbctl lr-nat-add lr0 dnat_and_snat 172.168.0.120 20.0.0.3 sw1-port1 30:54:00:00:00:04

# Add a snat entry
ovn-nbctl lr-nat-add lr0 snat 172.168.0.100 10.0.0.0/24
ovn-nbctl lr-nat-add lr0 snat 172.168.0.100 20.0.0.0/24

ovn-nbctl pg-add pg0 sw0-port1 sw0-port2 sw1-port1
ovn-nbctl acl-add pg0 from-lport 1002  "inport == @pg0 && ip4 && tcp.dst == 80" allow-related
ovn-nbctl acl-list pg0

ovn-nbctl pg-add pg0_drop sw0-port1 sw0-port2 sw1-port1
ovn-nbctl acl-add pg0_drop to-lport 1001 "outport == @pg0 && ip4" drop

ovn-nbctl acl-list pg0_drop

ovs-vsctl add-br br-ex
ovs-vsctl set open . external_ids:ovn-bridge-mappings=public:br-ex


add_phys_port() {
    name=$1
    mac=$2
    ip=$3
    mask=$4
    gw=$5
    iface_id=$6
    ip netns add $name
    ovs-vsctl add-port br-int $name -- set interface $name type=internal
    ip link set $name netns $name
    ip netns exec $name ip link set $name address $mac
    ip netns exec $name ip addr add $ip/$mask dev $name
    ip netns exec $name ip link set $name up
    ip netns exec $name ip route add default via $gw
    ovs-vsctl set Interface $name external_ids:iface-id=$iface_id
}


ovs-vsctl add-port br-int p1 -- \
    set Interface p1 external_ids:iface-id=sw0-port1
ovs-vsctl add-port br-int p2 -- \
    set Interface p2 external_ids:iface-id=sw1-port1



ovn-nbctl lb-add lb1 10.0.0.10:80 "10.0.0.3:80,20.0.0.3:80"


ovn-nbctl set load_balancer . ip_port_mappings:10.0.0.3="sw0-port1:10.0.0.2"
ovn-nbctl set load_balancer . ip_port_mappings:20.0.0.3=sw1-port1

ovn-nbctl ls-lb-add sw0 lb1

#sleep 3
ovn-sbctl show


