Add common code for network interfaces queries

Change-Id: Id5205e31b550078db2ae34ffe3dde94d08e9f26f
This commit is contained in:
asarfaty 2019-12-25 11:38:40 +02:00
parent df2dc98c8f
commit 3a5c2abc9b
6 changed files with 21 additions and 24 deletions

View File

@ -101,6 +101,15 @@ class NsxPluginBase(db_base_plugin_v2.NeutronDbPluginV2,
'network_id': [network_id]} 'network_id': [network_id]}
return self.get_ports(context, filters=port_filters) return self.get_ports(context, filters=port_filters)
def _get_network_interface_ports(self, context, net_id):
port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF],
'network_id': [net_id]}
return self.get_ports(context, filters=port_filters)
def _get_network_router_ids(self, context, net_id):
intf_ports = self._get_network_interface_ports(context, net_id)
return [port['device_id'] for port in intf_ports if port['device_id']]
def get_router_for_floatingip(self, context, internal_port, def get_router_for_floatingip(self, context, internal_port,
internal_subnet, external_network_id): internal_subnet, external_network_id):
router_id = super(NsxPluginBase, self).get_router_for_floatingip( router_id = super(NsxPluginBase, self).get_router_for_floatingip(

View File

@ -2793,11 +2793,10 @@ class NsxPluginV3Base(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
"a logical router.")) "a logical router."))
LOG.error(err_msg) LOG.error(err_msg)
raise n_exc.InvalidInput(error_message=err_msg) raise n_exc.InvalidInput(error_message=err_msg)
port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], intf_ports = self._get_network_interface_ports(
'network_id': [net_id]} context.elevated(), net_id)
intf_ports = self.get_ports(context.elevated(), filters=port_filters) router_ids = [port['device_id'] for port in intf_ports
router_ids = [port['device_id'] if port['device_id']]
for port in intf_ports if port['device_id']]
if len(router_ids) > 0: if len(router_ids) > 0:
err_msg = _("Only one subnet of each IP version in a network " err_msg = _("Only one subnet of each IP version in a network "
"%(net_id)s can be attached to router, one subnet " "%(net_id)s can be attached to router, one subnet "

View File

@ -15,8 +15,6 @@
import netaddr import netaddr
from oslo_log import log as logging from oslo_log import log as logging
from neutron.db import l3_db
from neutron_lib.api import validators from neutron_lib.api import validators
from neutron_lib import constants from neutron_lib import constants
from neutron_lib.db import api as db_api from neutron_lib.db import api as db_api
@ -225,12 +223,8 @@ class RouterDistributedDriver(router_driver.RouterBaseDriver):
_nsxv_plugin = self.plugin _nsxv_plugin = self.plugin
net_id, subnet_id = _nsxv_plugin._get_interface_info(context, net_id, subnet_id = _nsxv_plugin._get_interface_info(context,
interface_info) interface_info)
router_ids = _nsxv_plugin._get_network_router_ids(
port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], context.elevated(), net_id)
'network_id': [net_id]}
intf_ports = _nsxv_plugin.get_ports(context.elevated(),
filters=port_filters)
router_ids = [port['device_id'] for port in intf_ports]
all_routers = _nsxv_plugin.get_routers(context, all_routers = _nsxv_plugin.get_routers(context,
filters={'id': router_ids}) filters={'id': router_ids})
dist_routers = [router['id'] for router in all_routers dist_routers = [router['id'] for router in all_routers

View File

@ -1491,9 +1491,8 @@ class NsxV3Plugin(nsx_plugin_common.NsxPluginV3Base,
subnet_ids = (subnet['id'] for subnet in subnets) subnet_ids = (subnet['id'] for subnet in subnets)
# check if the subnet is attached to a router # check if the subnet is attached to a router
port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], interfaces = self._get_network_interface_ports(
'network_id': [original_port['network_id']]} context.elevated(), original_port['network_id'])
interfaces = self.get_ports(context.elevated(), filters=port_filters)
for interface in interfaces: for interface in interfaces:
for fixed_ip in interface['fixed_ips']: for fixed_ip in interface['fixed_ips']:
if fixed_ip['subnet_id'] in subnet_ids: if fixed_ip['subnet_id'] in subnet_ids:

View File

@ -15,7 +15,6 @@
import functools import functools
from neutron.db import l3_db
from neutron.services.flavors import flavors_plugin from neutron.services.flavors import flavors_plugin
from neutron_lib import exceptions as n_exc from neutron_lib import exceptions as n_exc
from oslo_log import helpers as log_helpers from oslo_log import helpers as log_helpers
@ -54,9 +53,8 @@ def get_network_from_subnet(context, plugin, subnet_id):
def get_router_from_network(context, plugin, subnet_id): def get_router_from_network(context, plugin, subnet_id):
subnet = plugin.get_subnet(context, subnet_id) subnet = plugin.get_subnet(context, subnet_id)
network_id = subnet['network_id'] network_id = subnet['network_id']
port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], ports = plugin._get_router_interface_ports_by_network(
'network_id': [network_id]} context.elevated(), network_id)
ports = plugin.get_ports(context.elevated(), filters=port_filters)
if ports: if ports:
router = plugin.get_router(context.elevated(), ports[0]['device_id']) router = plugin.get_router(context.elevated(), ports[0]['device_id'])
if router.get('external_gateway_info'): if router.get('external_gateway_info'):

View File

@ -15,7 +15,6 @@
import netaddr import netaddr
from oslo_log import log as logging from oslo_log import log as logging
from neutron.db import l3_db
from neutron_lib.api.definitions import allowedaddresspairs as addr_apidef from neutron_lib.api.definitions import allowedaddresspairs as addr_apidef
from neutron_lib.api.definitions import provider_net as pnet from neutron_lib.api.definitions import provider_net as pnet
from neutron_lib.api import validators from neutron_lib.api import validators
@ -127,9 +126,8 @@ def validate_config_for_migration(resource, event, trigger, **kwargs):
subnet['id'], transit_networks) subnet['id'], transit_networks)
# Network attached to multiple routers # Network attached to multiple routers
port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], intf_ports = plugin._get_network_interface_ports(
'network_id': [net['id']]} admin_context, net['id'])
intf_ports = plugin.get_ports(admin_context, filters=port_filters)
if len(intf_ports) > 1: if len(intf_ports) > 1:
n_errors = n_errors + 1 n_errors = n_errors + 1
LOG.error("Network %s has interfaces on multiple routers. " LOG.error("Network %s has interfaces on multiple routers. "