#!/bin/sh
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

case $0 in
    */*) dir0=`echo "$0" | sed 's,/[^/]*$,,'` ;;
    *) dir0=./ ;;
esac
. "$dir0/ovs-lib" || exit 1

for dir in "$sbindir" "$bindir" /sbin /bin /usr/sbin /usr/bin; do
    case :$PATH: in
        *:$dir:*) ;;
        *) PATH=$PATH:$dir ;;
    esac
done


ovnnb_active_conf_file="$etcdir/ovnnb-active.conf"
ovnsb_active_conf_file="$etcdir/ovnsb-active.conf"
ovn_northd_db_conf_file="$etcdir/ovn-northd-db-params.conf"
## ----- ##
## start ##
## ----- ##

pidfile_is_running () {
    pidfile=$1
    test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
} >/dev/null 2>&1

stop_nb_ovsdb() {
    if pidfile_is_running $DB_NB_PID; then
        ovs-appctl -t $OVN_RUNDIR/ovnnb_db.ctl exit
    fi
}

stop_sb_ovsdb() {
    if pidfile_is_running $DB_SB_PID; then
        ovs-appctl -t $OVN_RUNDIR/ovnsb_db.ctl exit
    fi
}

stop_ovsdb () {
    stop_nb_ovsdb
    stop_sb_ovsdb
}

demote_ovnnb() {
    if test ! -z "$DB_NB_SYNC_FROM_ADDR"; then
        echo "$DB_NB_SYNC_FROM_PROTO:$DB_NB_SYNC_FROM_ADDR:$DB_NB_SYNC_FROM_PORT" > $ovnnb_active_conf_file
    fi

    if test -e $ovnnb_active_conf_file; then
        ovs-appctl -t $OVN_RUNDIR/ovnnb_db.ctl ovsdb-server/set-active-ovsdb-server `cat $ovnnb_active_conf_file`
        ovs-appctl -t $OVN_RUNDIR/ovnnb_db.ctl ovsdb-server/connect-active-ovsdb-server
    else
        echo >&2 "$0: active server details not set"
        exit 1
    fi
}

demote_ovnsb() {
    if test ! -z "$DB_SB_SYNC_FROM_ADDR"; then
        echo "$DB_SB_SYNC_FROM_PROTO:$DB_SB_SYNC_FROM_ADDR:$DB_SB_SYNC_FROM_PORT" > $ovnsb_active_conf_file
    fi

    if test -e $ovnsb_active_conf_file; then
        ovs-appctl -t $OVN_RUNDIR/ovnsb_db.ctl ovsdb-server/set-active-ovsdb-server `cat $ovnsb_active_conf_file`
        ovs-appctl -t $OVN_RUNDIR/ovnsb_db.ctl ovsdb-server/connect-active-ovsdb-server
    else
        echo >&2 "$0: active server details not set"
        exit 1
    fi
}

promote_ovnnb() {
    rm -f $ovnnb_active_conf_file
    ovs-appctl -t $OVN_RUNDIR/ovnnb_db.ctl ovsdb-server/disconnect-active-ovsdb-server
}

promote_ovnsb() {
    rm -f $ovnsb_active_conf_file
    ovs-appctl -t $OVN_RUNDIR/ovnsb_db.ctl ovsdb-server/disconnect-active-ovsdb-server
}

start_ovsdb__() {
    local DB=$1 db=$2 schema_name=$3 table_name=$4
    local db_pid_file
    local cluster_local_addr
    local cluster_local_port
    local cluster_local_proto
    local cluster_remote_addr
    local cluster_remote_port
    local cluster_remote_proto
    local sync_from_proto
    local sync_from_addr
    local sync_from_port
    local file
    local schema
    local logfile
    local log
    local sock
    local detach
    local create_insecure_remote
    local port
    local addr
    local active_conf_file
    local use_remote_in_db
    local ovn_db_ssl_key
    local ovn_db_ssl_cert
    local ovn_db_ssl_cacert
    eval db_pid_file=\$DB_${DB}_PID
    eval cluster_local_addr=\$DB_${DB}_CLUSTER_LOCAL_ADDR
    eval cluster_local_port=\$DB_${DB}_CLUSTER_LOCAL_PORT
    eval cluster_local_proto=\$DB_${DB}_CLUSTER_LOCAL_PROTO
    eval cluster_remote_addr=\$DB_${DB}_CLUSTER_REMOTE_ADDR
    eval cluster_remote_port=\$DB_${DB}_CLUSTER_REMOTE_PORT
    eval cluster_remote_proto=\$DB_${DB}_CLUSTER_REMOTE_PROTO
    eval sync_from_proto=\$DB_${DB}_SYNC_FROM_PROTO
    eval sync_from_addr=\$DB_${DB}_SYNC_FROM_ADDR
    eval sync_from_port=\$DB_${DB}_SYNC_FROM_PORT
    eval file=\$DB_${DB}_FILE
    eval schema=\$DB_${DB}_SCHEMA
    eval logfile=\$OVN_${DB}_LOGFILE
    eval log=\$OVN_${DB}_LOG
    eval sock=\$DB_${DB}_SOCK
    eval detach=\$DB_${DB}_DETACH
    eval create_insecure_remote=\$DB_${DB}_CREATE_INSECURE_REMOTE
    eval port=\$DB_${DB}_PORT
    eval addr=\$DB_${DB}_ADDR
    eval active_conf_file=\$ovn${db}_active_conf_file
    eval use_remote_in_db=\$DB_${DB}_USE_REMOTE_IN_DB
    eval ovn_db_ssl_key=\$OVN_${DB}_DB_SSL_KEY
    eval ovn_db_ssl_cert=\$OVN_${DB}_DB_SSL_CERT
    eval ovn_db_ssl_cacert=\$OVN_${DB}_DB_SSL_CA_CERT

    install_dir "$OVN_RUNDIR"
    # Check and eventually start ovsdb-server for DB
    if pidfile_is_running $db_pid_file; then
        return
    fi

    if test ! -z "$cluster_local_addr"; then
        mode=cluster
    elif test ! -z "$sync_from_addr"; then
        mode=active_passive
        echo "$sync_from_proto:$sync_from_addr:\
$sync_from_port" > $active_conf_file
    else
        mode=standalone
    fi

    if test $mode = cluster; then
        local local=$cluster_local_proto:$cluster_local_addr:\
$cluster_local_port
        local remote=$cluster_remote_proto:$cluster_remote_addr:\
$cluster_remote_port
        if test -n "$cluster_remote_addr"; then
            join_cluster "$file" "$schema_name" "$local" "$remote"
        else
            create_cluster "$file" "$schema" "$local"
        fi
    else
        upgrade_db "$file" "$schema"
    fi

    set ovsdb-server
    set "$@" $log --log-file=$logfile
    set "$@" --remote=punix:$sock --pidfile=$db_pid_file
    set "$@" --unixctl=ovn${db}_db.ctl

    [ "$OVS_USER" != "" ] && set "$@" --user "$OVS_USER"

    if test X"$detach" != Xno; then
        set "$@" --detach --monitor
    else
        set exec "$@"
    fi

    if test X"$use_remote_in_db" != Xno; then
        set "$@" --remote=db:$schema_name,$table_name,connections
    fi

    if test X"$ovn_db_ssl_key" != X; then
        set "$@" --private-key=$ovn_db_ssl_key
    else
        set "$@" --private-key=db:$schema_name,SSL,private_key
    fi
    if test X"$ovn_db_ssl_cert" != X; then
        set "$@" --certificate=$ovn_db_ssl_cert
    else
        set "$@" --certificate=db:$schema_name,SSL,certificate
    fi
    if test X"$ovn_db_ssl_cacert" != X; then
        set "$@" --ca-cert=$ovn_db_ssl_cacert
    else
        set "$@" --ca-cert=db:$schema_name,SSL,ca_cert
    fi

    set "$@" --ssl-protocols=db:$schema_name,SSL,ssl_protocols
    set "$@" --ssl-ciphers=db:$schema_name,SSL,ssl_ciphers

    if test X"$create_insecure_remote" = Xyes; then
        set "$@" --remote=ptcp:$port:$addr
    fi

    if test $mode = active_passive; then
        set "$@" --sync-from=`cat $active_conf_file`
    fi

    "$@" "$file"

    # Initialize the database if it's running standalone,
    # active-passive, or is the first server in a cluster.
    if test -z "$cluster_remote_addr"; then
        ovn-${db}ctl init
    fi

    if test $mode = cluster; then
        upgrade_cluster "$schema" "unix:$sock"
    fi
}

start_nb_ovsdb() {
    start_ovsdb__ NB nb OVN_Northbound NB_Global
}

start_sb_ovsdb() {
    # Increase the limit on the number of open file descriptors, because
    # SB DB may connect to large number of chassises, on top of connections
    # for cluster members, northd, and serveral local unix sockets.
    MAXFD=8192
    if [ $(ulimit -n) -lt $MAXFD ]; then
        ulimit -n $MAXFD
    fi

    start_ovsdb__ SB sb OVN_Southbound SB_Global
}

start_ovsdb () {
    start_nb_ovsdb
    start_sb_ovsdb
}

sync_status() {
    ovs-appctl -t $OVN_RUNDIR/ovn${1}_db.ctl ovsdb-server/sync-status | awk '{if(NR==1) print $2}'
}

status_ovnnb() {
    if ! pidfile_is_running $DB_NB_PID; then
        echo "not-running"
    else
        echo "running/$(sync_status nb)"
    fi
}

status_ovnsb() {
    if ! pidfile_is_running $DB_SB_PID; then
        echo "not-running"
    else
        echo "running/$(sync_status sb)"
    fi
}

status_ovsdb () {
  if ! pidfile_is_running $DB_NB_PID; then
      log_success_msg "OVN Northbound DB is not running"
  else
      log_success_msg "OVN Northbound DB is running"
  fi

  if ! pidfile_is_running $DB_SB_PID; then
      log_success_msg "OVN Southbound DB is not running"
  else
      log_success_msg "OVN Southbound DB is running"
  fi
}

run_nb_ovsdb() {
    DB_NB_DETACH=no
    start_nb_ovsdb
}

run_sb_ovsdb() {
    DB_SB_DETACH=no
    start_sb_ovsdb
}

start_northd () {
    if [ ! -e $ovn_northd_db_conf_file ]; then
        if test X"$OVN_MANAGE_OVSDB" = Xyes; then
            start_ovsdb

            if ! pidfile_is_running $DB_NB_PID; then
                log_failure_msg "OVN Northbound DB is not running"
                exit
            fi
            if ! pidfile_is_running $DB_SB_PID; then
                log_failure_msg "OVN Southbound DB is not running"
                exit
            fi
        fi
        ovn_northd_params="--ovnnb-db=$OVN_NORTHD_NB_DB \
        --ovnsb-db=$OVN_NORTHD_SB_DB"
    else
        ovn_northd_params="`cat $ovn_northd_db_conf_file`"
    fi

    if daemon_is_running ovn-northd; then
        log_success_msg "ovn-northd is already running"
    else
        set ovn-northd
        if test X"$OVN_NORTHD_LOGFILE" != X; then
            set "$@" --log-file=$OVN_NORTHD_LOGFILE
        fi

        [ "$OVN_USER" != "" ] && set "$@" --user "$OVN_USER"

        set "$@" $OVN_NORTHD_LOG $ovn_northd_params

        OVS_RUNDIR=${OVN_RUNDIR} start_daemon "$OVN_NORTHD_PRIORITY" "$OVN_NORTHD_WRAPPER" "$@"
    fi
}

start_controller () {
    set ovn-controller "unix:$DB_SOCK"
    set "$@" $OVN_CONTROLLER_LOG
    if test X"$OVN_CONTROLLER_SSL_KEY" != X; then
        set "$@" --private-key=$OVN_CONTROLLER_SSL_KEY
    fi
    if test X"$OVN_CONTROLLER_SSL_CERT" != X; then
        set "$@" --certificate=$OVN_CONTROLLER_SSL_CERT
    fi
    if test X"$OVN_CONTROLLER_SSL_CA_CERT" != X; then
        set "$@" --ca-cert=$OVN_CONTROLLER_SSL_CA_CERT
    fi
    if test X"$OVN_CONTROLLER_SSL_BOOTSTRAP_CA_CERT" != X; then
        set "$@" --bootstrap-ca-cert=$OVN_CONTROLLER_SSL_BOOTSTRAP_CA_CERT
    fi

    [ "$OVN_USER" != "" ] && set "$@" --user "$OVN_USER"

    OVS_RUNDIR=${OVN_RUNDIR} start_daemon "$OVN_CONTROLLER_PRIORITY" "$OVN_CONTROLLER_WRAPPER" "$@"
}

start_controller_vtep () {
    set ovn-controller-vtep
    set "$@" -vconsole:emer -vsyslog:err -vfile:info
    if test X"$OVN_CONTROLLER_SSL_KEY" != X; then
        set "$@" --private-key=$OVN_CONTROLLER_SSL_KEY
    fi
    if test X"$OVN_CONTROLLER_SSL_CERT" != X; then
        set "$@" --certificate=$OVN_CONTROLLER_SSL_CERT
    fi
    if test X"$OVN_CONTROLLER_SSL_CA_CERT" != X; then
        set "$@" --ca-cert=$OVN_CONTROLLER_SSL_CA_CERT
    fi
    if test X"$OVN_CONTROLLER_SSL_BOOTSTRAP_CA_CERT" != X; then
        set "$@" --bootstrap-ca-cert=$OVN_CONTROLLER_SSL_BOOTSTRAP_CA_CERT
    fi
    if test X"$DB_SOCK" != X; then
        set "$@" --vtep-db=$DB_SOCK
    fi
    if test X"$DB_SB_SOCK" != X; then
        set "$@" --ovnsb-db=$DB_SB_SOCK
    fi

    [ "$OVN_USER" != "" ] && set "$@" --user "$OVN_USER"

    OVS_RUNDIR=${OVN_RUNDIR} start_daemon "$OVN_CONTROLLER_PRIORITY" "$OVN_CONTROLLER_WRAPPER" "$@"
}

## ---- ##
## stop ##
## ---- ##

stop_northd () {
    OVS_RUNDIR=${OVN_RUNDIR} stop_daemon ovn-northd

    if [ ! -e $ovn_northd_db_conf_file ]; then
        if test X"$OVN_MANAGE_OVSDB" = Xyes; then
            stop_ovsdb
        fi
    fi
}

stop_controller () {
    OVS_RUNDIR=${OVN_RUNDIR} stop_daemon ovn-controller "$@"
}

stop_controller_vtep () {
    OVS_RUNDIR=${OVN_RUNDIR} stop_daemon ovn-controller-vtep
}

## ------- ##
## restart ##
## ------- ##

restart_northd () {
    stop_northd
    start_northd
}

restart_controller () {
    stop_controller --restart
    start_controller
}

restart_controller_vtep () {
    stop_controller_vtep
    start_controller_vtep
}

restart_ovsdb () {
    stop_ovsdb
    start_ovsdb
}

restart_nb_ovsdb () {
    stop_nb_ovsdb
    start_nb_ovsdb
}

restart_sb_ovsdb () {
    stop_sb_ovsdb
    start_sb_ovsdb
}

## ---- ##
## main ##
## ---- ##

set_defaults () {
    OVN_MANAGE_OVSDB=yes

    OVS_RUNDIR=${OVS_RUNDIR:-${rundir}}
    OVN_RUNDIR=${OVN_RUNDIR:-${OVS_RUNDIR}}

    DB_NB_SOCK=$OVN_RUNDIR/ovnnb_db.sock
    DB_NB_PID=$OVN_RUNDIR/ovnnb_db.pid
    DB_NB_FILE=$dbdir/ovnnb_db.db
    DB_NB_ADDR=0.0.0.0
    DB_NB_PORT=6641
    DB_NB_SYNC_FROM_PROTO=tcp
    DB_NB_SYNC_FROM_ADDR=
    DB_NB_SYNC_FROM_PORT=6641

    DB_SB_SOCK=$OVN_RUNDIR/ovnsb_db.sock
    DB_SB_PID=$OVN_RUNDIR/ovnsb_db.pid
    DB_SB_FILE=$dbdir/ovnsb_db.db
    DB_SB_ADDR=0.0.0.0
    DB_SB_PORT=6642
    DB_SB_SYNC_FROM_PROTO=tcp
    DB_SB_SYNC_FROM_ADDR=
    DB_SB_SYNC_FROM_PORT=6642

    DB_NB_SCHEMA=$datadir/ovn-nb.ovsschema
    DB_SB_SCHEMA=$datadir/ovn-sb.ovsschema

    DB_SOCK=$OVN_RUNDIR/db.sock
    DB_CONF_FILE=$dbdir/conf.db

    OVN_NORTHD_PRIORITY=-10
    OVN_NORTHD_WRAPPER=
    OVN_CONTROLLER_PRIORITY=-10
    OVN_CONTROLLER_WRAPPER=

    OVN_USER=
    OVS_USER=

    OVN_CONTROLLER_LOG="-vconsole:emer -vsyslog:err -vfile:info"
    OVN_NORTHD_LOG="-vconsole:emer -vsyslog:err -vfile:info"
    OVN_NORTHD_LOGFILE=""
    OVN_NB_LOG="-vconsole:off -vfile:info"
    OVN_SB_LOG="-vconsole:off -vfile:info"
    OVN_NB_LOGFILE="$logdir/ovsdb-server-nb.log"
    OVN_SB_LOGFILE="$logdir/ovsdb-server-sb.log"

    OVN_CONTROLLER_SSL_KEY=""
    OVN_CONTROLLER_SSL_CERT=""
    OVN_CONTROLLER_SSL_CA_CERT=""
    OVN_CONTROLLER_SSL_BOOTSTRAP_CA_CERT=""

    DB_SB_CREATE_INSECURE_REMOTE="no"
    DB_NB_CREATE_INSECURE_REMOTE="no"

    MONITOR="yes"

    DB_NB_DETACH="yes"
    DB_SB_DETACH="yes"

    DB_NB_CLUSTER_LOCAL_ADDR=""
    DB_NB_CLUSTER_LOCAL_PROTO="tcp"
    DB_NB_CLUSTER_LOCAL_PORT=6643
    DB_NB_CLUSTER_REMOTE_ADDR=""
    DB_NB_CLUSTER_REMOTE_PROTO="tcp"
    DB_NB_CLUSTER_REMOTE_PORT=6643

    DB_SB_CLUSTER_LOCAL_ADDR=""
    DB_SB_CLUSTER_LOCAL_PROTO="tcp"
    DB_SB_CLUSTER_LOCAL_PORT=6644
    DB_SB_CLUSTER_REMOTE_ADDR=""
    DB_SB_CLUSTER_REMOTE_PROTO="tcp"
    DB_SB_CLUSTER_REMOTE_PORT=6644

    OVN_NORTHD_NB_DB="unix:$DB_NB_SOCK"
    OVN_NORTHD_SB_DB="unix:$DB_SB_SOCK"
    DB_NB_USE_REMOTE_IN_DB="yes"
    DB_SB_USE_REMOTE_IN_DB="yes"

    OVN_NB_DB_SSL_KEY=""
    OVN_NB_DB_SSL_CERT=""
    OVN_NB_DB_SSL_CA_CERT=""

    OVN_SB_DB_SSL_KEY=""
    OVN_SB_DB_SSL_CERT=""
    OVN_SB_DB_SSL_CA_CERT=""

}

set_option () {
    var=`echo "$option" | tr abcdefghijklmnopqrstuvwxyz- ABCDEFGHIJKLMNOPQRSTUVWXYZ_`
    eval set=\${$var+yes}
    eval old_value=\$$var
    if test X$set = X || \
        (test $type = bool && \
        test X"$old_value" != Xno && test X"$old_value" != Xyes); then
        echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
        return
    fi
    eval $var=\$value
}

usage () {
    set_defaults
    cat << EOF
$0: controls Open Virtual Network daemons
usage: $0 [OPTIONS] COMMAND

This program is intended to be invoked internally by Open Virtual Network
startup scripts.  System administrators should not normally invoke it directly.

Commands:
  start_northd                start ovn-northd
  start_ovsdb                 start ovn related ovsdb-server processes
  start_nb_ovsdb              start ovn northbound db ovsdb-server process
  start_sb_ovsdb              start ovn southbound db ovsdb-server process
  start_controller            start ovn-controller
  start_controller_vtep       start ovn-controller-vtep
  stop_northd                 stop ovn-northd
  stop_ovsdb                  stop ovn related ovsdb-server processes
  stop_nb_ovsdb               stop ovn northbound db ovsdb-server process
  stop_sb_ovsdb               stop ovn southbound db ovsdb-server process
  stop_controller             stop ovn-controller
  stop_controller_vtep        stop ovn-controller-vtep
  restart_northd              restart ovn-northd
  restart_ovsdb               restart ovn related ovsdb-server processes
  restart_nb_ovsdb            restart ovn northbound db ovsdb-server process
  restart_sb_ovsdb            restart ovn southbound db ovsdb-server process
  restart_controller          restart ovn-controller
  restart_controller_vtep     restart ovn-controller-vtep
  status_northd               status ovs-northd
  status_ovsdb                status related ovsdb-server processes
  status_controller           status ovn-controller
  status_controller_vtep      status ovn-controller-vtep
  promote_ovnnb               promote ovn northbound db backup server to active
  promote_ovnsb               promote ovn southbound db backup server to active
  demote_ovnnb                demote ovn northbound db active server to backup
  demote_ovnsb                demote ovn southbound db active server to backup
  run_nb_ovsdb                run ovn northbound db ovsdb-server process
  run_sb_ovsdb                run ovn southbound db ovsdb-server process

Options:
  --ovn-northd-priority=NICE     set ovn-northd's niceness (default: $OVN_NORTHD_PRIORITY)
  --ovn-northd-wrapper=WRAPPER   run with a wrapper like valgrind for debugging
  --ovn-controller-priority=NICE     set ovn-controller's niceness (default: $OVN_CONTROLLER_PRIORITY)
  --ovn-controller-wrapper=WRAPPER   run with a wrapper like valgrind for debugging
  --ovn-controller-ssl-key=KEY OVN Southbound SSL private key file
  --ovn-controller-ssl-cert=CERT OVN Southbound SSL certificate file
  --ovn-controller-ssl-ca-cert=CERT OVN Southbound SSL CA certificate file
  --ovn-controller-ssl-bootstrap-ca-cert=CERT Bootstrapped OVN Southbound SSL CA certificate file
  --ovn-nb-db-ssl-key=KEY OVN Northbound DB SSL private key file
  --ovn-nb-db-ssl-cert=CERT OVN Northbound DB SSL certificate file
  --ovn-nb-db-ssl-ca-cert=CERT OVN Northbound DB SSL CA certificate file
  --ovn-sb-db-ssl-key=KEY OVN Southbound DB SSL private key file
  --ovn-sb-db-ssl-cert=CERT OVN Southbound DB SSL certificate file
  --ovn-sb-db-ssl-ca-cert=CERT OVN Southbound DB SSL CA certificate file
  --ovn-manage-ovsdb=yes|no        Whether or not the OVN databases should be
                                   automatically started and stopped along
                                   with ovn-northd. The default is "yes". If
                                   this is set to "no", the "start_ovsdb" and
                                   "stop_ovsdb" commands must be used to start
                                   and stop the OVN databases.
  --ovn-controller-log=STRING        ovn controller process logging params (default: $OVN_CONTROLLER_LOG)
  --ovn-northd-log=STRING            ovn northd process logging params (default: $OVN_NORTHD_LOG)
  --ovn-northd-logfile=STRING        ovn northd process log file (default: $OVN_NORTHD_LOGFILE)
  --ovn-nb-log=STRING             ovn NB ovsdb-server processes logging params (default: $OVN_NB_LOG)
  --ovn-sb-log=STRING             ovn SB ovsdb-server processes logging params (default: $OVN_SB_LOG)
  --ovn-user="user[:group]"      pass the --user flag to the ovn daemons
  --ovs-user="user[:group]"      pass the --user flag to ovs daemons
  -h, --help                     display this help message

File location options:
  --db-sock=SOCKET     JSON-RPC socket name (default: $DB_SOCK)
  --db-nb-sock=SOCKET  OVN_Northbound db socket (default: $DB_NB_SOCK)
  --db-sb-scok=SOCKET  OVN_Southbound db socket (default: $DB_SB_SOCK)
  --db-nb-file=FILE    OVN_Northbound db file (default: $DB_NB_FILE)
  --db-sb-file=FILE    OVN_Southbound db file (default: $DB_SB_FILE)
  --db-nb-schema=FILE  OVN_Northbound db file (default: $DB_NB_SCHEMA)
  --db-sb-schema=FILE  OVN_Southbound db file (default: $DB_SB_SCHEMA)
  --db-nb-addr=ADDR    OVN Northbound db ptcp address (default: $DB_NB_ADDR)
  --db-nb-port=PORT    OVN Northbound db ptcp port (default: $DB_NB_PORT)
  --db-sb-addr=ADDR    OVN Southbound db ptcp address (default: $DB_SB_ADDR)
  --db-sb-port=PORT    OVN Southbound db ptcp port (default: $DB_SB_PORT)
  --ovn-nb-logfile=FILE OVN Northbound log file (default: $OVN_NB_LOGFILE)
  --ovn-sb-logfile=FILE OVN Southbound log file (default: $OVN_SB_LOGFILE)
  --db-nb-sync-from-addr=ADDR OVN Northbound active db tcp address (default: $DB_NB_SYNC_FROM_ADDR)
  --db-nb-sync-from-port=PORT OVN Northbound active db tcp port (default: $DB_NB_SYNC_FROM_PORT)
  --db-nb-sync-from-proto=PROTO OVN Northbound active db transport (default: $DB_NB_SYNC_FROM_PROTO)
  --db-nb-create-insecure-remote=yes|no Create ptcp OVN Northbound remote (default: $DB_NB_CREATE_INSECURE_REMOTE)
  --db-sb-sync-from-addr=ADDR OVN Southbound active db tcp address (default: $DB_SB_SYNC_FROM_ADDR)
  --db-sb-sync-from-port=ADDR OVN Southbound active db tcp port (default: $DB_SB_SYNC_FROM_PORT)
  --db-sb-sync-from-proto=PROTO OVN Southbound active db transport (default: $DB_SB_SYNC_FROM_PROTO)
  --db-sb-create-insecure-remote=yes|no Create ptcp OVN Southbound remote (default: $DB_SB_CREATE_INSECURE_REMOTE)
  --db-nb-cluster-local-addr=ADDR OVN_Northbound cluster local address \
  (default: $DB_NB_CLUSTER_LOCAL_ADDR)
  --db-nb-cluster-local-port=PORT OVN_Northbound cluster local tcp port \
  (default: $DB_NB_CLUSTER_LOCAL_PORT)
  --db-nb-cluster-local-proto=PROTO OVN_Northbound cluster local db transport \
  (default: $DB_NB_CLUSTER_LOCAL_PROTO)
  --db-nb-cluster-remote-addr=ADDR OVN_Northbound cluster remote address \
  (default: $DB_NB_CLUSTER_REMOTE_ADDR)
  --db-nb-cluster-remote-port=PORT OVN_Northbound cluster remote tcp port \
  (default: $DB_NB_CLUSTER_REMOTE_PORT)
  --db-nb-cluster-remote-proto=PROTO OVN_Northbound cluster remote db \
  transport (default: $DB_NB_CLUSTER_REMOTE_PROTO)
  --db-sb-cluster-local-addr=ADDR OVN_Southbound cluster local address \
  (default: $DB_SB_CLUSTER_LOCAL_ADDR)
  --db-sb-cluster-local-port=PORT OVN_Southbound cluster local tcp port \
  (default: $DB_SB_CLUSTER_LOCAL_PORT)
  --db-sb-cluster-local-proto=PROTO OVN_Southbound cluster local db transport \
  (default: $DB_SB_CLUSTER_LOCAL_PROTO)
  --db-sb-cluster-remote-addr=ADDR OVN_Southbound cluster remote address \
  (default: $DB_SB_CLUSTER_REMOTE_ADDR)
  --db-sb-cluster-remote-port=PORT OVN_Southbound cluster remote tcp port \
  (default: $DB_SB_CLUSTER_REMOTE_PORT)
  --db-sb-cluster-remote-proto=PROTO OVN_Southbound cluster remote db \
  transport (default: $DB_SB_CLUSTER_REMOTE_PROTO)
  --ovn-northd-nb-db=NB DB address(es) (default: $OVN_NORTHD_NB_DB)
  --ovn-northd-sb-db=SB DB address(es) (default: $OVN_NORTHD_SB_DB)
  --db-nb-use-remote-in-db=yes|no OVN_Northbound db listen on target connection table (default: $DB_NB_USE_REMOTE_IN_DB)
  --db-sb-use-remote-in-db=yes|no OVN_Southbound db listen on target connection table (default: $DB_SB_USE_REMOTE_IN_DB)

Default directories with "configure" option and environment variable override:
  logs: /usr/local/var/log/openvswitch (--with-logdir, OVS_LOGDIR)
  pidfiles and sockets: /usr/local/var/run/openvswitch (--with-rundir, OVS_RUNDIR)
  ovn-nb.db: /usr/local/etc/openvswitch (--with-dbdir, OVS_DBDIR)
  ovn-sb.db: /usr/local/etc/openvswitch (--with-dbdir, OVS_DBDIR)
  system configuration: /usr/local/etc (--sysconfdir, OVS_SYSCONFDIR)
  data files: /usr/local/share/openvswitch (--pkgdatadir, OVS_PKGDATADIR)
  user binaries: /usr/local/bin (--bindir, OVS_BINDIR)
  system binaries: /usr/local/sbin (--sbindir, OVS_SBINDIR)
EOF
}

set_defaults
command=
for arg
do
    case $arg in
        -h | --help)
            usage
            ;;
        --[a-z]*=*)
            option=`expr X"$arg" : 'X--\([^=]*\)'`
            value=`expr X"$arg" : 'X[^=]*=\(.*\)'`
            type=string
            set_option
            ;;
        --no-[a-z]*)
            option=`expr X"$arg" : 'X--no-\(.*\)'`
            value=no
            type=bool
            set_option
            ;;
        --[a-z]*)
            option=`expr X"$arg" : 'X--\(.*\)'`
            value=yes
            type=bool
            set_option
            ;;
        -*)
            echo >&2 "$0: unknown option \"$arg\" (use --help for help)"
            exit 1
            ;;
        *)
            if test X"$command" = X; then
                command=$arg
            else
                echo >&2 "$0: exactly one non-option argument required (use --help for help)"
                exit 1
            fi
            ;;
    esac
done
case $command in
    start_northd)
        start_northd
        ;;
    start_ovsdb)
        start_ovsdb
        ;;
    start_nb_ovsdb)
        start_nb_ovsdb
        ;;
    start_sb_ovsdb)
        start_sb_ovsdb
        ;;
    start_controller)
        start_controller
        ;;
    start_controller_vtep)
        start_controller_vtep
        ;;
    stop_northd)
        stop_northd
        ;;
    stop_ovsdb)
       stop_ovsdb
        ;;
    stop_nb_ovsdb)
       stop_nb_ovsdb
        ;;
    stop_sb_ovsdb)
       stop_sb_ovsdb
        ;;
    stop_controller)
        stop_controller
        ;;
    stop_controller_vtep)
        stop_controller_vtep
        ;;
    restart_northd)
        restart_northd
        ;;
    restart_ovsdb)
        restart_ovsdb
        ;;
    restart_nb_ovsdb)
        restart_nb_ovsdb
        ;;
    restart_sb_ovsdb)
        restart_sb_ovsdb
        ;;
    restart_controller)
        restart_controller
        ;;
    restart_controller_vtep)
        restart_controller_vtep
        ;;
    status_northd)
        daemon_status ovn-northd || exit 1
        ;;
    status_ovsdb)
        status_ovsdb
        ;;
    status_controller)
        daemon_status ovn-controller || exit 1
        ;;
    status_controller_vtep)
        daemon_status ovn-controller-vtep || exit 1
        ;;
    promote_ovnnb)
        promote_ovnnb
        ;;
    promote_ovnsb)
        promote_ovnsb
        ;;
    demote_ovnnb)
        demote_ovnnb
        ;;
    demote_ovnsb)
        demote_ovnsb
        ;;
    status_ovnnb)
        status_ovnnb
        ;;
    status_ovnsb)
        status_ovnsb
        ;;
    run_nb_ovsdb)
        run_nb_ovsdb
        ;;
    run_sb_ovsdb)
        run_sb_ovsdb
        ;;
    help)
        usage
        ;;
    preheat)
        echo >&2 "$0: preheating ovn to 350 degrees F."
        exit 1
        ;;
    '')
        echo >&2 "$0: missing command name (use --help for help)"
        exit 1
        ;;
    *)
        echo >&2 "$0: unknown command \"$command\" (use --help for help)"
        exit 1
        ;;
esac
