[ONOS-6593]Review and Refactor ONOS startup procedures in TestON

Change-Id: I509a8ee7a26c198957bebf59da5c85a0edb8b995
diff --git a/TestON/tests/FUNC/FUNCoptical/FUNCoptical.py b/TestON/tests/FUNC/FUNCoptical/FUNCoptical.py
index c7ae463..9d800a7 100644
--- a/TestON/tests/FUNC/FUNCoptical/FUNCoptical.py
+++ b/TestON/tests/FUNC/FUNCoptical/FUNCoptical.py
@@ -7,219 +7,75 @@
         self.default = ''
 
     def CASE1( self, main ):
-        import time
         import imp
+        import time
         import re
-
         """
-        - Construct tests variables
-        - GIT ( optional )
-            - Checkout ONOS master branch
-            - Pull latest ONOS code
-        - Building ONOS ( optional )
-            - Install ONOS package
-            - Build ONOS package
+           - Construct tests variables
+           - GIT ( optional )
+               - Checkout ONOS master branch
+               - Pull latest ONOS code
+           - Building ONOS ( optional )
+               - Install ONOS package
+               - Build ONOS package
         """
-        main.case( "Constructing test variables and building ONOS package" )
-        main.step( "Constructing test variables" )
-        main.caseExplanation = "This test case is mainly for loading " +\
-                               "from params file, and pull and build the " +\
-                               " latest ONOS package"
+        try:
+            from tests.dependencies.ONOSSetup import ONOSSetup
+            main.testSetUp = ONOSSetup()
+        except ImportError:
+            main.log.error( "ONOSSetup not found. exiting the test" )
+            main.exit()
+        main.testSetUp.envSetupDescription()
         stepResult = main.FALSE
-
         # Test variables
         try:
-            main.testOnDirectory = re.sub( "(/tests)$", "", main.testDir )
             main.apps = main.params[ 'ENV' ][ 'cellApps' ]
-            gitBranch = main.params[ 'GIT' ][ 'branch' ]
             main.dependencyPath = main.testOnDirectory + \
                                   main.params[ 'DEPENDENCY' ][ 'path' ]
             main.scale = ( main.params[ 'SCALE' ][ 'size' ] ).split( "," )
-            if main.ONOSbench.maxNodes:
-                main.maxNodes = int( main.ONOSbench.maxNodes )
-            else:
-                main.maxNodes = 0
             wrapperFile1 = main.params[ 'DEPENDENCY' ][ 'wrapper1' ]
             main.startUpSleep = int( main.params[ 'SLEEP' ][ 'startup' ] )
             main.checkIntentSleep = int( main.params[ 'SLEEP' ][ 'checkintent' ] )
             main.checkTopoAttempts = int( main.params[ 'SLEEP' ][ 'topoAttempts' ] )
-            gitPull = main.params[ 'GIT' ][ 'pull' ]
             main.switches = int( main.params[ 'MININET' ][ 'switch' ] )
             main.links = int( main.params[ 'MININET' ][ 'links' ] )
             main.hosts = int( main.params[ 'MININET' ][ 'hosts' ] )
             main.opticalTopo = main.params[ 'MININET' ][ 'toponame' ]
-            main.cellData = {}  # For creating cell file
             main.hostsData = {}
-            main.CLIs = []
-            main.ONOSip = []  # List of IPs of active ONOS nodes. CASE 2
             main.activeONOSip = []
             main.assertReturnString = ''  # Assembled assert return string
             main.cycle = 0  # How many times FUNCintent has run through its tests
 
-            main.ONOSip = main.ONOSbench.getOnosIps()
-
-            # Assigning ONOS cli handles to a list
-            for i in range( 1, main.maxNodes + 1 ):
-                main.CLIs.append( getattr( main, 'ONOScli' + str( i ) ) )
-
             # -- INIT SECTION, ONLY RUNS ONCE -- #
