Add option to look for IP based on iface

Change-Id: Icc186386b73f262224aad273040ace9ff5ce1bd4
diff --git a/TestON/drivers/common/cli/onosdriver.py b/TestON/drivers/common/cli/onosdriver.py
index 628101c..1abf258 100755
--- a/TestON/drivers/common/cli/onosdriver.py
+++ b/TestON/drivers/common/cli/onosdriver.py
@@ -2230,7 +2230,7 @@
 
         return passed
 
-    def getIpAddr( self ):
+    def getIpAddr( self, iface=None ):
         """
         Update self.ip_address with numerical ip address. If multiple IP's are
         located on the device, will attempt to use self.nicAddr to choose the
@@ -2240,7 +2240,7 @@
         ONLY WORKS WITH IPV4 ADDRESSES
         """
         try:
-            localhost = "127.0.0.1"
+            LOCALHOST = "127.0.0.1"
             ipPat = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
             pattern = re.compile( ipPat )
             match = re.search( pattern, self.ip_address )
@@ -2257,24 +2257,41 @@
                     nicMatch = re.search( nicPat, curIp )
                     if nicMatch:
                         return self.ip_address
-            # ELSE: attempt to get correct address.
-            raw = subprocess.check_output( "ifconfig")
+            # ELSE: IF iface, return ip of interface
+            cmd = "ifconfig"
+            ifPat = re.compile( "inet addr:({})".format( ipPat ) )
+            if iface:
+                cmd += " " + str( iface )
+            raw = subprocess.check_output( cmd.split() )
             ifPat = re.compile( "inet addr:({})".format( ipPat ) )
             ips = re.findall( ifPat, raw )
+            if iface:
+                if ips:
+                    ip = ips[0]
+                    self.ip_address = ip
+                    return ip
+                else:
+                    main.log.error( "Error finding ip, ifconfig output:".format( raw ) )
+            # ELSE: attempt to get address matching nicPat.
             if nicPat:
                 for ip in ips:
                     curMatch = re.search( nicPat, ip )
                     if curMatch:
                         self.ip_address = ip
                         return ip
-            else:
-                tmpList = [ ip for ip in ips if ip is not localhost ]
+            else:  # If only one non-localhost ip, return that
+                tmpList = [ ip for ip in ips if ip is not LOCALHOST ]
                 if len(tmpList) == 1:
                     curIp = tmpList[0]
                     self.ip_address = curIp
                     return curIp
+            # Either no non-localhost IPs, or more than 1
             main.log.warn( "getIpAddr failed to find a public IP address" )
-            return localhost
+            return LOCALHOST
+        except CalledProcessError:
+            main.log.exception( "Error executing ifconfig" )
+        except IndexError:
+            main.log.exception( "Error getting IP Address" )
         except Exception:
             main.log.exception( "Uncaught exception" )
 
diff --git a/TestON/tests/HA/HAscaling/HAscaling.params b/TestON/tests/HA/HAscaling/HAscaling.params
index 5b5358d..ff4e306 100644
--- a/TestON/tests/HA/HAscaling/HAscaling.params
+++ b/TestON/tests/HA/HAscaling/HAscaling.params
@@ -20,7 +20,10 @@
     <testcases>1,[2,8,21,3,8,4,5,14,16,17]*1,[6,8,3,7,4,15,17,9,8,4,10,8,4,11,8,4,12,8,4]*13,13</testcases>
 
     <scaling>1,3b,3,5b,5,7b,7,7b,5,5b,3,3b,1</scaling>
-    <serverPort>8000</serverPort>
+    <server>
+        <port>8000</port>
+        <interface>eth0</interface>
+    </server>
     <apps></apps>
     <ONOS_Configuration>
         <org.onosproject.net.intent.impl.compiler.IntentConfigurableRegistrator>
diff --git a/TestON/tests/HA/HAscaling/HAscaling.py b/TestON/tests/HA/HAscaling/HAscaling.py
index 43f89fb..2064f1e 100644
--- a/TestON/tests/HA/HAscaling/HAscaling.py
+++ b/TestON/tests/HA/HAscaling/HAscaling.py
@@ -130,7 +130,7 @@
             killResults = killResults and killed
 
         main.step( "Setup server for cluster metadata file" )
-        port = main.params['serverPort']
+        port = main.params['server']['port']
         rootDir = os.path.dirname( main.testFile ) + "/dependencies"
         main.log.debug( "Root dir: {}".format( rootDir ) )
         status = main.Server.start( main.ONOSbench,
@@ -227,7 +227,8 @@
                                  onfail="Copy backup config file failed" )
         # we need to modify the onos-service file to use remote metadata file
         # url for cluster metadata file
-        ip = main.ONOSbench.getIpAddr()
+        iface = main.params['server'].get( 'interface' )
+        ip = main.ONOSbench.getIpAddr( iface=iface )
         metaFile = "cluster.json"
         javaArgs = r"-Donos.cluster.metadata.uri=http:\/\/{}:{}\/{}".format( ip, port, metaFile )
         main.log.warn( javaArgs )
diff --git a/TestON/tests/HA/HAswapNodes/HAswapNodes.params b/TestON/tests/HA/HAswapNodes/HAswapNodes.params
index 7253612..d3729a4 100644
--- a/TestON/tests/HA/HAswapNodes/HAswapNodes.params
+++ b/TestON/tests/HA/HAswapNodes/HAswapNodes.params
@@ -19,7 +19,10 @@
     #CASE17: Check for basic functionality with distributed primitives
     <testcases>1,[2,8,21,3,8,4,5,14,16,17]*1,6,8,3,7,4,15,17,9,8,4,10,8,4,11,8,4,12,8,4,13</testcases>
 
-    <serverPort>8000</serverPort>
+    <server>
+        <port>8000</port>
+        <interface>eth0</interface>
+    </server>
     <apps></apps>
     <ONOS_Configuration>
         <org.onosproject.net.intent.impl.compiler.IntentConfigurableRegistrator>
diff --git a/TestON/tests/HA/HAswapNodes/HAswapNodes.py b/TestON/tests/HA/HAswapNodes/HAswapNodes.py
index 30efbbd..8ea1490 100644
--- a/TestON/tests/HA/HAswapNodes/HAswapNodes.py
+++ b/TestON/tests/HA/HAswapNodes/HAswapNodes.py
@@ -130,7 +130,7 @@
             killResults = killResults and killed
 
         main.step( "Setup server for cluster metadata file" )
-        port = main.params['serverPort']
+        port = main.params['server']['port']
         rootDir = os.path.dirname( main.testFile ) + "/dependencies"
         main.log.debug( "Root dir: {}".format( rootDir ) )
         status = main.Server.start( main.ONOSbench,
@@ -221,7 +221,8 @@
                                  onfail="Copy backup config file failed" )
         # we need to modify the onos-service file to use remote metadata file
         # url for cluster metadata file
-        ip = main.ONOSbench.getIpAddr()
+        iface = main.params['server'].get( 'interface' )
+        ip = main.ONOSbench.getIpAddr( iface=iface )
         metaFile = "cluster.json"
         javaArgs = r"-Donos.cluster.metadata.uri=http:\/\/{}:{}\/{}".format( ip, port, metaFile )
         main.log.warn( javaArgs )