Making REST API port as a command-line option for various admin commands.

Also cleaning up usage for consistency.

Change-Id: I1d8a10c063cab5992033b97d6efa60bba030ed9e
diff --git a/tools/package/runtime/bin/_check-json b/tools/package/runtime/bin/_check-json
index 2fde753..1eef25b 100644
--- a/tools/package/runtime/bin/_check-json
+++ b/tools/package/runtime/bin/_check-json
@@ -21,7 +21,6 @@
 # Utility for checking syntax of JSON files
 # -----------------------------------------------------------------------------
 
-
 checkJson() {
     cat ${1} | python -m json.tool >> /dev/null
     if [ "$?" -ne "0" ]; then
diff --git a/tools/package/runtime/bin/_rest-port b/tools/package/runtime/bin/_rest-port
new file mode 100644
index 0000000..15c2b15
--- /dev/null
+++ b/tools/package/runtime/bin/_rest-port
@@ -0,0 +1,44 @@
+#!/bin/bash
+
+
+#
+# Copyright 2015-present Open Networking Foundation
+#
+# 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 handling common REST API usage.
+# -----------------------------------------------------------------------------
+
+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_WEB_PORT=${ONOS_WEB_PORT:-8181}  # REST API port defaults to '8181'
+
+port=${ONOS_WEB_PORT}
+user=${ONOS_WEB_USER}
+password=${ONOS_WEB_PASS}
+fail="--fail"
+
+while getopts P:u:p:v?h o; do
+    case "$o" in
+        P) port=$OPTARG;;
+        u) user=$OPTARG;;
+        p) password=$OPTARG;;
+        v) fail="";;
+        *) usage;;
+    esac
+done
+
+let OPC=$OPTIND-1
+shift $OPC
diff --git a/tools/package/runtime/bin/onos-app b/tools/package/runtime/bin/onos-app
index 7ea93a7..4d5441d 100755
--- a/tools/package/runtime/bin/onos-app
+++ b/tools/package/runtime/bin/onos-app
@@ -19,29 +19,28 @@
 # -----------------------------------------------------------------------------
 # Tool to manage ONOS applications using REST API.
 # -----------------------------------------------------------------------------
+function usage {
+    echo "usage: onos-app [options] <node-ip> list" >&2
+    echo "       onos-app [options] <node-ip> {install|install!} <app-file>" >&2
+    echo "       onos-app [options] <node-ip> {reinstall|reinstall!} [<app-name>] <app-file>" >&2
+    echo "       onos-app [options] <node-ip> {activate|deactivate|uninstall} <app-name>" >&2
+    echo ""
+    echo "options: [-P port] [-u user] [-p password] [-v]"
+    exit 1
+}
 
-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'
-
+. $(dirname $0)/_rest-port
 . $(dirname $0)/_find-node
 
 node=$(find_node $1)
 cmd=${2:-list}
 app=${3}
 
-export URL=http://$node:8181/onos/v1/applications
+export URL=http://$node:$port/onos/v1/applications
 export HDR="-HContent-Type:application/octet-stream"
 export HAJ="-HContent-Type:application/json"
-export curl="curl -sS --user $ONOS_WEB_USER:$ONOS_WEB_PASS --noproxy localhost "
+export curl="curl $fail -sSL --user $user:$password --noproxy ${node} "
 
-# Prints usage help
-function usage {
-    echo "usage: onos-app <node-ip> list" >&2
-    echo "       onos-app <node-ip> {install|install!} <app-file>" >&2
-    echo "       onos-app <node-ip> {reinstall|reinstall!} [<app-name>] <app-file>" >&2
-    echo "       onos-app <node-ip> {activate|deactivate|uninstall} <app-name>" >&2
-    exit 1
-}
 
 # Extract app name from the specified *.oar file
 function appName {
diff --git a/tools/package/runtime/bin/onos-cfg b/tools/package/runtime/bin/onos-cfg
index 6855d03..abb3be6 100755
--- a/tools/package/runtime/bin/onos-cfg
+++ b/tools/package/runtime/bin/onos-cfg
@@ -19,10 +19,12 @@
 # -----------------------------------------------------------------------------
 # Tool to manage ONOS component configurations using REST API.
 # -----------------------------------------------------------------------------
+usage() {
+    echo "usage: onos-cfg [-P port] [-u user] [-p password] [-v] \\"
+    echo "          node [list|post|delete] component [JSON file if posting or deleting]"
+}
 
-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'
-
+. $(dirname $0)/_rest-port
 . $(dirname $0)/_find-node
 . $(dirname $0)/_check-json
 
@@ -34,12 +36,8 @@
 component=${3}
 file=${4}
 
-export URL=http://$node:8181/onos/v1/configuration/${component}
-export curl="curl ${fail} -sS --user $ONOS_WEB_USER:$ONOS_WEB_PASS --noproxy ${node} "
-
-usage() {
-    echo "Usage: onos-cfg node [list|post|delete] component [JSON file if posting or deleting]"
-}
+export URL=http://$node:$port/onos/v1/configuration/${component}
+export curl="curl ${fail} -sSL --user $user:$password --noproxy ${node} "
 
 if [ "$node" == "" -o "$component" == "" ]; then
      usage && exit 1
@@ -50,8 +48,7 @@
         ${curl} -X GET ${URL} && echo;;
     post|delete)
         checkJson "$file"
-        $curl -X $cmd -H 'Content-Type:application/json' \
-            ${URL} -d@$file && echo;;
+        $curl -X $cmd -H 'Content-Type:application/json' ${URL} -d@$file && echo;;
     *) usage && exit 1;;
 esac
 
diff --git a/tools/package/runtime/bin/onos-compile-yang b/tools/package/runtime/bin/onos-compile-yang
index e1bb987..22044b5 100755
--- a/tools/package/runtime/bin/onos-compile-yang
+++ b/tools/package/runtime/bin/onos-compile-yang
@@ -19,23 +19,20 @@
 # -----------------------------------------------------------------------------
 # Tool to compile the specified YANG file(s) using the ONOS live compilation.
 # -----------------------------------------------------------------------------
+function usage {
+    echo "usage: onos-compile-yang [-P port] [-u user] [-p password] [-v] \\"
+    echo "          <yang-file|zip-file|jar-file|directory>" >&2
+    exit 1
+}
 
-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'
-
+. $(dirname $0)/_rest-port
 . $(dirname $0)/_find-node
 
 node=$(find_node $1)
 yang=$2
 
-export URL=http://$node:8181/onos/yang/models
-export curl="curl -sS --user $ONOS_WEB_USER:$ONOS_WEB_PASS --noproxy localhost "
-
-# Prints usage help
-function usage {
-    echo "usage: onos-compile-yang <yang-file|zip-file|jar-file|directory>" >&2
-    exit 1
-}
+export URL=http://$node:$port/onos/yang/models
+export curl="curl $fail -sSL --user $user:$password --noproxy ${node} "
 
 [ -z $node -o "$node" = "-h" -o "$node" = "--help" -o "$node" = "-?" ] && usage
 
diff --git a/tools/package/runtime/bin/onos-diagnostics b/tools/package/runtime/bin/onos-diagnostics
index 9cccb0e..ee610d1 100755
--- a/tools/package/runtime/bin/onos-diagnostics
+++ b/tools/package/runtime/bin/onos-diagnostics
@@ -19,14 +19,8 @@
 # -----------------------------------------------------------------------------
 # Tool to collect cluster-wide diagnostics into a single tar stream.
 # -----------------------------------------------------------------------------
-
-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'
-
-. $(dirname $0)/_find-node
-
 function usage() {
-    echo "usage: $(basename $0) [-x] [-n name] [-u user] [-p password] [ip1 ip2...]"
+    echo "usage: $(basename $0) [-x] [-n name] [-P port] [-u user] [-p password] [ip1 ip2...]"
     echo ""
     echo "Environment Variables:"
     echo "    ONOS_INSTANCES    IPs or hostnames of ONOS cluster machines"
@@ -56,6 +50,12 @@
     exit 1
 }
 
+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_WEB_PORT=${ONOS_WEB_PORT:-8181}  # REST API port defaults to '8181'
+
+. $(dirname $0)/_find-node
+
 # TODO We should make SR commands optional
 CLI_COMMANDS=(
     "feature:repo-list"
@@ -105,10 +105,15 @@
     "mcast-host-show"
 )
 
+port=${ONOS_WEB_PORT}
+user=${ONOS_WEB_USER}
+password=${ONOS_WEB_PASS}
+
 # Scan arguments for user/password or other options...
-while getopts n:u:p:x?h o; do
+while getopts n:P:u:p:x?h o; do
     case "$o" in
         n) name=$OPTARG;;
+        P) port=$OPTARG;;
         u) user=$OPTARG;;
         p) password=$OPTARG;;
         x) extract=true;;
@@ -116,8 +121,6 @@
     esac
 done
 
-user=${user:-$ONOS_WEB_USER}
-password=${password:-$ONOS_WEB_PASS}
 let OPC=$OPTIND-1
 shift $OPC
 
@@ -138,7 +141,7 @@
     # Acquire locally obtained diagnostics via REST API and extract them
     printf "logs "
     curl -sS --fail --user $user:$password  \
-        http://$node:8181/onos/v1/diagnostics > ../$node.tar.gz
+        http://$node:$port/onos/v1/diagnostics > ../$node.tar.gz
     tar zxf ../$node.tar.gz && rm ../$node.tar.gz
 
     # Acquire remotely obtained diagnostics via ssh CLI
diff --git a/tools/package/runtime/bin/onos-netcfg b/tools/package/runtime/bin/onos-netcfg
index 5e22836..08635de 100755
--- a/tools/package/runtime/bin/onos-netcfg
+++ b/tools/package/runtime/bin/onos-netcfg
@@ -19,34 +19,30 @@
 # -----------------------------------------------------------------------------
 # ONOS network configuration uploader.
 # -----------------------------------------------------------------------------
+function usage() {
+     echo "usage: onos-netcfg [-v] [-P port] [-u user] [-p password] node file|DELETE [url]"
+     exit 1
+}
 
-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'
-
+. $(dirname $0)/_rest-port
 . $(dirname $0)/_find-node
 . $(dirname $0)/_check-json
 
-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
+export URL="http://$node:$port/onos/v1/network/configuration/${url}"
+export curl="curl ${fail} -sSL --user $user:$password --noproxy ${node} "
+
+[ "$node" == "" -o "$file" == "" ] && usage;
 
 method="POST"
 [ $(echo $file | awk '{print tolower($0)}') == "delete" ] && method="DELETE"
 
 if [ $method == "POST" ]; then
     checkJson $file
-    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
+    $curl -X POST -H 'Content-Type:application/json' $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}
+    $curl -X DELETE $URL
 fi