From b1cc242faddd4c378dfee7b65ed72b4b190b2467 Mon Sep 17 00:00:00 2001 From: Dmitrii Shcherbakov Date: Tue, 18 Apr 2023 20:04:06 +0300 Subject: Add a method to retrieve router gateway ports A method is added as opposed to having a synthetic field on a router for performance reasons: gateways will only be queried when needed to use the external gateways feature API calls. Partial-Bug: #2002687 Change-Id: Iddde9d986b024109bdb7c2aa777a1b017b6a35ab --- neutron/objects/router.py | 9 +++++++++ neutron/tests/unit/objects/test_router.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/neutron/objects/router.py b/neutron/objects/router.py index 2f1e314d93..f462a1b57c 100644 --- a/neutron/objects/router.py +++ b/neutron/objects/router.py @@ -163,6 +163,15 @@ class RouterPort(base.NeutronDbObject): query = query.distinct() return [r[0] for r in query] + @classmethod + @db_api.CONTEXT_READER + def get_gw_port_ids_by_router_id(cls, context, router_id): + query = context.session.query(l3.RouterPort) + query = query.filter( + l3.RouterPort.router_id == router_id, + l3.RouterPort.port_type == n_const.DEVICE_OWNER_ROUTER_GW) + return [rp.port_id for rp in query] + @base.NeutronObjectRegistry.register class DVRMacAddress(base.NeutronDbObject): diff --git a/neutron/tests/unit/objects/test_router.py b/neutron/tests/unit/objects/test_router.py index 5c86aec2fc..f1886db0c8 100644 --- a/neutron/tests/unit/objects/test_router.py +++ b/neutron/tests/unit/objects/test_router.py @@ -17,6 +17,7 @@ from unittest import mock import netaddr +from neutron_lib import constants from neutron_lib.db import api as db_api from oslo_utils import uuidutils @@ -206,6 +207,36 @@ class RouterPortDbObjectTestCase(obj_test_base.BaseDbObjectTestCase, {'router_id': lambda: self._create_test_router_id(), 'port_id': lambda: self._create_test_port_id()}) + def test_get_gw_port_ids_by_router_id(self): + router_id = self._create_test_router_id() + router_gws = [ + self._make_object({ + 'router_id': router_id, + 'port_id': self._create_test_port_id( + device_owner=constants.DEVICE_OWNER_ROUTER_GW), + 'port_type': constants.DEVICE_OWNER_ROUTER_GW}), + self._make_object({ + 'router_id': router_id, + 'port_id': self._create_test_port_id( + device_owner=constants.DEVICE_OWNER_ROUTER_GW), + 'port_type': constants.DEVICE_OWNER_ROUTER_GW, + }) + ] + for gw in router_gws: + gw.create() + + other = self._make_object({ + 'router_id': router_id, + 'port_id': self._create_test_port_id( + device_owner=constants.DEVICE_OWNER_ROUTER_INTF), + 'port_type': constants.DEVICE_OWNER_ROUTER_INTF, + }) + other.create() + + res_gws = self._test_class.get_gw_port_ids_by_router_id(self.context, + router_id) + self.assertCountEqual(res_gws, [rp.port_id for rp in router_gws]) + class DVRMacAddressIfaceObjectTestCase(obj_test_base.BaseObjectIfaceTestCase): -- cgit v1.2.1