Improving stw and adding ability to clean-up mininet properly between runs.

Change-Id: Idb163bfa0cfd13b0ed7b9e4715ca7a2c0ffe69f6
diff --git a/tools/test/bin/ogroup-opts b/tools/test/bin/ogroup-opts
index fe35be2..cdc623f 100644
--- a/tools/test/bin/ogroup-opts
+++ b/tools/test/bin/ogroup-opts
@@ -12,15 +12,16 @@
 complete -F _ogroup-opts onos-group
 
 
-# Tab completion settings for stc
+# Tab completion settings for stc & stw
 function _stc-opts () {
   local cur=${COMP_WORDS[COMP_CWORD]}
-  if [ $COMP_CWORD -eq 1 ]; then
+  if [ $COMP_CWORD -ge 1 ]; then
     COMPREPLY=( $( compgen -W "$(cd $ONOS_ROOT/tools/test/scenarios && ls -1 | sed 's/.xml//g')" -- $cur ) )
   fi
 }
 
 complete -F _stc-opts stc
+complete -F _stc-opts stw
 
 
 # Tab completion settings for stl
diff --git a/tools/test/bin/onos-mininet b/tools/test/bin/onos-mininet
index ef2c8ea..4ce341e 100755
--- a/tools/test/bin/onos-mininet
+++ b/tools/test/bin/onos-mininet
@@ -66,4 +66,8 @@
         $mininet -X "stuff \"^C\\n\"" 2>/dev/null && \
         $mininet -X "stuff \"exit\\n\"" 2>/dev/null
     ;;
+cleanup)
+    ssh -t -t $remote sudo mn -c
+    ssh -t -t $remote "ps -ef | grep 'tail -n1 screenlog.0' | grep -v grep | cut -c10-15 | xargs kill -9"
+    ;;
 esac
diff --git a/tools/test/bin/stw b/tools/test/bin/stw
index f78659d..d3a00c2 100755
--- a/tools/test/bin/stw
+++ b/tools/test/bin/stw
@@ -3,13 +3,40 @@
 # Loops the System Test Coordinator invocations while success/until failure.
 #-------------------------------------------------------------------------------
 
+count=0
+unset doSetup doTeardown
+
+# Scan arguments for user/password or other options...
+while getopts c:st o; do
+    case "$o" in
+        c) count=$OPTARG;;
+        s) doSetup=true;;
+        t) toTeardown=true;;
+    esac
+done
+let OPC=$OPTIND-1
+shift $OPC
+
+if [ -n "$doSetup" ]; then
+    printf "Setting up...\n"
+    stc setup || exit 1
+fi
+
 let run=1
-while true; do
+while [ $count -le 0 -o $run -le $count ]; do
     printf "\033]0;%s\007" "STC Run #$run"
     printf "Starting run %d...\n" $run
-    stc "$@"
+    for scenario in "${@:-smoke}"; do
+        printf "Running scenario %s...\n" $scenario
+        stc $scenario
+    done
     status=$?
     printf "Finished run %d...\n" $run
-    [ $status -ne 0 ] && break
+    [ $status -ne 0 ] && exit $status
     let run=run+1
 done
+
+if [ -n "$doTeardown" ]; then
+    printf "Tearing down...\n"
+    stc teardown || exit 1
+fi
diff --git a/tools/test/scenarios/net-setup.xml b/tools/test/scenarios/net-setup.xml
index 1856f57..1312049 100644
--- a/tools/test/scenarios/net-setup.xml
+++ b/tools/test/scenarios/net-setup.xml
@@ -19,6 +19,7 @@
         <step name="Push-Topos" exec="onos-push-topos ${OCN}"/>
 
         <step name="Stop-Mininet-If-Needed" env="~" exec="onos-mininet stop"/>
+        <step name="Clean-Mininet-If-Needed" env="~" exec="onos-mininet cleanup" requires="^"/>
         <step name="Wipe-Out-Data-Before" exec="onos-wipe-out" requires="^"/>
 
         <step name="Initial-Summary-Check" requires="~Wipe-Out-Data-Before"
