Adding ability to dump logs for failed steps.

Change-Id: Ib3cb6552018d45c0bb4066e15a6e9bc8f69e97e0
diff --git a/drivers/src/main/java/org/onosproject/driver/extensions/package-info.java b/drivers/src/main/java/org/onosproject/driver/extensions/package-info.java
index ee5d0d2..d9d2460 100644
--- a/drivers/src/main/java/org/onosproject/driver/extensions/package-info.java
+++ b/drivers/src/main/java/org/onosproject/driver/extensions/package-info.java
@@ -13,8 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 /**
- * Implementations of OpenFlow extensions.
+ * Processing of Nicira extensions.
  */
 package org.onosproject.driver.extensions;
diff --git a/tools/test/bin/stc b/tools/test/bin/stc
index 8737cf3..60a1720 100755
--- a/tools/test/bin/stc
+++ b/tools/test/bin/stc
@@ -20,6 +20,7 @@
 
 # If stcColor is not set, we will enable color if this is an interactive session
 [ -t 1 ] && interactive=true || interactive=false
+[ -t 1 ] && notInteractive=false || notInteractive=true
 
 # stc requires that ONOS_USE_SSH=true, but we will store the old value and reset it after
 sshSet=$([ -z ${ONOS_USE_SSH+x} ]) && oldSSH=$ONOS_USE_SSH
@@ -27,7 +28,8 @@
 
 # Run stc
 [ -z "$stcDebug" ] && DEBUG_OPTS=""
-stcColor=${stcColor:-$interactive} java $DEBUG_OPTS -jar $JAR $scenario "$@"
+stcColor=${stcColor:-$interactive} stcDumpLogs=${stcDumpLogs:-$notInteractive} \
+    java $DEBUG_OPTS -jar $JAR $scenario "$@"
 
 # Reset the old value of ONOS_USE_SSH
 [ $sshSet ] && export ONOS_USE_SSH=oldSSH || unset ONOS_USE_SSH
diff --git a/utils/stc/src/main/java/org/onlab/stc/Main.java b/utils/stc/src/main/java/org/onlab/stc/Main.java
index bc10ec7..ca04a7c 100644
--- a/utils/stc/src/main/java/org/onlab/stc/Main.java
+++ b/utils/stc/src/main/java/org/onlab/stc/Main.java
@@ -16,13 +16,16 @@
 package org.onlab.stc;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.io.Files;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.servlet.ServletHandler;
 import org.eclipse.jetty.util.log.Logger;
 import org.onlab.stc.Coordinator.Status;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.List;
@@ -64,10 +67,12 @@
     private String runToPatterns = "";
 
     private Coordinator coordinator;
+    private Compiler compiler;
     private Monitor monitor;
     private Listener delegate = new Listener();
 
     private static boolean useColor = Objects.equals("true", System.getenv("stcColor"));
+    private static boolean dumpLogs = Objects.equals("true", System.getenv("stcDumpLogs"));
 
     // usage: stc [<scenario-file>] [run]
     // usage: stc [<scenario-file>] run [from <from-patterns>] [to <to-patterns>]]
@@ -113,7 +118,7 @@
             Scenario scenario = Scenario.loadScenario(new FileInputStream(scenarioFile));
 
             // Elaborate scenario
-            Compiler compiler = new Compiler(scenario);
+            compiler = new Compiler(scenario);
             compiler.compile();
 
             // Setup the process flow coordinator
@@ -221,7 +226,7 @@
     /**
      * Internal delegate to monitor the process execution.
      */
-    private static class Listener implements StepProcessListener {
+    private class Listener implements StepProcessListener {
         @Override
         public void onStart(Step step, String command) {
             logStatus(currentTimeMillis(), step.name(), IN_PROGRESS, command);
@@ -230,6 +235,9 @@
         @Override
         public void onCompletion(Step step, Status status) {
             logStatus(currentTimeMillis(), step.name(), status, null);
+            if (dumpLogs && !(step instanceof Group) && status == FAILED) {
+                dumpLogs(step);
+            }
         }
 
         @Override
@@ -246,6 +254,18 @@
         }
     }
 
+    // Dumps the step logs to standard output.
+    private void dumpLogs(Step step) {
+        File logFile = new File(compiler.logDir(), step.name() + ".log");
+        try {
+            print(">>>>>");
+            Files.copy(logFile, System.out);
+            print("<<<<<");
+        } catch (IOException e) {
+            print("Unable to dump log file %s", logFile.getName());
+        }
+    }
+
     // Produces a description of event using the specified step status.
     private static String action(Status status) {
         return status == IN_PROGRESS ? "started" :