-            main.topo = imp.load_source( wrapperFile1,
-                                         main.dependencyPath +
-                                         wrapperFile1 +
-                                         ".py" )
-            if main.CLIs:
-                stepResult = main.TRUE
-            else:
-                main.log.error( "Did not properly created list of ONOS CLI handle" )
-                stepResult = main.FALSE
+            stepResult = main.testSetUp.envSetup( True )
         except Exception as e:
-            main.log.exception( e )
-            main.cleanup()
-            main.exit()
-
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="Successfully construct " +
-                                        "test variables ",
-                                 onfail="Failed to construct test variables" )
-
-        main.ONOSbench.getVersion( report=True )
+            main.testSetUp.envSetupException( e )
+        main.testSetUp.evnSetupConclusion( stepResult )
 
     def CASE2( self, main ):
         """
-        - Set up cell
-            - Create cell file
-            - Set cell file
-            - Verify cell file
-        - Kill ONOS process
-        - Uninstall ONOS cluster
-        - Verify ONOS start up
-        - Install ONOS cluster
-        - Connect to cli
+            - Set up cell
+                - Create cell file
+                - Set cell file
+                - Verify cell file
+            - Kill ONOS process
+            - Uninstall ONOS cluster
+            - Verify ONOS start up
+            - Install ONOS cluster
+            - Connect to cli
         """
-        main.cycle += 1
-
-        # main.scale[ 0 ] determines the current number of ONOS controller
-        main.numCtrls = int( main.scale[ 0 ] )
         main.flowCompiler = "Flow Rules"
