#!/bin/bash

# Create switch
SWITCH="ls"
LSPs=5

cleanup(){
    echo "Starting cleanup..."
    # echo "Deleting all namespaces..."
    for net in $(ip netns list | awk '{print $1}'); do ip netns del $net; done

    # echo "Stopping OVN and services..."
    systemctl stop ovn-controller ovn-northd

    # echo "Cleaning up kernel datapaths..."
    ovs-vsctl --if-exists del-br br-int || true

    # echo "Flushing any leftover OVN settings..."
    ovs-vsctl --if-exists remove open . external_ids ovn-remote || true
    ovs-vsctl --if-exists remove open . external_ids ovn-encap-type || true
    ovs-vsctl --if-exists remove open . external_ids ovn-encap-ip || true
    ovs-vsctl --if-exists remove open . external_ids system-id || true

    # echo "Stopping OVS service..."
    systemctl stop openvswitch

    # echo "Deleting OVS database files..."
    rm -rf /etc/openvswitch/*.db \
        /var/lib/openvswitch/* \
        /etc/openvswitch/*.pem \
        /var/log/openvswitch/*

    # echo "Deleting OVN database files..."
    rm -rf /var/lib/ovn/* \
        /etc/ovn/*.db \
        /etc/ovn/*.pem \
        /var/log/ovn/*

    echo "Cleanup completed..."

}

start_ovn(){
    echo "Starting OVS service..."
    systemctl start openvswitch

    echo "Starting OVN service..."
    systemctl start ovn-northd
    ovn-nbctl set-connection ptcp:6641
    ovn-sbctl set-connection ptcp:6642
    ovs-vsctl set open . external_ids:system-id=hv1 external_ids:ovn-remote=tcp:127.0.0.1:6642 external_ids:ovn-encap-type=geneve external_ids:ovn-encap-ip=127.0.0.1
    systemctl start ovn-controller

    echo "OVN started..."
}

main() {
    cleanup
    start_ovn
    echo ""
    echo "Starting FDP-1713..."
    echo "Creating switch  $SWITCH, lsp ports and ovs-ports..."
    ovn-nbctl ls-add $SWITCH

    # Add multiple ports to make it more realistic
    for i in $(seq 1 $LSPs); do
        PORT="lsp$i"
        OVS_PORT="ovs-port$i"  # Short name (max 15 chars)

        # Create logical ports
        ovn-nbctl lsp-add $SWITCH $PORT
        ovn-nbctl lsp-set-addresses $PORT "00:00:00:00:00:0$i 10.0.0.$i"

        # Create OVS internal port and bind it
        ovs-vsctl --if-exists del-port br-int test-p$i 2>/dev/null
        ovs-vsctl add-port br-int $OVS_PORT -- set interface $OVS_PORT type=internal external-ids:iface-id=$PORT
        ip link set $OVS_PORT up

        echo "  Binding: $PORT → $OVS_PORT"
    done

    echo "Waiting 5 seconds for bindings..."
    sleep 5

    # Verify bindings
    echo ""
    BOUND=$(ovn-sbctl find Port_Binding chassis!=[] | grep -c "logical_port")
    if [ "$BOUND" -eq "$LSPs" ]; then
        echo "PASS: Bound ports: $BOUND"
    else
        echo "FAIL: Bound ports are less than $BOUND"
    fi

    # Get ovn-controller PID
    PID=$(pgrep ovn-controller)
    echo ""
    echo "Monitoring ovn-cnotroller PID: $PID"


    # Monitor CPU
    CPU_BEFORE=$(ps -p $PID -o %cpu= | awk '{print int($1)}')
    echo ""
    if [ "$CPU_BEFORE" -lt 1 ]; then
        echo "PASS CPU usage by ovn-controller PID $PID at: $(date +%H:%M:%S),  $CPU_BEFORE%"
    else
        echo "FAIL CPU usage by ovn-controller PID $PID at: $(date +%H:%M:%S),  $CPU_BEFORE% is higher than 0%"
    fi

    # Add the complex ACL
    echo ""
    echo "Adding ACL at: $(date +%T)"
    ovn-nbctl acl-add $SWITCH from-lport 1001 \
    'ip4.dst == 0.0.0.0/0 && ip4.dst != {10.6.153.254/32, 10.0.0.0/8, 10.128.8.1/32, 10.129.0.1/32, 10.128.0.1/32, 10.131.0.1/32, 10.130.0.1/32, 10.130.6.1/32, 10.129.4.1/32, 10.129.2.1/32, 10.130.2.1/32, 10.131.4.1/32, 10.128.6.1/32, 10.131.8.1/32, 10.129.6.1/32, 10.131.6.1/32, 10.128.4.1/32, 10.130.4.1/32, 10.129.8.1/32, 100.64.0.0/10, 129.0.1.4/32, 129.0.1.5/32, 129.0.2.153/32, 144.42.16.0/24, 144.42.27.0/24, 144.42.28.0/24, 144.42.3.0/24, 144.42.34.0/24, 144.42.56.0/24, 169.254.0.0/16, 170.40.0.0/17, 172.16.0.0/12, 152.161.230.196/30, 152.162.200.128/30, 152.181.136.88/30, 152.181.52.4/30, 152.181.57.100/30, 152.181.57.112/30, 152.181.57.92/30, 152.181.58.12/30, 152.181.59.208/30, 152.181.60.44/30, 152.181.60.56/30, 152.183.126.220/30, 162.213.152.0/23, 165.79.15.0/24, 165.79.232.16/28, 165.79.254.0/24, 165.79.3.0/24, 191.237.22.167/32, 192.168.0.0/16, 198.105.201.0/24}' \
    allow-related
    #echo "ovn-nbctl acl-list $SWITCH:"
    #ovn-nbctl acl-list $SWITCH

    echo "Waiting 60 seconds..."
    sleep 60
    echo ""

    CPU_AFTER=$(ps -p $PID -o %cpu= | awk '{print int($1)}')

    if [ "$CPU_AFTER" -gt 5 ]; then
        echo "FAIL: CPU usage for ovn-controller PID $PID after 1 min is: at $(date +%H:%M:%S),  $CPU_AFTER% is higher than 5%"
    else
        echo "PASS: CPU usage for ovn-controller PID $PID after 1 min is: at $(date +%H:%M:%S),  $CPU_AFTER% less than 5%"
    fi

    ovn-nbctl ls-del $SWITCH
    systemctl restart ovn-controller
}

main
