Merge "Add new variables to record the timestamp"
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.params b/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.params
index 930aaf8..7bfb458 100755
--- a/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.params
+++ b/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.params
@@ -18,6 +18,8 @@
         <multiovs>multiovs.py</multiovs>
     </DEPENDENCY>
 
+    <TsharkPath>/tmp/scaleTopo_tshark_role_request</TsharkPath>
+
     <DATABASE>
         <dbPath>/tmp/scaleTopoResultDb</dbPath>
     </DATABASE>
@@ -30,6 +32,7 @@
         <start>New switch connection</start>
         <end>Topology DefaultTopology</end>
         <Disconnect>Switch disconnected callback</Disconnect>
+        <roleRequest>of_role_request</roleRequest>
     </SearchTerm>
 
     <GIT>
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.py b/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.py
index 7c2e2d2..1a1e38f 100644
--- a/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.py
+++ b/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.py
@@ -10,7 +10,6 @@
     def CASE1( self, main ):
         import os
         import imp
-
         """
         - Construct tests variables
         - GIT ( optional )
@@ -32,6 +31,8 @@
         gitBranch = main.params[ 'GIT' ][ 'branch' ]
         main.dependencyPath = main.testOnDirectory + \
                               main.params[ 'DEPENDENCY' ][ 'path' ]
+        main.tsharkResultPath = main.params[ 'TsharkPath' ]
+        main.roleRequest = main.params[ 'SearchTerm' ]['roleRequest']
         main.multiovs = main.params[ 'DEPENDENCY' ][ 'multiovs' ]
         main.topoName = main.params[ 'TOPOLOGY' ][ 'topology' ]
         main.numCtrls = int( main.params[ 'CTRL' ][ 'numCtrls' ] )
@@ -67,31 +68,38 @@
                 main.CLIs.append( getattr( main, 'ONOScli%s' % (i+1) ) )
 
         main.allinfo = {} # The dictionary to record all the data from karaf.log
+
         for i in range( 2 ):
             main.allinfo[ i ]={}
             for w in range ( 3 ):
                 # Totaltime: the time from the new switchConnection to its end
                 # swConnection: the time from the first new switchConnection to the last new switchConnection
+                # lastSwToLastRr: the time from the last new switchConnection to the last role request
+                # lastRrToLastTopology: the time form the last role request to the last topology
                 # disconnectRate: the rate that shows how many switch disconnect after connection
-                main.allinfo[ i ][ 'info' + str( w ) ]= { 'totalTime': 0, 'swConnection': 0,'lastSwToTopology': 0, 'disconnectRate': 0 }
+                main.allinfo[ i ][ 'info' + str( w ) ]= { 'totalTime': 0, 'swConnection': 0, 'lastSwToLastRr': 0, 'lastRrToLastTopology': 0, 'disconnectRate': 0 }
 
         main.dbFilePath = main.params[ 'DATABASE' ][ 'dbPath' ]
         main.log.info( "Create Database file " + main.dbFilePath )
         resultDB = open(main.dbFilePath, 'w+' )
         resultDB.close()
 
+
         main.startUp = imp.load_source( wrapperFile1,
                                         main.dependencyPath +
                                         wrapperFile1 +
                                         ".py" )
+
         main.scaleTopoFunction = imp.load_source( wrapperFile2,
                                                   main.dependencyPath +
                                                   wrapperFile2 +
                                                   ".py" )
+
         main.topo = imp.load_source( wrapperFile3,
                                      main.dependencyPath +
                                      wrapperFile3 +
                                      ".py" )
+
         main.ONOSbench.scp( main.Mininet1,
                             main.dependencyPath +
                             main.multiovs,
@@ -286,8 +294,19 @@
                                     " topology failed to start" )
 
         time.sleep( main.MNSleep )
+        main.log.info( "Clean up Tshark" )
+        with open(main.tsharkResultPath, "w" ) as tshark:
+            tshark.write( "" )
+        main.log.info( "Starting Tshark capture" )
+        main.ONOSbench.tsharkGrep( main.roleRequest, main.tsharkResultPath, grepOptions='-E' )
         main.CLIs[ 0 ].activateApp( "org.onosproject.openflow" )
         time.sleep( main.MNSleep )
+        main.log.info( "Stop Tshark" )
+        main.ONOSbench.tsharkStop()
+        main.log.info( "Get role request time" )
+        with open( main.tsharkResultPath, "r" ) as resultFile:
+            resultText = resultFile.readlines()
+            resultFile.close()
 
     def CASE11( self, main ):
         """
@@ -302,8 +321,10 @@
             main.allinfo[ 0 ][ 'info' + str( i )][ 'totalTime' ] = main.scaleTopoFunction.getInfoFromLog( main, main.searchTerm[ 'start' ], 'first', main.searchTerm[ 'end' ], 'last', index=i, funcMode='TD' )
             # Calculate switch connection time
             main.allinfo[ 0 ][ 'info' + str( i )][ 'swConnection' ] = main.scaleTopoFunction.getInfoFromLog( main, main.searchTerm[ 'start' ], 'first', main.searchTerm[ 'start' ], 'last', index=i, funcMode='TD' )
