diff --git a/neutron/db/allowedaddresspairs_db.py b/neutron/db/allowedaddresspairs_db.py index 1d9464ecbfc..d7083bbc8be 100644 --- a/neutron/db/allowedaddresspairs_db.py +++ b/neutron/db/allowedaddresspairs_db.py @@ -21,7 +21,7 @@ from neutron.db import db_base_plugin_v2 from neutron.common import utils from neutron.extensions import allowedaddresspairs as addr_pair -from neutron.objects import base as obj_base +from neutron.objects import exceptions from neutron.objects.port.extensions import (allowedaddresspairs as obj_addr_pair) @@ -50,7 +50,7 @@ class AllowedAddressPairsMixin(object): mac_address=mac_address, ip_address=ip_address) pair_obj.create() - except obj_base.NeutronDbObjectDuplicateEntry: + except exceptions.NeutronDbObjectDuplicateEntry: raise addr_pair.DuplicateAddressPairInRequest( mac_address=address_pair['mac_address'], ip_address=address_pair['ip_address']) diff --git a/neutron/objects/base.py b/neutron/objects/base.py index 0cae03e1cd1..37acdaf4114 100644 --- a/neutron/objects/base.py +++ b/neutron/objects/base.py @@ -15,9 +15,8 @@ import copy import functools import itertools -from neutron_lib import exceptions +from neutron_lib import exceptions as n_exc from oslo_db import exception as obj_exc -from oslo_utils import reflection from oslo_versionedobjects import base as obj_base from oslo_versionedobjects import fields as obj_fields import six @@ -28,53 +27,12 @@ from neutron.db import api as db_api from neutron.db import model_base from neutron.db import standard_attr from neutron.objects.db import api as obj_db_api +from neutron.objects import exceptions as o_exc from neutron.objects.extensions import standardattributes _NO_DB_MODEL = object() -#TODO(jlibosva): Move these classes to exceptions module -class NeutronObjectUpdateForbidden(exceptions.NeutronException): - message = _("Unable to update the following object fields: %(fields)s") - - -class NeutronDbObjectDuplicateEntry(exceptions.Conflict): - message = _("Failed to create a duplicate %(object_type)s: " - "for attribute(s) %(attributes)s with value(s) %(values)s") - - def __init__(self, object_class, db_exception): - super(NeutronDbObjectDuplicateEntry, self).__init__( - object_type=reflection.get_class_name(object_class, - fully_qualified=False), - attributes=db_exception.columns, - values=db_exception.value) - - -class NeutronDbObjectNotFoundByModel(exceptions.NotFound): - message = _("NeutronDbObject not found by model %(model)s.") - - -class NeutronPrimaryKeyMissing(exceptions.BadRequest): - message = _("For class %(object_type)s missing primary keys: " - "%(missing_keys)s") - - def __init__(self, object_class, missing_keys): - super(NeutronPrimaryKeyMissing, self).__init__( - object_type=reflection.get_class_name(object_class, - fully_qualified=False), - missing_keys=missing_keys - ) - - -class NeutronSyntheticFieldMultipleForeignKeys(exceptions.NeutronException): - message = _("Synthetic field %(field)s shouldn't have more than one " - "foreign key") - - -class NeutronSyntheticFieldsForeignKeysNotFound(exceptions.NeutronException): - message = _("%(child)s does not define a foreign key for %(parent)s") - - def get_updatable_fields(cls, fields): fields = fields.copy() for field in cls.fields_no_update: @@ -88,7 +46,7 @@ def get_object_class_by_model(model): obj_class = obj_class[0] if getattr(obj_class, 'db_model', _NO_DB_MODEL) is model: return obj_class - raise NeutronDbObjectNotFoundByModel(model=model.__name__) + raise o_exc.NeutronDbObjectNotFoundByModel(model=model.__name__) def register_filter_hook_on_model(model, filter_name): @@ -207,7 +165,7 @@ class NeutronObject(obj_base.VersionedObject, if bad_filters: bad_filters = ', '.join(bad_filters) msg = _("'%s' is not supported for filtering") % bad_filters - raise exceptions.InvalidInput(error_message=msg) + raise n_exc.InvalidInput(error_message=msg) @classmethod @abc.abstractmethod @@ -436,8 +394,8 @@ class NeutronDbObject(NeutronObject): all_keys = itertools.chain([cls.primary_keys], cls.unique_keys) if not any(lookup_keys.issuperset(keys) for keys in all_keys): missing_keys = set(cls.primary_keys).difference(lookup_keys) - raise NeutronPrimaryKeyMissing(object_class=cls.__name__, - missing_keys=missing_keys) + raise o_exc.NeutronPrimaryKeyMissing(object_class=cls.__name__, + missing_keys=missing_keys) with db_api.autonested_transaction(context.session): db_obj = obj_db_api.get_object( @@ -499,7 +457,7 @@ class NeutronDbObject(NeutronObject): fields = fields.copy() forbidden_updates = set(self.fields_no_update) & set(fields.keys()) if forbidden_updates: - raise NeutronObjectUpdateForbidden(fields=forbidden_updates) + raise o_exc.NeutronObjectUpdateForbidden(fields=forbidden_updates) return fields @@ -531,10 +489,11 @@ class NeutronDbObject(NeutronObject): objclass = objclasses[0] foreign_keys = objclass.foreign_keys.get(clsname) if not foreign_keys: - raise NeutronSyntheticFieldsForeignKeysNotFound( + raise o_exc.NeutronSyntheticFieldsForeignKeysNotFound( parent=clsname, child=objclass.__name__) if len(foreign_keys.keys()) > 1: - raise NeutronSyntheticFieldMultipleForeignKeys(field=field) + raise o_exc.NeutronSyntheticFieldMultipleForeignKeys( + field=field) synthetic_field_db_name = ( self.fields_need_translation.get(field, field)) @@ -567,7 +526,7 @@ class NeutronDbObject(NeutronObject): self.obj_context, self.db_model, self.modify_fields_to_db(fields)) except obj_exc.DBDuplicateEntry as db_exc: - raise NeutronDbObjectDuplicateEntry( + raise o_exc.NeutronDbObjectDuplicateEntry( object_class=self.__class__, db_exception=db_exc) self.from_db_object(db_obj) diff --git a/neutron/objects/common_types.py b/neutron/objects/common_types.py index ce75c2af706..73e2ab1f61b 100644 --- a/neutron/objects/common_types.py +++ b/neutron/objects/common_types.py @@ -15,18 +15,14 @@ import itertools import netaddr from neutron_lib import constants as lib_constants -from neutron_lib import exceptions + from oslo_versionedobjects import fields as obj_fields import six from neutron._i18n import _ from neutron.common import constants from neutron.extensions import dns as dns_ext - - -class NeutronRangeConstrainedIntegerInvalidLimit(exceptions.NeutronException): - message = _("Incorrect range limits specified: " - "start = %(start)s, end = %(end)s") +from neutron.objects import exceptions as o_exc class IPV6ModeEnumField(obj_fields.AutoTypedField): @@ -39,7 +35,7 @@ class RangeConstrainedInteger(obj_fields.Integer): self._start = int(start) self._end = int(end) except (TypeError, ValueError): - raise NeutronRangeConstrainedIntegerInvalidLimit( + raise o_exc.NeutronRangeConstrainedIntegerInvalidLimit( start=start, end=end) super(RangeConstrainedInteger, self).__init__(**kwargs) diff --git a/neutron/objects/exceptions.py b/neutron/objects/exceptions.py new file mode 100644 index 00000000000..ef0e4e4785e --- /dev/null +++ b/neutron/objects/exceptions.py @@ -0,0 +1,62 @@ +# 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._i18n import _ +from neutron_lib import exceptions +from oslo_utils import reflection + + +class NeutronObjectUpdateForbidden(exceptions.NeutronException): + message = _("Unable to update the following object fields: %(fields)s") + + +class NeutronDbObjectDuplicateEntry(exceptions.Conflict): + message = _("Failed to create a duplicate %(object_type)s: " + "for attribute(s) %(attributes)s with value(s) %(values)s") + + def __init__(self, object_class, db_exception): + super(NeutronDbObjectDuplicateEntry, self).__init__( + object_type=reflection.get_class_name(object_class, + fully_qualified=False), + attributes=db_exception.columns, + values=db_exception.value) + + +class NeutronDbObjectNotFoundByModel(exceptions.NotFound): + message = _("NeutronDbObject not found by model %(model)s.") + + +class NeutronPrimaryKeyMissing(exceptions.BadRequest): + message = _("For class %(object_type)s missing primary keys: " + "%(missing_keys)s") + + def __init__(self, object_class, missing_keys): + super(NeutronPrimaryKeyMissing, self).__init__( + object_type=reflection.get_class_name(object_class, + fully_qualified=False), + missing_keys=missing_keys + ) + + +class NeutronRangeConstrainedIntegerInvalidLimit(exceptions.NeutronException): + message = _("Incorrect range limits specified: " + "start = %(start)s, end = %(end)s") + + +class NeutronSyntheticFieldMultipleForeignKeys(exceptions.NeutronException): + message = _("Synthetic field %(field)s shouldn't have more than one " + "foreign key") + + +class NeutronSyntheticFieldsForeignKeysNotFound(exceptions.NeutronException): + message = _("%(child)s does not define a foreign key for %(parent)s") diff --git a/neutron/objects/trunk.py b/neutron/objects/trunk.py index db75b947a90..ddd5e49cf07 100644 --- a/neutron/objects/trunk.py +++ b/neutron/objects/trunk.py @@ -20,6 +20,7 @@ from oslo_versionedobjects import fields as obj_fields from neutron.db import api as db_api from neutron.objects import base +from neutron.objects import exceptions as o_exc from neutron.services.trunk import exceptions as t_exc from neutron.services.trunk import models @@ -72,7 +73,7 @@ class SubPort(base.NeutronDbObject): raise t_exc.TrunkNotFound(trunk_id=self.trunk_id) raise n_exc.PortNotFound(port_id=self.port_id) - except base.NeutronDbObjectDuplicateEntry: + except o_exc.NeutronDbObjectDuplicateEntry: raise t_exc.DuplicateSubPort( segmentation_type=self.segmentation_type, segmentation_id=self.segmentation_id, diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index 1214c68902e..b8751b4f9a9 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -39,6 +39,7 @@ from neutron import objects from neutron.objects import base from neutron.objects import common_types from neutron.objects.db import api as obj_db_api +from neutron.objects import exceptions as o_exc from neutron.objects import network as net_obj from neutron.objects import ports from neutron.objects import rbac_db @@ -598,7 +599,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): self._test_class.unique_keys))) obj_keys = self.generate_object_keys(self._test_class, non_unique_fields) - self.assertRaises(base.NeutronPrimaryKeyMissing, + self.assertRaises(o_exc.NeutronPrimaryKeyMissing, self._test_class.get_object, self.context, **obj_keys) @@ -678,7 +679,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): with mock.patch.object(obj_db_api, 'get_objects', return_value=self.db_objs): - self.assertRaises(base.exceptions.InvalidInput, + self.assertRaises(n_exc.InvalidInput, self._test_class.get_objects, self.context, **filters) @@ -694,14 +695,14 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): with mock.patch.object(obj_db_api, 'get_objects', side_effect=self.fake_get_objects): - self.assertRaises(base.exceptions.InvalidInput, + self.assertRaises(n_exc.InvalidInput, self._test_class.get_objects, self.context, **{synthetic_fields.pop(): 'xxx'}) def test_get_objects_invalid_fields(self): with mock.patch.object(obj_db_api, 'get_objects', side_effect=self.fake_get_objects): - self.assertRaises(base.exceptions.InvalidInput, + self.assertRaises(n_exc.InvalidInput, self._test_class.get_objects, self.context, fake_field='xxx') @@ -725,7 +726,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): self.assertEqual(expected, self._test_class.count(self.context)) def test_count_invalid_fields(self): - self.assertRaises(base.exceptions.InvalidInput, + self.assertRaises(n_exc.InvalidInput, self._test_class.count, self.context, fake_field='xxx') @@ -768,7 +769,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): with mock.patch.object(obj_db_api, 'create_object', side_effect=obj_exc.DBDuplicateEntry): obj = self._test_class(self.context, **self.obj_fields[0]) - self.assertRaises(base.NeutronDbObjectDuplicateEntry, obj.create) + self.assertRaises(o_exc.NeutronDbObjectDuplicateEntry, obj.create) def test_update_fields(self): if not self._test_class.primary_keys: @@ -871,7 +872,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): new_callable=mock.PropertyMock(return_value=['a', 'c']), create=True): obj = self._test_class(self.context, **self.obj_fields[0]) - self.assertRaises(base.NeutronObjectUpdateForbidden, obj.update) + self.assertRaises(o_exc.NeutronObjectUpdateForbidden, obj.update) def test_update_updates_from_db_object(self): with mock.patch.object(obj_db_api, 'update_object', @@ -923,7 +924,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): if key in self._test_class.primary_keys: setattr(obj, key, val) - self.assertRaises(base.NeutronObjectUpdateForbidden, obj.update) + self.assertRaises(o_exc.NeutronObjectUpdateForbidden, obj.update) def test_to_dict_synthetic_fields(self): cls_ = self._test_class @@ -1049,7 +1050,7 @@ class BaseDbObjectMultipleForeignKeysTestCase(_BaseObjectTestCase, def test_load_synthetic_db_fields_with_multiple_foreign_keys(self): obj = self._test_class(self.context, **self.obj_fields[0]) - self.assertRaises(base.NeutronSyntheticFieldMultipleForeignKeys, + self.assertRaises(o_exc.NeutronSyntheticFieldMultipleForeignKeys, obj.load_synthetic_db_fields) @@ -1060,7 +1061,7 @@ class BaseDbObjectForeignKeysNotFoundTestCase(_BaseObjectTestCase, def test_load_foreign_keys_not_belong_class(self): obj = self._test_class(self.context, **self.obj_fields[0]) - self.assertRaises(base.NeutronSyntheticFieldsForeignKeysNotFound, + self.assertRaises(o_exc.NeutronSyntheticFieldsForeignKeysNotFound, obj.load_synthetic_db_fields) @@ -1443,7 +1444,7 @@ class BaseDbObjectTestCase(_BaseObjectTestCase, def test_count_invalid_filters(self): for fields in self.obj_fields: self._make_object(fields).create() - self.assertRaises(base.exceptions.InvalidInput, + self.assertRaises(n_exc.InvalidInput, self._test_class.count, self.context, fake_field='xxx') @@ -1492,7 +1493,7 @@ class GetObjectClassByModelTestCase(UniqueObjectBase): self.assertIs(self.registered_object, found_obj) def test_not_registed_object_raises_exception(self): - with testtools.ExpectedException(base.NeutronDbObjectNotFoundByModel): + with testtools.ExpectedException(o_exc.NeutronDbObjectNotFoundByModel): base.get_object_class_by_model(self.not_registered_object.db_model)