From 1483934c9b30f66e73a8901441fee8cd42d75098 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Fri, 19 Feb 2021 13:52:22 +0000 Subject: [PATCH] Implement "IPAllocation" router ports allocated retrieval Implemented a method, in "IPAllocation" OVO class, to retrieve the allocated router ports. The new query used dispenses with one of the inner join queries required in the former query, making it faster. This method allows to filter by gateway IP address and can return the whole query list or just the first register found. Trivial-Fix Conflicts: neutron/objects/ports.py Change-Id: I1a5205303ebede533ffa6b3b85d4ec2f4b112426 (cherry picked from commit b7956d80ca481864502ba16a7c3a5f47891e11c4) (cherry picked from commit b84630ad0f4397196cf763df9f7c0e568297ed4c) --- neutron/db/db_base_plugin_v2.py | 13 +++++-------- neutron/objects/ports.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/neutron/db/db_base_plugin_v2.py b/neutron/db/db_base_plugin_v2.py index 185942de03e..c07df035254 100644 --- a/neutron/db/db_base_plugin_v2.py +++ b/neutron/db/db_base_plugin_v2.py @@ -622,15 +622,12 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon, # a subnet-update and a router-interface-add operation are # executed concurrently if cur_subnet and not ipv6_utils.is_ipv6_pd_enabled(s): + gateway_ip = str(cur_subnet['gateway_ip']) with db_api.CONTEXT_READER.using(context): - # TODO(electrocucaracha): Look a solution for Join in OVO - ipal = models_v2.IPAllocation - alloc_qry = context.session.query(ipal.port_id) - alloc_qry = alloc_qry.join("port", "routerport") - gateway_ip = str(cur_subnet['gateway_ip']) - allocated = alloc_qry.filter( - ipal.ip_address == gateway_ip, - ipal.subnet_id == cur_subnet['id']).first() + allocated = port_obj.IPAllocation.get_alloc_routerports( + context, cur_subnet['id'], gateway_ip=gateway_ip, + first=True) + if allocated and allocated.port_id: raise exc.GatewayIpInUse( ip_address=gateway_ip, diff --git a/neutron/objects/ports.py b/neutron/objects/ports.py index 33ed3627388..02fd646b467 100644 --- a/neutron/objects/ports.py +++ b/neutron/objects/ports.py @@ -215,6 +215,21 @@ class IPAllocation(base.NeutronDbObject): if alloc_db: return True + @classmethod + def get_alloc_routerports(cls, context, subnet_id, gateway_ip=None, + first=False): + alloc_qry = context.session.query(cls.db_model.port_id) + alloc_qry = alloc_qry.join( + l3.RouterPort, l3.RouterPort.port_id == cls.db_model.port_id) + alloc_qry = alloc_qry.filter(cls.db_model.subnet_id == subnet_id) + if gateway_ip: + alloc_qry = alloc_qry.filter(cls.db_model.ip_address == gateway_ip) + + if first: + return alloc_qry.first() + else: + return alloc_qry.all() + @base.NeutronObjectRegistry.register class PortDNS(base.NeutronDbObject):