diff --git a/tools/test/scenarios/net-teardown.xml b/tools/test/scenarios/net-teardown.xml
index e9a17ef..829e917 100644
--- a/tools/test/scenarios/net-teardown.xml
+++ b/tools/test/scenarios/net-teardown.xml
@@ -16,6 +16,7 @@
 <scenario name="net-setup" description="Network teardown steps">
     <group name="Net-Teardown">
         <step name="Stop-Mininet" exec="onos-mininet stop"/>
+        <step name="Cleanup-Mininet" exec="onos-mininet cleanup" requires="~Stop-Mininet"/>
         <XXXstep name="Wipe-Out-Data-After" requires="~Stop-Mininet" exec="onos-wipe-out"/>
     </group>
 </scenario>
\ No newline at end of file
diff --git a/tools/test/scenarios/smoke-internal.xml b/tools/test/scenarios/smoke-internal.xml
new file mode 100644
index 0000000..01c61d5
--- /dev/null
+++ b/tools/test/scenarios/smoke-internal.xml
@@ -0,0 +1,30 @@
+<!--
+  ~ Copyright 2016 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.
+  -->
+<scenario name="smoke-internal" description="Internals of ONOS smoke test">
+    <import file="${ONOS_SCENARIOS}/net-smoke.xml"/>
+
+    <import file="${ONOS_SCENARIOS}/archetypes.xml"/>
+    <dependency name="Archetypes" requires="~Net-Smoke"/>
+
+    <import file="${ONOS_SCENARIOS}/drivers-test.xml"/>
+    <dependency name="Drivers-Test" requires="~Net-Smoke,~Archetypes"/>
+
+    <import file="${ONOS_SCENARIOS}/meta-apps-test.xml"/>
+    <dependency name="Meta-App" requires="~Net-Smoke,~Drivers-Test"/>
+
+    <import file="${ONOS_SCENARIOS}/netcfg.xml"/>
+    <dependency name="Netcfg" requires="~Meta-App"/>
+</scenario>
diff --git a/tools/test/scenarios/smoke.xml b/tools/test/scenarios/smoke.xml
index cc3f662..dbca608 100644
--- a/tools/test/scenarios/smoke.xml
+++ b/tools/test/scenarios/smoke.xml
@@ -20,21 +20,13 @@
     <import file="${ONOS_SCENARIOS}/setup.xml"/>
     <dependency name="Setup" requires="Prerequisites,Net-Prerequisites"/>
 
-    <import file="${ONOS_SCENARIOS}/net-smoke.xml"/>
+    <import file="${ONOS_SCENARIOS}/smoke-internal.xml"/>
     <dependency name="Net-Smoke" requires="Setup"/>
-
-    <import file="${ONOS_SCENARIOS}/archetypes.xml"/>
-    <dependency name="Archetypes" requires="Setup,~Net-Smoke"/>
-
-    <import file="${ONOS_SCENARIOS}/drivers-test.xml"/>
-    <dependency name="Drivers-Test" requires="Setup,~Net-Smoke,~Archetypes"/>
-
-    <import file="${ONOS_SCENARIOS}/meta-apps-test.xml"/>
-    <dependency name="Meta-App" requires="Setup,~Net-Smoke,~Drivers-Test"/>
-
-    <import file="${ONOS_SCENARIOS}/netcfg.xml"/>
-    <dependency name="Netcfg" requires="Setup,~Meta-App"/>
+    <dependency name="Archetypes" requires="Setup"/>
+    <dependency name="Drivers-Test" requires="Setup"/>
+    <dependency name="Meta-App" requires="Setup"/>
+    <dependency name="Netcfg" requires="Setup"/>
 
     <import file="${ONOS_SCENARIOS}/wrapup.xml"/>
-    <dependency name="Wrapup" requires="~Archetypes,~Setup,~Net-Smoke,~Drivers-Test,~Meta-App,~Netcfg"/>
+    <dependency name="Wrapup" requires="~Setup,~Net-Smoke,~Archetypes,~Drivers-Test,~Meta-App,~Netcfg"/>
 </scenario>