Small refactoring and DB posting for sw latency results
diff --git a/TestON/tests/TopoPerfNext/TopoPerfNext.params b/TestON/tests/TopoPerfNext/TopoPerfNext.params
index c653090..dc2d519 100644
--- a/TestON/tests/TopoPerfNext/TopoPerfNext.params
+++ b/TestON/tests/TopoPerfNext/TopoPerfNext.params
@@ -72,6 +72,9 @@
         <portEventResultPath>
         /home/admin/ONLabTest/TestON/tests/TopoPerfNext/portEventResultDb.log
         </portEventResultPath>
+        <switchEventResultPath>
+        /home/admin/ONLabTest/TestON/tests/TopoPerfNext/switchEventResultDb.log
+        </switchEventResultPath>
     </DB>
 
     <JSON>
diff --git a/TestON/tests/TopoPerfNext/TopoPerfNext.py b/TestON/tests/TopoPerfNext/TopoPerfNext.py
index 0d2a596..0f37909 100644
--- a/TestON/tests/TopoPerfNext/TopoPerfNext.py
+++ b/TestON/tests/TopoPerfNext/TopoPerfNext.py
@@ -29,6 +29,10 @@
         global clusterCount
         #TODO: fix run number implementation
         global runNum 
+        global timeToPost
+
+        #Test run time
+        timeToPost = time.strftime("%Y-%m-%d %H:%M:%S")
         # Set initial cluster count
         clusterCount = 1
         ##
@@ -54,6 +58,9 @@
         topoCfgFile = main.params[ 'TEST' ][ 'topoConfigFile' ]
         topoCfgName = main.params[ 'TEST' ][ 'topoConfigName' ]
 
+        portEventResultPath = main.params[ 'DB' ][ 'portEventResultPath' ]
+        switchEventResultPath = main.params[ 'DB' ][ 'switchEventResultPath' ]
+
         mvnCleanInstall = main.params[ 'TEST' ][ 'mci' ]
         
         main.case( "Setting up test environment" )
@@ -78,10 +85,19 @@
         main.ONOSbench.onosUninstall( nodeIp=ONOS6Ip )
         main.ONOSbench.onosUninstall( nodeIp=ONOS7Ip )
 
+        main.step( "Clearing previous DB log files" )
+        fPortLog = open(portEventResultPath, 'w')
+        fPortLog.write('')
+        fPortLog.close()
+        fSwitchLog = open(switchEventResultPath, 'w')
+        fSwitchLog.write('')
+        fSwitchLog.close()
+        
         main.step( "Creating cell file" )
         cellFileResult = main.ONOSbench.createCellFile(
             BENCHIp, cellName, MN1Ip,
-            "onos-core,onos-app-metrics,onos-app-gui",
+            ("onos-core,onos-api,webconsole,onos-app-metrics,onos-app-gui,"
+            "onos-cli,onos-openflow"),
             ONOS1Ip )
 
         main.step( "Applying cell file to environment" )
@@ -117,23 +133,17 @@
 
         main.step( "Set cell for ONOS cli env" )
         main.ONOS1cli.setCell( cellName )
-        # main.ONOS2cli.setCell( cellName )
-        # main.ONOS3cli.setCell( cellName )
 
         main.step( "Creating ONOS package" )
         packageResult = main.ONOSbench.onosPackage()
 
         main.step( "Installing ONOS package" )
         install1Result = main.ONOSbench.onosInstall( node=ONOS1Ip )
-        #install2Result = main.ONOSbench.onosInstall( node=ONOS2Ip )
-        #install3Result = main.ONOSbench.onosInstall( node=ONOS3Ip )
 
         time.sleep( 10 )
 
         main.step( "Start onos cli" )
         cli1 = main.ONOS1cli.startOnosCli( ONOS1Ip )
