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

Change-Id: I509a8ee7a26c198957bebf59da5c85a0edb8b995
diff --git a/TestON/tests/USECASE/USECASE_SdnipFunctionCluster/USECASE_SdnipFunctionCluster.params b/TestON/tests/USECASE/USECASE_SdnipFunctionCluster/USECASE_SdnipFunctionCluster.params
index 0d6014f..e25086a 100644
--- a/TestON/tests/USECASE/USECASE_SdnipFunctionCluster/USECASE_SdnipFunctionCluster.params
+++ b/TestON/tests/USECASE/USECASE_SdnipFunctionCluster/USECASE_SdnipFunctionCluster.params
@@ -14,7 +14,10 @@
         <ip3>OC3</ip3>
         <port1>6653</port1>
     </CTRL>
-
+    <GIT>
+        <pull>False</pull>
+        <branch>master</branch>
+    </GIT>
     <DEPENDENCY>
         <path>/USECASE/USECASE_SdnipFunctionCluster/dependencies/</path>
         <topology>USECASE_SdnipI2MN_Cluster.py</topology>
diff --git a/TestON/tests/USECASE/USECASE_SdnipFunctionCluster/USECASE_SdnipFunctionCluster.py b/TestON/tests/USECASE/USECASE_SdnipFunctionCluster/USECASE_SdnipFunctionCluster.py
index c96fa40..a1fb405 100644
--- a/TestON/tests/USECASE/USECASE_SdnipFunctionCluster/USECASE_SdnipFunctionCluster.py
+++ b/TestON/tests/USECASE/USECASE_SdnipFunctionCluster/USECASE_SdnipFunctionCluster.py
@@ -32,8 +32,7 @@
         swResult = main.TRUE
         for i in range ( 1, int( main.params['config']['switchNum'] ) + 1 ):
             sw = "sw%s" % ( i )
