[ONOS-7475] Refactoring SR tests

Change-Id: I10e51fcb8b416167e6a338e9030933162640af89
diff --git a/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/SRLinkFailure.params b/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/SRLinkFailure.params
index ea3112c..7c546d4 100755
--- a/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/SRLinkFailure.params
+++ b/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/SRLinkFailure.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/SRLinkFailure/SRLinkFailure.py b/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/SRLinkFailure.py
index e8b6cf7..dfc6e6c 100644
--- a/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/SRLinkFailure.py
+++ b/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/SRLinkFailure.py
@@ -38,33 +38,17 @@
         Cause link failure
         Pingall
         """
-        from tests.USECASE.SegmentRouting.dependencies.Testcaselib import \
-            Testcaselib as run
-        if not hasattr( main, 'apps' ):
-            run.initTest( main )
+        try:
+            from tests.USECASE.SegmentRouting.SRLinkFailure.dependencies.SRLinkFailFuncs import SRLinkFailFuncs
+        except ImportError:
+            main.log.error( "SRLinkFailFuncs not found. Exiting the test" )
+            main.cleanAndExit()
+        try:
+            main.funcs
+        except ( NameError, AttributeError ):
+            main.funcs = SRLinkFailFuncs()
 
-        description = "Bridging and Routing sanity test with 2x2 Leaf-spine "
-        main.case( description )
-
-        main.cfgName = '2x2'
-        main.Cluster.setRunningNode( 1 )
-        run.installOnos( main )
-        run.startMininet( main, 'cord_fabric.py' )
-        # pre-configured routing and bridging test
-        run.checkFlows( main, minFlowCount=116 )
-        run.pingAll( main )
-        # link failure
-        run.killLink( main, 'spine101', 'leaf2', switches='4', links='6' )
-        run.pingAll( main, "CASE1_Failure" )
-        run.restoreLink( main, 'spine101', 'leaf2', 'of:0000000000000101',
-                         'of:0000000000000002', '2', '3', '4', '8' )
-        run.pingAll( main, "CASE1_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 )
+        main.funcs.runTest( main, 1, 1, '2x2', 116 )
 
     def CASE2( self, main ):
         """
@@ -74,31 +58,17 @@
         Cause link failure
         Pingall
         """
-        from tests.USECASE.SegmentRouting.dependencies.Testcaselib import \
-            Testcaselib as run
-        if not hasattr( main, 'apps' ):
-            run.initTest( main )
-        description = "Bridging and Routing sanity test with 4x4 Leaf-spine "
-        main.case( description )
-        main.cfgName = '4x4'
-        main.Cluster.setRunningNode( 1 )
-        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 )
-        # link failure
-        run.killLink( main, 'spine101', 'leaf2', switches='8', links='30' )
-        run.pingAll( main, "CASE2_Failure" )
-        run.restoreLink( main, 'spine101', 'leaf2', 'of:0000000000000101',
-                         'of:0000000000000002', '2', '3', '8', '32' )
-        run.pingAll( main, "CASE2_Recovery" )
-        # TODO Dynamic config of hosts in subnet
-        # TODO Dynamic config of host not in subnet
-        # TODO preconfigured xconnect
-        # TODO Vrouter integration
-        # TODO Mcast integration
-        run.cleanup( main )
+        try:
+            from tests.USECASE.SegmentRouting.SRLinkFailure.dependencies.SRLinkFailFuncs import SRLinkFailFuncs
+        except ImportError:
+            main.log.error( "SRLinkFailFuncs not found. Exiting the test" )
+            main.cleanAndExit()
+        try:
+            main.funcs
+        except ( NameError, AttributeError ):
+            main.funcs = SRLinkFailFuncs()
+
+        main.funcs.runTest( main, 2, 1, '4x4', 350 )
 
     def CASE4( self, main ):
         """