-
-        main.case( "Starting up " + str( main.numCtrls ) +
-                   " node(s) ONOS cluster" )
-        main.caseExplanation = "Set up ONOS with " + str( main.numCtrls ) +\
-                                " node(s) ONOS cluster"
-
-        #kill off all onos processes
-        main.log.info( "Safety check, killing all ONOS processes" +
-                       " before initiating environment setup" )
-
-        for i in range( main.maxNodes ):
-            main.ONOSbench.onosDie( main.ONOSip[ i ] )
-
-        print "NODE COUNT = ", main.numCtrls
-
-        tempOnosIp = []
-        for i in range( main.numCtrls ):
-            tempOnosIp.append( main.ONOSip[ i ] )
-
-        main.ONOSbench.createCellFile( main.ONOSbench.ip_address,
-                                       "temp", main.Mininet1.ip_address,
-                                       main.apps, tempOnosIp, main.ONOScli1.karafUser )
-
-        main.step( "Apply cell to environment" )
-        cellResult = main.ONOSbench.setCell( "temp" )
-        verifyResult = main.ONOSbench.verifyCell()
-        stepResult = cellResult and verifyResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="Successfully applied cell to " +
-                                        "environment",
-                                 onfail="Failed to apply cell to environment " )
-
-        main.step( "Creating ONOS package" )
-        packageResult = main.ONOSbench.buckBuild()
-        stepResult = packageResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="Successfully created ONOS package",
-                                 onfail="Failed to create ONOS package" )
-
-        time.sleep( main.startUpSleep )
-        main.step( "Uninstalling ONOS package" )
-        onosUninstallResult = main.TRUE
-        for ip in main.ONOSip:
-            onosUninstallResult = onosUninstallResult and \
-                    main.ONOSbench.onosUninstall( nodeIp=ip )
-        stepResult = onosUninstallResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="Successfully uninstalled ONOS package",
-                                 onfail="Failed to uninstall ONOS package" )
-
-        time.sleep( main.startUpSleep )
-        main.step( "Installing ONOS package" )
-        onosInstallResult = main.TRUE
-        del main.activeONOSip[:]
-        for i in range( main.numCtrls ):
-            onosInstallResult = onosInstallResult and \
-                    main.ONOSbench.onosInstall( node=main.ONOSip[ i ] )
-            # Populate activeONOSip
-            main.activeONOSip.append( main.ONOSip[ i ] )
-        stepResult = onosInstallResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="Successfully installed ONOS package",
-                                 onfail="Failed to install ONOS package" )
-
-        main.step( "Set up ONOS secure SSH" )
-        secureSshResult = main.TRUE
-        for i in range( int( main.numCtrls ) ):
-            secureSshResult = secureSshResult and main.ONOSbench.onosSecureSSH( node=main.ONOSip[ i ] )
-        utilities.assert_equals( expect=main.TRUE, actual=secureSshResult,
-                                 onpass="Test step PASS",
-                                 onfail="Test step FAIL" )
-
-        time.sleep( main.startUpSleep )
-        main.step( "Starting ONOS service" )
-        stopResult = main.TRUE
-        startResult = main.TRUE
-        onosIsUp = main.TRUE
-
-        for i in range( main.numCtrls ):
-            isUp = main.ONOSbench.isup( main.ONOSip[ i ] )
-            onosIsUp = onosIsUp and isUp
-            if isUp == main.TRUE:
-                main.log.report( "ONOS instance {0} is up and ready".format( i + 1 ) )
-            else:
-                main.log.report( "ONOS instance {0} may not be up, stop and ".format( i + 1 ) +
-                                 "start ONOS again " )
-                stopResult = stopResult and main.ONOSbench.onosStop( main.ONOSip[ i ] )
-                startResult = startResult and main.ONOSbench.onosStart( main.ONOSip[ i ] )
-                if not startResult or stopResult:
-                    main.log.report( "ONOS instance {0} did not start correctly.".format( i + 1 ) )
-        stepResult = onosIsUp and stopResult and startResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="ONOS service is ready on all nodes",
-                                 onfail="ONOS service did not start properly on all nodes" )
-
-        main.step( "Start ONOS cli" )
-        cliResult = main.TRUE
-        for i in range( main.numCtrls ):
-            cliResult = cliResult and \
-                        main.CLIs[ i ].startOnosCli( main.ONOSip[ i ] )
-        stepResult = cliResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="Successfully start ONOS cli",
-                                 onfail="Failed to start ONOS cli" )
-
-        # Remove the first element in main.scale list
-        main.scale.remove( main.scale[ 0 ] )
+        main.testSetUp.ONOSSetUp( main.LincOE, True )
 
     def CASE10( self, main ):
         """
             Start Mininet opticalTest Topology
         """
+        del main.activeONOSip[:]
+        for i in range( main.numCtrls ):
+            # Populate activeONOSip
+            main.activeONOSip.append( main.ONOSip[ i ] )
+
         main.case( "Mininet with Linc-OE startup" )
         main.step( "Push TopoDDriver.json to ONOS through onos-netcfg" )
         topoResult = main.ONOSbench.onosNetCfg( controllerIps=main.activeONOSip, path=main.dependencyPath,
@@ -234,7 +90,7 @@
         time.sleep( 10 )
         controllerIPs = ','.join( main.activeONOSip )
         cIps = ""
-        for i in range(0,4):
+        for i in range( 0, 4 ):
             cIps += controllerIPs + ' '
         opticalMnScript = main.LincOE.runOpticalMnScript( ctrllerIP=cIps, topology=main.opticalTopo )
         topoResult = opticalMnScript
@@ -248,17 +104,17 @@
         """
             Stop mininet
         """
-        main.log.report( "Stop Mininet topology" )
-        main.case( "Stop Mininet topology" )
-        main.caseExplanation = "Stopping the current mininet topology " +\
-                                "to start up fresh"
-
-        main.step( "Stopping Mininet Topology" )
-        topoResult = main.LincOE.stopNet( timeout=180 )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=topoResult,
-                                 onpass="Successfully stopped mininet",
-                                 onfail="Failed to stopped mininet" )
+        try:
+            from tests.dependencies.utils import Utils
+        except ImportError:
+            main.log.error( "Utils not found exiting the test" )
+            main.exit()
+        try:
+            main.Utils
+        except ( NameError, AttributeError ):
+            main.Utils = Utils()
+        main.Utils.mininetCleanIntro()
+        topoResult = main.Utils.mininetCleanup( main.LincOE, timeout=180 )
         # Exit if topology did not load properly
         if not topoResult:
             main.cleanup()
