Configure a docker network to connect to.
This currently attaches the test/quagga/radius containers to an existing docker network.
This is required if the test container needs to reside on the same network as the CiaB onos docker-network instances.
The manifest file option is: docker_network

Change-Id: I59615903580128c45c4a9001b602eb1c5e430c29
diff --git a/src/test/setup/cord-test.py b/src/test/setup/cord-test.py
index 369de0a..6fcd2d8 100755
--- a/src/test/setup/cord-test.py
+++ b/src/test/setup/cord-test.py
@@ -55,7 +55,7 @@
 
     def __init__(self, tests, instance = 0, num_instances = 1, ctlr_ip = None,
                  name = '', image = IMAGE, prefix = '', tag = 'candidate',
-                 env = None, rm = False, update = False):
+                 env = None, rm = False, update = False, network = None):
         self.tests = tests
         self.ctlr_ip = ctlr_ip
         self.rm = rm
@@ -92,6 +92,8 @@
             print('Starting test container %s, image %s, tag %s' %(self.name, self.image, self.tag))
             self.start(rm = False, volumes = volumes, environment = env,
                        host_config = host_config, tty = True)
+            if network is not None:
+                Container.connect_to_network(self.name, network)
 
     def execute_switch(self, cmd, shell = False):
         if self.olt:
@@ -434,7 +436,7 @@
         data_volume = '{}-data'.format(Onos.NAME) if test_manifest.shared_volume else None
         onos = Onos(image = Onos.IMAGE,
                     tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
-                    data_volume = data_volume, async = async_mode)
+                    data_volume = data_volume, async = async_mode, network = test_manifest.docker_network)
         if onos.running:
             onos_ips.append(onos.ipaddr)
     else:
@@ -450,7 +452,7 @@
             quagga_config = Onos.get_quagga_config(i)
             onos = Onos(name = name, image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
                         data_volume = data_volume, async = async_mode,
-                        quagga_config = quagga_config)
+                        quagga_config = quagga_config, network = test_manifest.docker_network)
             onos_instances.append(onos)
             if onos.running:
                 onos_ips.append(onos.ipaddr)
@@ -487,14 +489,16 @@
 
     if radius_ip is None:
         ##Start Radius container
-        radius = Radius(prefix = Container.IMAGE_PREFIX, update = update_map['radius'])
+        radius = Radius(prefix = Container.IMAGE_PREFIX, update = update_map['radius'],
+                        network = test_manifest.docker_network)
         radius_ip = radius.ip()
 
     print('Radius server running with IP %s' %radius_ip)
 
     if args.quagga == True:
         #Start quagga. Builds container if required
-        quagga = Quagga(prefix = Container.IMAGE_PREFIX, update = update_map['quagga'])
+        quagga = Quagga(prefix = Container.IMAGE_PREFIX, update = update_map['quagga'],
+                        network = test_manifest.docker_network)
 
     try:
         maas_api_key = FabricMAAS.get_api_key()
@@ -550,7 +554,8 @@
                               tag = nose_cnt['tag'],
                               env = test_cnt_env,
                               rm = False if args.keep else True,
-                              update = update_map['test'])
+                              update = update_map['test'],
+                              network = test_manifest.docker_network)
         test_slice_start = test_slice_end
         test_slice_end = test_slice_start + tests_per_container
         update_map['test'] = False
@@ -586,7 +591,8 @@
                               tag = nose_cnt['tag'],
                               env = test_cnt_env,
                               rm = False if args.keep else True,
-                              update = update_map['test'])
+                              update = update_map['test'],
+                              network = test_manifest.docker_network)
         if test_cnt.create and (test_manifest.start_switch or not test_manifest.olt):
             #For non parallel tests, we just restart the switch also for OLT's
             CordTester.switch_on_olt = False
@@ -690,7 +696,7 @@
     if onos_ip is None:
         data_volume = '{}-data'.format(Onos.NAME) if test_manifest.shared_volume else None
         onos = Onos(image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
-                    data_volume = data_volume, async = async_mode)
+                    data_volume = data_volume, async = async_mode, network = test_manifest.docker_network)
         if onos.running:
             onos_ips.append(onos.ipaddr)
     else:
@@ -706,7 +712,7 @@
             quagga_config = Onos.get_quagga_config(i)
             onos = Onos(name = name, image = Onos.IMAGE, tag = Onos.TAG, boot_delay = 60, cluster = cluster_mode,
                         data_volume = data_volume, async = async_mode,
-                        quagga_config = quagga_config)
+                        quagga_config = quagga_config, network = test_manifest.docker_network)
             onos_instances.append(onos)
             if onos.running:
                 onos_ips.append(onos.ipaddr)
@@ -734,14 +740,16 @@
 
     ##Start Radius container if not started
     if radius_ip is None:
-        radius = Radius(prefix = Container.IMAGE_PREFIX, update = update_map['radius'])
+        radius = Radius(prefix = Container.IMAGE_PREFIX, update = update_map['radius'],
+                        network = test_manifest.docker_network)
         radius_ip = radius.ip()
 
     print('Radius server running with IP %s' %radius_ip)
 
     if args.quagga == True:
         #Start quagga. Builds container if required
-        quagga = Quagga(prefix = Container.IMAGE_PREFIX, update = update_map['quagga'])
+        quagga = Quagga(prefix = Container.IMAGE_PREFIX, update = update_map['quagga'],
+                        network = test_manifest.docker_network)
         print('Quagga started')
 
     params = args.server.split(':')
@@ -785,7 +793,8 @@
                               tag = nose_cnt['tag'],
                               env = test_cnt_env,
                               rm = False,
-                              update = update_map['test'])
+                              update = update_map['test'],
+                              network = test_manifest.docker_network)
 
         if test_manifest.start_switch or not test_manifest.olt:
             test_cnt.start_switch()
diff --git a/src/test/utils/CordContainer.py b/src/test/utils/CordContainer.py
index 2c57823..908ef1b 100644
--- a/src/test/utils/CordContainer.py
+++ b/src/test/utils/CordContainer.py
@@ -24,6 +24,7 @@
 from itertools import chain
 from nsenter import Namespace
 from docker import Client
+from docker import utils as dockerutils
 from shutil import rmtree
 from OnosCtrl import OnosCtrl
 from OnosLog import OnosLog
@@ -103,6 +104,22 @@
         return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
 
     @classmethod
+    def connect_to_network(cls, name, network):
+        try:
+            cls.dckr.connect_container_to_network(name, network)
+            return True
+        except:
+            return False
+
+    @classmethod
+    def create_network(cls, network, subnet = None, gateway = None):
+        ipam_config = None
+        if subnet is not None and gateway is not None:
+            ipam_pool = dockerutils.create_ipam_pool(subnet = subnet, gateway = gateway)
+            ipam_config = dockerutils.create_ipam_config(pool_configs = [ipam_pool])
+        cls.dckr.create_network(network, driver='bridge', ipam = ipam_config)
+
+    @classmethod
     def cleanup(cls, image):
         cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
         for cnt in cnt_list:
@@ -435,7 +452,8 @@
 
     def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
                  boot_delay = 20, restart = False, network_cfg = None,
-                 cluster = False, data_volume = None, async = False, quagga_config = None):
+                 cluster = False, data_volume = None, async = False, quagga_config = None,
+                 network = None):
         if restart is True:
             ##Find the right image to restart
             running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
@@ -734,7 +752,7 @@
     NAME = 'cord-radius'
 
     def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
-                 boot_delay = 10, restart = False, update = False):
+                 boot_delay = 10, restart = False, update = False, network = None):
         super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
         if update is True or not self.img_exists():
             self.build_image(self.image_name)
@@ -750,6 +768,8 @@
             self.start(ports = self.ports, environment = self.env,
                        volumes = volumes,
                        host_config = host_config, tty = True)
+            if network is not None:
+                Container.connect_to_network(self.name, network)
             time.sleep(boot_delay)
 
     @classmethod
@@ -781,7 +801,8 @@
     NAME = 'cord-quagga'
 
     def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
-                 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
+                 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False,
+                 network = None):
         super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.QUAGGA_CONFIG)
         if update is True or not self.img_exists():
             self.build_image(self.image_name)
@@ -798,6 +819,8 @@
             self.start(ports = self.ports,
                        host_config = host_config,
                        volumes = volumes, tty = True)
+            if network is not None:
+                Container.connect_to_network(self.name, network)
             print('Starting Quagga on container %s' %self.name)
             self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
             time.sleep(boot_delay)
diff --git a/src/test/utils/TestManifest.py b/src/test/utils/TestManifest.py
index 78b01e1..74a95f6 100644
--- a/src/test/utils/TestManifest.py
+++ b/src/test/utils/TestManifest.py
@@ -35,6 +35,7 @@
             self.start_switch = args.start_switch
             self.image_prefix = args.prefix
             self.onos_image = args.onos
+            self.docker_network = None
             self.iterations = None
             self.server = '{}:{}'.format(CORD_TEST_HOST, CORD_TEST_PORT)
             self.jvm_heap_size = args.jvm_heap_size if args.jvm_heap_size else None
@@ -52,6 +53,7 @@
             self.start_switch = data.get('start_switch', self.olt)
             self.image_prefix = data.get('image_prefix', '')
             self.onos_image = data.get('onos_image', 'onosproject/onos:latest')
+            self.docker_network = data.get('docker_network', None)
             self.server = data.get('test_server', '{}:{}'.format(CORD_TEST_HOST, CORD_TEST_PORT))
             self.iterations = data.get('iterations', None)
             self.jvm_heap_size = data.get('jvm_heap_size', None)