From 6fa6e4135cf2caa9523b2aed01b10fd82856d5b9 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Thu, 2 Jul 2020 17:18:28 +0000 Subject: [PATCH] Relocate _delete_expired_default_network_segment_ranges Relocate "_delete_expired_default_network_segment_ranges" to avoid code redundancy. Now instead of searching all objects and deleting one by one, "delete_objects" is used. Trivial-Fix Change-Id: I1753263cb15ce2988ac4ccae03b7395069f2c4e9 --- neutron/plugins/ml2/drivers/helpers.py | 8 ++++++++ neutron/plugins/ml2/drivers/type_tunnel.py | 13 ------------- neutron/plugins/ml2/drivers/type_vlan.py | 13 ------------- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/neutron/plugins/ml2/drivers/helpers.py b/neutron/plugins/ml2/drivers/helpers.py index 9e3d97c861a..f99d32d1961 100644 --- a/neutron/plugins/ml2/drivers/helpers.py +++ b/neutron/plugins/ml2/drivers/helpers.py @@ -15,6 +15,7 @@ import functools +from neutron_lib import context from neutron_lib.db import api as db_api from neutron_lib import exceptions from neutron_lib.plugins import constants as plugin_constants @@ -151,3 +152,10 @@ class SegmentTypeDriver(BaseTypeDriver): return alloc raise db_exc.RetryRequest( exceptions.NoNetworkFoundInMaximumAllowedAttempts()) + + @db_api.retry_db_errors + def _delete_expired_default_network_segment_ranges(self): + ctx = context.get_admin_context() + with db_api.CONTEXT_WRITER.using(ctx): + filters = {'default': True, 'network_type': self.get_type()} + ns_range.NetworkSegmentRange.delete_objects(ctx, **filters) diff --git a/neutron/plugins/ml2/drivers/type_tunnel.py b/neutron/plugins/ml2/drivers/type_tunnel.py index fece0fcee7c..9b9e32c6225 100644 --- a/neutron/plugins/ml2/drivers/type_tunnel.py +++ b/neutron/plugins/ml2/drivers/type_tunnel.py @@ -163,19 +163,6 @@ class _TunnelTypeDriverBase(helpers.SegmentTypeDriver, metaclass=abc.ABCMeta): range_obj.NetworkSegmentRange(ctx, **res)) new_default_range_obj.create() - @db_api.retry_db_errors - def _delete_expired_default_network_segment_ranges(self): - ctx = context.get_admin_context() - with db_api.CONTEXT_WRITER.using(ctx): - filters = { - 'default': True, - 'network_type': self.get_type(), - } - old_default_range_objs = range_obj.NetworkSegmentRange.get_objects( - ctx, **filters) - for obj in old_default_range_objs: - obj.delete() - @db_api.retry_db_errors def _get_network_segment_ranges_from_db(self): ranges = [] diff --git a/neutron/plugins/ml2/drivers/type_vlan.py b/neutron/plugins/ml2/drivers/type_vlan.py index 6d16b05ad45..af49d019d49 100644 --- a/neutron/plugins/ml2/drivers/type_vlan.py +++ b/neutron/plugins/ml2/drivers/type_vlan.py @@ -77,19 +77,6 @@ class VlanTypeDriver(helpers.SegmentTypeDriver): range_obj.NetworkSegmentRange(ctx, **res)) new_default_range_obj.create() - @db_api.retry_db_errors - def _delete_expired_default_network_segment_ranges(self): - ctx = context.get_admin_context() - with db_api.CONTEXT_WRITER.using(ctx): - filters = { - 'default': True, - 'network_type': p_const.TYPE_VLAN, - } - old_default_range_objs = range_obj.NetworkSegmentRange.get_objects( - ctx, **filters) - for obj in old_default_range_objs: - obj.delete() - def _parse_network_vlan_ranges(self): try: self.network_vlan_ranges = plugin_utils.parse_network_vlan_ranges(