From 45b9960fdd4e33bfe72ba269f00002b3336e6c29 Mon Sep 17 00:00:00 2001 From: rossella Date: Tue, 12 Jan 2016 23:40:01 +0100 Subject: [PATCH] Port allowed address pairs to OVO This patch introduces Oslo VersionedObject for the port extension allowed address pairs. Implements: blueprint adopt-oslo-versioned-objects-for-db Change-Id: I8d03528f8f45f5f50fa467b39245a513a37c5d89 Co-Authored-By: Martin Hickey --- .../port/extensions/allowedaddresspairs.py | 57 +++++++++++++++++++ neutron/tests/tools.py | 13 +++++ .../extensions/test_allowedaddresspairs.py | 38 +++++++++++++ neutron/tests/unit/objects/test_base.py | 2 + neutron/tests/unit/objects/test_objects.py | 1 + 5 files changed, 111 insertions(+) create mode 100644 neutron/objects/port/extensions/allowedaddresspairs.py create mode 100644 neutron/tests/unit/objects/port/extensions/test_allowedaddresspairs.py diff --git a/neutron/objects/port/extensions/allowedaddresspairs.py b/neutron/objects/port/extensions/allowedaddresspairs.py new file mode 100644 index 00000000000..861a3ed9c49 --- /dev/null +++ b/neutron/objects/port/extensions/allowedaddresspairs.py @@ -0,0 +1,57 @@ +# 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 netaddr + +from oslo_versionedobjects import base as obj_base +from oslo_versionedobjects import fields as obj_fields + +from neutron.db import allowedaddresspairs_db as models +from neutron.objects import base +from neutron.objects import common_types + + +@obj_base.VersionedObjectRegistry.register +class AllowedAddressPair(base.NeutronDbObject): + # Version 1.0: Initial version + VERSION = '1.0' + + db_model = models.AllowedAddressPair + + primary_keys = ['port_id', 'mac_address', 'ip_address'] + + fields = { + 'port_id': obj_fields.UUIDField(), + 'mac_address': common_types.MACAddressField(), + 'ip_address': obj_fields.IPAddressField(), + } + + # TODO(mhickey): get rid of it once we switch the db model to using + # custom types. + def modify_fields_to_db(self, fields): + result = super(AllowedAddressPair, self).modify_fields_to_db(fields) + if 'ip_address' in result: + result['ip_address'] = str(result['ip_address']) + if 'mac_address' in result: + result['mac_address'] = str(result['mac_address']) + return result + + # TODO(mhickey): get rid of it once we switch the db model to using + # custom types. + @classmethod + def modify_fields_from_db(cls, db_obj): + fields = super(AllowedAddressPair, cls).modify_fields_from_db(db_obj) + if 'ip_address' in fields: + fields['ip_address'] = netaddr.IPAddress(fields['ip_address']) + if 'mac_address' in fields: + fields['mac_address'] = netaddr.EUI(fields['mac_address']) + return fields diff --git a/neutron/tests/tools.py b/neutron/tests/tools.py index 97b9d691618..adc0ae8c6d6 100644 --- a/neutron/tests/tools.py +++ b/neutron/tests/tools.py @@ -30,6 +30,7 @@ import six import neutron from neutron.api.v2 import attributes from neutron.common import constants +from neutron.common import ipv6_utils class AttributeMapMemento(fixtures.Fixture): @@ -268,6 +269,18 @@ def get_random_ip_network(version=4): return netaddr.IPNetwork(get_random_cidr(version=version)) +def get_random_ip_address(version=4): + if version == 4: + ip_string = '10.%d.%d.%d' % (random.randint(3, 254), + random.randint(3, 254), + random.randint(3, 254)) + return netaddr.IPAddress(ip_string) + else: + ip = ipv6_utils.get_ipv6_addr_by_EUI64('2001:db8::/64', + get_random_mac()) + return ip + + def is_bsd(): """Return True on BSD-based systems.""" diff --git a/neutron/tests/unit/objects/port/extensions/test_allowedaddresspairs.py b/neutron/tests/unit/objects/port/extensions/test_allowedaddresspairs.py new file mode 100644 index 00000000000..fbd23408acb --- /dev/null +++ b/neutron/tests/unit/objects/port/extensions/test_allowedaddresspairs.py @@ -0,0 +1,38 @@ +# 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 itertools + +from neutron import context +from neutron.objects.port.extensions import allowedaddresspairs +from neutron.tests.unit.objects import test_base as obj_test_base +from neutron.tests.unit import testlib_api + + +class AllowedAddrPairsIfaceObjTestCase(obj_test_base.BaseObjectIfaceTestCase): + + _test_class = allowedaddresspairs.AllowedAddressPair + + +#TODO(mhickey): Add common base db test class specifically for port extensions +class AllowedAddrPairsDbObjTestCase(obj_test_base.BaseDbObjectTestCase, + testlib_api.SqlTestCase): + + _test_class = allowedaddresspairs.AllowedAddressPair + + def setUp(self): + super(AllowedAddrPairsDbObjTestCase, self).setUp() + self.context = context.get_admin_context() + self._create_test_network() + self._create_test_port(self._network) + for obj in itertools.chain(self.db_objs, self.obj_fields): + obj['port_id'] = self._port['id'] diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index 6bfcd753e89..beab109392c 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -240,6 +240,8 @@ FIELD_TYPE_VALUE_GENERATOR_MAP = { common_types.ListOfIPNetworksField: get_list_of_random_networks, common_types.IPVersionEnumField: tools.get_random_ip_version, obj_fields.DateTimeField: timeutils.utcnow, + obj_fields.IPAddressField: tools.get_random_ip_address, + common_types.MACAddressField: tools.get_random_EUI, } diff --git a/neutron/tests/unit/objects/test_objects.py b/neutron/tests/unit/objects/test_objects.py index bfe04db8c8e..55edba0b711 100644 --- a/neutron/tests/unit/objects/test_objects.py +++ b/neutron/tests/unit/objects/test_objects.py @@ -28,6 +28,7 @@ from neutron.tests import tools object_data = { 'ExtraDhcpOpt': '1.0-632f689cbeb36328995a7aed1d0a78d3', 'PortSecurity': '1.0-cf5b382a0112080ec4e0f23f697c7ab2', + 'AllowedAddressPair': '1.0-0d7380d7d4a32f72e6ae509af1476297', 'QosBandwidthLimitRule': '1.1-4e44a8f5c2895ab1278399f87b40a13d', 'QosDscpMarkingRule': '1.1-0313c6554b34fd10c753cb63d638256c', 'QosRuleType': '1.1-8a53fef4c6a43839d477a85b787d22ce',