Merge "OVO: add SubnetServiceType object and code integration."

This commit is contained in:
Jenkins 2016-11-28 20:38:48 +00:00 committed by Gerrit Code Review
commit 8568320c37
5 changed files with 86 additions and 14 deletions

View File

@ -182,16 +182,16 @@ class IpamBackendMixin(db_base_plugin_common.DbBasePluginCommon):
return result_pools
def _update_subnet_service_types(self, context, subnet_id, s):
old_types = context.session.query(
sst_model.SubnetServiceType).filter_by(subnet_id=subnet_id)
old_types = subnet_obj.SubnetServiceType.get_objects(
context, subnet_id=subnet_id)
for service_type in old_types:
context.session.delete(service_type)
service_type.delete()
updated_types = s.pop('service_types')
for service_type in updated_types:
new_type = sst_model.SubnetServiceType(
subnet_id=subnet_id,
service_type=service_type)
context.session.add(new_type)
new_type = subnet_obj.SubnetServiceType(context,
subnet_id=subnet_id,
service_type=service_type)
new_type.create()
return updated_types
def update_db_subnet(self, context, subnet_id, s, oldpools):
@ -552,10 +552,9 @@ class IpamBackendMixin(db_base_plugin_common.DbBasePluginCommon):
if validators.is_attr_set(service_types):
for service_type in service_types:
service_type_entry = sst_model.SubnetServiceType(
subnet_id=subnet.id,
service_type=service_type)
context.session.add(service_type_entry)
service_type_obj = subnet_obj.SubnetServiceType(
context, subnet_id=subnet.id, service_type=service_type)
service_type_obj.create()
self.save_allocation_pools(context, subnet,
subnet_request.allocation_pools)
@ -599,6 +598,7 @@ class IpamBackendMixin(db_base_plugin_common.DbBasePluginCommon):
return query.filter(models_v2.Subnet.network_id == network_id)
def _query_filter_service_subnets(self, query, service_type):
# TODO(korzen) use SubnetServiceType OVO here
ServiceType = sst_model.SubnetServiceType
query = query.add_entity(ServiceType)
query = query.outerjoin(ServiceType)

View File

@ -16,6 +16,7 @@ from oslo_versionedobjects import base as obj_base
from oslo_versionedobjects import fields as obj_fields
from neutron.common import utils
from neutron.db.models import subnet_service_type
from neutron.db import models_v2
from neutron.db import rbac_db_models
from neutron.objects import base
@ -136,6 +137,23 @@ class IPAllocationPool(base.NeutronDbObject):
return result
@obj_base.VersionedObjectRegistry.register
class SubnetServiceType(base.NeutronDbObject):
# Version 1.0: Initial version
VERSION = '1.0'
db_model = subnet_service_type.SubnetServiceType
foreign_keys = {'Subnet': {'subnet_id': 'id'}}
primary_keys = ['subnet_id', 'service_type']
fields = {
'subnet_id': obj_fields.UUIDField(),
'service_type': obj_fields.StringField()
}
# RBAC metaclass is not applied here because 'shared' attribute of Subnet
# is dependent on Network 'shared' state, and in Subnet object
# it can be read-only. The necessary changes are applied manually:
@ -168,11 +186,12 @@ class Subnet(base.NeutronDbObject):
nullable=True),
'host_routes': obj_fields.ListOfObjectsField('Route', nullable=True),
'ipv6_ra_mode': common_types.IPV6ModeEnumField(nullable=True),
'ipv6_address_mode': common_types.IPV6ModeEnumField(nullable=True)
'ipv6_address_mode': common_types.IPV6ModeEnumField(nullable=True),
'service_types': obj_fields.ListOfStringsField(nullable=True)
}
synthetic_fields = ['allocation_pools', 'dns_nameservers', 'host_routes',
'shared']
'service_types', 'shared']
foreign_keys = {'Network': {'network_id': 'id'}}
@ -189,6 +208,8 @@ class Subnet(base.NeutronDbObject):
def obj_load_attr(self, attrname):
if attrname == 'shared':
return self._load_shared()
if attrname == 'service_types':
return self._load_service_types()
super(Subnet, self).obj_load_attr(attrname)
def _load_shared(self, db_obj=None):
@ -210,9 +231,21 @@ class Subnet(base.NeutronDbObject):
setattr(self, 'shared', shared)
self.obj_reset_changes(['shared'])
def _load_service_types(self, db_obj=None):
if db_obj:
service_types = db_obj.get('service_types', [])
else:
service_types = SubnetServiceType.get_objects(self.obj_context,
subnet_id=self.id)
self.service_types = [service_type['service_type'] for
service_type in service_types]
self.obj_reset_changes(['service_types'])
def from_db_object(self, db_obj):
super(Subnet, self).from_db_object(db_obj)
self._load_shared(db_obj)
self._load_service_types(db_obj)
@classmethod
def modify_fields_from_db(cls, db_obj):

