From 7a427bcd2aa3dc444c8362429405786dde21d2f0 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Fri, 3 Jul 2020 10:47:52 +0000 Subject: [PATCH] Remove _router_exists method Instead of retrieving the full DB object, "objects_exist" OVO method will be faster. Trivial-Fix Change-Id: Iaf08c75d6a3721c05db9e3858c0136847b0102a6 --- neutron/db/l3_db.py | 10 ++-------- neutron/tests/unit/db/test_l3_db.py | 7 +++---- neutron/tests/unit/db/test_l3_dvr_db.py | 10 +++++----- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/neutron/db/l3_db.py b/neutron/db/l3_db.py index adca7d700ae..08a5528f14e 100644 --- a/neutron/db/l3_db.py +++ b/neutron/db/l3_db.py @@ -1517,13 +1517,6 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase, filters = filters or {} return l3_obj.FloatingIP.count(context, **filters) - def _router_exists(self, context, router_id): - try: - self.get_router(context.elevated(), router_id) - return True - except l3_exc.RouterNotFound: - return False - def prevent_l3_port_deletion(self, context, port_id): """Checks to make sure a port is allowed to be deleted. @@ -1561,7 +1554,8 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase, "%(port_id)s no longer exists, allowing deletion.", {'f_id': port['device_id'], 'port_id': port['id']}) return - elif not self._router_exists(context, port['device_id']): + elif not l3_obj.Router.objects_exist(context.elevated(), + id=port['device_id']): LOG.debug("Router %(router_id)s corresponding to port " "%(port_id)s no longer exists, allowing deletion.", {'router_id': port['device_id'], diff --git a/neutron/tests/unit/db/test_l3_db.py b/neutron/tests/unit/db/test_l3_db.py index 5111621fa0f..eeff600df4c 100644 --- a/neutron/tests/unit/db/test_l3_db.py +++ b/neutron/tests/unit/db/test_l3_db.py @@ -22,7 +22,6 @@ from neutron_lib.callbacks import resources from neutron_lib import constants as n_const from neutron_lib import context from neutron_lib import exceptions as n_exc -from neutron_lib.exceptions import l3 as l3_exc from neutron_lib.plugins import constants as plugin_constants from neutron_lib.plugins import directory from neutron_lib.plugins import utils as plugin_utils @@ -217,9 +216,9 @@ class TestL3_NAT_dbonly_mixin(base.BaseTestCase): 'device_owner': n_const.DEVICE_OWNER_ROUTER_INTF, 'device_id': '44', 'id': 'f', 'fixed_ips': [{'ip_address': '1.1.1.1', 'subnet_id': '4'}]} - self.db.get_router = mock.Mock() - self.db.get_router.side_effect = l3_exc.RouterNotFound(router_id='44') - self.db.prevent_l3_port_deletion(mock.Mock(), None) + with mock.patch.object(l3_obj.Router, 'objects_exist', + return_value=False): + self.db.prevent_l3_port_deletion(mock.Mock(), None) @mock.patch.object(directory, 'get_plugin') def test_prevent_l3_port_existing_router(self, gp): diff --git a/neutron/tests/unit/db/test_l3_dvr_db.py b/neutron/tests/unit/db/test_l3_dvr_db.py index 76c95ca48ca..9f90810b114 100644 --- a/neutron/tests/unit/db/test_l3_dvr_db.py +++ b/neutron/tests/unit/db/test_l3_dvr_db.py @@ -347,11 +347,11 @@ class L3DvrTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase): plugin = mock.Mock() directory.add_plugin(plugin_constants.CORE, plugin) plugin.get_port.return_value = port - self.mixin._router_exists = mock.Mock(return_value=True) - self.assertRaises(exceptions.ServicePortInUse, - self.mixin.prevent_l3_port_deletion, - self.ctx, - port['id']) + with mock.patch.object(router_obj.Router, 'objects_exist', + return_value=True): + self.assertRaises(exceptions.ServicePortInUse, + self.mixin.prevent_l3_port_deletion, + self.ctx, port['id']) def test_prevent_delete_floatingip_agent_gateway_port(self): port = {