Merge "New method to discover host     - Add retry to check topology     - Using arping to discover host     - Rename the folder"
diff --git a/TestON/drivers/common/cli/emulator/mininetclidriver.py b/TestON/drivers/common/cli/emulator/mininetclidriver.py
index 0a1d25f..b65b887 100644
--- a/TestON/drivers/common/cli/emulator/mininetclidriver.py
+++ b/TestON/drivers/common/cli/emulator/mininetclidriver.py
@@ -1159,11 +1159,11 @@
             main.exit()
         return response
 
-    def links( self ):
+    def links( self, timeout=20 ):
         main.log.info( self.name + ": List network links" )
         try:
             response = self.execute( cmd='links', prompt='mininet>',
-                                     timeout=20 )
+                                     timeout=timeout )
         except pexpect.EOF:
             main.log.error( self.name + ": EOF exception found" )
             main.log.error( self.name + ":     " + self.handle.before )
@@ -1984,7 +1984,7 @@
             response = main.FALSE
         return response
 
-    def arping( self, srcHost="", dstHost="10.128.20.211", ethDevice="" ):
+    def arping( self, srcHost="", dstHost="10.128.20.211", ethDevice="", output=main.TRUE ):
         """
         Description:
             Sends arp message from mininet host for hosts discovery
@@ -1998,7 +1998,8 @@
             ethDevice = '-I ' + ethDevice + ' '
         cmd = srcHost + " arping -c1 " + ethDevice + dstHost
         try:
-            main.log.info( "Sending: " + cmd )
+            if output:
+                main.log.info( "Sending: " + cmd )
             self.handle.sendline( cmd )
             i = self.handle.expect( [ "mininet>", "arping: " ] )
             if i == 0:
@@ -2507,7 +2508,7 @@
                 hosts[ name ] = { "interfaces": interfaces }
         return hosts
 
-    def getLinks( self ):
+    def getLinks( self, timeout=20 ):
         """
         Gathers information about current Mininet links. These links may not
         be up if one of the ports is down.