View File

@ -398,6 +398,7 @@ FIELD_TYPE_VALUE_GENERATOR_MAP = {
obj_fields.DateTimeField: tools.get_random_datetime,
obj_fields.IntegerField: tools.get_random_integer,
obj_fields.StringField: tools.get_random_string,
obj_fields.ListOfStringsField: tools.get_random_string_list,
obj_fields.UUIDField: uuidutils.generate_uuid,
obj_fields.ObjectField: lambda: None,
obj_fields.ListOfObjectsField: lambda: [],

View File

@ -59,9 +59,10 @@ object_data = {
'SecurityGroup': '1.0-e26b90c409b31fd2e3c6fcec402ac0b9',
'SecurityGroupRule': '1.0-e9b8dace9d48b936c62ad40fe1f339d5',
'SegmentHostMapping': '1.0-521597cf82ead26217c3bd10738f00f0',
'Subnet': '1.0-b71e720f45fff2a39759940e010be7d1',
'Subnet': '1.0-9c19023a61b42d29fbf3766df380e5b7',
'SubnetPool': '1.0-e8300bfbc4762cc88a7f6205b52da2f8',
'SubnetPoolPrefix': '1.0-13c15144135eb869faa4a76dc3ee3b6c',
'SubnetServiceType': '1.0-05ae4cdb2a9026a697b143926a1add8c',
'SubPort': '1.0-72c8471068db1f0491b5480fe49b52bb',
'Trunk': '1.0-80ebebb57f2b0dbb510f84d91421ed10',
}

View File

@ -110,6 +110,25 @@ class RouteDbObjectTestCase(obj_test_base.BaseDbObjectTestCase,
obj['subnet_id'] = self._subnet['id']
class SubnetServiceTypeObjectIfaceTestCase(
obj_test_base.BaseObjectIfaceTestCase):
_test_class = subnet.SubnetServiceType
class SubnetServiceTypeDbObjectTestCase(obj_test_base.BaseDbObjectTestCase,
testlib_api.SqlTestCase):
_test_class = subnet.SubnetServiceType
def setUp(self):
super(SubnetServiceTypeDbObjectTestCase, self).setUp()
self._create_test_network()
self._create_test_subnet(self._network)
for obj in itertools.chain(self.db_objs, self.obj_fields, self.objs):
obj['subnet_id'] = self._subnet['id']
class SubnetObjectIfaceTestCase(obj_test_base.BaseObjectIfaceTestCase):
_test_class = subnet.Subnet
@ -222,3 +241,21 @@ class SubnetDbObjectTestCase(obj_test_base.BaseDbObjectTestCase,
fetched_public_subnet = (
self._test_class.get_object(new_ctx, id=shared_subnet.id))
self.assertEqual(shared_subnet, fetched_public_subnet)
def test_get_service_types(self):
obj = self._make_object(self.obj_fields[0])
obj.create()
service_type_obj = subnet.SubnetServiceType(
self.context, subnet_id=obj.id, service_type='dhcp-agent')
service_type_obj.create()
listed_obj = subnet.Subnet.get_object(self.context, id=obj.id)
self.assertEqual([service_type_obj.service_type],
listed_obj.service_types)
# Try to load the service_types by obj_load_attr
obj1 = self._make_object(self.obj_fields[0])
self.assertEqual([service_type_obj.service_type],
obj1.service_types)