Merge "Implement moveHost and SRRouting test CASE651"
diff --git a/TestON/drivers/common/cli/emulator/mininetclidriver.py b/TestON/drivers/common/cli/emulator/mininetclidriver.py
index a0a64e9..0cea933 100644
--- a/TestON/drivers/common/cli/emulator/mininetclidriver.py
+++ b/TestON/drivers/common/cli/emulator/mininetclidriver.py
@@ -909,9 +909,11 @@
         else:
             return main.TRUE
 
-    def moveHost( self, host, oldSw, newSw ):
+    def moveHost( self, host, oldSw, newSw, macAddr=None, prefixLen=None ):
         """
            Moves a host from one switch to another on the fly
+           If macAddr is specified, change MAC address of the host interface
+           to specified MAC address.
            Note: The intf between host and oldSw when detached
                 using detach(), will still show up in the 'net'
                 cmd, because switch.detach() doesn't affect switch.intfs[]
@@ -941,7 +943,10 @@
                 self.handle.sendline( cmd )
                 self.handle.expect( "mininet>" )
 
-                cmd = "px macaddr = hintf.MAC()"
+                if macAddr is None:
+                    cmd = "px macaddr = hintf.MAC()"
+                else:
+                    cmd = 'px macaddr = "%s"' % macAddr
                 print "cmd3= ", cmd
                 self.handle.sendline( cmd )
                 self.handle.expect( "mininet>" )
@@ -960,7 +965,7 @@
 
                 # Determine hostintf and Newswitchintf
                 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\
-                      ")[0]"
+                      ")[-1]"
                 print "cmd6= ", cmd
                 self.handle.sendline( cmd )
                 self.handle.expect( "mininet>" )
@@ -972,7 +977,8 @@
                 self.handle.expect( "mininet>" )
 
                 # Set ipaddress of the host-newSw interface
-                cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)"
+                cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf, " \
+                                     "prefixLen = %s )" % str( prefixLen )
                 print "cmd7 = ", cmd
                 self.handle.sendline( cmd )
                 self.handle.expect( "mininet>" )
@@ -1010,9 +1016,11 @@
                 main.log.exception( self.name + ": Uncaught exception!" )
                 return main.FALSE
 
-    def moveHostv6( self, host, oldSw, newSw ):
+    def moveHostv6( self, host, oldSw, newSw, macAddr=None ):
         """
            Moves a host from one switch to another on the fly
+           If macAddr is specified, change MAC address of the host interface
+           to specified MAC address.
            Note: The intf between host and oldSw when detached
                 using detach(), will still show up in the 'net'
                 cmd, because switch.detach() doesn't affect switch.intfs[]
@@ -1043,7 +1051,10 @@
                 self.handle.sendline( cmd )
                 self.handle.expect( "mininet>" )
 
-                cmd = "px macaddr = hintf.MAC()"
+                if macAddr is None:
+                    cmd = "px macaddr = hintf.MAC()"
+                else:
+                    cmd = 'px macaddr = "%s"' % macAddr
                 print "cmd3= ", cmd
                 self.handle.sendline( cmd )
                 self.handle.expect( "mininet>" )
@@ -1062,7 +1073,7 @@
 
                 # Determine hostintf and Newswitchintf
                 cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\
-                      ")[0]"
+                      ")[-1]"
                 print "cmd6= ", cmd
                 self.handle.sendline( cmd )
                 self.handle.expect( "mininet>" )
diff --git a/TestON/tests/USECASE/SegmentRouting/SRRouting/SRRouting.py b/TestON/tests/USECASE/SegmentRouting/SRRouting/SRRouting.py
index fc3f0b3..dafbacd 100644
--- a/TestON/tests/USECASE/SegmentRouting/SRRouting/SRRouting.py
+++ b/TestON/tests/USECASE/SegmentRouting/SRRouting/SRRouting.py
@@ -1134,3 +1134,27 @@
                          int( main.params[ "TOPO" ][ "linkNum" ] ), 3 )
         verify( main )
         lib.cleanup( main, copyKarafLog=False, removeHostComponent=True )
