Esempio n. 1
0
 def add_auto_addrs_on_network_ports(self, context, subnet, ipam_subnet):
     """For an auto-address subnet, add addrs for ports on the net."""
     with context.session.begin(subtransactions=True):
         network_id = subnet['network_id']
         port_qry = context.session.query(models_v2.Port)
         ports = port_qry.filter(
             and_(
                 models_v2.Port.network_id == network_id,
                 ~models_v2.Port.device_owner.in_(
                     constants.ROUTER_INTERFACE_OWNERS_SNAT)))
         updated_ports = []
         for port in ports:
             ip_address = self._calculate_ipv6_eui64_addr(
                 context, subnet, port['mac_address'])
             allocated = models_v2.IPAllocation(network_id=network_id,
                                                port_id=port['id'],
                                                ip_address=ip_address,
                                                subnet_id=subnet['id'])
             try:
                 # Do the insertion of each IP allocation entry within
                 # the context of a nested transaction, so that the entry
                 # is rolled back independently of other entries whenever
                 # the corresponding port has been deleted.
                 with context.session.begin_nested():
                     context.session.add(allocated)
                 updated_ports.append(port['id'])
             except db_exc.DBReferenceError:
                 LOG.debug(
                     "Port %s was deleted while updating it with an "
                     "IPv6 auto-address. Ignoring.", port['id'])
         return updated_ports
 def add_auto_addrs_on_network_ports(self, context, subnet, ipam_subnet):
     """For an auto-address subnet, add addrs for ports on the net."""
     # TODO(kevinbenton): remove after bug/1666493 is resolved
     if subnet['id'] != ipam_subnet.subnet_manager.neutron_id:
         raise RuntimeError(
             "Subnet manager doesn't match subnet. %s != %s" %
             (subnet['id'], ipam_subnet.subnet_manager.neutron_id))
     # TODO(ataraday): switched for writer when flush_on_subtransaction
     # will be available for neutron
     with context.session.begin(subtransactions=True):
         network_id = subnet['network_id']
         port_qry = context.session.query(models_v2.Port)
         ports = port_qry.filter(
             and_(
                 models_v2.Port.network_id == network_id,
                 ~models_v2.Port.device_owner.in_(
                     constants.ROUTER_INTERFACE_OWNERS_SNAT)))
         updated_ports = []
         ipam_driver = driver.Pool.get_instance(None, context)
         factory = ipam_driver.get_address_request_factory()
         for port in ports:
             ip = {
                 'subnet_id': subnet['id'],
                 'subnet_cidr': subnet['cidr'],
                 'eui64_address': True,
                 'mac': port['mac_address']
             }
             ip_request = factory.get_request(context, port, ip)
             # TODO(kevinbenton): remove after bug/1666493 is resolved
             LOG.debug(
                 "Requesting with IP request: %s port: %s ip: %s "
                 "for subnet %s and ipam_subnet %s", ip_request, port, ip,
                 subnet, ipam_subnet)
             ip_address = ipam_subnet.allocate(ip_request)
             allocated = models_v2.IPAllocation(network_id=network_id,
                                                port_id=port['id'],
                                                ip_address=ip_address,
                                                subnet_id=subnet['id'])
             try:
                 # Do the insertion of each IP allocation entry within
                 # the context of a nested transaction, so that the entry
                 # is rolled back independently of other entries whenever
                 # the corresponding port has been deleted.
                 with db_api.context_manager.writer.using(context):
                     context.session.add(allocated)
                 updated_ports.append(port['id'])
             except db_exc.DBReferenceError:
                 LOG.debug(
                     "Port %s was deleted while updating it with an "
                     "IPv6 auto-address. Ignoring.", port['id'])
                 LOG.debug("Reverting IP allocation for %s", ip_address)
                 # Do not fail if reverting allocation was unsuccessful
                 try:
                     ipam_subnet.deallocate(ip_address)
                 except Exception:
                     LOG.debug("Reverting IP allocation failed for %s",
                               ip_address)
         return updated_ports
 def _make_fixed_ip(self, port_id, network_id, subnet_id, ip):
     session = self.context.session
     ip_allocation = models_v2.IPAllocation(port_id=port_id,
                                            ip_address=ip,
                                            subnet_id=subnet_id,
                                            network_id=network_id)
     session.add(ip_allocation)
     session.flush()
     return ip_allocation
Esempio n. 4
0
    def create_ipallocation(self, ipa):
        with self._session.begin(subtransactions=True):
            ipallocation = nmodels.IPAllocation(ip_address=ipa["ip_address"],
                                                network_id=ipa["network_id"],
                                                port_id=ipa["port_id"],
                                                subnet_id=ipa["subnet_id"])
            self._session.add(ipallocation)

        return ipallocation
Esempio n. 5
0
    def _create_router(self, gw_port=True, num_ports=2, create_routes=True):
        # GW CIDR: 10.0.0.0/24
        # Interface CIDRS: 10.0.1.0/24, 10.0.2.0/24, etc.
        router_id = uuidutils.generate_uuid()
        port_gw_cidr = netaddr.IPNetwork('10.0.0.0/24')
        rports = []
        if gw_port:
            port_gw = models_v2.Port(
                id=uuidutils.generate_uuid(),
                fixed_ips=[
                    models_v2.IPAllocation(ip_address=str(port_gw_cidr.ip + 1))
                ])
            rports.append(
                l3_models.RouterPort(router_id=router_id, port=port_gw))
        else:
            port_gw = None

        port_cidrs = []
        port_subnets = []
        for idx in range(num_ports):
            cidr = port_gw_cidr.cidr.next(idx + 1)
            port = models_v2.Port(
                id=uuidutils.generate_uuid(),
                fixed_ips=[
                    models_v2.IPAllocation(ip_address=str(cidr.ip + 1))
                ])
            port_cidrs.append(cidr)
            rports.append(l3_models.RouterPort(router_id=router_id, port=port))
            port_subnets.append({'cidr': str(cidr)})

        routes = []
        if create_routes:
            for cidr in [*port_cidrs, port_gw_cidr]:
                routes.append(
                    l3_models.RouterRoute(destination=str(cidr.next(100)),
                                          nexthop=str(cidr.ip + 10)))
        return (l3_models.Router(id=router_id,
                                 attached_ports=rports,
                                 route_list=routes,
                                 gw_port_id=port_gw.id if port_gw else None),
                port_subnets)
Esempio n. 6
0
def write_neutron_db(Session, port):
    session = Session()
    port_ref = models_v2.Port()
    values = dict(port)
    port_ref.fixed_ips = [models_v2.IPAllocation()]
    fixed_ip_dict = dict(values.pop('fixed_ips')[0])
    fixed_ip_dict['ip_address'] = str(fixed_ip_dict['ip_address'])
    port_ref.fixed_ips[0].update(fixed_ip_dict)
    port_ref.update(values)
    port_ref.mac_address = str(port_ref.mac_address)
    session.add(port_ref)
    session.commit()
Esempio n. 7
0
 def add_auto_addrs_on_network_ports(self, context, subnet, ipam_subnet):
     """For an auto-address subnet, add addrs for ports on the net."""
     with context.session.begin(subtransactions=True):
         network_id = subnet['network_id']
         port_qry = context.session.query(models_v2.Port)
         ports = port_qry.filter(
             and_(
                 models_v2.Port.network_id == network_id,
                 ~models_v2.Port.device_owner.in_(
                     constants.ROUTER_INTERFACE_OWNERS_SNAT)))
         updated_ports = []
         ipam_driver = driver.Pool.get_instance(None, context)
         factory = ipam_driver.get_address_request_factory()
         for port in ports:
             ip = {
                 'subnet_id': subnet['id'],
                 'subnet_cidr': subnet['cidr'],
                 'eui64_address': True,
                 'mac': port['mac_address']
             }
             ip_request = factory.get_request(context, port, ip)
             try:
                 ip_address = ipam_subnet.allocate(ip_request)
                 allocated = models_v2.IPAllocation(network_id=network_id,
                                                    port_id=port['id'],
                                                    ip_address=ip_address,
                                                    subnet_id=subnet['id'])
                 # Do the insertion of each IP allocation entry within
                 # the context of a nested transaction, so that the entry
                 # is rolled back independently of other entries whenever
                 # the corresponding port has been deleted.
                 with context.session.begin_nested():
                     context.session.add(allocated)
                 updated_ports.append(port['id'])
             except db_exc.DBReferenceError:
                 LOG.debug(
                     "Port %s was deleted while updating it with an "
                     "IPv6 auto-address. Ignoring.", port['id'])
                 LOG.debug("Reverting IP allocation for %s", ip_address)
                 # Do not fail if reverting allocation was unsuccessful
                 try:
                     ipam_subnet.deallocate(ip_address)
                 except Exception:
                     LOG.debug("Reverting IP allocation failed for %s",
                               ip_address)
             except ipam_exc.IpAddressAlreadyAllocated:
                 LOG.debug(
                     "Port %s got IPv6 auto-address in a concurrent "
                     "create or update port request. Ignoring.", port['id'])
         return updated_ports
