#!/bin/bash
# ONOS developer BASH profile conveniences
# Simply include in your own .bash_aliases or .bash_profile

# Root of the ONOS source tree
export ONOS_ROOT=${ONOS_ROOT:-~/onos}

# This is a hack to remove symlinks from the ONOS_ROOT path. To be removed when
# the protobuf buck rules can handle symlinks
ONOS_ROOT=$(pushd $ONOS_ROOT >/dev/null && pwd -P && popd >/dev/null)

# Rely on developer preference for automatically ignited apps
# Default to drivers and gui2
export ONOS_APPS=${ONOS_APPS:-drivers,openflow,gui2}

# Setup some environmental context for developers
if [ -z "${JAVA_HOME}" ]; then
    if [ -x /usr/libexec/java_home ]; then
        export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
    elif [ -d /usr/lib/jvm/java-8-oracle ]; then
        export JAVA_HOME="/usr/lib/jvm/java-8-oracle"
    elif [ -d /usr/lib/jvm/java-8-openjdk-amd64 ]; then
        export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
    fi
fi

export MAVEN=${MAVEN:-~/Applications/apache-maven-3.3.9}

# Clean up Karaf environment variables to avoid issues when switching from ONOS 1.x to 2.x
unset KARAF_VERSION KARAF_ROOT KARAF_LOG

# Setup a path
export PATH="$PATH:$ONOS_ROOT/tools/dev/bin"
export PATH="$PATH:$ONOS_ROOT/tools/test/bin:$ONOS_ROOT/tools/test/scenarios/bin"
export RUN_PACK_PATH=${RUN_PACK_PATH:-$ONOS_ROOT/tools/package/runtime/bin}
export PATH="$RUN_PACK_PATH:$PATH"
export PATH="$PATH:$ONOS_ROOT/tools/build"
export PATH="$PATH:$MAVEN/bin"

# Setup cell enviroment
export ONOS_CELL=${ONOS_CELL:-local}

# Setup default web user/password
export ONOS_WEB_USER=onos
export ONOS_WEB_PASS=rocks

# Setup default location of test scenarios
export ONOS_SCENARIOS=$ONOS_ROOT/tools/test/scenarios

# Setup path to Mininet custom scripts
export ONOS_MN_PY=$ONOS_ROOT/tools/dev/mininet/onos.py
export BMV2_MN_PY=$ONOS_ROOT/tools/dev/mininet/bmv2.py

# Convenience utility to warp to various ONOS source projects
# e.g. 'o api', 'o dev', 'o'
function o {
    cd $(find $ONOS_ROOT/ -type d -and \( -name 'bazel-*' -o -name 'buck-out' -o -name '.git' -o -name 'target' -o -name 'gen-src' -o -name 'src' \) -prune -o -type d | \
        egrep "${1:-$ONOS_ROOT}" | awk '{print length($1)"\t"$1}'  | sort -n | cut -f2 | head -n 1)
}

# Short-hand for ONOS build, package and test.
alias op="SHLVL=1 bazel build //:onos"
alias ot="bazel run //:buildifier_check && bazel query 'tests(//...)' | grep -v "coverage" | SHLVL=1 xargs bazel test --test_summary=terse --test_output=errors"
alias ob="op && ot"
alias obd="SHLVL=1 bazel build //docs:external //docs:internal"

alias ok="SHLVL=1 bazel run //:onos-local --"
alias oh="onos localhost halt"

alias ol='onos-log'
alias ow='onos-watch'
alias ocl='onos-check-logs'
alias oi='setPrimaryInstance'

# Short-hand for tailing and searching the ONOS (karaf) log
alias tl='$ONOS_ROOT/tools/dev/bin/onos-local-log'

function filterLocalLog {
    tl | grep --colour=always -E -e "${1-org.onlab|org.onosproject}"
}
alias tlo='filterLocalLog'
alias tle='tlo "ERROR|WARN|Exception|Error"'

function filterLog {
    ol | grep --colour=always -E -e "${1-org.onlab|org.onosproject}"
}
alias olo='filterLog'
alias ole='olo "ERROR|WARN|Exception|Error"'

# Pretty-print JSON output
alias pp='python -m json.tool'

# Short-hand to launch Java API docs, REST API docs and ONOS GUI
alias docs='open $ONOS_ROOT/docs/target/site/apidocs/index.html'
alias rsdocs='onos-rsdocs'
alias gui='onos-gui'

# Short-hand for 'mvn clean install' for us lazy folk
alias mci='mvn clean install'

# Git annotated one-line log
alias gil='git log --oneline --decorate=short'

# Test related conveniences

# SSH to a specified ONOS instance
alias sshctl='onos-ssh'
alias sshnet='onos-ssh $OCN'

# Runs a sequence of STC scenarios until the first failure.
function stcs {
    for s in "$@"; do
        if [[ $s =~ ^[0-9]*$ ]]; then
            printf "Waiting %d seconds...\n" $s
            sleep $s
        else
            printf "Running scenario %s...\n" $s
            stc $s || return 1
        fi
    done
}

# Applies the settings in the specified topology recipe file or lists current
# topo recipe definition if no topo recipe file is given.
function topo {
    topo=${1:-""}
    case "$topo" in
    "")
        env | egrep "ONOS_TOPO"
        env | egrep "(OTD|OTL|OTH)="
        ;;

    *)
        [ ! -f $ONOS_ROOT/tools/test/topos/$1.recipe ] && echo "No such topo recipe: $1" >&2 && return 1
        unset ONOS_TOPO OTD OTL OTH ONOS_DEVICES ONOS_HOSTS
        ONOS_DEVICES=""
        ONOS_HOSTS=""
        unset $(env | sed -n 's:\(^OT[DLH][0-9]\{1,\}\)=.*:\1 :g p')
        export ONOS_TOPO=$1
        . $ONOS_ROOT/tools/test/topos/$1.recipe
        let d=1; while [ $d -le $OTD ]; do
            dev="$(printf 'of:%016x' $d)"
            export OTD$d=$dev; export ONOS_DEVICES="$ONOS_DEVICES $dev"
            let d=d+1;
        done
        let h=1; while [ $h -le $OTH ]; do
            host="$(printf '00:00:00:00:00:%02x/-1' $h)"
            export OTH$h=$host; export ONOS_HOSTS="$ONOS_HOSTS $host"
            let h=h+1
        done
        topo
    esac
}

