]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Ensure names are truncated to accommodate NVP limit
authorarmando-migliaccio <amigliaccio@nicira.com>
Fri, 20 Sep 2013 00:27:29 +0000 (17:27 -0700)
committerarmando-migliaccio <amigliaccio@nicira.com>
Fri, 20 Sep 2013 01:14:31 +0000 (18:14 -0700)
Switches created through Advanced Plugin should have
their name truncated as well. To this aim, move things
around to avoid awkward imports or code duplications

Fixes bug #1227927

Change-Id: I3cc69356227d3bd63603ee1e53d82e6561f4cad0

neutron/plugins/nicira/common/utils.py [new file with mode: 0644]
neutron/plugins/nicira/dbexts/nicira_qos_db.py
neutron/plugins/nicira/nvplib.py
neutron/plugins/nicira/vshield/edge_appliance_driver.py
neutron/tests/unit/nicira/test_nvplib.py

diff --git a/neutron/plugins/nicira/common/utils.py b/neutron/plugins/nicira/common/utils.py
new file mode 100644 (file)
index 0000000..57d08bb
--- /dev/null
@@ -0,0 +1,29 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 VMware, Inc.
+# All Rights Reserved
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+from neutron.openstack.common import log
+
+LOG = log.getLogger(__name__)
+MAX_DISPLAY_NAME_LEN = 40
+
+
+def check_and_truncate(display_name):
+    if display_name and len(display_name) > MAX_DISPLAY_NAME_LEN:
+        LOG.debug(_("Specified name:'%s' exceeds maximum length. "
+                    "It will be truncated on NVP"), display_name)
+        return display_name[:MAX_DISPLAY_NAME_LEN]
+    return display_name or ''
index 86a4cd57ba1e822d3fd8aa7469e1412bd2db5aa2..14d5b42cf1d1f65e31577ee66731a6675cc29b6e 100644 (file)
@@ -26,8 +26,8 @@ from neutron.db import model_base
 from neutron.db import models_v2
 from neutron.openstack.common import log
 from neutron.openstack.common import uuidutils
+from neutron.plugins.nicira.common import utils
 from neutron.plugins.nicira.extensions import nvp_qos as ext_qos
-from neutron.plugins.nicira import nvplib
 
 
 LOG = log.getLogger(__name__)
@@ -309,6 +309,6 @@ class NVPQoSDbMixin(ext_qos.QueuePluginBase):
             if attr.is_attr_set(queue.get(api_name))
         )
         if 'display_name' in nvp_queue:
-            nvp_queue['display_name'] = nvplib._check_and_truncate_name(
+            nvp_queue['display_name'] = utils.check_and_truncate(
                 nvp_queue['display_name'])
         return nvp_queue
index e0792957fea58994a5ced8c6cd2bab8bbd3d5500..0842a24445193cdebe82d2e41c96c2ea9abd7ea1 100644 (file)
@@ -30,8 +30,8 @@ from neutron.common import constants
 from neutron.common import exceptions as exception
 from neutron.openstack.common import excutils
 from neutron.openstack.common import log
-from neutron.plugins.nicira.common import (
-    exceptions as nvp_exc)
+from neutron.plugins.nicira.common import exceptions as nvp_exc
+from neutron.plugins.nicira.common import utils
 from neutron.plugins.nicira import NvpApiClient
 from neutron.version import version_info
 
@@ -55,8 +55,6 @@ LQUEUE_RESOURCE = "lqueue"
 GWSERVICE_RESOURCE = "gateway-service"
 # Current neutron version
 NEUTRON_VERSION = version_info.release_string()
-# Other constants for NVP resource
-MAX_DISPLAY_NAME_LEN = 40
 # Constants for NAT rules
 MATCH_KEYS = ["destination_ip_addresses", "destination_port_max",
               "destination_port_min", "source_ip_addresses",
@@ -87,7 +85,7 @@ def device_id_to_vm_id(device_id, obfuscate=False):
     # To fit it into an NVP tag we need to hash it, however device_id
     # used for ports associated to VM's are small enough so let's skip the
     # hashing
-    if len(device_id) > MAX_DISPLAY_NAME_LEN or obfuscate:
+    if len(device_id) > utils.MAX_DISPLAY_NAME_LEN or obfuscate:
         return hashlib.sha1(device_id).hexdigest()
     else:
         return device_id
@@ -149,14 +147,6 @@ def _build_uri_path(resource,
     return uri_path
 
 
-def _check_and_truncate_name(display_name):
-    if display_name and len(display_name) > MAX_DISPLAY_NAME_LEN:
-        LOG.debug(_("Specified name:'%s' exceeds maximum length. "
-                    "It will be truncated on NVP"), display_name)
-        return display_name[:MAX_DISPLAY_NAME_LEN]
-    return display_name or ''
-
-
 def get_cluster_version(cluster):
     """Return major/minor version #."""
     # Get control-cluster nodes
@@ -239,7 +229,7 @@ def create_lswitch(cluster, tenant_id, display_name,
                    neutron_net_id=None,
                    shared=None,
                    **kwargs):
-    lswitch_obj = {"display_name": _check_and_truncate_name(display_name),
+    lswitch_obj = {"display_name": utils.check_and_truncate(display_name),
                    "transport_zones": transport_zones_config,
                    "tags": [{"tag": tenant_id, "scope": "os_tid"},
                             {"tag": NEUTRON_VERSION, "scope": "quantum"}]}
@@ -261,7 +251,7 @@ def create_lswitch(cluster, tenant_id, display_name,
 def update_lswitch(cluster, lswitch_id, display_name,
                    tenant_id=None, **kwargs):
     uri = _build_uri_path(LSWITCH_RESOURCE, resource_id=lswitch_id)
-    lswitch_obj = {"display_name": _check_and_truncate_name(display_name),
+    lswitch_obj = {"display_name": utils.check_and_truncate(display_name),
                    "tags": [{"tag": tenant_id, "scope": "os_tid"},
                             {"tag": NEUTRON_VERSION, "scope": "quantum"}]}
     if "tags" in kwargs:
@@ -295,7 +285,7 @@ def create_l2_gw_service(cluster, tenant_id, display_name, devices):
                  "device_id": device['interface_name'],
                  "type": "L2Gateway"} for device in devices]
     gwservice_obj = {
-        "display_name": _check_and_truncate_name(display_name),
+        "display_name": utils.check_and_truncate(display_name),
         "tags": tags,
         "gateways": gateways,
         "type": "L2GatewayServiceConfig"
@@ -308,7 +298,7 @@ def create_l2_gw_service(cluster, tenant_id, display_name, devices):
 def _prepare_lrouter_body(name, tenant_id, router_type,
                           distributed=None, **kwargs):
     body = {
-        "display_name": _check_and_truncate_name(name),
+        "display_name": utils.check_and_truncate(name),
         "tags": [{"tag": tenant_id, "scope": "os_tid"},
                  {"tag": NEUTRON_VERSION, "scope": "quantum"}],
         "routing_config": {
@@ -459,7 +449,7 @@ def update_l2_gw_service(cluster, gateway_id, display_name):
     if not display_name:
         # Nothing to update
         return gwservice_obj
-    gwservice_obj["display_name"] = _check_and_truncate_name(display_name)
+    gwservice_obj["display_name"] = utils.check_and_truncate(display_name)
     return do_request("PUT", _build_uri_path(GWSERVICE_RESOURCE,
                                              resource_id=gateway_id),
                       json.dumps(gwservice_obj), cluster=cluster)
@@ -471,7 +461,7 @@ def update_implicit_routing_lrouter(cluster, r_id, display_name, nexthop):
         # Nothing to update
         return lrouter_obj
     # It seems that this is faster than the doing an if on display_name
-    lrouter_obj["display_name"] = (_check_and_truncate_name(display_name) or
+    lrouter_obj["display_name"] = (utils.check_and_truncate(display_name) or
                                    lrouter_obj["display_name"])
     if nexthop:
         nh_element = lrouter_obj["routing_config"].get(
@@ -808,7 +798,7 @@ def update_port(cluster, lswitch_uuid, lport_uuid, neutron_port_id, tenant_id,
                 mac_learning_enabled=None, allowed_address_pairs=None):
     lport_obj = dict(
         admin_status_enabled=admin_status_enabled,
-        display_name=_check_and_truncate_name(display_name),
+        display_name=utils.check_and_truncate(display_name),
         tags=[dict(scope='os_tid', tag=tenant_id),
               dict(scope='q_port_id', tag=neutron_port_id),
               dict(scope='vm_id', tag=device_id_to_vm_id(device_id)),
@@ -839,7 +829,7 @@ def create_lport(cluster, lswitch_uuid, tenant_id, neutron_port_id,
                  security_profiles=None, queue_id=None,
                  mac_learning_enabled=None, allowed_address_pairs=None):
     """Creates a logical port on the assigned logical switch."""
-    display_name = _check_and_truncate_name(display_name)
+    display_name = utils.check_and_truncate(display_name)
     lport_obj = dict(
         admin_status_enabled=admin_status_enabled,
         display_name=display_name,
@@ -1092,7 +1082,7 @@ def create_security_profile(cluster, tenant_id, security_profile):
                      {'ethertype': 'IPv6'}]}
     tags = [dict(scope='os_tid', tag=tenant_id),
             dict(scope='quantum', tag=NEUTRON_VERSION)]
-    display_name = _check_and_truncate_name(security_profile.get('name'))
+    display_name = utils.check_and_truncate(security_profile.get('name'))
     body = mk_body(
         tags=tags, display_name=display_name,
         logical_port_ingress_rules=(
index 9d15dc16daa11b2fc43ec0e04f2838fffde9001f..fe3128133c844117432e3ddb55173aa3841b09db 100644 (file)
@@ -20,6 +20,7 @@
 from neutron.openstack.common import excutils
 from neutron.openstack.common import jsonutils
 from neutron.openstack.common import log as logging
+from neutron.plugins.nicira.common import utils
 from neutron.plugins.nicira.vshield.common import (
     constants as vcns_const)
 from neutron.plugins.nicira.vshield.common.constants import RouterStatus
@@ -625,7 +626,7 @@ class EdgeApplianceDriver(object):
 
     def create_lswitch(self, name, tz_config):
         lsconfig = {
-            'display_name': name,
+            'display_name': utils.check_and_truncate(name),
             "tags": [],
             "type": "LogicalSwitchConfig",
             "_schema": "/ws.v1/schema/LogicalSwitchConfig",
index 5b2374e60879c949e89e2c816d3eb44ffcd5a8a7..eb0823f7b3810bff38942606738316d33bbee2a7 100644 (file)
@@ -24,6 +24,7 @@ from neutron.common import constants
 from neutron.common import exceptions
 from neutron.plugins.nicira.common import config  # noqa
 from neutron.plugins.nicira.common import exceptions as nvp_exc
+from neutron.plugins.nicira.common import utils
 from neutron.plugins.nicira import nvp_cluster
 from neutron.plugins.nicira import NvpApiClient
 from neutron.plugins.nicira import nvplib
@@ -1483,18 +1484,18 @@ class NvplibMiscTestCase(base.BaseTestCase):
 
     def test_check_and_truncate_name_with_none(self):
         name = None
-        result = nvplib._check_and_truncate_name(name)
+        result = utils.check_and_truncate(name)
         self.assertEqual('', result)
 
     def test_check_and_truncate_name_with_short_name(self):
         name = 'foo_port_name'
-        result = nvplib._check_and_truncate_name(name)
+        result = utils.check_and_truncate(name)
         self.assertEqual(name, result)
 
     def test_check_and_truncate_name_long_name(self):
         name = 'this_is_a_port_whose_name_is_longer_than_40_chars'
-        result = nvplib._check_and_truncate_name(name)
-        self.assertEqual(len(result), nvplib.MAX_DISPLAY_NAME_LEN)
+        result = utils.check_and_truncate(name)
+        self.assertEqual(len(result), utils.MAX_DISPLAY_NAME_LEN)
 
 
 def _nicira_method(method_name, module_name='nvplib'):