Fix Jenkins build
[nfvbench.git] / nfvbench / chaining.py
index b9ed48b..d6f67f9 100644 (file)
@@ -158,6 +158,10 @@ class ChainVnfPort(object):
                     'binding:vnic_type': vnic_type
                 }
             }
+            subnet_id = chain_network.get_subnet_uuid()
+            if subnet_id:
+                body['port']['fixed_ips'] = [{'subnet_id': subnet_id}]
+
             port = self.manager.neutron_client.create_port(body)
             self.port = port['port']
             LOG.info('Created port %s', name)
@@ -240,11 +244,13 @@ class ChainNetwork(object):
             self.name = self.name + suffix
         self.segmentation_id = self._get_item(network_config.segmentation_id,
                                               chain_id, auto_index=True)
+        self.subnet_name = self._get_item(network_config.subnet, chain_id)
         self.physical_network = self._get_item(network_config.physical_network, chain_id)
 
         self.reuse = False
         self.network = None
         self.vlan = None
+        self.router_name = None
         if manager.config.l3_router and hasattr(network_config, 'router_name'):
             self.router_name = network_config.router_name
         try:
@@ -278,7 +284,7 @@ class ChainNetwork(object):
                 return item_field[index]
             except IndexError:
                 raise ChainException("List %s is too short for chain index %d" %
-                                     (str(item_field), index))
+                                     (str(item_field), index)) from IndexError
         # single value is configured
         if auto_index:
             return item_field + index
@@ -351,6 +357,18 @@ class ChainNetwork(object):
         """
         return self.network['id']
 
+    def get_subnet_uuid(self):
+        """
+        Extract UUID of this subnet network.
+
+        :return: UUID of this subnet network
+        """
+        for subnet in self.network['subnets']:
+            if self.subnet_name == self.manager.neutron_client \
+                    .show_subnet(subnet)['subnet']['name']:
+                return subnet
+        return None
+
     def get_vlan(self):
         """
         Extract vlan for this network.
@@ -482,7 +500,7 @@ class ChainVnf(object):
             vnf_gateway1_cidr = g1cidr
             vnf_gateway2_cidr = g2cidr
 
-        with open(BOOT_SCRIPT_PATHNAME, 'r') as boot_script:
+        with open(BOOT_SCRIPT_PATHNAME, 'r', encoding="utf-8") as boot_script:
             content = boot_script.read()
         vm_config = {
             'forwarder': config.vm_forwarder,
@@ -924,7 +942,10 @@ class Chain(object):
         if port_index:
             # this will pick the last item in array
             port_index = -1
-        return self.networks[port_index].get_vlan()
+        # This string filters networks connected to TG, in case of
+        # l3-router feature we have 4 networks instead of 2
+        networks = [x for x in self.networks if not x.router_name]
+        return networks[port_index].get_vlan()
 
     def get_vxlan(self, port_index):
         """Get the VXLAN id on a given port.
@@ -1191,7 +1212,8 @@ class ChainManager(object):
             self.vlans = [self._check_list('vlans[0]', self.config.vlans[0], re_vlan),
                           self._check_list('vlans[1]', self.config.vlans[1], re_vlan)]
         except IndexError:
-            raise ChainException('vlans parameter is mandatory. Set valid value in config file')
+            raise ChainException(
+                'vlans parameter is mandatory. Set valid value in config file') from IndexError
 
     def _get_dest_macs_from_config(self):
         re_mac = "[0-9a-fA-F]{2}([-:])[0-9a-fA-F]{2}(\\1[0-9a-fA-F]{2}){4}$"
@@ -1205,6 +1227,8 @@ class ChainManager(object):
         # if it is a single int or mac, make it a list of 1 int
         if isinstance(ll, (int, str)):
             ll = [ll]
+        else:
+            ll = list(ll)
         for item in ll:
             if not re.match(pattern, str(item)):
                 raise ChainException("Invalid format '{item}' specified in {fname}"
@@ -1317,6 +1341,7 @@ class ChainManager(object):
             lookup_only = True
             ext_net = self.config.external_networks
             net_cfg = [AttrDict({'name': name,
+                                 'subnet': None,
                                  'segmentation_id': None,
                                  'physical_network': None})
                        for name in [ext_net.left, ext_net.right]]
@@ -1516,7 +1541,7 @@ class ChainManager(object):
             hypervisor = self.get_hypervisor_from_mac(dst_mac)
             if hypervisor:
                 LOG.info('Found hypervisor for EXT chain: %s', hypervisor.hypervisor_hostname)
-                return[':' + hypervisor.hypervisor_hostname]
+                return [':' + hypervisor.hypervisor_hostname]
         # no openstack = no chains
         return []