-            # Calculate the time from last switch connection to the end
-            main.allinfo[ 0 ][ 'info' + str( i )][ 'lastSwToTopology' ] =  main.allinfo[ 0 ][ 'info' + str( i )][ 'totalTime' ] -  main.allinfo[ 0 ][ 'info' + str( i )][ 'swConnection' ]
+            # Calculate the time from last switch connection to the last role request
+            main.allinfo[ 0 ][ 'info' + str( i )][ 'lastSwToLastRr' ] = main.scaleTopoFunction.compareTimeDiffWithRoleRequest( main, main.searchTerm[ 'start' ], 'last', index=i )
+            # Calculate the time from the last role request to the last topology
+            main.allinfo[ 0 ][ 'info' + str( i )][ 'lastRrToLastTopology' ] = main.scaleTopoFunction.compareTimeDiffWithRoleRequest( main, main.searchTerm[ 'end' ], 'last', index=i )
             # Calculate the disconnecti rate
             main.allinfo[ 0 ][ 'info' + str( i )][ 'disconnectRate' ] = main.scaleTopoFunction.getInfoFromLog( main, main.searchTerm[ 'Disconnect' ], 'num', main.searchTerm[ 'start' ], 'num', index=i, funcMode='DR' )
         main.log.debug( "The data is " + str( main.allinfo[ 0 ] ) )
@@ -477,10 +498,10 @@
                 slowestNode = i
             # Calculate switch connection time
             main.allinfo[ 1 ][ 'info' + str( i )][ 'swConnection' ] = main.scaleTopoFunction.getInfoFromLog( main, main.searchTerm[ 'start' ], 'first', main.searchTerm[ 'start' ], 'last', index=i, funcMode='TD' )
-            # Calculate the time from last switch connection to the end
-            main.allinfo[ 1 ][ 'info' + str( i )][ 'lastSwToTopology' ] =  main.allinfo[ 1 ][
-            'info' + str( i )][ 'totalTime' ] -  main.allinfo[ 1 ][ 'info' + str( i )][
-            'swConnection' ]
+            # Calculate the time from last switch connection to the last role request
+            main.allinfo[ 1 ][ 'info' + str( i )][ 'lastSwToLastRr' ] = main.scaleTopoFunction.compareTimeDiffWithRoleRequest( main, main.searchTerm[ 'start' ], 'last', index=i )
+            # Calculate the time from the last role request to the last topology
+            main.allinfo[ 1 ][ 'info' + str( i )][ 'lastRrToLastTopology' ] = main.scaleTopoFunction.compareTimeDiffWithRoleRequest( main, main.searchTerm[ 'end' ], 'last', index=i )
             # Calculate the disconnecti rate
             main.allinfo[ 1 ][ 'info' + str( i )][ 'disconnectRate' ] = main.scaleTopoFunction.getInfoFromLog( main, main.searchTerm[ 'Disconnect' ], 'num', main.searchTerm[ 'start' ],'num', index=i, funcMode='DR' )
 
@@ -496,7 +517,8 @@
                 # put result from second capture into data base
                 temp += "," + str( "%.2f" % main.allinfo[ 1 ][ 'info' + str( slowestNode )][ 'totalTime' ] )
                 temp += "," + str( "%.2f" % main.allinfo[ 1 ][ 'info' + str( slowestNode )][ 'swConnection' ] )
-                temp += "," + str( "%.2f" % main.allinfo[ 1 ][ 'info' + str( slowestNode )][ 'lastSwToTopology' ] )
+                temp += "," + str( "%.2f" % main.allinfo[ 1 ][ 'info' + str( slowestNode )][ 'lastSwToLastRr' ] )
+                temp += "," + str( "%.2f" % main.allinfo[ 1 ][ 'info' + str( slowestNode )][ 'lastRrToLastTopology' ] )
                 temp += "," + str( "%.2f" % main.allinfo[ 1 ][ 'info' + str( slowestNode )][ 'disconnectRate' ] )
                 temp += "\n"
                 dbFile.write( temp )
@@ -512,4 +534,4 @@
                                                               "flow",
                                                               "ERROR",
                                                               "Except" ],
-                                                            "s" )
+                                                              "s" )
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/dependencies/scaleTopoFunction.py b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/scaleTopoFunction.py
index 8db74cb..254956c 100644
--- a/TestON/tests/SCPF/SCPFscaleTopo/dependencies/scaleTopoFunction.py
+++ b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/scaleTopoFunction.py
@@ -32,6 +32,44 @@
         main.log.error( "Got wrong string from log" )
         return -1
 
+def getRoleRequestTimeFromTshark( main ):
+    try:
+        main.log.info( "Get role request time" )
+        with open(main.tsharkResultPath, "r" ) as resultFile:
+            resultText = resultFile.readlines()
+            # select the last role request string
+            roleRequestString = resultText[ len( resultText ) - 1 ]
+            main.log.info( roleRequestString )
+            # get timestamp from role request string
+            roleRequestTime = roleRequestString.split( " " )
+            resultFile.close()
+            return float(roleRequestTime[1])
+    except IndexError:
+        main.log.error("Got wrong role request string from Tshark file")
+        return -1
+
+def compareTimeDiffWithRoleRequest(main, term, Mode, index=0 ):
+    '''
+    Description:
+        Compare the time difference between the time of target term and the time of role request
+        Inclides onosclidriver functions
+
+    '''
+    try:
+        termInfo = main.CLIs[ index ].logSearch( term, mode=Mode)
+        termTime = getTimestampFromString( main, termInfo[ 0 ] )
+        roleRequestTime = getRoleRequestTimeFromTshark( main )
+        if termTime == -1 or roleRequestTime == -1:
+            main.writeData = -1
+            main.log.error( "Can't compare the difference with role request time" )
+            return -1
+	# Only concern about the absolute value of difference.
+        return abs( roleRequestTime - termTime )
+    except IndexError:
+        main.log.error( "Catch the wrong information of search term " )
+        main.writeData = -1
+        return -1
+
 def getInfoFromLog( main, term1, mode1, term2, mode2, index=0, funcMode='TD' ):
     '''
     Description: