#!/bin/bash

if [ "$#" -ne 1 ]; then
    echo "Illegal number of parameters"
    exit 1
fi
echo "ZZZ $1"

START(){
    hostname=$(hostname)
    loopbackIP={{host_loopback_IP}}

    echo "Starting OpenVSwitch and OVN on $hostname with $loopbackIP and pointing to OVN controller {{OVN_SB_Connection}}"

    DIR="/var/run/openvswitch/db.sock"
    if [ -d "$DIR" ]; then
        rm -rf $DIR
        echo "Removed extraneous dir $DIR"
    fi

    /usr/share/openvswitch/scripts/ovs-ctl start --system-id=$hostname
    #Instruct OVSDB to listen on 127.0.0.2:6640 so the ovs-vsctl command from within a container can access OpenVswitch
    ovs-appctl -t ovsdb-server ovsdb-server/add-remote ptcp:6640
    ovs-vsctl del-br br-int
    
    ovs-vsctl set open . external-ids:ovn-bridge=br-int
    ovs-vsctl set open . external-ids:ovn-remote={{OVN_SB_Connection}}
    ovs-vsctl set open . external-ids:ovn-encap-type=geneve
    ovs-vsctl set open . external-ids:ovn-encap-ip=$loopbackIP
    /usr/share/ovn/scripts/ovn-ctl start_controller
    echo "OVS and OVN started"

{% if ansible_fqdn in groups['compute_nodes'] %}
    echo "Bringing online br-provider for WAN1"
    ovs-vsctl --may-exist add-br br-provider
    ovs-vsctl set open . external-ids:ovn-bridge-mappings=WAN1:br-provider
    ovs-vsctl --may-exist add-port br-provider bond0.11
{% endif %}


}
STOP() {
    echo "Stopping OpenVSwitch and OVN"
    /usr/share/ovn/scripts/ovn-ctl stop_controller
    /usr/share/ovn/scripts/ovn-ctl stop_ovsdb
    /usr/share/openvswitch/scripts/ovs-ctl stop
    echo "OpenVSwitch and OVN Stopped"
}

case $1 in
  START|STOP) "$1" ;;
esac