-            swResult = swResult and main.Mininet.assignSwController( sw,
-                                                 [ONOS1Ip, ONOS2Ip, ONOS3Ip] )
+            swResult = swResult and main.Mininet.assignSwController( sw, main.ONOSip )
         utilities.assert_equals( expect=main.TRUE,
                                  actual=swResult,
                                  onpass="Successfully connect all switches to ONOS",
@@ -57,143 +56,24 @@
         import time
         import os
         from operator import eq
-
-        main.case( "Setting up ONOS environment" )
-
-        cellName = main.params[ 'ENV' ][ 'cellName' ]
-        global ONOS1Ip
-        global ONOS2Ip
-        global ONOS3Ip
-        ONOS1Ip = os.getenv( main.params[ 'CTRL' ][ 'ip1' ] )
-        ONOS2Ip = os.getenv( main.params[ 'CTRL' ][ 'ip2' ] )
-        ONOS3Ip = os.getenv( main.params[ 'CTRL' ][ 'ip3' ] )
-        ipList = [ ONOS1Ip, ONOS2Ip, ONOS3Ip ]
-
         global p64514
         global p64515
         global p64516
-        p64514 = main.params['config']['p64514']
-        p64515 = main.params['config']['p64515']
-        p64516 = main.params['config']['p64516']
+        p64514 = main.params[ 'config' ][ 'p64514' ]
+        p64515 = main.params[ 'config' ][ 'p64515' ]
+        p64516 = main.params[ 'config' ][ 'p64516' ]
 
-        main.step( "Copying config files" )
-        src = os.path.dirname( main.testFile ) + "/network-cfg.json"
-        dst = main.ONOSbench.home + "/tools/package/config/network-cfg.json"
-        status = main.ONOSbench.scp( main.ONOSbench, src, dst, direction="to" )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=status,
-                                 onpass="Copy config file succeeded",
-                                 onfail="Copy config file failed" )
-
-        main.step( "Create cell file" )
-        cellAppString = main.params[ 'ENV' ][ 'appString' ]
-        main.ONOSbench.createCellFile( main.ONOSbench.ip_address, cellName,
-                                       main.Mininet.ip_address,
-                                       cellAppString, ipList, main.ONOScli1.karafUser )
-
-        main.step( "Applying cell variable to environment" )
-        cellResult = main.ONOSbench.setCell( cellName )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=cellResult,
-                                 onpass="Set cell succeeded",
-                                 onfail="Set cell failed" )
-
-        main.step( "Verify cell connectivity" )
-        verifyResult = main.ONOSbench.verifyCell()
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=verifyResult,
-                                 onpass="Verify cell succeeded",
-                                 onfail="Verify cell failed" )
-
-        branchName = main.ONOSbench.getBranchName()
-        main.log.report( "ONOS is on branch: " + branchName )
-
-        main.step( "Uninstalling ONOS" )
-        uninstallResult = main.ONOSbench.onosUninstall( ONOS1Ip ) \
-                          and main.ONOSbench.onosUninstall( ONOS2Ip ) \
-                          and main.ONOSbench.onosUninstall( ONOS3Ip )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=uninstallResult,
-                                 onpass="Uninstall ONOS from nodes succeeded",
-                                 onfail="Uninstall ONOS form nodes failed" )
-
-        main.ONOSbench.getVersion( report=True )
-
-        main.step( "Creating ONOS package" )
-        packageResult = main.ONOSbench.buckBuild()
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=packageResult,
-                                 onpass="Package ONOS succeeded",
-                                 onfail="Package ONOS failed" )
-
-        main.step( "Installing ONOS package" )
-        onos1InstallResult = main.ONOSbench.onosInstall( options="-f",
-                                                         node=ONOS1Ip )
-        onos2InstallResult = main.ONOSbench.onosInstall( options="-f",
-                                                         node=ONOS2Ip )
-        onos3InstallResult = main.ONOSbench.onosInstall( options="-f",
-                                                         node=ONOS3Ip )
-        onosInstallResult = onos1InstallResult and onos2InstallResult \
-                            and onos3InstallResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=onosInstallResult,
-                                 onpass="Install ONOS to nodes succeeded",
-                                 onfail="Install ONOS to nodes failed" )
-
-        main.step( "Set up ONOS secure SSH" )
-        secureSshResult = main.ONOSbench.onosSecureSSH( node=ONOS1Ip )
-        secureSshResult = secureSshResult and main.ONOSbench.onosSecureSSH( node=ONOS2Ip )
-        secureSshResult = secureSshResult and main.ONOSbench.onosSecureSSH( node=ONOS3Ip )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=secureSshResult,
-                                 onpass="Set up ONOS secure SSH succeeded",
-                                 onfail="Set up ONOS secure SSH failed " )
-
-        main.step( "Checking if ONOS is up yet" )
-        onos1UpResult = main.ONOSbench.isup( ONOS1Ip, timeout=420 )
-        onos2UpResult = main.ONOSbench.isup( ONOS2Ip, timeout=420 )
-        onos3UpResult = main.ONOSbench.isup( ONOS3Ip, timeout=420 )
-        onosUpResult = onos1UpResult and onos2UpResult and onos3UpResult
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=onosUpResult,
-                                 onpass="ONOS nodes are up",
-                                 onfail="ONOS nodes are NOT up" )
-
-        main.step( "Checking if ONOS CLI is ready" )
-        main.CLIs = []
-        cliResult1 = main.ONOScli1.startOnosCli( ONOS1Ip,
-                commandlineTimeout=100, onosStartTimeout=600 )
-        main.CLIs.append( main.ONOScli1 )
-        cliResult2 = main.ONOScli2.startOnosCli( ONOS2Ip,
-                commandlineTimeout=100, onosStartTimeout=600 )
-        main.CLIs.append( main.ONOScli2 )
-        cliResult3 = main.ONOScli3.startOnosCli( ONOS3Ip,
-                commandlineTimeout=100, onosStartTimeout=600 )
-        main.CLIs.append( main.ONOScli3 )
-        cliResult = cliResult1 and cliResult2 and cliResult3
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=cliResult,
-                                 onpass="ONOS CLIs are ready",
-                                 onfail="ONOS CLIs are not ready" )
-
-        main.step( "Checking if ONOS CLI is ready for issuing commands" )
-        for i in range( 10 ):
-            ready = True
-            for cli in main.CLIs:
-                output = cli.summary()
-                if not output:
-                    ready = False
-            if ready:
-                break
-            time.sleep( 30 )
-        utilities.assert_equals( expect=True, actual=ready,
-                                 onpass="ONOS summary command succeded",
-                                 onfail="ONOS summary command failed" )
-
-        if not ready:
-            main.log.error( "ONOS startup failed!" )
-            main.cleanup()
+        try:
+            from tests.USECASE.dependencies.sdnipBaseFunction import SdnBase
+        except ImportError:
+            main.log.error( "sdnBase not found. exiting the test" )
             main.exit()
+        try:
+            main.sdnBase
+        except ( NameError, AttributeError ):
+            main.sdnBase = SdnBase()
+
+        main.sdnBase.initSetup()
 
     def CASE200( self, main ):
         main.case( "Activate sdn-ip application" )
@@ -229,7 +109,6 @@
             main.cleanup()
             main.exit()
 
-
     def CASE102( self, main ):
         '''
         This test case is to load the methods from other Python files, and create
@@ -244,9 +123,8 @@
                                           wrapperFile1 +
                                           ".py" )
         # Create tunnels
-        main.Functions.setupTunnel( main, '1.1.1.2', 2000, ONOS1Ip, 2000 )
-        main.Functions.setupTunnel( main, '1.1.1.4', 2000, ONOS2Ip, 2000 )
-        main.Functions.setupTunnel( main, '1.1.1.6', 2000, ONOS3Ip, 2000 )
+        for i in range ( len( main.ONOSip ) ):
+            main.Functions.setupTunnel( main, '1.1.1.' + str( ( i + 1 ) * 2 ), 2000, main.ONOSip[ i ], 2000 )
 
         main.log.info( "Wait SDN-IP to finish installing connectivity intents \
         and the BGP paths in data plane are ready..." )
@@ -282,29 +160,7 @@
         '''
         point-to-point intents test for each BGP peer and BGP speaker pair
         '''
-        import time
-        main.case( "Check point-to-point intents" )
-        main.log.info( "There are %s BGP peers in total "
-                       % main.params[ 'config' ][ 'peerNum' ] )
-        main.step( "Check P2P intents number from ONOS CLI" )
-
-        getIntentsResult = main.ONOScli1.intents( jsonFormat=True )
-        bgpIntentsActualNum = \
-            main.QuaggaCliSpeaker1.extractActualBgpIntentNum( getIntentsResult )
-        bgpIntentsExpectedNum = int( main.params[ 'config' ][ 'peerNum' ] ) * 6 * 2
-        if bgpIntentsActualNum != bgpIntentsExpectedNum:
-            time.sleep( int( main.params['timers']['RouteDelivery'] ) )
-            getIntentsResult = main.ONOScli1.intents( jsonFormat=True )
-            bgpIntentsActualNum = \
-                main.QuaggaCliSpeaker1.extractActualBgpIntentNum( getIntentsResult )
-        main.log.info( "bgpIntentsExpected num is:" )
-        main.log.info( bgpIntentsExpectedNum )
-        main.log.info( "bgpIntentsActual num is:" )
-        main.log.info( bgpIntentsActualNum )
-        utilities.assert_equals( expect=bgpIntentsExpectedNum,
-                                 actual=bgpIntentsActualNum,
-                                 onpass="PointToPointIntent Intent Num is correct!",
-                                 onfail="PointToPointIntent Intent Num is wrong!" )
+        main.sdnBase.pToPIntentTest( 12 )
 
     def CASE3( self, main ):
         '''
@@ -323,56 +179,7 @@
         allRoutesExpected.append( "9.0.0.0/24" + "/" + "10.0.9.1" )
         allRoutesExpected.append( "20.0.0.0/24" + "/" + "10.0.20.1" )
 
-        getRoutesResult = main.ONOScli1.routes( jsonFormat=True )
-        allRoutesActual = \
-            main.QuaggaCliSpeaker1.extractActualRoutesMaster( getRoutesResult )
-        allRoutesStrExpected = str( sorted( allRoutesExpected ) )
-        allRoutesStrActual = str( allRoutesActual ).replace( 'u', "" )
-        if allRoutesStrActual != allRoutesStrExpected:
-            time.sleep( int( main.params['timers']['RouteDelivery'] ) )
-            getRoutesResult = main.ONOScli1.routes( jsonFormat=True )
-            allRoutesActual = \
-                main.QuaggaCliSpeaker1.extractActualRoutesMaster( getRoutesResult )
-            allRoutesStrActual = str( allRoutesActual ).replace( 'u', "" )
-
-        main.log.info( "Routes expected:" )
-        main.log.info( allRoutesStrExpected )
-        main.log.info( "Routes get from ONOS CLI:" )
-        main.log.info( allRoutesStrActual )
-        utilities.assert_equals( expect=allRoutesStrExpected,
-                                 actual=allRoutesStrActual,
-                                 onpass="Routes are correct!",
-                                 onfail="Routes are wrong!" )
-
-        main.step( "Check M2S intents installed" )
-        getIntentsResult = main.ONOScli1.intents( jsonFormat=True )
-        routeIntentsActualNum = \
-            main.QuaggaCliSpeaker1.extractActualRouteIntentNum( getIntentsResult )
-        routeIntentsExpectedNum = 7
-        if routeIntentsActualNum != routeIntentsExpectedNum:
-            time.sleep( int( main.params['timers']['RouteDelivery'] ) )
-            getIntentsResult = main.ONOScli1.intents( jsonFormat=True )
-            routeIntentsActualNum = \
-                main.QuaggaCliSpeaker1.extractActualRouteIntentNum( getIntentsResult )
-
-        main.log.info( "MultiPointToSinglePoint Intent Num expected is:" )
-        main.log.info( routeIntentsExpectedNum )
-        main.log.info( "MultiPointToSinglePoint Intent NUM Actual is:" )
-        main.log.info( routeIntentsActualNum )
-        utilities.assert_equals( expect=routeIntentsExpectedNum,
-                                 actual=routeIntentsActualNum,
-                                 onpass="MultiPointToSinglePoint Intent Num is correct!",
-                                 onfail="MultiPointToSinglePoint Intent Num is wrong!" )
-
-        main.step( "Check whether all flow status are ADDED" )
-        flowCheck = utilities.retry( main.ONOScli1.checkFlowsState,
-                                     main.FALSE,
-                                     kwargs={'isPENDING':False},
-                                     attempts=10 )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=flowCheck,
-                                 onpass="Flow status is correct!",
-                                 onfail="Flow status is wrong!" )
+        main.sdnBase.routeAndIntentCheck( allRoutesExpected, 7 )
 
 
     def CASE4( self, main ):
@@ -386,154 +193,27 @@
         main.Functions.pingHostToHost(main,
                                       hosts=["h64517", "h64518"],
                                       expectAllSuccess=True)
-        main.Functions.pingHostToHost(main,
-                                      hosts=["h64519", "h64520"],
-                                      expectAllSuccess=True)
+        main.Functions.pingHostToHost( main,
+                                       hosts=[ "h64519", "h64520" ],
+                                       expectAllSuccess=True )
 
     def CASE5( self, main ):
         '''
         Cut links to peers one by one, check routes/intents
         '''
-        import time
-        main.case( "Bring down links and check routes/intents" )
-        main.step( "Bring down the link between sw32 and p64514" )
-        linkResult1 = main.Mininet.link( END1="sw32", END2="p64514",
-                                         OPTION="down" )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=linkResult1,
-                                 onpass="Bring down link succeeded!",
-                                 onfail="Bring down link failed!" )
-
-        if linkResult1 == main.TRUE:
-            time.sleep( int( main.params[ 'timers' ][ 'RouteDelivery' ] ) )
-            main.Functions.checkRouteNum( main, 6 )
-            main.Functions.checkM2SintentNum( main, 6 )
-        else:
-            main.log.error( "Bring down link failed!" )
-            main.cleanup()
-            main.exit()
-
-        main.step( "Bring down the link between sw8 and p64515" )
-        linkResult2 = main.Mininet.link( END1="sw8", END2="p64515",
-                                         OPTION="down" )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=linkResult2,
-                                 onpass="Bring down link succeeded!",
-                                 onfail="Bring down link failed!" )
-        if linkResult2 == main.TRUE:
-            time.sleep( int( main.params[ 'timers' ][ 'RouteDelivery' ] ) )
-            main.Functions.checkRouteNum( main, 5 )
-            main.Functions.checkM2SintentNum( main, 5 )
-        else:
-            main.log.error( "Bring down link failed!" )
-            main.cleanup()
-            main.exit()
-
-        main.step( "Bring down the link between sw28 and p64516" )
-        linkResult3 = main.Mininet.link( END1="sw28", END2="p64516",
-                                         OPTION="down" )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=linkResult3,
-                                 onpass="Bring down link succeeded!",
-                                 onfail="Bring down link failed!" )
-        if linkResult3 == main.TRUE:
-            time.sleep( int( main.params[ 'timers' ][ 'RouteDelivery' ] ) )
-            main.Functions.checkRouteNum( main, 4 )
-            main.Functions.checkM2SintentNum( main, 4 )
-        else:
-            main.log.error( "Bring down link failed!" )
-            main.cleanup()
-            main.exit()
-
-        main.step( "Check whether all flow status are ADDED" )
-        flowCheck = utilities.retry( main.ONOScli1.checkFlowsState,
-                                     main.FALSE,
-                                     kwargs={'isPENDING':False},
-                                     attempts=10 )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=flowCheck,
-                                 onpass="Flow status is correct!",
-                                 onfail="Flow status is wrong!" )
-
-        # Ping test
-        main.Functions.pingSpeakerToPeer( main, speakers=["spk1"],
-                       peers=["p64514", "p64515", "p64516"],
-                       expectAllSuccess=False )
-        main.Functions.pingHostToHost( main,
-                        hosts=["h64514", "h64515", "h64516"],
-                        expectAllSuccess=False )
+        main.sdnBase.linkUpDownCheck( "p64514", "p64515", "p64516",
+                                      6, 6, 5, 5, 4, 4,
+                                      "spk1", [ "h64514", "h64515", "h64516" ],
+                                      "down" )
 
     def CASE6( self, main ):
         '''
         Recover links to peers one by one, check routes/intents
         '''
-        import time
-        main.case( "Bring up links and check routes/intents" )
-        main.step( "Bring up the link between sw32 and p64514" )
-        linkResult1 = main.Mininet.link( END1="sw32", END2="p64514",
-                                         OPTION="up" )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=linkResult1,
-                                 onpass="Bring up link succeeded!",
-                                 onfail="Bring up link failed!" )
-        if linkResult1 == main.TRUE:
-            time.sleep( int( main.params[ 'timers' ][ 'RouteDelivery' ] ) )
-            main.Functions.checkRouteNum( main, 5 )
-            main.Functions.checkM2SintentNum( main, 5 )
-        else:
-            main.log.error( "Bring up link failed!" )
-            main.cleanup()
-            main.exit()
-
-        main.step( "Bring up the link between sw8 and p64515" )
-        linkResult2 = main.Mininet.link( END1="sw8", END2="p64515",
-                                         OPTION="up" )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=linkResult2,
-                                 onpass="Bring up link succeeded!",
-                                 onfail="Bring up link failed!" )
-        if linkResult2 == main.TRUE:
-            time.sleep( int( main.params[ 'timers' ][ 'RouteDelivery' ] ) )
-            main.Functions.checkRouteNum( main, 6 )
-            main.Functions.checkM2SintentNum( main, 6 )
-        else:
-            main.log.error( "Bring up link failed!" )
-            main.cleanup()
-            main.exit()
-
-        main.step( "Bring up the link between sw28 and p64516" )
-        linkResult3 = main.Mininet.link( END1="sw28", END2="p64516",
-                                         OPTION="up" )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=linkResult3,
-                                 onpass="Bring up link succeeded!",
-                                 onfail="Bring up link failed!" )
-        if linkResult3 == main.TRUE:
-            time.sleep( int( main.params[ 'timers' ][ 'RouteDelivery' ] ) )
-            main.Functions.checkRouteNum( main, 7 )
-            main.Functions.checkM2SintentNum( main, 7 )
-        else:
-            main.log.error( "Bring up link failed!" )
-            main.cleanup()
-            main.exit()
-
-        main.step( "Check whether all flow status are ADDED" )
-        flowCheck = utilities.retry( main.ONOScli1.checkFlowsState,
-                                     main.FALSE,
-                                     kwargs={'isPENDING':False},
-                                     attempts=10 )
-        utilities.assert_equals( expect=main.TRUE,
-                                 actual=flowCheck,
-                                 onpass="Flow status is correct!",
-                                 onfail="Flow status is wrong!" )
-
-        # Ping test
-        main.Functions.pingSpeakerToPeer( main, speakers=["spk1"],
-                       peers=["p64514", "p64515", "p64516"],
-                       expectAllSuccess=True )
-        main.Functions.pingHostToHost( main,
-                        hosts=["h64514", "h64515", "h64516"],
-                        expectAllSuccess=True )
+        main.sdnBase.linkUpDownCheck( "p64514", "p64515", "p64516",
+                                      5, 5, 6, 6, 7, 7,
+                                      "spk1", [ "h64514", "h64515", "h64516" ],
+                                      "up" )
 
     def CASE7( self, main ):
         '''
@@ -627,7 +307,7 @@
                                  onpass="Starting switch succeeded!",
                                  onfail="Starting switch failed!" )
 
-        result2 = main.Mininet.assignSwController( "sw32", ONOS1Ip )
+        result2 = main.Mininet.assignSwController( "sw32", main.ONOSip[ 0 ] )
         utilities.assert_equals( expect=main.TRUE,
                                  actual=result2,
                                  onpass="Connect switch to ONOS succeeded!",
@@ -738,7 +418,7 @@
         utilities.assert_equals( expect=main.TRUE, actual=result1,
                                  onpass="Starting switch succeeded!",
                                  onfail="Starting switch failed!" )
-        result2 = main.Mininet.assignSwController( "sw11", ONOS1Ip )
+        result2 = main.Mininet.assignSwController( "sw11", main.ONOSip[ 0 ] )
         utilities.assert_equals( expect=main.TRUE, actual=result2,
                                  onpass="Connect switch to ONOS succeeded!",
                                  onfail="Connect switch to ONOS failed!" )
@@ -874,12 +554,9 @@
                 main.log.info( leaderIP )
 
         main.step( "Uninstall ONOS/SDN-IP leader node" )
-        if leaderIP == ONOS1Ip:
-            uninstallResult = main.ONOSbench.onosStop( ONOS1Ip )
-        elif leaderIP == ONOS2Ip:
-            uninstallResult = main.ONOSbench.onosStop( ONOS2Ip )
-        else:
-            uninstallResult = main.ONOSbench.onosStop( ONOS3Ip )
+        for ip in main.ONOSip:
+            if leaderIP == ip:
+                uninstallResult = main.ONOSbench.onosStop( ip )
 
         utilities.assert_equals( expect=main.TRUE,
                                  actual=uninstallResult,
@@ -890,7 +567,7 @@
             main.exit()
         time.sleep( int( main.params[ 'timers' ][ 'RouteDelivery' ] ) )
 
-        if leaderIP == ONOS1Ip:
+        if leaderIP == main.ONOSip[ 0 ]:
             main.Functions.checkRouteNum( main, 7, ONOScli="ONOScli2" )
             main.Functions.checkM2SintentNum( main, 7, ONOScli="ONOScli2" )
             main.Functions.checkP2PintentNum( main, 30 * 2, ONOScli="ONOScli2" )