From 3a5c2abc9b4bd54eca94eb33b91bf713870d6081 Mon Sep 17 00:00:00 2001 From: asarfaty Date: Wed, 25 Dec 2019 11:38:40 +0200 Subject: [PATCH] Add common code for network interfaces queries Change-Id: Id5205e31b550078db2ae34ffe3dde94d08e9f26f --- vmware_nsx/plugins/common/plugin.py | 9 +++++++++ vmware_nsx/plugins/common_v3/plugin.py | 9 ++++----- .../plugins/nsx_v/drivers/distributed_router_driver.py | 10 ++-------- vmware_nsx/plugins/nsx_v3/plugin.py | 5 ++--- .../services/lbaas/nsx_v3/implementation/lb_utils.py | 6 ++---- .../shell/admin/plugins/nsxv/resources/migration.py | 6 ++---- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/vmware_nsx/plugins/common/plugin.py b/vmware_nsx/plugins/common/plugin.py index 5727ba3993..0fd9f8dd96 100644 --- a/vmware_nsx/plugins/common/plugin.py +++ b/vmware_nsx/plugins/common/plugin.py @@ -101,6 +101,15 @@ class NsxPluginBase(db_base_plugin_v2.NeutronDbPluginV2, 'network_id': [network_id]} 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, internal_subnet, external_network_id): router_id = super(NsxPluginBase, self).get_router_for_floatingip( diff --git a/vmware_nsx/plugins/common_v3/plugin.py b/vmware_nsx/plugins/common_v3/plugin.py index 8a16f8ed6a..e54de44cac 100644 --- a/vmware_nsx/plugins/common_v3/plugin.py +++ b/vmware_nsx/plugins/common_v3/plugin.py @@ -2793,11 +2793,10 @@ class NsxPluginV3Base(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, "a logical router.")) LOG.error(err_msg) raise n_exc.InvalidInput(error_message=err_msg) - port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], - 'network_id': [net_id]} - intf_ports = self.get_ports(context.elevated(), filters=port_filters) - router_ids = [port['device_id'] - for port in intf_ports if port['device_id']] + intf_ports = self._get_network_interface_ports( + context.elevated(), net_id) + router_ids = [port['device_id'] for port in intf_ports + if port['device_id']] if len(router_ids) > 0: err_msg = _("Only one subnet of each IP version in a network " "%(net_id)s can be attached to router, one subnet " diff --git a/vmware_nsx/plugins/nsx_v/drivers/distributed_router_driver.py b/vmware_nsx/plugins/nsx_v/drivers/distributed_router_driver.py index c8b4ca58ab..e2cc5e2e99 100644 --- a/vmware_nsx/plugins/nsx_v/drivers/distributed_router_driver.py +++ b/vmware_nsx/plugins/nsx_v/drivers/distributed_router_driver.py @@ -15,8 +15,6 @@ import netaddr from oslo_log import log as logging -from neutron.db import l3_db - from neutron_lib.api import validators from neutron_lib import constants from neutron_lib.db import api as db_api @@ -225,12 +223,8 @@ class RouterDistributedDriver(router_driver.RouterBaseDriver): _nsxv_plugin = self.plugin net_id, subnet_id = _nsxv_plugin._get_interface_info(context, interface_info) - - port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], - '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] + router_ids = _nsxv_plugin._get_network_router_ids( + context.elevated(), net_id) all_routers = _nsxv_plugin.get_routers(context, filters={'id': router_ids}) dist_routers = [router['id'] for router in all_routers diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index 5ea33a999d..2286f4e878 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -1491,9 +1491,8 @@ class NsxV3Plugin(nsx_plugin_common.NsxPluginV3Base, subnet_ids = (subnet['id'] for subnet in subnets) # check if the subnet is attached to a router - port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], - 'network_id': [original_port['network_id']]} - interfaces = self.get_ports(context.elevated(), filters=port_filters) + interfaces = self._get_network_interface_ports( + context.elevated(), original_port['network_id']) for interface in interfaces: for fixed_ip in interface['fixed_ips']: if fixed_ip['subnet_id'] in subnet_ids: diff --git a/vmware_nsx/services/lbaas/nsx_v3/implementation/lb_utils.py b/vmware_nsx/services/lbaas/nsx_v3/implementation/lb_utils.py index b887831a86..b4fbcacfc3 100644 --- a/vmware_nsx/services/lbaas/nsx_v3/implementation/lb_utils.py +++ b/vmware_nsx/services/lbaas/nsx_v3/implementation/lb_utils.py @@ -15,7 +15,6 @@ import functools -from neutron.db import l3_db from neutron.services.flavors import flavors_plugin from neutron_lib import exceptions as n_exc 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): subnet = plugin.get_subnet(context, subnet_id) network_id = subnet['network_id'] - port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], - 'network_id': [network_id]} - ports = plugin.get_ports(context.elevated(), filters=port_filters) + ports = plugin._get_router_interface_ports_by_network( + context.elevated(), network_id) if ports: router = plugin.get_router(context.elevated(), ports[0]['device_id']) if router.get('external_gateway_info'): diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/migration.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/migration.py index 13c5f3f6b4..ac48f683ff 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/migration.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/migration.py @@ -15,7 +15,6 @@ import netaddr 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 provider_net as pnet from neutron_lib.api import validators @@ -127,9 +126,8 @@ def validate_config_for_migration(resource, event, trigger, **kwargs): subnet['id'], transit_networks) # Network attached to multiple routers - port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], - 'network_id': [net['id']]} - intf_ports = plugin.get_ports(admin_context, filters=port_filters) + intf_ports = plugin._get_network_interface_ports( + admin_context, net['id']) if len(intf_ports) > 1: n_errors = n_errors + 1 LOG.error("Network %s has interfaces on multiple routers. "