@@ -2524,7 +2525,7 @@
               hosts, this is just the eth#.
         """
         self.update()
-        response = self.links().split( '\n' )
+        response = self.links(timeout=timeout).split( '\n' )
 
         # Examples:
         # s1-eth3<->s2-eth1 (OK OK)
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.params b/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.params
index 986145e..84af370 100755
--- a/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.params
+++ b/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.params
@@ -11,7 +11,7 @@
     <testcases>1,2,[10,300,11,100,300,11,200,300,11,1000]*3</testcases>
 
     <DEPENDENCY>
-        <path>/tests/SCPFscaleTopo/dependencies/</path>
+        <path>/tests/SCPF/SCPFscaleTopo/dependencies/</path>
         <wrapper1>startUp</wrapper1>
         <wrapper2>scaleTopoFunction</wrapper2>
         <wrapper3>topo</wrapper3>
@@ -37,7 +37,7 @@
         <balance>10</balance>
         <nodeSleep>10</nodeSleep>
         <pingall>15</pingall>
-        <MNsleep>5</MNsleep>
+        <MNsleep>120</MNsleep>
     </SLEEP>
 
     <TIMEOUT>
@@ -52,7 +52,7 @@
 
     <TOPOLOGY>
         <topology>torus</topology>
-        <scale>5,10,15</scale>
+        <scale>15,20,25</scale>
     </TOPOLOGY>
 
 </PARAMS>
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.py b/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.py
index 830fbf2..25565dc 100644
--- a/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.py
+++ b/TestON/tests/SCPF/SCPFscaleTopo/SCPFscaleTopo.py
@@ -64,17 +64,14 @@
                                         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,
@@ -247,6 +244,13 @@
             main.currScale = main.topoScale.pop(0)
         else: main.log.error( "topology scale is empty" )
 
+        # remove device before setup topology
+        devices = main.topo.getAllDevices( main )
+        if( devices[0] != '[]' ): # because devices is a list witch contain 3 string, not contain list!
+            temp = json.loads( devices[0] )
+            devicesIdList = []
+            for d in temp:
+                main.CLIs[0].deviceRemove( d.get('id').encode() )
 
         main.step( "Starting up TORUS %sx%s topology" % (main.currScale, main.currScale) )
 
@@ -279,58 +283,74 @@
         main.caseExplanation = "Pinging all hosts and comparing topology " +\
                 "elements between Mininet and ONOS"
 
-        main.log.info( "Pinging all hosts" )
-
-        # the pingall timeout is depend on the number of total host
-        pingTimeout = int( main.pingTimeout * float( int(main.currScale) * int(main.currScale ) ) )
-        pingResult = main.Mininet1.pingall( pingTimeout )
-
         main.log.info( "Gathering topology information" )
-
         time.sleep( main.MNSleep )
 
         devicesResults = main.TRUE
         linksResults = main.TRUE
         hostsResults = main.TRUE
         stepResult = main.TRUE
-        devices = main.topo.getAllDevices( main )
-        hosts = main.topo.getAllHosts( main )
-        ports = main.topo.getAllPorts( main )
-        links = main.topo.getAllLinks( main )
-        clusters = main.topo.getAllClusters( main )
-        mnSwitches = main.Mininet1.getSwitches()
-        mnLinks = main.Mininet1.getLinks()
-        mnHosts = main.Mininet1.getHosts()
-
         main.step( "Comparing MN topology to ONOS topology" )
-        for controller in range(len(main.activeNodes)):
-            controllerStr = str( main.activeNodes[controller] + 1 )
-            if devices[ controller ] and ports[ controller ] and\
-                "Error" not in devices[ controller ] and\
-                "Error" not in ports[ controller ]:
 
-                currentDevicesResult = main.Mininet1.compareSwitches(
-                        mnSwitches,
-                        json.loads( devices[ controller ] ),
-                        json.loads( ports[ controller ] ) )
-            else:
-                currentDevicesResult = main.FALSE
- 
-            if links[ controller ] and "Error" not in links[ controller ]:
-                currentLinksResult = main.Mininet1.compareLinks(
-                        mnSwitches, mnLinks,
-                        json.loads( links[ controller ] ) )
-            else:
-                currentLinksResult = main.FALSE
+        compareRetry=0
+        while compareRetry <3:
+            #While loop for retry
+            devices = main.topo.getAllDevices( main )
+            hosts = main.topo.getAllHosts( main )
+            ports = main.topo.getAllPorts( main )
+            links = main.topo.getAllLinks( main)
+            clusters = main.topo.getAllClusters( main )
+            mnSwitches = main.Mininet1.getSwitches()
+            mnLinks = main.Mininet1.getLinks(timeout=180)
+            mnHosts = main.Mininet1.getHosts()
 
-            if hosts[ controller ] or "Error" not in hosts[ controller ]:
-                currentHostsResult = main.Mininet1.compareHosts(
-                        mnHosts,
-                        json.loads( hosts[ controller ] ) )
-            else:
-                currentHostsResult = main.FALSE
+            for controller in range(len(main.activeNodes)):
+                controllerStr = str( main.activeNodes[controller] + 1 )
+                if devices[ controller ] and ports[ controller ] and\
+                    "Error" not in devices[ controller ] and\
+                    "Error" not in ports[ controller ]:
 
-            stepResult = stepResult and currentDevicesResult and currentLinksResult and currentHostsResult
+                    currentDevicesResult = main.Mininet1.compareSwitches(
+                            mnSwitches,
+                            json.loads( devices[ controller ] ),
+                            json.loads( ports[ controller ] ) )
+                else:
+                    currentDevicesResult = main.FALSE
+
+                if links[ controller ] and "Error" not in links[ controller ]:
+                    currentLinksResult = main.Mininet1.compareLinks(
+                            mnSwitches, mnLinks,
+                            json.loads( links[ controller ] ) )
+                else:
+                    currentLinksResult = main.FALSE
+
+                if hosts[ controller ] or "Error" not in hosts[ controller ]:
+                    currentHostsResult = main.Mininet1.compareHosts(
+                            mnHosts,
+                            json.loads( hosts[ controller ] ) )
+                else:
+                    currentHostsResult = main.FALSE
+
+                stepResult = currentDevicesResult and currentLinksResult and currentHostsResult
+            if stepResult:
+                break
+            compareRetry += 1
+
+        # host discover
+        hostList=[]
+        for i in range( 1, int(main.currScale)+1 ):
+            for j in range( 1, int(main.currScale)+1 ):
+                hoststr = "h" + str(i)+ "x" + str(j)
+                hostList.append(hoststr)
+        totalNum = main.topo.sendArpPackage(main, hostList)
+        # check host number
+        main.log.info("{} hosts has been discovered".format( totalNum ))
+        if int(totalNum) == ( int(main.currScale) * int(main.currScale) ):
+            main.log.info("All hosts has been discovered")
+            stepResult = stepResult and main.TRUE
+        else:
+            main.log.warn("Hosts number is not correct!")
+            stepResult = stepResult and main.FALSE
 
         utilities.assert_equals( expect=main.TRUE,
                                  actual=stepResult,
@@ -341,7 +361,7 @@
 
     def CASE100( self, main ):
         '''
-           Bring Down node 3 
+           Bring Down node 3
         '''
 
         main.case("Balancing Masters and bring ONOS node 3 down: TORUS %sx%s" % (main.currScale, main.currScale))
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/Dependency/multiovs.py b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/multiovs.py
similarity index 100%
rename from TestON/tests/SCPF/SCPFscaleTopo/Dependency/multiovs.py
rename to TestON/tests/SCPF/SCPFscaleTopo/dependencies/multiovs.py
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/Dependency/newFuncTopo.py b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/newFuncTopo.py
similarity index 100%
rename from TestON/tests/SCPF/SCPFscaleTopo/Dependency/newFuncTopo.py
rename to TestON/tests/SCPF/SCPFscaleTopo/dependencies/newFuncTopo.py
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/Dependency/scaleTopoFunction.py b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/scaleTopoFunction.py
similarity index 100%
rename from TestON/tests/SCPF/SCPFscaleTopo/Dependency/scaleTopoFunction.py
rename to TestON/tests/SCPF/SCPFscaleTopo/dependencies/scaleTopoFunction.py
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/Dependency/spine.py b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/spine.py
similarity index 100%
rename from TestON/tests/SCPF/SCPFscaleTopo/Dependency/spine.py
rename to TestON/tests/SCPF/SCPFscaleTopo/dependencies/spine.py
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/Dependency/startUp.py b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/startUp.py
similarity index 100%
rename from TestON/tests/SCPF/SCPFscaleTopo/Dependency/startUp.py
rename to TestON/tests/SCPF/SCPFscaleTopo/dependencies/startUp.py
diff --git a/TestON/tests/SCPF/SCPFscaleTopo/Dependency/topo.py b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/topo.py
similarity index 81%
rename from TestON/tests/SCPF/SCPFscaleTopo/Dependency/topo.py
rename to TestON/tests/SCPF/SCPFscaleTopo/dependencies/topo.py
index 0dbb02d..7a8c0c6 100644
--- a/TestON/tests/SCPF/SCPFscaleTopo/Dependency/topo.py
+++ b/TestON/tests/SCPF/SCPFscaleTopo/dependencies/topo.py
@@ -97,4 +97,19 @@
         clusters.append( t.result )
     return clusters
 
-
+def sendArpPackage( main, hostList ):
+    import json
+    """
+        send arping package from host
+        return the total hosts number from Onos
+    """
+    main.log.info("Sending Arping package...")
+    if isinstance(hostList, list):
+        threads = []
+        for h in hostList:
+            main.Mininet1.arping( srcHost=h, dstHost="10.0.0.1", output=main.FALSE )
+    else:
+        main.Mininet1.arping(srcHost=hostList)
+    summaryStr = json.loads( main.CLIs[0].summary().encode() )
+    hostNum = summaryStr.get('hosts')
+    return hostNum