Esempio n. 8
0
    def _update_ips_for_port(self, context, port):
        LOG.info(_LI("_update_ips_for_port called()"))
        port['fixed_ips'] is not attributes.ATTR_NOT_SPECIFIED
        filter = {'network_id': [port['network_id']]}
        subnets = self._core_plugin.get_subnets(context, filters=filter)
        result = self._core_plugin.ipam._generate_ip(context, subnets)
        LOG.info(_LI("_update_ips_for_port generated ip %s"), result)

        allocated = models_v2.IPAllocation(network_id=port['network_id'],
                                           port_id=port['id'],
                                           ip_address=result['ip_address'],
                                           subnet_id=result['subnet_id'])
        context.session.add(allocated)
        return result
Esempio n. 9
0
 def _store_ip_allocation(context, ip_address, network_id, subnet_id,
                          port_id):
     LOG.debug(
         "Allocated IP %(ip_address)s "
         "(%(network_id)s/%(subnet_id)s/%(port_id)s)", {
             'ip_address': ip_address,
             'network_id': network_id,
             'subnet_id': subnet_id,
             'port_id': port_id
         })
     allocated = models_v2.IPAllocation(network_id=network_id,
                                        port_id=port_id,
                                        ip_address=ip_address,
                                        subnet_id=subnet_id)
     context.session.add(allocated)
Esempio n. 10
0
    def _update_port_ip(self, context, port, new_ip):
        subid = port['fixed_ips'][0]['subnet_id']
        new_fixed_ips = {}
        new_fixed_ips['subnet_id'] = subid
        new_fixed_ips['ip_address'] = new_ip
        ips, prev_ips = self._update_ips_for_port(context, port["network_id"],
                                                  port['id'],
                                                  port["fixed_ips"],
                                                  [new_fixed_ips])

        # Update ips if necessary
        for ip in ips:
            allocated = models_v2.IPAllocation(network_id=port['network_id'],
                                               port_id=port['id'],
                                               ip_address=ip['ip_address'],
                                               subnet_id=ip['subnet_id'])
            context.session.add(allocated)
Esempio n. 11
0
 def _store_ip_allocation(context, ip_address, network_id, subnet_id,
                          port_id):
     LOG.debug(
         "Allocated IP %(ip_address)s "
         "(%(network_id)s/%(subnet_id)s/%(port_id)s)", {
             'ip_address': ip_address,
             'network_id': network_id,
             'subnet_id': subnet_id,
             'port_id': port_id
         })
     allocated = models_v2.IPAllocation(network_id=network_id,
                                        port_id=port_id,
                                        ip_address=ip_address,
                                        subnet_id=subnet_id)
     port_db = context.session.query(models_v2.Port).get(port_id)
     port_db.fixed_ips.append(allocated)
     port_db.fixed_ips.sort(
         key=lambda fip: (fip['ip_address'], fip['subnet_id']))
 def _allocate_ips_for_subnet(self, context, subnet, ipam_subnet):
     """Allocates an IP from the subnet for each port in the same network"""
     network_id = subnet['network_id']
     ports = (context.session.query(
         models_v2.Port).filter(models_v2.Port.network_id == network_id))
     compute_ports = [p for p in ports if self._is_deviceowner_compute(p)]
     ipam_driver = driver.Pool.get_instance(None, context)
     factory = ipam_driver.get_address_request_factory()
     updated_ports = []
     for port in compute_ports:
         count = len(port.get('fixed_ips', []))
         if count >= cfg.CONF.max_vlan_ips_per_port:
             # Automatically add IP addresses for the first X VLAN subnets.
             # If there are more subnets in the system then the user needs
             # to manage the IP address assignments manually.
             continue
         ip = {'subnet_id': subnet['id']}
         ip_request = factory.get_request(context, port, ip)
         ip_address = ipam_subnet.allocate(ip_request)
         allocated = models_v2.IPAllocation(network_id=network_id,
                                            port_id=port['id'],
                                            ip_address=ip_address,
                                            subnet_id=subnet['id'])
         try:
             # Do the insertion of each IP allocation entry within
             # the context of a nested transaction, so that the entry
             # is rolled back independently of other entries whenever
             # the corresponding port has been deleted.
             with context.session.begin_nested():
                 context.session.add(allocated)
             updated_ports.append(port['id'])
         except db_exc.DBReferenceError:
             LOG.debug(
                 "Port %s was deleted while updating it with an "
                 "VLAN subnet address. Ignoring.", port['id'])
             LOG.debug("Reverting IP allocation for %s", ip_address)
             # Do not fail if reverting allocation was unsuccessful
             try:
                 ipam_subnet.deallocate(ip_address)
             except Exception:
                 LOG.debug("Reverting IP allocation failed for %s",
                           ip_address)
     return updated_ports
 def _store_ip_allocation(context, ip_address, network_id, subnet_id,
                          port_id):
     LOG.debug(
         "Allocated IP %(ip_address)s "
         "(%(network_id)s/%(subnet_id)s/%(port_id)s)", {
             'ip_address': ip_address,
             'network_id': network_id,
             'subnet_id': subnet_id,
             'port_id': port_id
         })
     allocated = models_v2.IPAllocation(network_id=network_id,
                                        port_id=port_id,
                                        ip_address=ip_address,
                                        subnet_id=subnet_id)
     context.session.add(allocated)
     # NOTE(kevinbenton): We add this to the session info so the sqlalchemy
     # object isn't immediately garbage collected. Otherwise when the
     # fixed_ips relationship is referenced a new persistent object will be
     # added to the session that will interfere with retry operations.
     # See bug 1556178 for details.
     context.session.info.setdefault('allocated_ips', []).append(allocated)