# Lists available topo recipes
function topos {
    for topo in $(ls -1 $ONOS_ROOT/tools/test/topos/*.recipe); do
        name=$(basename $topo .recipe)
        printf "%-16s  %s\n" \
            "$([ $name = $ONOS_TOPO ] && echo $name '*' || echo $name)" \
            "$(grep '^#' $topo | head -n 1)"
    done
}

# Sets the primary instance to the specified instance number.
function setPrimaryInstance {
    export ONOS_INSTANCES=$(env | grep 'OC[0-9]*=' | sort | cut -d= -f2)

    if [ -z "${OCC1-}" ]; then
        aux=/tmp/cell-$$.aux
        let i=1
        for n in $ONOS_INSTANCES; do
            echo "export OCC$i=$n" >> $aux
            let i=i+1
        done
        [ -f $aux ] && source $aux && rm $aux
    fi

    export ONOS_CORES=$(env | grep 'OCC[0-9]*=' | sort | cut -d= -f2)
    export OCI=$(env | egrep "OC[0-9]+" | sort | egrep OC${1:-1} | cut -d= -f2)
    echo $OCI
}

# Sets minority (OCMI) and majority (OCMA) variables
function setMinorityMajority {
    nodes=($(env | grep 'OC[0-9]*=' | sort | cut -d= -f2))
    min=1
    maj=1
    node_count="${#nodes[@]}"
    if [ $node_count -gt 1 ]; then
        middle=$(expr "$node_count" / "2")
        index=0
        for node in "${nodes[@]}"; do
            if [ "$index" -gt "$middle" ]; then
                export OCMI${min}=${node}
                min=$(expr $min + 1)
            else
                export OCMA${maj}=${node}
                maj=$(expr $maj + 1)
            fi
            index=$(expr $index + 1)
        done
    fi
}

# Open Networking Foundation shared test cell warden address
export CELL_WARDEN="10.192.19.72"
export CELL_SLAVE_1=$CELL_WARDEN
export CELL_SLAVE_2="10.192.19.71"
export CELL_SLAVE_3="10.192.19.70"
export CELL_SLAVES="$CELL_SLAVE_1 $CELL_SLAVE_2 $CELL_SLAVE_3"

# Clears cell environment
function clearCell {
    unset ONOS_CELL ONOS_IP ONOS_BOOT_FEATURES
    unset OCI OCN OCT ONOS_INSTANCES ONOS_CORES ONOS_FEATURES
    unset ONOS_USER ONOS_GROUP ONOS_WEB_USER ONOS_WEB_PASS
    unset $(env | sed -n 's:\(^OC[0-9]\{1,\}\)=.*:\1 :g p')
    unset $(env | sed -n 's:\(^OCC[0-9]\{1,\}\)=.*:\1 :g p')
    unset $(env | sed -n 's:\(^OCMI[0-9]\{1,\}\)=.*:\1 :g p')
    unset $(env | sed -n 's:\(^OCMA[0-9]\{1,\}\)=.*:\1 :g p')
}

# Applies the settings in the specified cell file or lists current cell definition
# if no cell file is given.
function cell {
    cell=${1:-""}
    case "$cell" in
    "borrow")
        clearCell
        aux="/tmp/cell-$$"
        duration=${2:-0}
        spec=${3:-5+3+1}
        spec=${spec//+/%2B}
        user=${4:-$(id -un)}
        hint=${5:-""}
        query="duration=$duration&spec=$spec&user=$user&cellNameHint=$hint"
        curl -sS -X POST "http://$CELL_WARDEN:4321/?$query" -d "$(cat ~/.ssh/id_rsa.pub)" > $aux
        . $aux
        rm -f $aux
        setPrimaryInstance 1 >/dev/null
        setMinorityMajority >/dev/null
        onos-verify-cell
        topo default >/dev/null
        ;;
    "return")
        curl -sS -X DELETE "http://$CELL_WARDEN:4321/?user=${2:-$(id -un)}"
        clearCell
        ;;

    "status")
        curl -sS "http://$CELL_WARDEN:4321/" | sort
        ;;

    "")
        env | egrep "^ONOS_CELL"
        env | egrep "^OCC[0-9]+" | sort
        env | egrep "^OC[0-9]+" | sort
        env | egrep "^OC[INT]" | sort
        env | egrep "^ONOS_" | egrep -v 'ONOS_ROOT|ONOS_CELL|ONOS_INSTANCES|ONOS_CORES|ONOS_DEVICES|ONOS_HOSTS' | sort
        ;;

    *)
        [ ! -f $ONOS_ROOT/tools/test/cells/$1 ] && echo "No such cell: $1" >&2 && return 1
        clearCell
        export ONOS_USER=sdn
        export ONOS_GROUP=sdn
        export ONOS_WEB_USER=onos
        export ONOS_WEB_PASS=rocks
        export ONOS_CELL=$1
        . $ONOS_ROOT/tools/test/cells/$1
        setPrimaryInstance 1 >/dev/null
        cell
        topo default >/dev/null
    esac
}

# Lists available cells
function cells {
    for cell in $(ls -1 $ONOS_ROOT/tools/test/cells); do
        printf "%-16s  %s\n" \
            "$([ ! -z $ONOS_CELL ] && [ $cell = $ONOS_CELL ] && echo $cell '*' || echo $cell)" \
            "$(grep '^#' $ONOS_ROOT/tools/test/cells/$cell | head -n 1)"
    done
}

# Find a process by regex
function spy {
    ps -ef | egrep "$@" | grep -v egrep
}

# Kill a process by regex
function nuke {
    spy "$@" | cut -c7-11 | xargs kill
}

# Edit a cell file by providing a cell name; opens the cell file in $EDITOR.
function vicell {
    local apply=false
    local create=false
    local ${cdf:=$ONOS_CELL}
    local cpath="${ONOS_ROOT}/tools/test/cells/"

    if [ "$1" = "-h" ] ; then
        printf "usage: vicell [file] [options]\n\noptions:\n"
        printf "\t[file]: cell name (default: current cell)\n"
        printf "\t-a: apply the cell after editing\n"
        printf "\t-e: [editor] set EDITOR to [editor] (default: *vi*)\n"
        printf "\t-c: create cell file if none exist\n\n"
        return 1
    fi

    while [ $# -gt 0 ]; do
        case "$1" in
            -a) apply=true ;;
            -e) EDITOR=$2; shift ;;
            -c) create=true ;;
            *) cdf="$1" ;;
        esac
        shift
    done

    if [ ! -e "${cpath}${cdf}" ] && ! ($create) ; then
        printf "${cdf} : no such cell\n" && return 1
    fi

    if [ -z "${EDITOR}" ] || [ -x "$(which ${EDITOR})" ]; then
        unset EDITOR && vi ${cpath}${cdf}
    else
        $EDITOR ${cpath}${cdf}
    fi
    ($apply) && cell ${cdf}
}

# Autocomplete for certain utilities
. ${ONOS_ROOT}/tools/test/bin/ogroup-opts


# Load AT&T MPLS topo GEO data
alias atttopo='onos-netcfg $OCI $ONOS_ROOT/tools/test/topos/attmpls-cfg.json'

# Load UK topo GEO data
alias uktopo='onos-netcfg $OCI $ONOS_ROOT/tools/test/topos/uk-cfg.json'

# Mininet command that uses BMv2 instead of OVS
alias mn-bmv2='sudo -E mn --custom $BMV2_MN_PY --switch onosbmv2 --controller remote,ip=$OCI'
alias mn-stratum='sudo -E mn --custom $BMV2_MN_PY --switch stratum --controller remote,ip=$OCI'