-        #cli2 = main.ONOS2cli.startOnosCli( ONOS2Ip )
-        #cli3 = main.ONOS3cli.startOnosCli( ONOS3Ip )
 
         utilities.assert_equals( expect=main.TRUE,
                                 actual=cellFileResult and cellApplyResult and
@@ -168,6 +178,7 @@
         import os
         import numpy
         global clusterCount
+        global timeToPost
 
         ONOS1Ip = main.params[ 'CTRL' ][ 'ip1' ]
         ONOS2Ip = main.params[ 'CTRL' ][ 'ip2' ]
@@ -193,6 +204,7 @@
 
         debugMode = main.params[ 'TEST' ][ 'debugMode' ]
         onosLog = main.params[ 'TEST' ][ 'onosLogFile' ]
+        resultPath = main.params[ 'DB' ][ 'switchEventResultPath' ]
 
         # Threshold for the test
         thresholdStr = main.params[ 'TEST' ][ 'singleSwThreshold' ]
@@ -543,10 +555,17 @@
                     ofpToGraphLatNodeIter[6][i] = deltaOfpGraph7
                     main.log.info("ONOS7 iter"+str(i)+" ofp-to-graph: "+
                             str(deltaOfpGraph7)+" ms")
+                    
+            main.log.info("Switch up discovery latency")
+                
+            main.log.info("Starting tshark capture")
 
             main.step( "Remove switch from controller" )
             main.Mininet1.deleteSwController( "s1" )
 
+            #TODO: del controller does not have an OFP message.
+            #      However, we can capture TCP Fin,Ack as T0
+
             time.sleep( 5 )
 
         # END of for loop iteration
@@ -557,6 +576,7 @@
         ofpToGraphAvg = 0
         endToEndList = []
         ofpToGraphList = []
+        dbCmdList = []
 
         for node in range( 0, clusterCount ):
             # The latency 2d array was initialized to 0. 
@@ -574,14 +594,22 @@
                 if item > 0.0:
                     ofpToGraphList.append(item)
 
-            endToEndAvg = numpy.mean(endToEndList)
+            endToEndAvg = round(numpy.mean(endToEndList), 2)
             ofpToGraphAvg = numpy.mean(ofpToGraphList)
+            endToEndStd = round(numpy.std(endToEndList), 2)
 
             main.log.report( " - Node "+str(node+1)+" Summary - " )
             main.log.report( " End-to-end Avg: "+
                              str(round(endToEndAvg,2))+" ms"+
                              " End-to-end Std dev: "+
-                             str(round(numpy.std(endToEndList),2))+" ms")
+                             str(round(endToEndStd,2))+" ms")
+            
+            dbCmdList.append(
+                "INSERT INTO switch_latency_tests VALUES("
+                   "'"+timeToPost+"','switch_latency_results',"
+                   ""+runNum+","+str(clusterCount)+",'baremetal"+str(node+1)+"',"
+                   ""+str(endToEndAvg)+","+str(endToEndStd)+",0,0);"
+            )
             #main.log.report( " Ofp-to-graph Avg: "+
             #                 str(round(ofpToGraphAvg,2))+" ms"+
             #                 " Ofp-to-graph Std dev: "+
@@ -591,6 +619,13 @@
         if debugMode == 'on':
             main.ONOS1.cpLogsToDir( "/opt/onos/log/karaf.log",
                                       "/tmp/", copyFileName="sw_lat_karaf" )
+        
+        #Write to file for posting to DB
+        fResult = open(resultPath, 'a')
+        for line in dbCmdList:
+            if line:
+                fResult.write(line+"\n")
+        fResult.close()
 
         #TODO: correct assert
         assertion = main.TRUE
@@ -616,6 +651,7 @@
         import numpy
         global clusterCount
         global runNum
+        global timeToPost
 
         ONOS1Ip = main.params[ 'CTRL' ][ 'ip1' ]
         ONOS2Ip = main.params[ 'CTRL' ][ 'ip2' ]
@@ -637,7 +673,6 @@
         debugMode = main.params[ 'TEST' ][ 'debugMode' ]
         postToDB = main.params[ 'DB' ][ 'postToDB' ]
         resultPath = main.params[ 'DB' ][ 'portEventResultPath' ]
-        timeToPost = time.strftime("%Y-%m-%d %H:%M:%S")
 
         localTime = time.strftime( '%x %X' )
         localTime = localTime.replace( "/", "" )
@@ -1165,6 +1200,9 @@
             portDownDevAvg = round(numpy.mean(portDownDevList), 2)
             portDownGraphAvg = round(numpy.mean(portDownGraphList), 2)
 
+            portUpStdDev = round(numpy.std(portUpGraphList),2)
+            portDownStdDev = round(numpy.std(portDownGraphList),2)
+
             main.log.report( " - Node "+str(node+1)+" Summary - " )
             #main.log.report( " Port up ofp-to-device "+
             #                 str(round(portUpDevAvg, 2))+" ms")
@@ -1179,7 +1217,8 @@
                 "INSERT INTO port_latency_tests VALUES("
                    "'"+timeToPost+"','port_latency_results',"
                    ""+runNum+","+str(clusterCount)+",'baremetal"+str(node+1)+"',"
-                   ""+str(portUpGraphAvg)+",0,"+str(portDownGraphAvg)+",0);"
+                   ""+str(portUpGraphAvg)+","+str(portUpStdDev)+
+                   ","+str(portDownGraphAvg)+","+str(portDownStdDev)+");"
             )
         
         #Write to file for posting to DB
@@ -1189,6 +1228,8 @@
                 fResult.write(line+"\n")
         fResult.close()
 
+        print dbCmdList
+
         # Remove switches from controller for next test
         main.Mininet1.deleteSwController( "s1" )
         main.Mininet1.deleteSwController( "s2" )