Esempio n. 14
0
 def add_auto_addrs_on_network_ports(self, context, subnet, ipam_subnet):
     """For an auto-address subnet, add addrs for ports on the net."""
     with context.session.begin(subtransactions=True):
         network_id = subnet['network_id']
         port_qry = context.session.query(models_v2.Port)
         ports = port_qry.filter(
             and_(
                 models_v2.Port.network_id == network_id,
                 ~models_v2.Port.device_owner.in_(
                     constants.ROUTER_INTERFACE_OWNERS_SNAT)))
         updated_ports = []
         for port in ports:
             ip_request = ipam_req.AutomaticAddressRequest(
                 prefix=subnet['cidr'], mac=port['mac_address'])
             ip_address = ipam_subnet.allocate(ip_request)
             allocated = models_v2.IPAllocation(network_id=network_id,
                                                port_id=port['id'],
                                                ip_address=ip_address,
                                                subnet_id=subnet['id'])
             try:
                 # Do the insertion of each IP allocation entry within
                 # the context of a nested transaction, so that the entry
                 # is rolled back independently of other entries whenever
                 # the corresponding port has been deleted.
                 with context.session.begin_nested():
                     context.session.add(allocated)
                 updated_ports.append(port['id'])
             except db_exc.DBReferenceError:
                 LOG.debug(
                     "Port %s was deleted while updating it with an "
                     "IPv6 auto-address. Ignoring.", port['id'])
                 LOG.debug("Reverting IP allocation for %s", ip_address)
                 # Do not fail if reverting allocation was unsuccessful
                 try:
                     ipam_subnet.deallocate(ip_address)
                 except Exception:
                     LOG.debug("Reverting IP allocation failed for %s",
                               ip_address)
         return updated_ports
