#!/bin/bash
# -----------------------------------------------------------------------------
# Provides a unioform way to simulate power on/off control over an ONOS node.
# If environment variable HARD_POWER_OFF is set and the ONOS_CELL value
# indicates that the cell is a borrowed shared cell LXC machine, it will use
# a REST call to warden, which then uses lxc-stop/lxc-start command to
# simulate a power control operations. Otherwise, it will just use the soft
# 'onos-die' command.
# -----------------------------------------------------------------------------

function usage() {
    echo "usage: $(basename $0) node-ip {on|off} [user]" >&2 && exit 1
}

[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
. $ONOS_ROOT/tools/build/envDefaults

node=${1:-$OCI}
cmd=${2:-on}
user=${3:$(id -un)}

if [ "$node" = "--help" ]; then
    usage

elif [ "$cmd" = "on" ]; then
    if [ -n "$HARD_POWER_OFF" -a $ONOS_CELL = "borrow" ]; then
        curl -sS -X PUT "http://$CELL_WARDEN:4321/?cmd=powerOn&nodeIp=$node&user=$user"
    else
        onos-service $node start
    fi

elif [ "$cmd" = "off" ]; then
    if [ -n "$HARD_POWER_OFF" -a $ONOS_CELL = "borrow" ]; then
        curl -sS -X PUT "http://$CELL_WARDEN:4321/?cmd=powerOff&nodeIp=$node&user=$user"
    else
        onos-die $node
    fi

else
    usage
fi

