def _add_csnat_router_interface_port( self, context, router, network_id, subnet_id, do_pop=True): """Add SNAT interface to the specified router and subnet.""" port_data = {'tenant_id': '', 'network_id': network_id, 'fixed_ips': [{'subnet_id': subnet_id}], 'device_id': router.id, 'device_owner': const.DEVICE_OWNER_ROUTER_SNAT, 'admin_state_up': True, 'name': ''} snat_port = p_utils.create_port(self._core_plugin, context, {'port': port_data}) if not snat_port: msg = _("Unable to create the SNAT Interface Port") raise n_exc.BadRequest(resource='router', msg=msg) with context.session.begin(subtransactions=True): router_port = l3_models.RouterPort( port_id=snat_port['id'], router_id=router.id, port_type=const.DEVICE_OWNER_ROUTER_SNAT ) context.session.add(router_port) if do_pop: return self.l3plugin._populate_mtu_and_subnets_for_ports( context, [snat_port]) return snat_port
def _setup_delete_current_gw_port_deletes_dvr_internal_ports( self, port=None, gw_port=True, new_network_id='ext_net_id_2'): router_db = { 'name': 'foo_router', 'admin_state_up': True, 'distributed': True } router = self._create_router(router_db) if gw_port: with self.subnet(cidr='10.10.10.0/24') as subnet: port_dict = { 'device_id': router.id, 'device_owner': const.DEVICE_OWNER_ROUTER_GW, 'admin_state_up': True, 'fixed_ips': [{'subnet_id': subnet['subnet']['id'], 'ip_address': '10.10.10.100'}] } net_id = subnet['subnet']['network_id'] port_res = self.create_port(net_id, port_dict) port_res_dict = self.deserialize(self.fmt, port_res) with self.ctx.session.begin(subtransactions=True): port_db = self.ctx.session.query(models_v2.Port).filter_by( id=port_res_dict['port']['id']).one() router.gw_port = port_db router_port = l3_models.RouterPort( router_id=router.id, port_id=port_db.id, port_type=const.DEVICE_OWNER_ROUTER_GW ) self.ctx.session.add(router) self.ctx.session.add(router_port) else: net_id = None plugin = mock.Mock() directory.add_plugin(plugin_constants.CORE, plugin) with mock.patch.object(l3_dvr_db.l3_db.L3_NAT_db_mixin, 'router_gw_port_has_floating_ips', return_value=False),\ mock.patch.object( self.mixin, '_get_router') as grtr,\ mock.patch.object( self.mixin, 'delete_csnat_router_interface_ports') as del_csnat_port,\ mock.patch.object( self.mixin, 'delete_floatingip_agent_gateway_port') as del_agent_gw_port,\ mock.patch.object( self.mixin.l3_rpc_notifier, 'delete_fipnamespace_for_ext_net') as del_fip: plugin.get_ports.return_value = port grtr.return_value = router self.mixin._delete_current_gw_port( self.ctx, router['id'], router, new_network_id) return router, plugin, net_id, del_csnat_port,\ del_agent_gw_port, del_fip
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)
def _create_ha_port_binding(self, context, router_id, port_id): try: with context.session.begin(): routerportbinding = l3_models.RouterPort( port_id=port_id, router_id=router_id, port_type=constants.DEVICE_OWNER_ROUTER_HA_INTF) context.session.add(routerportbinding) portbinding = l3ha_model.L3HARouterAgentPortBinding( port_id=port_id, router_id=router_id) context.session.add(portbinding) return portbinding except db_exc.DBReferenceError as e: with excutils.save_and_reraise_exception() as ctxt: if isinstance(e.inner_exception, sql_exc.IntegrityError): ctxt.reraise = False LOG.debug( 'Failed to create HA router agent PortBinding, ' 'Router %s has already been removed ' 'by concurrent operation', router_id) raise l3.RouterNotFound(router_id=router_id)