Test: Configure dhcp relay ports while setting up the cord-tester environment.
Use the dhcp relay ports instead of uplink ports in dhcp relay test.
Make sure not to start the ovs switch on the host if already started.
Applicable when running with olt config and while running tests
parallely using the --num-containers option.

Change-Id: I5d6e8f26ce0cc6fa013338bedbc106210939d65a
diff --git a/src/test/dhcprelay/dhcprelayTest.py b/src/test/dhcprelay/dhcprelayTest.py
index a5a7cf6..dee0aa4 100644
--- a/src/test/dhcprelay/dhcprelayTest.py
+++ b/src/test/dhcprelay/dhcprelayTest.py
@@ -99,7 +99,12 @@
         cls.olt = OltConfig(olt_conf_file = cls.olt_conf_file)
         cls.port_map, _ = cls.olt.olt_port_map()
         if cls.port_map:
-            cls.relay_interface_port = cls.port_map['uplink']
+            ##Per subscriber, we use 1 relay port
+            try:
+                relay_port = cls.port_map[cls.port_map['relay_ports'][0]]
+            except:
+                relay_port = cls.port_map['uplink']
+            cls.relay_interface_port = relay_port
             cls.relay_interfaces = (cls.port_map[cls.relay_interface_port],)
         else:
             cls.relay_interface_port = 100
@@ -111,13 +116,15 @@
             for port in cls.port_map['ports']:
                 port_num = cls.port_map[port]
                 if port_num == cls.port_map['uplink']:
-                    ##configure the dhcp server virtual interface on the same subnet as client interface
-                    ip = cls.get_host_ip(1)
-                else:
-                    ip = cls.get_host_ip(port_num)
+                    continue
+                ip = cls.get_host_ip(port_num)
                 mac = cls.get_mac(port)
                 interface_list.append((port_num, ip, mac))
 
+            #configure dhcp server virtual interface on the same subnet as first client interface
+            relay_ip = cls.get_host_ip(interface_list[0][0])
+            relay_mac = cls.get_mac(cls.port_map[cls.relay_interface_port])
+            interface_list.append((cls.relay_interface_port, relay_ip, relay_mac))
             cls.onos_interface_load(interface_list)
 
     @classmethod
diff --git a/src/test/setup/cord-test.py b/src/test/setup/cord-test.py
index 4f406e3..8bc17ea 100755
--- a/src/test/setup/cord-test.py
+++ b/src/test/setup/cord-test.py
@@ -37,6 +37,7 @@
                        ('/var/run/docker.sock', '/var/run/docker.sock')
                        )
     basename = 'cord-tester'
+    switch_on_olt = False
     IMAGE = 'cord-test/nose'
     ALL_TESTS = ('tls', 'dhcp', 'dhcprelay','igmp', 'subscriber', 'cordSubscriber', 'vrouter', 'flows', 'proxyarp', 'acl')
 
@@ -93,6 +94,9 @@
         s_file,s_sandbox = ('of-bridge-local.sh',self.tester_base) if self.olt else ('of-bridge.sh',self.sandbox_setup)
         ovs_cmd = os.path.join(s_sandbox, s_file) + ' {0}'.format(self.switch)
         if self.olt:
+            if CordTester.switch_on_olt is True:
+                return
+            CordTester.switch_on_olt = True
             ovs_cmd += ' {0}'.format(self.ctlr_ip)
             print('Starting OVS on the host')
         else:
@@ -126,7 +130,8 @@
         start_vlan += port_num
         uplink = self.port_map['uplink']
         wan = self.port_map['wan']
-        for port in self.port_map['ports']:
+        port_list = self.port_map['ports'] + self.port_map['relay_ports']
+        for port in port_list:
             guest_if = port
             local_if = '{0}_{1}'.format(guest_if, port_num+1)
             guest_ip = '{0}.{1}/24'.format(tester_intf_subnet, port_num+1)
@@ -398,7 +403,6 @@
         if test_cnt.create and (args.start_switch or not args.olt):
             test_cnt.start_switch()
         if test_cnt.create and test_cnt.olt:
-            print('Test container create with port num: %d' %port_num)
             _, port_num = test_cnt.setup_intfs(port_num = port_num)
 
     thread_pool = ThreadPool(len(test_containers), queue_size = 1, wait_timeout=1)
@@ -415,6 +419,8 @@
                               rm = False if args.keep else True,
                               update = update_map['test'])
         if test_cnt.create and (args.start_switch or not args.olt):
+            #For non parallel tests, we just restart the switch also for OLT's
+            CordTester.switch_on_olt = False
             test_cnt.start_switch()
         if test_cnt.create and test_cnt.olt:
             test_cnt.setup_intfs(port_num = port_num)
diff --git a/src/test/utils/OltConfig.py b/src/test/utils/OltConfig.py
index f3450a2..730b23d 100644
--- a/src/test/utils/OltConfig.py
+++ b/src/test/utils/OltConfig.py
@@ -1,12 +1,12 @@
-# 
+#
 # Copyright 2016-present Ciena Corporation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
-# 
+#
 # http://www.apache.org/licenses/LICENSE-2.0
-# 
+#
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -40,11 +40,17 @@
             port_map = {}
             if self.olt_conf['port_map'].has_key('ports'):
                 port_map['ports'] = self.olt_conf['port_map']['ports']
+                num_ports = len(port_map['ports'])
             else:
                 port_map['ports'] = []
                 num_ports = int(self.olt_conf['port_map']['num_ports'])
                 for port in xrange(0, num_ports*2, 2):
                     port_map['ports'].append('veth{}'.format(port))
+            ##also add dhcprelay ports. We add as many relay ports as subscriber ports
+            relay_ports = num_ports
+            port_map['relay_ports'] = []
+            for port in xrange(relay_ports*2, relay_ports*4, 2):
+                port_map['relay_ports'].append('veth{}'.format(port))
             port_num = 1
             port_map['uplink'] = int(self.olt_conf['uplink'])
             port_map['wan'] = None
@@ -59,6 +65,11 @@
                     ##create tx,rx map
                     port_list.append( (port_map['uplink'], port_num ) )
                 port_num += 1
+            ##build the port and inverse map for relay ports
+            for port in port_map['relay_ports']:
+                port_map[port_num] = port
+                port_map[port] = port_num
+                port_num += 1
             port_map['start_vlan'] = 0
             if self.olt_conf['port_map'].has_key('host'):
                 port_map['host'] = self.olt_conf['port_map']['host']