+
+    def CASE651( self, main ):
+        """
+        Move a single-homed host from port A to port B in DAAS-1
+        Test connectivity (expect no failure)
+
+        Repeat with DAAS-2
+        """
+        import time
+        from tests.USECASE.SegmentRouting.SRRouting.dependencies.SRRoutingTest import *
+        from tests.USECASE.SegmentRouting.dependencies.Testcaselib import Testcaselib as lib
+        main.case( "Move a single-homed host to another port in the same DAAS" )
+        setupTest( main, test_idx=651, onosNodes=3 )
+        main.Cluster.active( 0 ).CLI.balanceMasters()
+        time.sleep( float( main.params[ 'timers' ][ 'balanceMasterSleep' ] ) )
+        verify( main )
+
+        h1v4cfg = '{"of:0000000000000001/7" : { "interfaces" : [ { "ips" : [ "10.1.0.254/24" ], "vlan-untagged": 10 } ] } }'
+        lib.moveHost( main, "h1v4", "leaf1", "leaf1", "10.1.0.254", prefixLen=24, cfg=h1v4cfg )
+        verify( main )
+
+        h13v4cfg = '''{"of:0000000000000006/7" : { "interfaces" : [ { "ips" : [ "10.5.20.254/24" ], "vlan-untagged": 20 } ] } }'''
+        lib.moveHost( main, "h13v4", "leaf6", "leaf6", "10.5.20.254", prefixLen=24, cfg=h13v4cfg )
+        verify( main )
diff --git a/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py b/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
index b008984..ce6d20c 100644
--- a/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
+++ b/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
@@ -1202,3 +1202,48 @@
         utilities.assert_equals( expect=main.TRUE, actual=result,
                                  onpass="Location verification passed",
                                  onfail="Location verification failed" )
+
+    @staticmethod
+    def moveHost( main, hostName, srcSw, dstSw, gw, macAddr=None, prefixLen=None, cfg='', ipv6=False ):
+        """
+        Move specified host from srcSw to dstSw.
+        If srcSw and dstSw are same, the host will be moved from current port to
+        next available port.
+        Required:
+            hostName: name of the host. e.g., "h1"
+            srcSw: name of the switch that the host is attached to. e.g., "leaf1"
+            dstSw: name of the switch that the host will be moved to. e.g., "leaf2"
+            gw: ip address of the gateway of the new location
+        Optional:
+            macAddr: if specified, change MAC address of the host to the specified MAC address.
+            prefixLen: prefix length
+            cfg: port configuration as JSON string
+            ipv6: Use True to move IPv6 host
+        """
+
+        if not hasattr( main, 'Mininet1' ):
+            main.log.warn( "moveHost is supposed to be used only in Mininet." )
+            return
+
+        if ipv6:
+            main.Mininet1.moveHostv6( hostName, srcSw, dstSw, macAddr )
+        else:
+            main.Mininet1.moveHost( hostName, srcSw, dstSw, macAddr, prefixLen )
+
+        main.Mininet1.changeDefaultGateway( hostName, gw )
+        if cfg:
+            main.Cluster.active( 0 ).REST.setNetCfg( json.loads( cfg ),
+                                                     subjectClass="ports" )
+
+        main.Mininet1.discoverHosts( [ hostName, ] )
+
+        # Update expectedHost when MAC address is changed.
+        if macAddr is not None:
+            ipAddr = main.expectedHosts[ "network" ][ hostName ]
+            if ipAddr is not None:
+                for hostName, ip in main.expectedHosts[ "onos" ].items():
+                    if ip == ipAddr:
+                        vlan = hostName.split( "/" )[ -1 ]
+                        del main.expectedHosts[ "onos" ][ hostName ]
+                        main.expectedHosts[ "onos" ][ "{}/{}".format( macAddr, vlan ) ] = ip
+                        break