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

Change-Id: I509a8ee7a26c198957bebf59da5c85a0edb8b995
diff --git a/TestON/tests/FUNC/FUNCintentRest/FUNCintentRest.py b/TestON/tests/FUNC/FUNCintentRest/FUNCintentRest.py
index e1d26b0..0e96d69 100644
--- a/TestON/tests/FUNC/FUNCintentRest/FUNCintentRest.py
+++ b/TestON/tests/FUNC/FUNCintentRest/FUNCintentRest.py
@@ -19,7 +19,6 @@
         import time
         import imp
         import re
-
         """
         - Construct tests variables
         - GIT ( optional )
@@ -29,26 +28,22 @@
             - 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.topology = main.params[ 'DEPENDENCY' ][ 'topology' ]
             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' ]
             wrapperFile2 = main.params[ 'DEPENDENCY' ][ 'wrapper2' ]
             wrapperFile3 = main.params[ 'DEPENDENCY' ][ 'wrapper3' ]
@@ -60,73 +55,30 @@
             main.addIntentSleep = int( main.params[ 'SLEEP' ][ 'addIntent' ] )
             main.checkTopoAttempts = int( main.params[ 'SLEEP' ][ 'topoAttempts' ] )
             main.flowDurationSleep = int( main.params[ 'SLEEP' ][ 'flowDuration' ] )
-            gitPull = main.params[ 'GIT' ][ 'pull' ]
             main.numSwitch = int( main.params[ 'MININET' ][ 'switch' ] )
             main.numLinks = int( main.params[ 'MININET' ][ 'links' ] )
-            main.cellData = {}  # for creating cell file
             main.hostsData = {}
-            main.RESTs = []
-            main.CLIs = []
-            main.ONOSip = []
             main.scapyHostNames = main.params[ 'SCAPY' ][ 'HOSTNAMES' ].split( ',' )
             main.scapyHosts = []  # List of scapy hosts for iterating
             main.assertReturnString = ''  # Assembled assert return string
             main.cycle = 0  # How many times FUNCintent has run through its tests
 
-            main.ONOSip = main.ONOSbench.getOnosIps()
-            print main.ONOSip
-
-            # Assigning ONOS cli handles to a list
-            try:
-                for i in range( 1, main.maxNodes + 1 ):
-                    main.RESTs.append( getattr( main, 'ONOSrest' + str( i ) ) )
-                    main.CLIs.append( getattr( main, 'ONOScli' + str( i ) ) )
-            except AttributeError:
-                main.log.warn( "A " + str( main.maxNodes ) + " node cluster " +
-                               "was defined in env variables, but only " +
-                               str( len( main.RESTs ) ) +
-                               " nodes were defined in the .topo file. " +
-                               "Using " + str( len( main.RESTs ) ) +
-                               " nodes for the test." )
-
             # -- INIT SECTION, ONLY RUNS ONCE -- #
-            main.startUp = imp.load_source( wrapperFile1,
-                                            main.dependencyPath +
-                                            wrapperFile1 +
-                                            ".py" )
 
             main.intentFunction = imp.load_source( wrapperFile2,
                                                    main.dependencyPath +
                                                    wrapperFile2 +
                                                    ".py" )
 
-            main.topo = imp.load_source( wrapperFile3,
-                                         main.dependencyPath +
-                                         wrapperFile3 +
-                                         ".py" )
-
             copyResult1 = main.ONOSbench.scp( main.Mininet1,
                                               main.dependencyPath +
                                               main.topology,
                                               main.Mininet1.home + "custom/",
                                               direction="to" )
-            if main.RESTs and 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, 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 ):
         """
@@ -140,327 +92,21 @@
         - 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.initialized = main.TRUE
-
-        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" )
-
-        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 )
-
-        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( "Installing ONOS package" )
-        onosInstallResult = main.TRUE
-        for i in range( main.numCtrls ):
-            onosInstallResult = onosInstallResult and \
-                    main.ONOSbench.onosInstall( node=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 ):
-            onosIsUp = onosIsUp and main.ONOSbench.isup( main.ONOSip[ i ] )
-            if onosIsUp == 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",
-                                 onfail="ONOS service did not start properly" )
-        if not stepResult:
-            main.initialized = main.FALSE
-
-        # Start an ONOS cli to provide functionality that is not currently
-        # supported by the Rest API remove this when Leader Checking is supported
-        # by the REST API
-
-        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" )
-        if not stepResult:
-            main.initialized = main.FALSE
-
-        # Remove the first element in main.scale list
-        main.scale.remove( main.scale[ 0 ] )
-
+        main.initialized = main.testSetUp.ONOSSetUp( main.Mininet1, True )
         main.intentFunction.report( main )
 
     def CASE8( self, main ):
-        # OLD FUNCintentRest CASE 8
-        # This remains here for archiving and reference purposes and will be
-        # removed when the new FUNCintentRest is verified to work.
-        # """
-        # Compare Topo
-        # """
-        # import json
-
-        # main.case( "Compare ONOS Topology view to Mininet topology" )
-        # main.caseExplanation = "Compare topology elements between Mininet" +\
-        #                         " and ONOS"
-
-        # main.step( "Gathering topology information" )
-        # # TODO: add a paramaterized sleep here
-        # devicesResults = main.TRUE  # Overall Boolean for device correctness
-        # linksResults = main.TRUE  # Overall Boolean for link correctness
-        # hostsResults = main.TRUE  # Overall Boolean for host correctness
-        # 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( main.numCtrls ):
-        #     controllerStr = str( 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
-        #     utilities.assert_equals( expect=main.TRUE,
-        #                              actual=currentDevicesResult,
-        #                              onpass="ONOS" + controllerStr +
-        #                              " Switches view is correct",
-        #                              onfail="ONOS" + controllerStr +
-        #                              " Switches view is incorrect" )
-
-        #     if links[ controller ] and "Error" not in links[ controller ]:
-        #         currentLinksResult = main.Mininet1.compareLinks(
-        #                 mnSwitches, mnLinks,
-        #                 json.loads( links[ controller ] ) )
-        #     else:
-        #         currentLinksResult = main.FALSE
-        #     utilities.assert_equals( expect=main.TRUE,
-        #                              actual=currentLinksResult,
-        #                              onpass="ONOS" + controllerStr +
-        #                              " links view is correct",
-        #                              onfail="ONOS" + controllerStr +
-        #                              " links view is incorrect" )
-
-        #     if hosts[ controller ] or "Error" not in hosts[ controller ]:
-        #         currentHostsResult = main.Mininet1.compareHosts(
-        #                 mnHosts,
-        #                 json.loads( hosts[ controller ] ) )
-        #     else:
-        #         currentHostsResult = main.FALSE
-        #     utilities.assert_equals( expect=main.TRUE,
-        #                              actual=currentHostsResult,
-        #                              onpass="ONOS" + controllerStr +
-        #                              " hosts exist in Mininet",
-        #                              onfail="ONOS" + controllerStr +
-        #                              " hosts don't match Mininet" )
-
-        # NEW FUNCintentRest Case 8 as based off of the CASE 8 from FUNCintent
-        """
-        Compare ONOS Topology to Mininet Topology
-        """
-        import json
-
-        main.case( "Compare ONOS Topology view to Mininet topology" )
-        main.caseExplanation = "Compare topology elements between Mininet" +\
-                                " and ONOS"
-
-        main.log.info( "Gathering topology information from Mininet" )
-        devicesResults = main.FALSE  # Overall Boolean for device correctness
-        linksResults = main.FALSE  # Overall Boolean for link correctness
-        hostsResults = main.FALSE  # Overall Boolean for host correctness
-        deviceFails = []  # Nodes where devices are incorrect
-        linkFails = []  # Nodes where links are incorrect
-        hostFails = []  # Nodes where hosts are incorrect
-        attempts = main.checkTopoAttempts  # Remaining Attempts
-
-        mnSwitches = main.Mininet1.getSwitches()
-        mnLinks = main.Mininet1.getLinks()
-        mnHosts = main.Mininet1.getHosts()
-
-        main.step( "Comparing Mininet topology to ONOS topology" )
-
-        while ( attempts >= 0 ) and\
-                ( not devicesResults or not linksResults or not hostsResults ):
-            time.sleep( 2 )
-            if not devicesResults:
-                devices = main.topo.getAllDevices( main )
-                ports = main.topo.getAllPorts( main )
-                devicesResults = main.TRUE
-                deviceFails = []  # Reset for each failed attempt
-            if not linksResults:
-                links = main.topo.getAllLinks( main )
-                linksResults = main.TRUE
-                linkFails = []  # Reset for each failed attempt
-            if not hostsResults:
-                hosts = main.topo.getAllHosts( main )
-                hostsResults = main.TRUE
-                hostFails = []  # Reset for each failed attempt
-
-            #  Check for matching topology on each node
-            for controller in range( main.numCtrls ):
-                controllerStr = str( controller + 1 )  # ONOS node number
-                # Compare Devices
-                if devices[ controller ] and ports[ controller ] and\
-                        "Error" not in devices[ controller ] and\
-                        "Error" not in ports[ controller ]:
-
-                    try:
-                        deviceData = json.loads( devices[ controller ] )
-                        portData = json.loads( ports[ controller ] )
-                    except ( TypeError, ValueError ):
-                        main.log.error( "Could not load json: {0} or {1}".format( str( devices[ controller ] ),
-                                                                                  str( ports[ controller ] ) ) )
-                        currentDevicesResult = main.FALSE
-                    else:
-                        currentDevicesResult = main.Mininet1.compareSwitches(
-                            mnSwitches, deviceData, portData )
-                else:
-                    currentDevicesResult = main.FALSE
-                if not currentDevicesResult:
-                    deviceFails.append( controllerStr )
-                devicesResults = devicesResults and currentDevicesResult
-                # Compare Links
-                if links[ controller ] and "Error" not in links[ controller ]:
-                    try:
-                        linkData = json.loads( links[ controller ] )
-                    except ( TypeError, ValueError ):
-                        main.log.error( "Could not load json:" + str( links[ controller ] ) )
-                        currentLinksResult = main.FALSE
-                    else:
-                        currentLinksResult = main.Mininet1.compareLinks(
-                            mnSwitches, mnLinks, linkData )
-                else:
-                    currentLinksResult = main.FALSE
-                if not currentLinksResult:
-                    linkFails.append( controllerStr )
-                linksResults = linksResults and currentLinksResult
-                # Compare Hosts
-                if hosts[ controller ] and "Error" not in hosts[ controller ]:
-                    try:
-                        hostData = json.loads( hosts[ controller ] )
-                    except ( TypeError, ValueError ):
-                        main.log.error( "Could not load json:" + str( hosts[ controller ] ) )
-                        currentHostsResult = main.FALSE
-                    else:
-                        currentHostsResult = main.Mininet1.compareHosts(
-                                mnHosts, hostData )
-                else:
-                    currentHostsResult = main.FALSE
-                if not currentHostsResult:
-                    hostFails.append( controllerStr )
-                hostsResults = hostsResults and currentHostsResult
-            # Decrement Attempts Remaining
-            attempts -= 1
-
-        utilities.assert_equals( expect=[],
-                                 actual=deviceFails,
-                                 onpass="ONOS correctly discovered all devices",
-                                 onfail="ONOS incorrectly discovered devices on nodes: " +
-                                 str( deviceFails ) )
-        utilities.assert_equals( expect=[],
-                                 actual=linkFails,
-                                 onpass="ONOS correctly discovered all links",
-                                 onfail="ONOS incorrectly discovered links on nodes: " +
-                                 str( linkFails ) )
-        utilities.assert_equals( expect=[],
-                                 actual=hostFails,
-                                 onpass="ONOS correctly discovered all hosts",
-                                 onfail="ONOS incorrectly discovered hosts on nodes: " +
-                                 str( hostFails ) )
-        topoResults = hostsResults and linksResults and devicesResults
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=topoResults,
-                                 onpass="ONOS correctly discovered the topology",
-                                 onfail="ONOS incorrectly discovered the topology" )
+        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.topoRelated.compareTopos( main.Mininet1, main.checkTopoAttempts )
 
     def CASE9( self, main ):
         """
@@ -643,7 +289,7 @@
             main.initialized = main.FALSE
 
     def CASE15( self, main ):
-        """
+        """main.topo.
             Discover all hosts with scapy arp packets and store its data to a dictionary
         """
         if main.initialized == main.FALSE:
@@ -733,6 +379,15 @@
         """
             Stop mininet and remove scapy hosts
         """
+        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.log.report( "Stop Mininet and Scapy" )
         main.case( "Stop Mininet and Scapy" )
         main.caseExplanation = "Stopping the current mininet topology " +\
@@ -756,13 +411,7 @@
                                  onpass="Successfully stopped scapy and removed host components",
                                  onfail="Failed to stop mininet and scapy" )
 
-        main.step( "Stopping Mininet Topology" )
-        mininetResult = main.Mininet1.stopNet()
-
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=stepResult,
-                                 onpass="Successfully stop mininet",
-                                 onfail="Failed to stop mininet" )
+        mininetResult = main.Utils.mininetCleanup( main.Mininet1 )
         # Exit if topology did not load properly
         if not ( mininetResult and scapyResult ):
             main.cleanup()
@@ -772,34 +421,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 CASE1000( self, main ):
         """
             Add host intents between 2 host: