#!/bin/bash
#
# lib/neutron_ml2_mlnx
# Functions to control the configuration and operation of the neutron ml2 mlnx service
# <do not include this template file in ``stack.sh``!>

# Dependencies:
#
# - ``functions`` file
# - ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
# - <list other global vars that are assumed to be defined>

# ``stack.sh`` calls the entry points in this order:
#
# - is_neutron_ml2_mlnx_enabled
# - install_neutron_ml2_mlnx
# - configure_neutron_ml2_mlnx
# - init_neutron_ml2_mlnx
# - start_neutron_ml2_mlnx
# - stop_neutron_ml2_mlnx
# - cleanup_neutron_ml2_mlnx

# Save trace setting
XTRACE=$(set +o | grep xtrace)
set +o xtrace


# Defaults
# --------

# <define global variables here that belong to this project>

# Set up default directories
MLNX_AGENT_BINARY="$NEUTRON_BIN_DIR/neutron-mlnx-agent"

source ${DEST}/neutron_ml2_mlnx/devstack/lib/eswitchd
source ${DEST}/neutron_ml2_mlnx/devstack/lib/mlnx_dnsmasq

# Entry Points
# ------------

# cleanup_neutron_ml2_mlnx() - Remove residual data files, anything left over from previous
# runs that a clean run would need to clean up
function cleanup_neutron_ml2_mlnx {
    # kill instances (nova)
    # delete image files (glance)
    # This function intentionally left blank
    :
}

# configure_neutron_ml2_mlnx() - Set config files, create data dirs, etc
function configure_neutron_ml2_mlnx {
    if is_service_enabled mlnx-agt; then
        if [[ -z "$PHYSICAL_INTERFACE_MAPPINGS" ]] && [[ -n "$PHYSICAL_NETWORK" ]] && [[ -n "$PHYSICAL_INTERFACE" ]]; then
            PHYSICAL_INTERFACE_MAPPINGS=$PHYSICAL_NETWORK:$PHYSICAL_INTERFACE
        fi
        if [[ -n "$PHYSICAL_INTERFACE_MAPPINGS" ]]; then
            iniset /$Q_PLUGIN_CONF_FILE eswitch physical_interface_mappings $PHYSICAL_INTERFACE_MAPPINGS
        fi
        iniset /$Q_PLUGIN_CONF_FILE securitygroup noop
        if [[ -n "${UPDATE_CLIENT_ID}" ]]; then
            iniset /$NEUTRON_DCHP_CONF DEFAULT dhcp_broadcast_reply True
        fi
    fi
    if is_service_enabled eswitchd; then
        configure_eswitchd
    fi
    if is_service_enabled mlnx_dnsmasq && [[ -z "${UPDATE_CLIENT_ID}" ]]; then
        configure_mlnx_dnsmasq
    fi
}

# init_neutron_ml2_mlnx() - Initialize databases, etc.
function init_neutron_ml2_mlnx {
    # clean up from previous (possibly aborted) runs
    # create required data files
    :
}

# install_neutron_ml2_mlnx() - Collect source and prepare
function install_neutron_ml2_mlnx {
    if is_service_enabled eswitchd; then
        install_eswitchd
    fi
    setup_develop $DEST/neutron_ml2_mlnx
}

# start_neutron_ml2_mlnx() - Start running processes, including screen
function start_neutron_ml2_mlnx {
    # The quoted command must be a single command and not include an
    # shell metacharacters, redirections or shell builtins.
    if is_service_enabled eswitchd; then
        start_eswitchd
    fi
    if is_service_enabled mlnx-agt; then
        run_process mlnx-agt "$MLNX_AGENT_BINARY --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini"
    fi
    sleep 10
}

# stop_neutron_ml2_mlnx() - Stop running processes (non-screen)
function stop_neutron_ml2_mlnx {
    if is_service_enabled eswitchd; then
        stop_eswitchd
        cleanup_eswitch
    fi
    if is_service_enabled mlnx-agt; then
        stop_process mlnx-agt
    fi
}

# Restore xtrace
$XTRACE

# Tell emacs to use shell-script-mode
## Local variables:
## mode: shell-script
## End:
