[ONOS-7475] Refactoring SR tests

Change-Id: I10e51fcb8b416167e6a338e9030933162640af89
diff --git a/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/SRClusterRestart.params b/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/SRClusterRestart.params
index abe305e..05222f0 100755
--- a/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/SRClusterRestart.params
+++ b/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/SRClusterRestart.params
@@ -13,6 +13,8 @@
     </SCALE>
 
     <DEPENDENCY>
+        <useCommonConf>True</useCommonConf>
+        <useCommonTopo>True</useCommonTopo>
         <topology>cord_fabric.py</topology>
     </DEPENDENCY>
 
diff --git a/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/SRClusterRestart.py b/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/SRClusterRestart.py
index 0e8844a..035d386 100644
--- a/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/SRClusterRestart.py
+++ b/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/SRClusterRestart.py
@@ -36,30 +36,17 @@
         Induce CLUSTER restart
         Pingall
         """
-        description = "Cluster Restart test with 2x2 Leaf-spine "
-        main.case( description )
-        from tests.USECASE.SegmentRouting.dependencies.Testcaselib import \
-            Testcaselib as run
-        if not hasattr( main, 'apps' ):
-            run.initTest( main )
-        main.cfgName = '2x2'
-        main.Cluster.setRunningNode( 3 )
-        run.installOnos( main )
-        run.startMininet( main, 'cord_fabric.py' )
-        # pre-configured routing and bridging test
-        run.checkFlows( main, minFlowCount=116 )
-        run.pingAll( main )
-        run.killOnos( main, [ 0, 1, 2 ], '4', '8', '0' )
-        run.pingAll( main, 'CASE1_Failure', dumpflows=False )
-        run.recoverOnos( main, [ 0, 1, 2 ], '4', '8', '3' )
-        run.checkFlows( main, minFlowCount=116, tag='CASE1_Failure' )
-        run.pingAll( main, 'CASE1_Failure' )
-        # TODO Dynamic config of hosts in subnet
-        # TODO Dynamic config of host not in subnet
-        # TODO Dynamic config of vlan xconnect
-        # TODO Vrouter integration
-        # TODO Mcast integration
-        run.cleanup( main )
+        try:
+            from tests.USECASE.SegmentRouting.SRClusterRestart.dependencies.SRClusterRestartFuncs import SRClusterRestartFuncs
+        except ImportError:
+            main.log.error( "SRClusterRestartFuncs not found. Exiting the test" )
+            main.cleanAndExit()
+        try:
+            main.funcs
+        except ( NameError, AttributeError ):
+            main.funcs = SRClusterRestartFuncs()
+        main.funcs.runTest( main, 1, 3,
+                            '2x2', 116, 'Failure' )
 
     def CASE2( self, main ):
         """
@@ -69,30 +56,17 @@
         Induce Cluster Restart
         Pingall
         """
-        description = "Cluster Restart test with 4x4 Leaf-spine "
-        main.case( description )
-        from tests.USECASE.SegmentRouting.dependencies.Testcaselib import \
-            Testcaselib as run
-        if not hasattr( main, 'apps' ):
-            run.initTest( main )
-        main.cfgName = '4x4'
-        main.Cluster.setRunningNode( 3 )
-        run.installOnos( main )
-        run.startMininet( main, 'cord_fabric.py', args="--leaf=4 --spine=4" )
-        # pre-configured routing and bridging test
-        run.checkFlows( main, minFlowCount=350 )
-        run.pingAll( main )
-        run.killOnos( main, [ 0, 1, 2 ], '8', '32', '0' )
-        run.pingAll( main, 'CASE2_Failure', dumpflows=False )
-        run.recoverOnos( main, [ 0, 1, 2 ], '8', '32', '3' )
-        run.checkFlows( main, minFlowCount=350, tag='CASE2_Failure' )
-        run.pingAll( main, 'CASE3_Recovery' )
-        # TODO Dynamic config of hosts in subnet
-        # TODO Dynamic config of host not in subnet
-        # TODO Dynamic config of vlan xconnect
-        # TODO Vrouter integration
-        # TODO Mcast integration
-        run.cleanup( main )
+        try:
+            from tests.USECASE.SegmentRouting.SRClusterRestart.dependencies.SRClusterRestartFuncs import SRClusterRestartFuncs
+        except ImportError:
+            main.log.error( "SRClusterRestartFuncs not found. Exiting the test" )
+            main.cleanAndExit()
+        try:
+            main.funcs
+        except ( NameError, AttributeError ):
+            main.funcs = SRClusterRestartFuncs()
+        main.funcs.runTest( main, 2, 3,
+                            '4x4', 350, 'Failure' )
 
     def CASE3( self, main ):
         """
@@ -102,27 +76,15 @@
         Induce Cluster Restart
         Pingall
         """
-        description = "Cluster Restart test with single switch "
-        main.case( description )
-        from tests.USECASE.SegmentRouting.dependencies.Testcaselib import \
-            Testcaselib as run
-        if not hasattr( main, 'apps' ):
-            run.initTest( main )
-        main.cfgName = '0x1'
-        main.Cluster.setRunningNode( 3 )
-        run.installOnos( main )
-        run.startMininet( main, 'cord_fabric.py', args="--leaf=1 --spine=0" )
-        # pre-configured routing and bridging test
-        run.checkFlows( main, minFlowCount=15 )
-        run.pingAll( main )
-        run.killOnos( main, [ 0, 1, 2 ], '1', '0', '0' )
-        run.pingAll( main, 'CASE3_Failure', dumpflows=False )
-        run.recoverOnos( main, [ 0, 1, 2 ], '1', '0', '3' )
-        run.checkFlows( main, minFlowCount=15, tag='CASE3_Failure' )
-        run.pingAll( main, 'CASE3_Failure' )
-        # TODO Dynamic config of hosts in subnet
-        # TODO Dynamic config of host not in subnet
-        # TODO Dynamic config of vlan xconnect
-        # TODO Vrouter integration
-        # TODO Mcast integration
-        run.cleanup( main )
+        try:
+            from tests.USECASE.SegmentRouting.SRClusterRestart.dependencies.SRClusterRestartFuncs import SRClusterRestartFuncs
+        except ImportError:
+            main.log.error( "SRClusterRestartFuncs not found. Exiting the test" )
+            main.cleanAndExit()
+        try:
+            main.funcs
+        except ( NameError, AttributeError ):
+            main.funcs = SRClusterRestartFuncs()
+        main.funcs.runTest( main, 3, 3,
+                            '0x1', 15, 'Failure' )
+
diff --git a/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/dependencies/SRClusterRestartFuncs.py b/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/dependencies/SRClusterRestartFuncs.py
new file mode 100644
index 0000000..f36719f
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/dependencies/SRClusterRestartFuncs.py
@@ -0,0 +1,69 @@
+"""
+Copyright 2017 Open Networking Foundation ( ONF )
+
+Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
+the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
+or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
+
+    TestON is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 2 of the License, or
+    ( at your option ) any later version.
+
+    TestON is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with TestON.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+from tests.USECASE.SegmentRouting.dependencies.Testcaselib import Testcaselib as run
+
+class SRClusterRestartFuncs():
+
+    def __init__( self ):
+        self.default = ''
+        self.topo = dict()
+        self.topo[ '0x1' ] = ( 0, 1, '--leaf=1 --spine=0', 'single switch' )
+        self.topo[ '2x2' ] = ( 2, 2, '', '2x2 Leaf-spine' )
+        self.topo[ '4x4' ] = ( 4, 4, '--leaf=4 --spine=4', '4x4 Leaf-spine' )
+
+    def runTest( self, main, caseNum, numNodes, Topo, minFlow, testing, killList=[ 0, 1, 2 ] ):
+        description = "Cluster Restart test with " + self.topo[ Topo ][ 3 ]
+        caseTitle = 'CASE{}_'.format( caseNum ) + testing
+        main.case( description )
+        if not hasattr( main, 'apps' ):
+            run.initTest( main )
+        main.cfgName = Topo
+        main.Cluster.setRunningNode( numNodes )
+        run.installOnos( main )
+        run.loadJson( main )
+        run.loadChart( main )
+        if hasattr( main, 'Mininet1' ):
+            run.startMininet( main, 'cord_fabric.py', args=self.topo[ Topo ][ 2 ] )
+        else:
+            # Run the test with physical devices
+            # TODO: connect TestON to the physical network
+            pass
+        # pre-configured routing and bridging test
+        run.checkFlows( main, minFlowCount=minFlow )
+        run.pingAll( main )
+        switch = '{}'.format( self.topo[ Topo ][ 0 ] + self.topo[ Topo ][ 1 ] )
+        link = '{}'.format( ( self.topo[ Topo ][ 0 ] + self.topo[ Topo ][ 1 ] ) * self.topo[ Topo ][ 0 ] )
+        run.killOnos( main, killList, switch, link, '0' )
+        run.pingAll( main, caseTitle, dumpflows=False )
+        run.recoverOnos( main, killList, switch, link, '{}'.format( numNodes ) )
+        run.checkFlows( main, minFlowCount=minFlow, tag=caseTitle )
+        run.pingAll( main, caseTitle )
+        # TODO Dynamic config of hosts in subnet
+        # TODO Dynamic config of host not in subnet
+        # TODO Dynamic config of vlan xconnect
+        # TODO Vrouter integration
+        # TODO Mcast integration
+        if hasattr( main, 'Mininet1' ):
+            run.cleanup( main )
+        else:
+            # TODO: disconnect TestON from the physical network
+            pass
diff --git a/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/dependencies/__init__.py b/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/dependencies/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/SRClusterRestart/dependencies/__init__.py