Esempio n. 15
0
 def setUp(self):
     super(TestL3GwModeMixin, self).setUp()
     plugin = __name__ + '.' + TestDbIntPlugin.__name__
     self.setup_coreplugin(plugin)
     self.target_object = TestDbIntPlugin()
     # Patch the context
     ctx_patcher = mock.patch('neutron.context', autospec=True)
     mock_context = ctx_patcher.start()
     self.addCleanup(db_api.clear_db)
     self.context = mock_context.get_admin_context()
     # This ensure also calls to elevated work in unit tests
     self.context.elevated.return_value = self.context
     self.context.session = db_api.get_session()
     # Create sample data for tests
     self.ext_net_id = _uuid()
     self.int_net_id = _uuid()
     self.int_sub_id = _uuid()
     self.tenant_id = 'the_tenant'
     self.network = models_v2.Network(id=self.ext_net_id,
                                      tenant_id=self.tenant_id,
                                      admin_state_up=True,
                                      status=constants.NET_STATUS_ACTIVE)
     self.net_ext = external_net_db.ExternalNetwork(
         network_id=self.ext_net_id)
     self.context.session.add(self.network)
     # The following is to avoid complains from sqlite on
     # foreign key violations
     self.context.session.flush()
     self.context.session.add(self.net_ext)
     self.router = l3_db.Router(id=_uuid(),
                                name=None,
                                tenant_id=self.tenant_id,
                                admin_state_up=True,
                                status=constants.NET_STATUS_ACTIVE,
                                enable_snat=True,
                                gw_port_id=None)
     self.context.session.add(self.router)
     self.context.session.flush()
     self.router_gw_port = models_v2.Port(
         id=FAKE_GW_PORT_ID,
         tenant_id=self.tenant_id,
         device_id=self.router.id,
         device_owner=l3_db.DEVICE_OWNER_ROUTER_GW,
         admin_state_up=True,
         status=constants.PORT_STATUS_ACTIVE,
         mac_address=FAKE_GW_PORT_MAC,
         network_id=self.ext_net_id)
     self.router.gw_port_id = self.router_gw_port.id
     self.context.session.add(self.router)
     self.context.session.add(self.router_gw_port)
     self.context.session.flush()
     self.fip_ext_port = models_v2.Port(
         id=FAKE_FIP_EXT_PORT_ID,
         tenant_id=self.tenant_id,
         admin_state_up=True,
         device_id=self.router.id,
         device_owner=l3_db.DEVICE_OWNER_FLOATINGIP,
         status=constants.PORT_STATUS_ACTIVE,
         mac_address=FAKE_FIP_EXT_PORT_MAC,
         network_id=self.ext_net_id)
     self.context.session.add(self.fip_ext_port)
     self.context.session.flush()
     self.int_net = models_v2.Network(id=self.int_net_id,
                                      tenant_id=self.tenant_id,
                                      admin_state_up=True,
                                      status=constants.NET_STATUS_ACTIVE)
     self.int_sub = models_v2.Subnet(id=self.int_sub_id,
                                     tenant_id=self.tenant_id,
                                     ip_version=4,
                                     cidr='3.3.3.0/24',
                                     gateway_ip='3.3.3.1',
                                     network_id=self.int_net_id)
     self.router_port = models_v2.Port(
         id=FAKE_ROUTER_PORT_ID,
         tenant_id=self.tenant_id,
         admin_state_up=True,
         device_id=self.router.id,
         device_owner=l3_db.DEVICE_OWNER_ROUTER_INTF,
         status=constants.PORT_STATUS_ACTIVE,
         mac_address=FAKE_ROUTER_PORT_MAC,
         network_id=self.int_net_id)
     self.router_port_ip_info = models_v2.IPAllocation(
         port_id=self.router_port.id,
         network_id=self.int_net.id,
         subnet_id=self.int_sub_id,
         ip_address='3.3.3.1')
     self.context.session.add(self.int_net)
     self.context.session.add(self.int_sub)
     self.context.session.add(self.router_port)
     self.context.session.add(self.router_port_ip_info)
     self.context.session.flush()
     self.fip_int_port = models_v2.Port(id=FAKE_FIP_INT_PORT_ID,
                                        tenant_id=self.tenant_id,
                                        admin_state_up=True,
                                        device_id='something',
                                        device_owner='compute:nova',
                                        status=constants.PORT_STATUS_ACTIVE,
                                        mac_address=FAKE_FIP_INT_PORT_MAC,
                                        network_id=self.int_net_id)
     self.fip_int_ip_info = models_v2.IPAllocation(
         port_id=self.fip_int_port.id,
         network_id=self.int_net.id,
         subnet_id=self.int_sub_id,
         ip_address='3.3.3.3')
     self.fip = l3_db.FloatingIP(id=_uuid(),
                                 floating_ip_address='1.1.1.2',
                                 floating_network_id=self.ext_net_id,
                                 floating_port_id=FAKE_FIP_EXT_PORT_ID,
                                 fixed_port_id=None,
                                 fixed_ip_address=None,
                                 router_id=None)
     self.context.session.add(self.fip_int_port)
     self.context.session.add(self.fip_int_ip_info)
     self.context.session.add(self.fip)
     self.context.session.flush()
     self.fip_request = {
         'port_id': FAKE_FIP_INT_PORT_ID,
         'tenant_id': self.tenant_id
     }