@@ -307,33 +163,16 @@
         """
             Copy the karaf.log files after each testcase cycle
         """
-        main.log.report( "Copy karaf logs" )
-        main.case( "Copy karaf logs" )
-        main.caseExplanation = "Copying the karaf logs to preserve them through" +\
-                               "reinstalling ONOS"
-        main.step( "Copying karaf logs" )
-        stepResult = main.TRUE
-        scpResult = main.TRUE
-        copyResult = main.TRUE
-        for i in range( main.numCtrls ):
-            main.node = main.CLIs[ i ]
-            ip = main.ONOSip[ i ]
-            main.node.ip_address = ip
-            scpResult = scpResult and main.ONOSbench.scp( main.node,
-                                                          "/opt/onos/log/karaf.log",
-                                                          "/tmp/karaf.log",
-                                                          direction="from" )
-            copyResult = copyResult and main.ONOSbench.cpLogsToDir( "/tmp/karaf.log", main.logdir,
-                                                                    copyFileName=( "karaf.log.node{0}.cycle{1}".format( str( i + 1 ), str( main.cycle ) ) ) )
-            if scpResult and copyResult:
-                stepResult = main.TRUE and stepResult
-            else:
-                stepResult = main.FALSE and stepResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="Successfully copied remote ONOS logs",
-                                 onfail="Failed to copy remote ONOS logs" )
-
+        try:
+            from tests.dependencies.utils import Utils
+        except ImportError:
+            main.log.error( "Utils not found exiting the test" )
+            main.exit()
+        try:
+            main.Utils
+        except ( NameError, AttributeError ):
+            main.Utils = Utils()
+        main.Utils.copyKarafLog()
     def CASE21( self, main ):
         """
             Run pingall to discover all hosts
@@ -379,7 +218,15 @@
         Compare ONOS Topology to Mininet Topology
         """
         import json
-
+        try:
+            from tests.dependencies.topology import Topology
+        except ImportError:
+            main.log.error( "Topology not found exiting the test" )
+            main.exit()
+        try:
+            main.topoRelated
+        except ( NameError, AttributeError ):
+            main.topoRelated = Topology()
         main.case( "Compare ONOS Topology view to Mininet topology" )
         main.caseExplanation = "Compare topology elements between Mininet" +\
                                 " and ONOS"
@@ -403,16 +250,16 @@
                 ( not devicesResults or not linksResults or not hostsResults ):
             time.sleep( 2 )
             if not devicesResults:
-                devices = main.topo.getAllDevices( main )
-                ports = main.topo.getAllPorts( main )
+                devices = main.topoRelated.getAllDevices( main.numCtrls, False )
+                ports = main.topoRelated.getAllPorts( main.numCtrls, False )
                 devicesResults = main.TRUE
                 deviceFails = []  # Reset for each attempt
             if not linksResults:
-                links = main.topo.getAllLinks( main )
+                links = main.topoRelated.getAllLinks( main.numCtrls, False )
                 linksResults = main.TRUE
                 linkFails = []  # Reset for each attempt
             if not hostsResults:
-                hosts = main.topo.getAllHosts( main )
+                hosts = main.topoRelated.getAllHosts( main.numCtrls, False )
                 hostsResults = main.TRUE
                 hostFails = []  # Reset for each attempt
 
@@ -507,6 +354,7 @@
                                  onpass="ONOS correctly discovered the topology",
                                  onfail="ONOS incorrectly discovered the topology" )
 
+
     def CASE31( self, main ):
         import time
         """