From 86c74f9cdfda538b738b9d2b407d2e749da3442a Mon Sep 17 00:00:00 2001 From: armando-migliaccio Date: Wed, 15 Jan 2014 14:15:51 -0800 Subject: [PATCH] Rename Queue related methods for VMware NSX plugin This is the first step of many for renaming/refactoring nvplib and related modules. Starting with queues, whose code is small and less daunting. Partial-implements blueprint: nicira-plugin-renaming Change-Id: I2db899883c61f30c8b15703c96290872e853d670 --- neutron/plugins/nicira/NeutronPlugin.py | 14 ++-- neutron/plugins/nicira/common/utils.py | 3 +- .../dbexts/{nicira_qos_db.py => qos_db.py} | 36 +++------- neutron/plugins/nicira/nsxlib/queue.py | 72 +++++++++++++++++++ neutron/plugins/nicira/nvplib.py | 26 ------- neutron/tests/unit/nicira/nsxlib/__init__.py | 0 .../tests/unit/nicira/nsxlib/test_queue.py | 68 ++++++++++++++++++ .../tests/unit/nicira/test_nicira_plugin.py | 10 +-- neutron/tests/unit/nicira/test_nvplib.py | 46 ------------ 9 files changed, 162 insertions(+), 113 deletions(-) rename neutron/plugins/nicira/dbexts/{nicira_qos_db.py => qos_db.py} (91%) create mode 100644 neutron/plugins/nicira/nsxlib/queue.py create mode 100644 neutron/tests/unit/nicira/nsxlib/__init__.py create mode 100644 neutron/tests/unit/nicira/nsxlib/test_queue.py diff --git a/neutron/plugins/nicira/NeutronPlugin.py b/neutron/plugins/nicira/NeutronPlugin.py index 28f6c4788..2f543c870 100644 --- a/neutron/plugins/nicira/NeutronPlugin.py +++ b/neutron/plugins/nicira/NeutronPlugin.py @@ -71,11 +71,12 @@ from neutron.plugins.nicira.dbexts import distributedrouter as dist_rtr from neutron.plugins.nicira.dbexts import maclearning as mac_db from neutron.plugins.nicira.dbexts import nicira_db from neutron.plugins.nicira.dbexts import nicira_networkgw_db as networkgw_db -from neutron.plugins.nicira.dbexts import nicira_qos_db as qos_db +from neutron.plugins.nicira.dbexts import qos_db from neutron.plugins.nicira import dhcpmeta_modes from neutron.plugins.nicira.extensions import maclearning as mac_ext from neutron.plugins.nicira.extensions import nvp_networkgw as networkgw from neutron.plugins.nicira.extensions import nvp_qos as ext_qos +from neutron.plugins.nicira.nsxlib import queue as queuelib from neutron.plugins.nicira import NvpApiClient from neutron.plugins.nicira import nvplib @@ -2170,17 +2171,16 @@ class NvpPluginV2(addr_pair_db.AllowedAddressPairsMixin, def create_qos_queue(self, context, qos_queue, check_policy=True): q = qos_queue.get('qos_queue') self._validate_qos_queue(context, q) - q['id'] = nvplib.create_lqueue(self.cluster, - self._nvp_lqueue(q)) + q['id'] = queuelib.create_lqueue(self.cluster, q) return super(NvpPluginV2, self).create_qos_queue(context, qos_queue) - def delete_qos_queue(self, context, id, raise_in_use=True): - filters = {'queue_id': [id]} + def delete_qos_queue(self, context, queue_id, raise_in_use=True): + filters = {'queue_id': [queue_id]} queues = self._get_port_queue_bindings(context, filters) if queues: if raise_in_use: raise ext_qos.QueueInUseByPort() else: return - nvplib.delete_lqueue(self.cluster, id) - return super(NvpPluginV2, self).delete_qos_queue(context, id) + queuelib.delete_lqueue(self.cluster, queue_id) + return super(NvpPluginV2, self).delete_qos_queue(context, queue_id) diff --git a/neutron/plugins/nicira/common/utils.py b/neutron/plugins/nicira/common/utils.py index 07b456c12..273ec97b2 100644 --- a/neutron/plugins/nicira/common/utils.py +++ b/neutron/plugins/nicira/common/utils.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +from neutron.api.v2.attributes import is_attr_set from neutron.openstack.common import log from neutron.version import version_info @@ -32,7 +33,7 @@ def get_tags(**kwargs): def check_and_truncate(display_name): - if display_name and len(display_name) > MAX_DISPLAY_NAME_LEN: + if is_attr_set(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] diff --git a/neutron/plugins/nicira/dbexts/nicira_qos_db.py b/neutron/plugins/nicira/dbexts/qos_db.py similarity index 91% rename from neutron/plugins/nicira/dbexts/nicira_qos_db.py rename to neutron/plugins/nicira/dbexts/qos_db.py index 14d5b42cf..56d1d416c 100644 --- a/neutron/plugins/nicira/dbexts/nicira_qos_db.py +++ b/neutron/plugins/nicira/dbexts/qos_db.py @@ -1,6 +1,6 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # -# Copyright 2013 Nicira Networks, Inc. All rights reserved. +# 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 @@ -14,7 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. # -# @author: Aaron Rosen, Nicira, Inc import sqlalchemy as sa from sqlalchemy import orm @@ -26,7 +25,6 @@ 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 @@ -92,23 +90,23 @@ class NVPQoSDbMixin(ext_qos.QueuePluginBase): context.session.add(qos_queue) return self._make_qos_queue_dict(qos_queue) - def get_qos_queue(self, context, id, fields=None): + def get_qos_queue(self, context, queue_id, fields=None): return self._make_qos_queue_dict( - self._get_qos_queue(context, id), fields) + self._get_qos_queue(context, queue_id), fields) - def _get_qos_queue(self, context, id): + def _get_qos_queue(self, context, queue_id): try: - return self._get_by_id(context, QoSQueue, id) + return self._get_by_id(context, QoSQueue, queue_id) except exc.NoResultFound: - raise ext_qos.QueueNotFound(id=id) + raise ext_qos.QueueNotFound(id=queue_id) def get_qos_queues(self, context, filters=None, fields=None): return self._get_collection(context, QoSQueue, self._make_qos_queue_dict, filters=filters, fields=fields) - def delete_qos_queue(self, context, id): - qos_queue = self._get_qos_queue(context, id) + def delete_qos_queue(self, context, queue_id): + qos_queue = self._get_qos_queue(context, queue_id) with context.session.begin(subtransactions=True): context.session.delete(qos_queue) @@ -294,21 +292,3 @@ class NVPQoSDbMixin(ext_qos.QueuePluginBase): # Max can be None if max and min > max: raise ext_qos.QueueMinGreaterMax() - - def _nvp_lqueue(self, queue): - """Convert fields to nvp fields.""" - nvp_queue = {} - params = {'name': 'display_name', - 'qos_marking': 'qos_marking', - 'min': 'min_bandwidth_rate', - 'max': 'max_bandwidth_rate', - 'dscp': 'dscp'} - nvp_queue = dict( - (nvp_name, queue.get(api_name)) - for api_name, nvp_name in params.iteritems() - if attr.is_attr_set(queue.get(api_name)) - ) - if 'display_name' in nvp_queue: - nvp_queue['display_name'] = utils.check_and_truncate( - nvp_queue['display_name']) - return nvp_queue diff --git a/neutron/plugins/nicira/nsxlib/queue.py b/neutron/plugins/nicira/nsxlib/queue.py new file mode 100644 index 000000000..00e0aa427 --- /dev/null +++ b/neutron/plugins/nicira/nsxlib/queue.py @@ -0,0 +1,72 @@ +# Copyright 2014 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.api.v2 import attributes as attr +from neutron.common import exceptions as exception +from neutron.openstack.common import excutils +from neutron.openstack.common import jsonutils +from neutron.openstack.common import log +from neutron.plugins.nicira.common import utils +from neutron.plugins.nicira import NvpApiClient +from neutron.plugins.nicira.nvplib import _build_uri_path +from neutron.plugins.nicira.nvplib import do_request + +HTTP_POST = "POST" +HTTP_DELETE = "DELETE" + +LQUEUE_RESOURCE = "lqueue" + +LOG = log.getLogger(__name__) + + +def create_lqueue(cluster, queue_data): + params = { + 'name': 'display_name', + 'qos_marking': 'qos_marking', + 'min': 'min_bandwidth_rate', + 'max': 'max_bandwidth_rate', + 'dscp': 'dscp' + } + queue_obj = dict( + (nvp_name, queue_data.get(api_name)) + for api_name, nvp_name in params.iteritems() + if attr.is_attr_set(queue_data.get(api_name)) + ) + if 'display_name' in queue_obj: + queue_obj['display_name'] = utils.check_and_truncate( + queue_obj['display_name']) + + queue_obj['tags'] = utils.get_tags() + try: + return do_request(HTTP_POST, + _build_uri_path(LQUEUE_RESOURCE), + jsonutils.dumps(queue_obj), + cluster=cluster)['uuid'] + except NvpApiClient.NvpApiException: + # FIXME(salv-orlando): This should not raise NeutronException + with excutils.save_and_reraise_exception(): + raise exception.NeutronException() + + +def delete_lqueue(cluster, queue_id): + try: + do_request(HTTP_DELETE, + _build_uri_path(LQUEUE_RESOURCE, + resource_id=queue_id), + cluster=cluster) + except Exception: + # FIXME(salv-orlando): This should not raise NeutronException + with excutils.save_and_reraise_exception(): + raise exception.NeutronException() diff --git a/neutron/plugins/nicira/nvplib.py b/neutron/plugins/nicira/nvplib.py index 8344123bb..6447430ab 100644 --- a/neutron/plugins/nicira/nvplib.py +++ b/neutron/plugins/nicira/nvplib.py @@ -1408,29 +1408,3 @@ def get_function_by_version(func_name, nvp_ver): msg = _('NVP version is not set. Unable to complete request ' 'correctly. Check log for NVP communication errors.') raise NvpApiClient.ServiceUnavailable(message=msg) - - -# ----------------------------------------------------------------------------- -# QOS API Calls -# ----------------------------------------------------------------------------- -def create_lqueue(cluster, lqueue): - uri = _build_uri_path(LQUEUE_RESOURCE) - lqueue['tags'] = [{'tag': NEUTRON_VERSION, 'scope': 'quantum'}] - try: - return do_request(HTTP_POST, uri, json.dumps(lqueue), - cluster=cluster)['uuid'] - except NvpApiClient.NvpApiException: - # FIXME(salv-orlando): This should not raise QauntumException - LOG.exception(_("Failed to create logical queue")) - raise exception.NeutronException() - - -def delete_lqueue(cluster, id): - try: - do_request(HTTP_DELETE, _build_uri_path(LQUEUE_RESOURCE, - resource_id=id), - cluster=cluster) - except Exception: - # FIXME(salv-orlando): This should not raise QauntumException - LOG.exception(_("Failed to delete logical queue")) - raise exception.NeutronException() diff --git a/neutron/tests/unit/nicira/nsxlib/__init__.py b/neutron/tests/unit/nicira/nsxlib/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/neutron/tests/unit/nicira/nsxlib/test_queue.py b/neutron/tests/unit/nicira/nsxlib/test_queue.py new file mode 100644 index 000000000..1a0377762 --- /dev/null +++ b/neutron/tests/unit/nicira/nsxlib/test_queue.py @@ -0,0 +1,68 @@ +# Copyright (c) 2014 VMware, Inc. +# +# 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. +# + +import mock + +from neutron.common import exceptions +from neutron.plugins.nicira.nsxlib import queue as queuelib +from neutron.plugins.nicira import NvpApiClient +from neutron.tests.unit.nicira.test_nvplib import NvplibTestCase + + +class TestLogicalQueueLib(NvplibTestCase): + + def setUp(self): + super(TestLogicalQueueLib, self).setUp() + self.fake_queue = { + 'name': 'fake_queue', + 'min': 0, 'max': 256, + 'dscp': 0, 'qos_marking': False + } + + def test_create_and_get_lqueue(self): + queue_id = queuelib.create_lqueue( + self.fake_cluster, self.fake_queue) + queue_res = queuelib.do_request( + 'GET', + queuelib._build_uri_path('lqueue', resource_id=queue_id), + cluster=self.fake_cluster) + self.assertEqual(queue_id, queue_res['uuid']) + self.assertEqual('fake_queue', queue_res['display_name']) + + def test_create_lqueue_nsx_error_raises(self): + def raise_nsx_exc(*args, **kwargs): + raise NvpApiClient.NvpApiException() + + with mock.patch.object(queuelib, 'do_request', new=raise_nsx_exc): + self.assertRaises( + exceptions.NeutronException, queuelib.create_lqueue, + self.fake_cluster, self.fake_queue) + + def test_delete_lqueue(self): + queue_id = queuelib.create_lqueue( + self.fake_cluster, self.fake_queue) + queuelib.delete_lqueue(self.fake_cluster, queue_id) + self.assertRaises(exceptions.NotFound, + queuelib.do_request, + 'GET', + queuelib._build_uri_path( + 'lqueue', resource_id=queue_id), + cluster=self.fake_cluster) + + def test_delete_non_existing_lqueue_raises(self): + self.assertRaises(exceptions.NeutronException, + queuelib.delete_lqueue, + self.fake_cluster, 'whatever') diff --git a/neutron/tests/unit/nicira/test_nicira_plugin.py b/neutron/tests/unit/nicira/test_nicira_plugin.py index c006d27ff..a640fdf95 100644 --- a/neutron/tests/unit/nicira/test_nicira_plugin.py +++ b/neutron/tests/unit/nicira/test_nicira_plugin.py @@ -42,11 +42,12 @@ from neutron.openstack.common import uuidutils from neutron.plugins.nicira.common import exceptions as nvp_exc from neutron.plugins.nicira.common import sync from neutron.plugins.nicira.dbexts import nicira_db -from neutron.plugins.nicira.dbexts import nicira_qos_db as qos_db +from neutron.plugins.nicira.dbexts import qos_db from neutron.plugins.nicira.extensions import distributedrouter as dist_router from neutron.plugins.nicira.extensions import nvp_networkgw from neutron.plugins.nicira.extensions import nvp_qos as ext_qos from neutron.plugins.nicira import NeutronPlugin +from neutron.plugins.nicira import nsxlib from neutron.plugins.nicira import NvpApiClient from neutron.plugins.nicira.NvpApiClient import NVPVersion from neutron.plugins.nicira import nvplib @@ -1041,11 +1042,11 @@ class NvpQoSTestExtensionManager(object): return [] -class TestNiciraQoSQueue(NiciraPluginV2TestCase): +class TestQoSQueue(NiciraPluginV2TestCase): def setUp(self, plugin=None): cfg.CONF.set_override('api_extensions_path', NVPEXT_PATH) - super(TestNiciraQoSQueue, self).setUp() + super(TestQoSQueue, self).setUp() ext_mgr = NvpQoSTestExtensionManager() self.ext_api = test_extensions.setup_extensions_middleware(ext_mgr) @@ -1073,7 +1074,6 @@ class TestNiciraQoSQueue(NiciraPluginV2TestCase): body['qos_queue']['dscp'] = dscp if default: body['qos_queue']['default'] = default - res = self._create_qos_queue('json', body) qos_queue = self.deserialize('json', res) if res.status_int >= 400: @@ -1096,7 +1096,7 @@ class TestNiciraQoSQueue(NiciraPluginV2TestCase): def test_create_trusted_qos_queue(self): with mock.patch.object(qos_db.LOG, 'info') as log: - with mock.patch.object(nvplib, 'do_request', + with mock.patch.object(nsxlib.queue, 'do_request', return_value={"uuid": "fake_queue"}): with self.qos_queue(name='fake_lqueue', min=34, max=44, qos_marking='trusted', default=False) as q: diff --git a/neutron/tests/unit/nicira/test_nvplib.py b/neutron/tests/unit/nicira/test_nvplib.py index e640e3209..0b005a69d 100644 --- a/neutron/tests/unit/nicira/test_nvplib.py +++ b/neutron/tests/unit/nicira/test_nvplib.py @@ -1269,52 +1269,6 @@ class TestNvplibSecurityProfile(NvplibTestCase): self.fake_cluster, 'whatever') -class TestNvplibLQueue(NvplibTestCase): - - def test_create_and_get_lqueue(self): - queue_id = nvplib.create_lqueue(self.fake_cluster, - {'display_name': 'fake_queue', - 'min_bandwidth_rate': 0, - 'max_bandwidth_rate': 256, - 'dscp': 0, - 'qos_marking': False}) - queue_res = nvplib.do_request( - nvplib.HTTP_GET, - nvplib._build_uri_path('lqueue', resource_id=queue_id), - cluster=self.fake_cluster) - self.assertEqual(queue_id, queue_res['uuid']) - self.assertEqual('fake_queue', queue_res['display_name']) - - def test_create_lqueue_nvp_error_raises(self): - def raise_nvp_exc(*args, **kwargs): - raise NvpApiClient.NvpApiException() - - with mock.patch.object(nvplib, 'do_request', new=raise_nvp_exc): - self.assertRaises( - exceptions.NeutronException, nvplib.create_lqueue, - self.fake_cluster, {}) - - def test_delete_lqueue(self): - queue_id = nvplib.create_lqueue(self.fake_cluster, - {'display_name': 'fake_queue', - 'min_bandwidth_rate': 0, - 'max_bandwidth_rate': 256, - 'dscp': 0, - 'qos_marking': False}) - nvplib.delete_lqueue(self.fake_cluster, queue_id) - self.assertRaises(exceptions.NotFound, - nvplib.do_request, - nvplib.HTTP_GET, - nvplib._build_uri_path( - 'lqueue', resource_id=queue_id), - cluster=self.fake_cluster) - - def test_delete_non_existing_lqueue_raises(self): - self.assertRaises(exceptions.NeutronException, - nvplib.delete_lqueue, - self.fake_cluster, 'whatever') - - class TestNvplibLogicalPorts(NvplibTestCase): def _create_switch_and_port(self, tenant_id='pippo', -- 2.45.2