Save ONOS diags on failure in UP4, QoS Mobile and QoS non-mobile tests

Change-Id: I1e2320163d1cd547594c25a768f842d9ae16925d
diff --git a/TestON/tests/USECASE/SegmentRouting/QOS/QOS.py b/TestON/tests/USECASE/SegmentRouting/QOS/QOS.py
index 1b20ce8..6f05db0 100644
--- a/TestON/tests/USECASE/SegmentRouting/QOS/QOS.py
+++ b/TestON/tests/USECASE/SegmentRouting/QOS/QOS.py
@@ -83,4 +83,5 @@
         main.step("Teardown")
         trex.teardown()
         up4.teardown()
+        run.saveOnosDiagsIfFailure(main)
         run.cleanup(main)
diff --git a/TestON/tests/USECASE/SegmentRouting/QOSNonMobile/dependencies/QOSNonMobileTest.py b/TestON/tests/USECASE/SegmentRouting/QOSNonMobile/dependencies/QOSNonMobileTest.py
index 9a52313..45e9d8c 100644
--- a/TestON/tests/USECASE/SegmentRouting/QOSNonMobile/dependencies/QOSNonMobileTest.py
+++ b/TestON/tests/USECASE/SegmentRouting/QOSNonMobile/dependencies/QOSNonMobileTest.py
@@ -175,6 +175,7 @@
 
         main.step("Teardown")
         trex.teardown()
+        run.saveOnosDiagsIfFailure(main)
         run.cleanup(main)
 
     def __cleanupTrafficSelector(self, traffic_selector):
diff --git a/TestON/tests/USECASE/SegmentRouting/UP4/UP4.py b/TestON/tests/USECASE/SegmentRouting/UP4/UP4.py
index 1395671..456a6b5 100644
--- a/TestON/tests/USECASE/SegmentRouting/UP4/UP4.py
+++ b/TestON/tests/USECASE/SegmentRouting/UP4/UP4.py
@@ -62,6 +62,8 @@
 
         main.step("Stop scapy and p4rt client")
         up4.teardown()
+
+        run.saveOnosDiagsIfFailure(main)
         run.cleanup(main)
 
     def CASE2(self, main):
@@ -256,6 +258,8 @@
         main.step("Stop scapy and p4rt client")
         up4.teardown()
         bess_host.stopScapy()
+
+        run.saveOnosDiagsIfFailure(main)
         run.cleanup(main)
 
     def CASE3(self, main):
@@ -380,6 +384,8 @@
             onfail="ONOS instances have different number of flows: (%d, %d, %d)" % (
                 onos_0_flow_count, onos_1_flow_count, onos_2_flow_count)
         )
+
+        run.saveOnosDiagsIfFailure(main)
         run.cleanup(main)
 
     def CASE4(self, main):
@@ -432,6 +438,14 @@
         )
 
         onosPod = main.params["UP4_delete_pod"]
+
+        # Save ONOS diags of the POD we are killing otherwise we lose ONOS logs
+        main.ONOSbench.onosDiagnosticsK8s(
+            [onosPod],
+            main.logdir,
+            "-CASE%d-%s_BeforeKill" % (main.CurrentTestCaseNumber, onosPod)
+        )
+
         # Exit from previous port forwarding, because we need to restore
         # port-forwarding after ONOS reboot.
         kubectl_0.clearBuffer()
@@ -575,4 +589,5 @@
 
         run.checkFlows(main, minFlowCount=initial_flow_count)
 
+        run.saveOnosDiagsIfFailure(main)
         run.cleanup(main)
diff --git a/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py b/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
index 3aba8fc..59466be 100644
--- a/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
+++ b/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
@@ -2005,3 +2005,9 @@
         utilities.assert_equals( expect=True, actual=logging,
                                  onpass="Reset log levels",
                                  onfail="Failed to reset log levels" )
+
+    @staticmethod
+    def saveOnosDiagsIfFailure( main ):
+        if main.FALSE in main.stepResultsList:
+            # Some step has failed
+            Testcaselib.saveOnosDiagnostics( main )