]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Refactor ml2 manager
authorManish Godara <manishg@yahoo-inc.com>
Wed, 21 Jan 2015 23:49:43 +0000 (15:49 -0800)
committerManish Godara <manishg@yahoo-inc.com>
Mon, 2 Feb 2015 16:53:32 +0000 (08:53 -0800)
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
neutron/plugins/ml2/managers.py

index 2dc966ae729bc050e7ba6eb487148e9964977c9c..4bb19fac75d7606deb94efe295428ee5b9adc346 100644 (file)
@@ -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)
 
index 0f6f6e850431e37f5cd7758a5e8369303976f9a4..ae962ba3cf39bb65bce50b5304d07fe9130db7f7 100644 (file)
@@ -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],