#!/bin/sh

#
# OnApp, Sergey Kviato <sergey.kviato@onapp.com>
#

### BEGIN INIT INFO
# Provides:          Open vSwitch
# Required-Start:    $network $time $syslog
# Required-Stop:     $syslog
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Script manage power cicle of OVS
# Description:       Open vSwitch is allows VMs to communicate by virtual network
### END INIT INFO

OVSVSCTL=$(whereis -b ovs-vsctl | awk '{print $2}')
OVSOFCTL=$(whereis -b ovs-ofctl | awk '{print $2}')
OVSDB=$(whereis -b ovsdb-server | awk '{print $2}')
VSWITCHD=$(whereis -b ovs-vswitchd | awk '{print $2}')
RUNPATH="/var/run/openvswitch"
OVSVSCTL="$OVSVSCTL --db=unix:$RUNPATH/db.sock"
DBPATH="/etc/openvswitch"
CONFIG="/etc/onapp.conf"
SWITCH="onapp-ovs0"
INTERFACE=""
lock_file=/tmp/ovs_run.lck

if [ -e $lock_file ] ; then
	echo "Lock file found. Another copy of script is runing."
	exit 0
else
	/bin/touch $lock_file
fi

trap "/bin/rm -f $lock_file" 0 1 2 5 15

init_switch() {
for bridge in `$OVSVSCTL list-br | /bin/grep onapp-ovs`
do
    if $OVSVSCTL br-exists $bridge
    then
    $OVSOFCTL del-flows $bridge
    $OVSOFCTL add-flow $bridge "dl_type=0x0806 dl_dst=01:00:00:00:00:00/01:00:00:00:00:00 action=all"
    fi

done

/onapp/tools/interface --conf $CONFIG

}



start_ovsdb() {
# StartUP database server

$OVSDB $DBPATH/conf.db --remote=punix:$RUNPATH/db.sock --remote=db:Open_vSwitch,manager_options --private-key=db:SSL,private_key --certificate=db:SSL,certificate --bootstrap-ca-cert=db:SSL,ca_cert --pidfile --detach > /dev/null 2>&1
}

start_vswitchd() {
# StartUP vSwitch server
$VSWITCHD --pidfile --detach unix:$RUNPATH/db.sock > /dev/null 2>&1
}

start_brcompatd() {
# StartUP compatibility server
sleep 3
/usr/local/sbin/ovs-brcompatd --pidfile --detach --appctl=/usr/local/bin/ovs-appctl --vsctl=/usr/local/bin/ovs-vsctl > /dev/null 2>&1
}

status() {
for d in ovsdb vswitchd
do
    if /usr/bin/pgrep $d 1>/dev/null; then
	echo "Affirmative, $d is running at PID `/usr/bin/pgrep $d`..."
    else
	echo "I think we've got a problem, $d is not run"
    fi
done
}

case "$1" in
    start)
#    # Remove standart Linux bridge and friends
#    for module in bridge stp llc
#    do
#	    if lsmod | grep $module 1>/dev/null; then
#		/sbin/rmmod $module
#	    fi
#    done
#    sleep 1
    # Load OVS module
    if /sbin/lsmod | grep openvswitch 1>/dev/null; then
	echo "Affirmative, kernel ovs module present"
    else
	echo "Load OVS module"
	if /sbin/modprobe openvswitch_mod 1>/dev/null; then
		echo "Done"
	elif /sbin/modprobe openvswitch 1>/dev/null; then
		echo "Done"
	else
		echo "Cant load module."
		exit 1
	fi

	sleep 1
    fi
	
#    # Load compatibility module
#    if lsmod | grep brcompat_mod 1>/dev/null; then
#	echo "Affirmative, kernel brcompat module present"
#    else
#	echo "Inserting brcompat module"
#	/sbin/insmod /etc/ovs/$KERNEL/brcompat_mod.ko
#	sleep 1
#    fi

#    for d in ovsdb vswitchd brcompatd
    for d in ovsdb vswitchd

    do
	if /usr/bin/pgrep $d 1>/dev/null; then
	echo "$d deamon is run now, so stop one before."
	else
	echo -n "$d deamon is not running. Starting ..."
	start_$d

	for i in 1 2 3 4 5 6 7 8 9 10; do
    	    if /usr/bin/pgrep $d 1>/dev/null ; then
		echo " Done."
		break
	    else
		if [ $i -eq 10 ]; then
		echo "$d server was not started!"
		else
		echo -n "."
		fi
	    fi
	sleep 1
	done
	fi
    done
    echo "OVS init"
    init_switch

	;;
    stop)
    for d in ovsdb vswitchd
    do
	echo -n "Shutting down $d ..."
	kill `/usr/bin/pgrep $d` > /dev/null 2>&1
	if /usr/bin/pgrep $d 1>/dev/null; then
	FAIL=0
		for i in 1 2 3 4 5 6 7 8 9 10; do
			if /usr/bin/pgrep $d 1>/dev/null ; then
			    echo -n "."
			    if [ $i -eq 10 ]; then FAIL=1; fi
			else
			    echo " Done."
			    break
			fi
		    sleep 1
		done

		if [ $FAIL -eq 1 ]; then
		# server was not stoped in normal mode, so need to kill one.
		echo "I'm sorry. I'm afraid I can't just stop $d. Killing $d"
		kill -9 `/usr/bin/pgrep $d` > /dev/null 2>&1
		
		for i in 1 2 3 4 5 6 7 8 9 10; do
		PID=`/usr/bin/pgrep $d`
		    if ps -p $PID 1>/dev/null ; then
		    	echo -n "."
		    	if [ $i -eq 10]; then echo " Fail."; fi
		    else
		    	echo " Done."
		    	break
		    fi
			sleep 1
		done
		fi
#	    exit 1
	else
	    echo " Done."
	fi
	done
	;;
    status)
	status
	;;
    *)
	echo "Usage: $0 {start|stop|status}"
	exit 1
	;;
esac

