Make test tools available in installed ONOS

Part of ONOS-6597
onos-app, onos-netcfg

Change-Id: Ic58156700e357c9564e9bd9fe1173310ec6f0502
diff --git a/BUCK b/BUCK
index 773e0c6..4045d77 100644
--- a/BUCK
+++ b/BUCK
@@ -53,7 +53,6 @@
     root = 'onos-test-%s' % ONOS_VERSION,
     srcs = glob(['tools/test/**/*']) + [
                'tools/dev/bash_profile',
-               'tools/dev/bin/onos-app',
                'tools/dev/bin/onos-create-app',
                'tools/build/envDefaults'
            ],
diff --git a/tools/dev/bash_profile b/tools/dev/bash_profile
index 65c7003..6308334 100644
--- a/tools/dev/bash_profile
+++ b/tools/dev/bash_profile
@@ -25,6 +25,7 @@
 # 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 PATH="$ONOS_ROOT/tools/package/runtime/bin:$PATH"
 export PATH="$PATH:$ONOS_ROOT/tools/build"
 export PATH="$PATH:$MAVEN/bin:$KARAF_ROOT/bin"
 
diff --git a/tools/package/BUCK b/tools/package/BUCK
index 4c63cc7..4e8ab7e 100644
--- a/tools/package/BUCK
+++ b/tools/package/BUCK
@@ -20,7 +20,7 @@
 
 genrule(
   name = 'onos-karaf',
-  srcs = glob(['bin/*', 'etc/*', 'init/*']),
+  srcs = glob(['bin/*', 'etc/*', 'init/*', 'runtime/bin/*']),
   bash = '$(location :onos-prep-karaf) $OUT $(location %s) %s $(location %s)'\
             % (KARAF, ONOS_VERSION, BRANDING),
   out = 'karaf.zip',
@@ -41,7 +41,7 @@
 
 genrule(
   name = 'onos-package',
-  srcs = glob(['bin/*', 'etc/*', 'init/*', 'config/*']),
+  srcs = glob(['bin/*', 'etc/*', 'init/*', 'config/*', 'runtime/bin/*']),
   out = 'onos.tar.gz',
   bash = '$(exe //buck-tools:onos-stage) $OUT ' + ONOS_VERSION + ' $(location :onos-karaf) ' + ' '.join(sources),
   visibility = [ 'PUBLIC' ],
diff --git a/tools/package/onos-prep-karaf b/tools/package/onos-prep-karaf
index 8e83fdf..a6ab98f 100755
--- a/tools/package/onos-prep-karaf
+++ b/tools/package/onos-prep-karaf
@@ -52,6 +52,7 @@
 
 # Stage the ONOS admin scripts and patch in Karaf service wrapper extras
 cp -r bin $PREFIX
+cp -r runtime/bin/* $PREFIX/bin/
 cp -r init $PREFIX
 cp -r etc/* $PREFIX/$KARAF_DIR/etc/
 
diff --git a/tools/package/runtime/bin/_find-node b/tools/package/runtime/bin/_find-node
new file mode 100644
index 0000000..51f6364
--- /dev/null
+++ b/tools/package/runtime/bin/_find-node
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+
+#
+# Copyright 2015-present Open Networking Laboratory
+#
+# 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.
+#
+
+# -----------------------------------------------------------------------------
+# Utility for converting a number of a node in a cell to the node's address
+# -----------------------------------------------------------------------------
+
+validate_number () {
+    local re="^[0-9]+$"
+    if [[ ! $1 =~ $re ]] ; then
+    return 1
+    fi
+
+    return 0
+}
+
+find_node () {
+    if validate_number $1 ; then
+    # input is a number, try to find if an OC node is defined
+
+    oc_try="OC$1"
+    node=${!oc_try}
+
+    if [ -n "$node" ]; then
+            # node lookup succeeded, return node
+        echo $node
+    else
+            # node lookup failed, return original input
+        echo $1
+    fi
+
+    else
+    echo $1
+    fi
+
+    return 0
+}
diff --git a/tools/dev/bin/onos-app b/tools/package/runtime/bin/onos-app
similarity index 69%
rename from tools/dev/bin/onos-app
rename to tools/package/runtime/bin/onos-app
index d724f96..c07d327 100755
--- a/tools/dev/bin/onos-app
+++ b/tools/package/runtime/bin/onos-app
@@ -1,9 +1,35 @@
 #!/bin/bash
+
+#
+# Copyright 2015-present Open Networking Laboratory
+#
+# 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.
+#
+
 # -----------------------------------------------------------------------------
 # Tool to manage ONOS applications using REST API.
 # -----------------------------------------------------------------------------
 
-node=${1:-$OCI}
+# If ONOS_HOME is set, respect its value.
+# If ONOS_HOME is not set (e.g. in the init or service environment),
+# set it based on this script's path.
+ONOS_HOME=${ONOS_HOME:-$(cd $(dirname $0)/.. >/dev/null 2>&1 && pwd)}
+ONOS_WEB_USER=${ONOS_WEB_USER:-onos} # ONOS WEB User defaults to 'onos'
+ONOS_WEB_PASS=${ONOS_WEB_PASS:-rocks} # ONOS WEB Password defaults to 'rocks'
+
+. ${ONOS_HOME}/bin/_find-node
+
+node=$(find_node $1)
 cmd=${2:-list}
 app=${3}
 
diff --git a/tools/package/runtime/bin/onos-netcfg b/tools/package/runtime/bin/onos-netcfg
new file mode 100755
index 0000000..fe19d8e
--- /dev/null
+++ b/tools/package/runtime/bin/onos-netcfg
@@ -0,0 +1,59 @@
+#!/bin/bash
+
+#
+# Copyright 2015-present Open Networking Laboratory
+#
+# 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.
+#
+
+# -----------------------------------------------------------------------------
+# ONOS network configuration uploader.
+# -----------------------------------------------------------------------------
+
+# If ONOS_HOME is set, respect its value.
+# If ONOS_HOME is not set (e.g. in the init or service environment),
+# set it based on this script's path.
+ONOS_HOME=${ONOS_HOME:-$(cd $(dirname $0)/.. >/dev/null 2>&1 && pwd)}
+ONOS_WEB_USER=${ONOS_WEB_USER:-onos} # ONOS WEB User defaults to 'onos'
+ONOS_WEB_PASS=${ONOS_WEB_PASS:-rocks} # ONOS WEB Password defaults to 'rocks'
+
+. ${ONOS_HOME}/bin/_find-node
+
+fail="--fail"
+[ "$1" == "-v" ] && shift && fail=""
+
+node=$(find_node $1)
+file="${2}"
+url="${3}"
+
+if [ "$node" == "" -o "$file" == "" ]; then
+     echo "Usage: onos-netcfg [-v] node file|DELETE [url]"
+     exit 1
+fi
+
+method="POST"
+[ $(echo $file | awk '{print tolower($0)}') == "delete" ] && method="DELETE"
+
+if [ $method == "POST" ]; then
+    # Validate JSON
+    cat $file | python -m json.tool >> /dev/null
+    if [ "$?" -ne "0" ]; then
+        echo "Not valid JSON File" && exit 1
+    fi
+    curl $fail -sSL --user $ONOS_WEB_USER:$ONOS_WEB_PASS \
+        -X POST -H 'Content-Type:application/json' \
+        http://$node:8181/onos/v1/network/configuration/${url} -d@$file
+elif [ $method == "DELETE" ]; then
+    curl $fail -sSL --user $ONOS_WEB_USER:$ONOS_WEB_PASS \
+        -X DELETE http://$node:8181/onos/v1/network/configuration/${url}
+fi
diff --git a/tools/test/bin/onos-netcfg b/tools/test/bin/onos-netcfg
deleted file mode 100755
index a308bbc..0000000
--- a/tools/test/bin/onos-netcfg
+++ /dev/null
@@ -1,32 +0,0 @@
-#!/bin/bash
-# -----------------------------------------------------------------------------
-# ONOS network configuration uploader.
-# -----------------------------------------------------------------------------
-
-[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
-. $ONOS_ROOT/tools/build/envDefaults
-. $ONOS_ROOT/tools/test/bin/find-node.sh
-
-fail="--fail"
-[ "$1" == "-v" ] && shift && fail=""
-
-node=$(find_node $1)
-file="${2:-$ONOS_ROOT/tools/test/topos/oe-linear-3.json}"
-url="${3}"
-
-method="POST"
-[ $(echo $file | awk '{print tolower($0)}') == "delete" ] && method="DELETE"
-
-if [ $method == "POST" ]; then
-    # Validate JSON
-    cat $file | python -m json.tool >> /dev/null
-    if [ "$?" -ne "0" ]; then
-        echo "Not valid JSON File" && exit 1
-    fi
-    curl $fail -sSL --user $ONOS_WEB_USER:$ONOS_WEB_PASS \
-        -X POST -H 'Content-Type:application/json' \
-        http://$node:8181/onos/v1/network/configuration/${url} -d@$file
-elif [ $method == "DELETE" ]; then
-    curl $fail -sSL --user $ONOS_WEB_USER:$ONOS_WEB_PASS \
-        -X DELETE http://$node:8181/onos/v1/network/configuration/${url}
-fi