# Defaults for nsx-agent initscript
# sourced by /etc/init.d/nsx-agent
# installed at /etc/default/nsx-agent by the maintainer scripts

VARS="""CLOUDNET_DATA_DIR \
        ONIX_CORE_LIMIT \
        ONIX_FD_LIMIT \
        VERBOSE_LEVEL \
        ONIX_MGMT_TIMEOUT \
        ONIX_OPENFLOW_TIMEOUT \
        ONIX_OVS_MGMT_TIMEOUT \
        ONIX_OVS_OPENFLOW_TIMEOUT \
        ONIX_RPC_TCP_TIMEOUT \
        UPGRADE_PLUGIN \
        TRANSLATED_SOCKET \
        CLOUDNET_LOCATION \
        EXTRA_ARGS"""

declare -A ENVVARS
# save any env variables that are set
for v in $VARS; do
    if [ -n "${!v}" ]; then
        ENVVARS[$v]=${!v}
    fi
done

# CLOUDNET_DATA_DIR: Where Onix components can keep data on disk
CLOUDNET_DATA_DIR=/var/vmware/nsx-agent/data

# ONIX_CORE_LIMIT: Maximum size for core dumps.
#
# Leaving this unset will use the system default.  Setting it to 0
# will disable core dumps.  Setting it to "unlimited" will dump all
# core files regardless of size.
ONIX_CORE_LIMIT=unlimited

# ONIX_FD_LIMIT:
#
# This sets the maximum number of file descriptors the NOX process
# may have open at the same time.  Leaving this unset will use the
# system default (usually 1024). On my debian, the max number of descriptors
# allowed for ALL processes is 334822.
ONIX_FD_LIMIT=100000

# VERBOSE_LEVEL:
#
# How much debugging output should be written to the log files?
# 0 = lowest
VERBOSE_LEVEL=1

# ONIX_MGMT_TIMEOUT:
#
# How many milliseconds should Onix wait before disconnecting an idle OVSDB
# management connection (default 60000 = 60 seconds)
ONIX_MGMT_TIMEOUT=60000

# ONIX_OPENFLOW_TIMEOUT:
#
# How many seconds should Onix wait before disconnecting an idle openflow
# connection (default 60 seconds)
ONIX_OPENFLOW_TIMEOUT=60

# ONIX_OVS_MGMT_TIMEOUT:
#
# How many idle milliseconds should OVS wait before disconnecting an idle
# management connection.
ONIX_OVS_MGMT_TIMEOUT=60000

# ONIX_OVS_OPENFLOW_TIMEOUT:
#
# How many idle milliseconds should OVS wait before disconnecting an idle
# openflow connection.
ONIX_OVS_OPENFLOW_TIMEOUT=20000

# ONIX_RPC_TCP_TIMEOUT:
#
# How many idle milliseconds should controllers wait before disconnecting an
# idle rpc-tcp connection to another controller.
ONIX_RPC_TCP_TIMEOUT=20000

#######################
# Arguments relating to distribution
# These can all be overridden in cloudnet-extra or in the environment
#######################


# WEBSERVER_IF:
#
# The interface on which to listen for webserver API requests.
# Defaults to "" (all interfaces).
WEBSERVER_IF=""

# UPGRADE_PLUGIN:
#
# The name of the plugin component (implementing the TStorageInitRPCPlugin
# interface) that upgrades info in the CDB to make an old snapshot
# compatible with a new NVP version.
# example: UPGRADE_PLUGIN="python-test-upgrade-name-to-upper"

# TRANSLATED_SOCKET:
#
# An absolute path to the Unix domain socket that the translation daemon is
# supposed to be listening on.
TRANSLATED_SOCKET=/var/run/nicira-nvp-translated

#######################

CLOUDNET_LOCATION="/opt/vmware/nsx-agent/bin"
NVP_BIN_LOCATION="/opt/nvp/bin"

# since this file is automatically overwritten on install, we use
# another file to override custom values for the defaults in this file.
# Environment variables can, in turn, override those settings.
if [ -f /etc/default/cloudnet-extra ]; then
    source /etc/default/cloudnet-extra
fi

# bring env variables back to override the values in the file
for v in ${!ENVVARS[@]}; do
    eval "$v=\"${ENVVARS[$v]}\""
done

##############################################

function output_comp() {
    local comp=$1
    local args=$2
    if [ -n "$args" ]; then
        printf "$comp=$args"
    else
        printf "$comp"
    fi
}

UPGRADE_COMPONENTS=""
if [ -n "$UPGRADE_PLUGIN" ]; then
   UPGRADE_COMPONENTS="$UPGRADE_PLUGIN tstorage-server=plugin=$CLOUDNET_LOCATION/cpp/storage/tstorage-init-rpc-plugin.so"
fi

NSX_ISSUE_FILE="/etc/nsx_issue"
NSX_BMS="nsx-bms"
NSX_BMC="nsx-bmc"
is_bms_node() {
    if [ -f $NSX_ISSUE_FILE ] && grep -q $NSX_BMS $NSX_ISSUE_FILE; then
       return 0
    fi
    return 1
}
is_bmc_node() {
    if [ -f $NSX_ISSUE_FILE ] && grep -q $NSX_BMC $NSX_ISSUE_FILE; then
       return 0
    fi
    return 1
}

BMS_APP_ARGS=""
NSX_AGENT_INIT_ARGS=""
if is_bms_node; then
    BMS_APP_ARGS="""`output_comp bms-app enable_bms=true`"""
    NSX_AGENT_INIT_ARGS="enable_bms=true"
fi

BMC_APP_ARGS=""
BMC_PIPE_APP_ARGS=""
if is_bmc_node; then
    BMC_APP_ARGS="""`output_comp bmc-app enable_bmc=true`"""
    BMC_PIPE_APP_ARGS="""`output_comp vm-command-relay chroot_path=/var/run/vmware/nsx-agent`"""
    NSX_AGENT_INIT_ARGS="enable_bmc=true"
fi

MGMT_TIMEOUTS="min_tcp_keepalive_timeout_ms=$[ONIX_MGMT_TIMEOUT/2],max_tcp_keepalive_timeout_ms=$ONIX_MGMT_TIMEOUT"

OPENFLOW_TIMEOUTS="connection_timeout_secs=$ONIX_OPENFLOW_TIMEOUT"

OVS_TIMEOUTS="ovs_config_timeout_ms=$[ONIX_OVS_MGMT_TIMEOUT/2],ovs_openflow_timeout_ms=$[ONIX_OVS_OPENFLOW_TIMEOUT/2]"

RPC_TCP_TIMEOUTS="min_tcp_keepalive_timeout_ms=$[ONIX_RPC_TCP_TIMEOUT/2],max_tcp_keepalive_timeout_ms=$ONIX_RPC_TCP_TIMEOUT"

first_comps="""\
    `output_comp hypervisor:chassis` \
    `output_comp connection-cluster:null`"""

CLOUDNET_ARGS="""-l $CLOUDNET_LOCATION\
                 $first_comps \
                 $EXTRA_ARGS \
                 $UPGRADE_COMPONENTS \
                 `output_comp nlog nlog_hypervisor_chassis_enable_fixed_point=true` \
                 `output_comp nsxa-ctl-server` \
                 `output_comp nsx-agent-init $NSX_AGENT_INIT_ARGS` \
                 $BMS_APP_ARGS $BMC_APP_ARGS $BMC_PIPE_APP_ARGS \
                 `output_comp json-rpc-broker listen=punix:/var/run/vmware/nsx-agent/nsxagent_ovsdb.sock,$MGMT_TIMEOUTS` \
                 `output_comp openflow-config-export-ovsdb $OVS_TIMEOUTS` \
                 `output_comp openflow punix:/var/run/vmware/nsx-agent/nsxagent_vswitchd.sock,check_connection_ids=true,$OPENFLOW_TIMEOUTS` \
                 """