Esempio n. 16
0
 def setUp(self):
     super(TestL3GwModeMixin, self).setUp()
     plugin = __name__ + '.' + TestDbIntPlugin.__name__
     self.setup_coreplugin(plugin)
     self.target_object = TestDbIntPlugin()
     # Patch the context
     ctx_patcher = mock.patch('neutron_lib.context', autospec=True)
     mock_context = ctx_patcher.start()
     self.context = mock_context.get_admin_context()
     # This ensure also calls to elevated work in unit tests
     self.context.elevated.return_value = self.context
     self.context.session = db_api.get_writer_session()
     # Create sample data for tests
     self.ext_net_id = _uuid()
     self.int_net_id = _uuid()
     self.int_sub_id = _uuid()
     self.tenant_id = 'the_tenant'
     self.network = net_obj.Network(self.context,
                                    id=self.ext_net_id,
                                    project_id=self.tenant_id,
                                    admin_state_up=True,
                                    status=constants.NET_STATUS_ACTIVE)
     self.net_ext = net_obj.ExternalNetwork(self.context,
                                            network_id=self.ext_net_id)
     self.network.create()
     self.net_ext.create()
     self.router = l3_models.Router(id=_uuid(),
                                    name=None,
                                    tenant_id=self.tenant_id,
                                    admin_state_up=True,
                                    status=constants.NET_STATUS_ACTIVE,
                                    enable_snat=True,
                                    gw_port_id=None)
     self.context.session.add(self.router)
     self.context.session.flush()
     self.router_gw_port = port_obj.Port(
         self.context,
         id=FAKE_GW_PORT_ID,
         project_id=self.tenant_id,
         device_id=self.router.id,
         device_owner=l3_db.DEVICE_OWNER_ROUTER_GW,
         admin_state_up=True,
         status=constants.PORT_STATUS_ACTIVE,
         mac_address=netaddr.EUI(FAKE_GW_PORT_MAC),
         network_id=self.ext_net_id)
     self.router_gw_port.create()
     self.router.gw_port_id = self.router_gw_port.id
     self.context.session.add(self.router)
     self.context.session.flush()
     self.fip_ext_port = port_obj.Port(
         self.context,
         id=FAKE_FIP_EXT_PORT_ID,
         project_id=self.tenant_id,
         admin_state_up=True,
         device_id=self.router.id,
         device_owner=l3_db.DEVICE_OWNER_FLOATINGIP,
         status=constants.PORT_STATUS_ACTIVE,
         mac_address=netaddr.EUI(FAKE_FIP_EXT_PORT_MAC),
         network_id=self.ext_net_id)
     self.fip_ext_port.create()
     self.context.session.flush()
     self.int_net = net_obj.Network(self.context,
                                    id=self.int_net_id,
                                    project_id=self.tenant_id,
                                    admin_state_up=True,
                                    status=constants.NET_STATUS_ACTIVE)
     self.int_sub = subnet_obj.Subnet(
         self.context,
         id=self.int_sub_id,
         project_id=self.tenant_id,
         ip_version=4,
         cidr=utils.AuthenticIPNetwork('3.3.3.0/24'),
         gateway_ip=netaddr.IPAddress('3.3.3.1'),
         network_id=self.int_net_id)
     self.router_port = port_obj.Port(
         self.context,
         id=FAKE_ROUTER_PORT_ID,
         project_id=self.tenant_id,
         admin_state_up=True,
         device_id=self.router.id,
         device_owner=l3_db.DEVICE_OWNER_ROUTER_INTF,
         status=constants.PORT_STATUS_ACTIVE,
         mac_address=netaddr.EUI(FAKE_ROUTER_PORT_MAC),
         network_id=self.int_net_id)
     self.router_port_ip_info = models_v2.IPAllocation(
         port_id=self.router_port.id,
         network_id=self.int_net.id,
         subnet_id=self.int_sub_id,
         ip_address='3.3.3.1')
     self.int_net.create()
     self.int_sub.create()
     self.router_port.create()
     self.context.session.add(self.router_port_ip_info)
     self.context.session.flush()
     self.fip_int_port = port_obj.Port(
         self.context,
         id=FAKE_FIP_INT_PORT_ID,
         project_id=self.tenant_id,
         admin_state_up=True,
         device_id='something',
         device_owner=constants.DEVICE_OWNER_COMPUTE_PREFIX + 'nova',
         status=constants.PORT_STATUS_ACTIVE,
         mac_address=netaddr.EUI(FAKE_FIP_INT_PORT_MAC),
         network_id=self.int_net_id)
     self.fip_int_ip_info = models_v2.IPAllocation(
         port_id=self.fip_int_port.id,
         network_id=self.int_net.id,
         subnet_id=self.int_sub_id,
         ip_address='3.3.3.3')
     self.fip = l3_models.FloatingIP(id=_uuid(),
                                     floating_ip_address='1.1.1.2',
                                     floating_network_id=self.ext_net_id,
                                     floating_port_id=FAKE_FIP_EXT_PORT_ID,
                                     fixed_port_id=None,
                                     fixed_ip_address=None,
                                     router_id=None)
     self.fip_int_port.create()
     self.context.session.add(self.fip_int_ip_info)
     self.context.session.add(self.fip)
     self.context.session.flush()
     self.fip_request = {
         'port_id': FAKE_FIP_INT_PORT_ID,
         'tenant_id': self.tenant_id
     }
