Make initial work for the ONOS to be executed parallel

Change-Id: I11e0efb85fc82a8b4d1f49e2d599995ff14a7d51
diff --git a/TestON/tests/dependencies/Cluster.py b/TestON/tests/dependencies/Cluster.py
index 825f368..5c6e679 100644
--- a/TestON/tests/dependencies/Cluster.py
+++ b/TestON/tests/dependencies/Cluster.py
@@ -164,15 +164,16 @@
         """
         self.command( "createCellFile",
                       args=[ main.ONOSbench.ip_address,
-                            cellName,
-                            Mininet if isinstance(Mininet, str) else
-                            Mininet.ip_address,
-                            main.apps,
-                            ips,
-                            main.ONOScell.karafUser,
-                            useSSH ],
+                             cellName,
+                             Mininet if isinstance(Mininet, str) else
+                             Mininet.ip_address,
+                             main.apps,
+                             ips,
+                             main.ONOScell.karafUser,
+                             useSSH ],
                       specificDriver=1,
                       getFrom=0 if installMax else 1 )
+
     def uninstall( self, uninstallMax ):
         """
         Description:
@@ -183,11 +184,15 @@
         Returns:
             Returns main.TRUE if it successfully uninstalled.
         """
-        onosUninstallResult = main.TRUE
-        for ctrl in self.controllers if uninstallMax else self.runningNodes:
-            onosUninstallResult = onosUninstallResult and \
-                                  main.ONOSbench.onosUninstall( nodeIp=ctrl.ipAddress )
-        return onosUninstallResult
+        result = main.TRUE
+        uninstallResult = self.command( "onosUninstall",
+                                        kwargs={ "nodeIp":"ipAddress" },
+                                        specificDriver=1,
+                                        getFrom=0 if uninstallMax else 1,
+                                        funcFromCtrl=True )
+        for uninstallR in uninstallResult:
+            result = result and uninstallR
+        return result
 
     def applyCell( self, cellName, installMax=False ):
         """
@@ -224,19 +229,23 @@
         stopResult = main.TRUE
         startResult = main.TRUE
         onosIsUp = main.TRUE
-        i = 0
-        for ctrl in self.runningNodes:
-            onosIsUp = onosIsUp and main.ONOSbench.isup( ctrl.ipAddress )
-            if onosIsUp == main.TRUE:
-                main.log.report( "ONOS instance {0} is up and ready".format( i + 1 ) )
+        onosUp = self.command( "isup",
+                                 args=[ "ipAddress" ],
+                                 specificDriver=1,
+                                 getFrom=1,
+                                 funcFromCtrl=True )
+        for i in range( len( onosUp ) ):
+            ctrl = self.controllers[ i ]
+            onosIsUp = onosIsUp and onosUp[ i ]
+            if onosUp[ i ] == main.TRUE:
+                main.log.report( ctrl.name + " is up and ready" )
             else:
-                main.log.report( "ONOS instance {0} may not be up, stop and ".format( i + 1 ) +
+                main.log.report( ctrl.name + " may not be up, stop and " +
                                  "start ONOS again " )
                 stopResult = stopResult and main.ONOSbench.onosStop( ctrl.ipAddress )
                 startResult = startResult and main.ONOSbench.onosStart( ctrl.ipAddress )
                 if not startResult or stopResult:
-                    main.log.report( "ONOS instance {0} did not start correctly.".format( i + 1 ) )
-            i += 1
+                    main.log.report( ctrl.name + " did not start correctly." )
         return onosIsUp and stopResult and startResult
 
     def kill( self, killMax, stopOnos ):
@@ -253,11 +262,14 @@
             Returns main.TRUE if successfully killing it.
         """
         result = main.TRUE
-        for ctrl in self.controllers if killMax else self.runningNodes:
-            if stopOnos:
-                result = result and main.ONOSbench.onosStop( ctrl.ipAddress )
-            result = result and main.ONOSbench.onosKill( ctrl.ipAddress )
-            ctrl.active = False
+        killResult = self.command( "onosKill",
+                                   args=[ "ipAddress" ],
+                                   specificDriver=1,
+                                   getFrom=0 if killMax else 1,
+                                   funcFromCtrl=True )
+        for i in range( len ( killResult ) ):
+            result = result and killResult[ i ]
+            self.controllers[ i ].active = False
         return result
 
     def ssh( self ):
@@ -269,10 +281,15 @@
             Returns main.TRUE if it successfully setup the ssh to
             the onos.
         """
-        secureSshResult = main.TRUE
-        for ctrl in self.runningNodes:
-            secureSshResult = secureSshResult and main.ONOSbench.onosSecureSSH( node=ctrl.ipAddress )
-        return secureSshResult
+        result = main.TRUE
+        sshResult = self.command( "onosSecureSSH",
+                                   kwargs={ "node":"ipAddress" },
+                                   specificDriver=1,
+                                   getFrom=1,
+                                   funcFromCtrl=True )
+        for sshR in sshResult:
+            result = result and sshR
+        return result
 
     def install( self, installMax=True, installParallel=True ):
         """
@@ -293,9 +310,9 @@
                 options = "-nf"
             if installParallel:
                 t= main.Thread( target=ctrl.Bench.onosInstall,
-                                 name="install-" + ctrl.name,
-                                 kwargs={ "node" : ctrl.ipAddress,
-                                          "options" : options } )
+                                name="install-" + ctrl.name,
+                                kwargs={ "node" : ctrl.ipAddress,
+                                         "options" : options } )
                 threads.append( t )
                 t.start()
             else:
@@ -315,20 +332,16 @@
         Returns:
             Returns main.TRUE if it successfully started.
         """
-        cliResults =  main.TRUE
-        threads = []
-        for ctrl in self.runningNodes:
-            t = main.Thread( target=ctrl.CLI.startOnosCli,
-                             name="startCli-" + ctrl.name,
-                             args=[ ctrl.ipAddress ] )
-            threads.append( t )
-            t.start()
-            ctrl.active = True
-
-        for t in threads:
-            t.join()
-            cliResults = cliResults and t.result
-        return cliResults
+        result = main.TRUE
+        cliResults = self.command( "startOnosCli",
+                                   args=[ "ipAddress" ],
+                                   specificDriver=2,
+                                   getFrom=1,
+                                   funcFromCtrl=True )
+        for i in range ( len( cliResults ) ):
+            result = result and cliResults[ i ]
+            self.controllers[ i ].active = True
+        return result
 
     def printResult( self, results, activeList, logLevel="debug" ):
         """
@@ -388,7 +401,8 @@
         return all( resultOne == result for result in results )
 
     def command( self, function, args=(), kwargs={}, returnBool=False,
-                 specificDriver=0, contentCheck=False, getFrom=2 ):
+                 specificDriver=0, contentCheck=False, getFrom=2,
+                 funcFromCtrl=False ):
         """
         Description:
             execute some function of the active nodes.
@@ -410,6 +424,8 @@
                 2 - active nodes
                 1 - current running nodes
                 0 - all nodes
+            * funcFromCtrl - specific function of the args/kwargs
+                 from each controller from the list of the controllers
         Returns:
             Returns results if not returnBool and not contentCheck
             Returns checkTruthValue of the result if returnBool
@@ -421,16 +437,24 @@
         results = []
         for ctrl in fromNode[ getFrom ]:
             try:
+                funcArgs = []
+                funcKwargs = {}
                 f = getattr( ( ctrl if not specificDriver else
                              getattr( ctrl, drivers[ specificDriver ] ) ), function )
+                if funcFromCtrl:
+                    if args:
+                        for i in range( len( args ) ):
+                            funcArgs.append( getattr( ctrl, args[ i ] ) )
+                    if kwargs:
+                        for k in kwargs:
+                            funcKwargs.update( { k:getattr( ctrl, kwargs[ k ] ) } )
             except AttributeError:
                 main.log.error( "Function " + function + " not found. Exiting the Test." )
                 main.cleanAndExit()
-
             t = main.Thread( target=f,
                              name=function + "-" + ctrl.name,
-                             args=args,
-                             kwargs=kwargs )
+                             args=funcArgs if funcFromCtrl else args,
+                             kwargs=funcKwargs if funcFromCtrl else kwargs )
             threads.append( t )
             t.start()