From 9213a5a9f89f9b0833e9d0dc088aaf3ce99a4d8f Mon Sep 17 00:00:00 2001 From: Manish Godara Date: Wed, 21 Jan 2015 15:49:43 -0800 Subject: [PATCH] Refactor ml2 manager Refactor code to facilitate other changes. Keeping the refactored code in different patch for ease of review. This patch mainly consolidates the keys that are referenced together most of the time in reference to providernet extension. By doing this, the subsequent changes are less repetitive and makes the code little cleaner as well. Change-Id: Idc0648d5c4688c8f797cc5427b71c2a3919ce722 Related-Bug: #1333475 --- neutron/extensions/providernet.py | 5 ++--- neutron/plugins/ml2/managers.py | 31 ++++++++++++------------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/neutron/extensions/providernet.py b/neutron/extensions/providernet.py index 2dc966ae7..4bb19fac7 100644 --- a/neutron/extensions/providernet.py +++ b/neutron/extensions/providernet.py @@ -21,7 +21,7 @@ from neutron.common import exceptions as n_exc NETWORK_TYPE = 'provider:network_type' PHYSICAL_NETWORK = 'provider:physical_network' SEGMENTATION_ID = 'provider:segmentation_id' - +ATTRIBUTES = (NETWORK_TYPE, PHYSICAL_NETWORK, SEGMENTATION_ID) EXTENDED_ATTRIBUTES_2_0 = { 'networks': { NETWORK_TYPE: {'allow_post': True, 'allow_put': True, @@ -49,8 +49,7 @@ def _raise_if_updates_provider_attributes(attrs): This method is used for plugins that do not support updating provider networks. """ - immutable = (NETWORK_TYPE, PHYSICAL_NETWORK, SEGMENTATION_ID) - if any(attributes.is_attr_set(attrs.get(a)) for a in immutable): + if any(attributes.is_attr_set(attrs.get(a)) for a in ATTRIBUTES): msg = _("Plugin does not support updating provider attributes") raise n_exc.InvalidInput(error_message=msg) diff --git a/neutron/plugins/ml2/managers.py b/neutron/plugins/ml2/managers.py index 0f6f6e850..ae962ba3c 100644 --- a/neutron/plugins/ml2/managers.py +++ b/neutron/plugins/ml2/managers.py @@ -72,11 +72,9 @@ class TypeManager(stevedore.named.NamedExtensionManager): LOG.info(_LI("Tenant network_types: %s"), self.tenant_network_types) def _process_provider_segment(self, segment): - network_type = self._get_attribute(segment, provider.NETWORK_TYPE) - physical_network = self._get_attribute(segment, - provider.PHYSICAL_NETWORK) - segmentation_id = self._get_attribute(segment, - provider.SEGMENTATION_ID) + (network_type, physical_network, + segmentation_id) = (self._get_attribute(segment, attr) + for attr in provider.ATTRIBUTES) if attributes.is_attr_set(network_type): segment = {api.NETWORK_TYPE: network_type, @@ -88,23 +86,19 @@ class TypeManager(stevedore.named.NamedExtensionManager): msg = _("network_type required") raise exc.InvalidInput(error_message=msg) + def _get_segment_attributes(self, network): + return {attr: self._get_attribute(network, attr) + for attr in provider.ATTRIBUTES} + def _process_provider_create(self, network): - if any(attributes.is_attr_set(network.get(f)) - for f in (provider.NETWORK_TYPE, provider.PHYSICAL_NETWORK, - provider.SEGMENTATION_ID)): + if any(attributes.is_attr_set(network.get(attr)) + for attr in provider.ATTRIBUTES): # Verify that multiprovider and provider attributes are not set # at the same time. if attributes.is_attr_set(network.get(mpnet.SEGMENTS)): raise mpnet.SegmentsSetInConjunctionWithProviders() - network_type = self._get_attribute(network, provider.NETWORK_TYPE) - physical_network = self._get_attribute(network, - provider.PHYSICAL_NETWORK) - segmentation_id = self._get_attribute(network, - provider.SEGMENTATION_ID) - segments = [{provider.NETWORK_TYPE: network_type, - provider.PHYSICAL_NETWORK: physical_network, - provider.SEGMENTATION_ID: segmentation_id}] + segments = [self._get_segment_attributes(network)] return [self._process_provider_segment(s) for s in segments] elif attributes.is_attr_set(network.get(mpnet.SEGMENTS)): segments = [self._process_provider_segment(s) @@ -125,9 +119,8 @@ class TypeManager(stevedore.named.NamedExtensionManager): segments = db.get_network_segments(context.session, id) if not segments: LOG.error(_LE("Network %s has no segments"), id) - network[provider.NETWORK_TYPE] = None - network[provider.PHYSICAL_NETWORK] = None - network[provider.SEGMENTATION_ID] = None + for attr in provider.ATTRIBUTES: + network[attr] = None elif len(segments) > 1: network[mpnet.SEGMENTS] = [ {provider.NETWORK_TYPE: segment[api.NETWORK_TYPE], -- 2.45.2