#!/bin/bash
# Cleanup neutron OVS bridges. To be called on startup to avoid
# "difficult-to-debug" issues with partially configured resources.

NEUTRON_OVS_CONF=/var/lib/config-data/puppet-generated/neutron/etc/neutron/plugins/ml2/openvswitch_agent.ini

if [ -e ${NEUTRON_OVS_CONF} ];
then
    INT_BRIDGE=`crudini --get ${NEUTRON_OVS_CONF} ovs integration_bridge`
    TUN_BRIDGE=`crudini --get ${NEUTRON_OVS_CONF} ovs tunnel_bridge`
fi

for port in `ovs-vsctl list-ports ${INT_BRIDGE:-"br-int"}`;
do
    skip_cleanup=`ovs-vsctl --if-exists get Interface $port external_ids:skip_cleanup`
    if ! [[ "x$skip_cleanup" == "x\"true\"" ]];
    then
        ovs-vsctl del-port ${INT_BRIDGE:-"br-int"} $port
    fi
done

ovs-vsctl --if-exists del-br ${TUN_BRIDGE:-"br-tun"}

# Clean up trunk port bridges
for br in $(ovs-vsctl list-br | egrep 'tbr-[0-9a-f\-]+'); do
    ovs-vsctl --if-exists del-br $br
done