@@ -108,31 +78,17 @@
         Cause link failure
         Pingall
         """
-        from tests.USECASE.SegmentRouting.dependencies.Testcaselib import \
-            Testcaselib as run
-        if not hasattr( main, 'apps' ):
-            run.initTest( main )
-        description = "Bridging and Routing sanity test with 2x2 Leaf-spine "
-        main.case( description )
-        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 )
-        # link failure
-        run.killLink( main, 'spine101', 'leaf2', switches='4', links='6' )
-        run.pingAll( main, "CASE3_Failure" )
-        run.restoreLink( main, 'spine101', 'leaf2', 'of:0000000000000101',
-                         'of:0000000000000002', '2', '3', '4', '8' )
-        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.SRLinkFailure.dependencies.SRLinkFailFuncs import SRLinkFailFuncs
+        except ImportError:
+            main.log.error( "SRLinkFailFuncs not found. Exiting the test" )
+            main.cleanAndExit()
+        try:
+            main.funcs
+        except ( NameError, AttributeError ):
+            main.funcs = SRLinkFailFuncs()
+
+        main.funcs.runTest( main, 3, 3, '2x2', 116 )
 
     def CASE5( self, main ):
         """
@@ -142,28 +98,14 @@
         Cause link failure
         Pingall
         """
-        from tests.USECASE.SegmentRouting.dependencies.Testcaselib import \
-            Testcaselib as run
-        if not hasattr( main, 'apps' ):
-            run.initTest( main )
-        description = "Bridging and Routing sanity test with 4x4 Leaf-spine "
-        main.case( description )
-        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 )
-        # link failure
-        run.killLink( main, 'spine101', 'leaf2', switches='8', links='30' )
-        run.pingAll( main, "CASE2_Failure" )
-        run.restoreLink( main, 'spine101', 'leaf2', 'of:0000000000000101',
-                         'of:0000000000000002', '2', '3', '8', '32' )
-        run.pingAll( main, "CASE2_Recovery" )
-        # TODO Dynamic config of hosts in subnet
-        # TODO Dynamic config of host not in subnet
-        # TODO preconfigured xconnect
-        # TODO Vrouter integration
-        # TODO Mcast integration
-        run.cleanup( main )
+        try:
+            from tests.USECASE.SegmentRouting.SRLinkFailure.dependencies.SRLinkFailFuncs import SRLinkFailFuncs
+        except ImportError:
+            main.log.error( "SRLinkFailFuncs not found. Exiting the test" )
+            main.cleanAndExit()
+        try:
+            main.funcs
+        except ( NameError, AttributeError ):
+            main.funcs = SRLinkFailFuncs()
+
+        main.funcs.runTest( main, 4, 3, '4x4', 350 )
diff --git a/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/dependencies/SRLinkFailFuncs.py b/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/dependencies/SRLinkFailFuncs.py
new file mode 100644
index 0000000..1837cf4
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/dependencies/SRLinkFailFuncs.py
@@ -0,0 +1,72 @@
+"""
+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 SRLinkFailFuncs():
+
+    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' )
+        self.switchOne = 'spine101'
+        self.switchTwo = 'leaf2'
+        self.dpidOne = 'of:0000000000000101'
+        self.dpidTwo = 'of:0000000000000002'
+        self.portOne = '2'
+        self.portTwo = '3'
+
+    def runTest( self, main, caseNum, numNodes, Topo, minFlow ):
+        if not hasattr( main, 'apps' ):
+            run.initTest( main )
+
+        description = "Bridging and Routing Link Failure test with " + self.topo[ Topo ][ 3 ] + " and {} Onos".format( numNodes )
+        main.case( description )
+
+        main.cfgName = Topo
+        main.Cluster.setRunningNode( numNodes )
+        run.installOnos( main )
+        run.loadJson( main )
+        run.loadChart( main )
+        run.startMininet( main, 'cord_fabric.py', args=self.topo[ Topo ][ 2 ] )
+        # pre-configured routing and bridging test
+        run.checkFlows( main, minFlowCount=minFlow )
+        run.pingAll( main )
+        switch = self.topo[ Topo ][ 0 ] + self.topo[ Topo ][ 1 ]
+        link = ( self.topo[ Topo ][ 0 ] + self.topo[ Topo ][ 1 ] ) * self.topo[ Topo ][ 0 ]
+        # link failure
+        run.killLink( main, self.switchOne, self.switchTwo, switches='{}'.format( switch ), links='{}'.format( link - 2 ) )
+        run.pingAll( main, "CASE{}_Failure".format( caseNum ) )
+        run.restoreLink( main, self.switchOne, self.switchTwo, self.dpidOne,
+                         self.dpidTwo, self.portOne, self.portTwo, '{}'.format( switch ), '{}'.format( link ) )
+        run.pingAll( main, "CASE{}_Recovery".format( caseNum ) )
+        # 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/SRLinkFailure/dependencies/__init__.py b/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/dependencies/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/SRLinkFailure/dependencies/__init__.py