Esempio n. 17
0
    def _populate_neutron_db(self):
        self.plugin.create_network(self.ctx, {"network": {
            "tenant_id": self.tenant_id,
            "id": self.net_id,
            "shared": False,
            "name": "test_net_1",
            "admin_state_up": True,
            "description": ""
        }})
        self.plugin.create_subnetpool(self.ctx, {"subnetpool": {
            "tenant_id": self.tenant_id,
            "id": self.ip_pool_id,
            "name": "default_test_pool",
            "prefixes": ["192.168.0.0", "192.168.1.0", "192.168.2.0"],
            # "min_prefix": 16,
            "min_prefixlen": 16,
            # "max_prefix": "",
            "max_prefixlen": 32,
            # "default_prefix": "",
            "default_prefixlen": 32,
            # "default_quota": "",
            # "address_scope_id": "",
            "is_default": True,
            "shared": True,
            "description": ""
        }})
        self.plugin.create_port(self.ctx, {"port": {
            "tenant_id": self.tenant_id,
            "name": "test_port_1",
            "id": self.port_id_1,
            "network_id": self.net_id,
            "fixed_ips": constants.ATTR_NOT_SPECIFIED,
            "admin_state_up": True,
            "device_id": "123",
            "device_owner": "admin",
            "description": ""
        }})
        self.plugin.create_port(self.ctx, {"port": {
            "tenant_id": self.tenant_id,
            "name": "test_port_2",
            "id": self.port_id_2,
            "network_id": self.net_id,
            "fixed_ips": constants.ATTR_NOT_SPECIFIED,
            "admin_state_up": True,
            "device_id": "1234",
            "device_owner": "admin",
            "description": ""
        }})

        subnet = self.plugin.create_subnet(self.ctx, {"subnet": {
            "tenant_id": self.tenant_id,
            "name": "subnet_192_168",
            "cidr": "192.168.0.0/32",
            "ip_version": 4,
            "network_id": self.net_id,
            "subnetpool_id": self.ip_pool_id,
            "allocation_pools": [],
            "enable_dhcp": True,
            "dns_nameservers": [],
            "host_routes": []
        }})

        neutron_db = [
            ml2_models.PortBinding(
                port_id=self.port_id_1,
                host=self.host,
                vif_type="ovs"
            ),
            ml2_models.PortBindingLevel(
                port_id=self.port_id_1,
                host=self.host,
                driver=nsxv3_constants.NSXV3,
                level=1
            ),
            models_v2.IPAllocation(
                port_id=self.port_id_1,
                ip_address="192.168.0.100",
                subnet_id=subnet.get("id"),
                network_id=self.net_id
            ),
            QosPolicy(
                id=self.qos_id_1,
                project_id=self.tenant_id,
                name="Test_QOS_1"
            ),
            trunk_model.Trunk(
                id=self.trunk_id_1,
                project_id=self.tenant_id,
                name="test_trunk_1",
                port_id=self.port_id_1
            ),
            sg_model.SecurityGroup(
                id=self.sg_id_1,
                project_id=self.tenant_id,
                name="test_sg_1",
            )
        ]

        with self.session.begin(subtransactions=True):
            for entry in neutron_db:
                self.session.add(entry)