From 995e646b114b57f752dcb3b3c3506de5fc7d289c Mon Sep 17 00:00:00 2001 From: Anindita Das Date: Fri, 9 Sep 2016 08:07:42 +0000 Subject: [PATCH] Integration of Allocation/Endpoints OVO This patch integrates the Oslo-Versioned Objects created for VlanAllocation, VxlanAllocation, VxlanEndpoints, GreAllocation, GreEndpoints, GeneveAllocation and GeneveEndpoints into the code base. Partially-Implements: blueprint adopt-oslo-versioned-objects-for-db Change-Id: I0d592bae452876b24c28ca4cc4bf6392b5ab6493 Co-Authored-By: Victor Morales --- neutron/plugins/ml2/drivers/helpers.py | 8 ++- neutron/plugins/ml2/drivers/type_geneve.py | 7 ++- neutron/plugins/ml2/drivers/type_gre.py | 4 +- neutron/plugins/ml2/drivers/type_tunnel.py | 6 ++- neutron/plugins/ml2/drivers/type_vlan.py | 53 ++++++++++--------- neutron/plugins/ml2/drivers/type_vxlan.py | 4 +- .../plugins/ml2/drivers/test_type_vlan.py | 10 ++-- 7 files changed, 51 insertions(+), 41 deletions(-) diff --git a/neutron/plugins/ml2/drivers/helpers.py b/neutron/plugins/ml2/drivers/helpers.py index 7f4fd2cef11..30cf3f26042 100644 --- a/neutron/plugins/ml2/drivers/helpers.py +++ b/neutron/plugins/ml2/drivers/helpers.py @@ -24,6 +24,7 @@ from oslo_log import log from neutron._i18n import _LE from neutron.common import exceptions as exc from neutron.db import api as db_api +from neutron.objects import base as base_obj from neutron.plugins.common import utils as p_utils from neutron.plugins.ml2 import driver_api as api @@ -58,8 +59,11 @@ class SegmentTypeDriver(BaseTypeDriver): def __init__(self, model): super(SegmentTypeDriver, self).__init__() - self.model = model - self.primary_keys = set(dict(model.__table__.columns)) + if issubclass(model, base_obj.NeutronDbObject): + self.model = model.db_model + else: + self.model = model + self.primary_keys = set(dict(self.model.__table__.columns)) self.primary_keys.remove("allocated") # TODO(ataraday): get rid of this method when old TypeDriver won't be used diff --git a/neutron/plugins/ml2/drivers/type_geneve.py b/neutron/plugins/ml2/drivers/type_geneve.py index 13ed3414a4c..c9de7a45a23 100644 --- a/neutron/plugins/ml2/drivers/type_geneve.py +++ b/neutron/plugins/ml2/drivers/type_geneve.py @@ -20,8 +20,7 @@ from oslo_log import log from neutron._i18n import _LE from neutron.conf.plugins.ml2.drivers import driver_type -from neutron.db.models.plugins.ml2 import geneveallocation \ - as geneve_model +from neutron.objects.plugins.ml2 import geneveallocation as geneve_obj from neutron.plugins.common import constants as p_const from neutron.plugins.ml2.drivers import type_tunnel @@ -33,8 +32,8 @@ driver_type.register_ml2_drivers_geneve_opts() class GeneveTypeDriver(type_tunnel.EndpointTunnelTypeDriver): def __init__(self): - super(GeneveTypeDriver, self).__init__(geneve_model.GeneveAllocation, - geneve_model.GeneveEndpoints) + super(GeneveTypeDriver, self).__init__(geneve_obj.GeneveAllocation, + geneve_obj.GeneveEndpoint) self.max_encap_size = cfg.CONF.ml2_type_geneve.max_header_size def get_type(self): diff --git a/neutron/plugins/ml2/drivers/type_gre.py b/neutron/plugins/ml2/drivers/type_gre.py index a15e29d4d42..984d5a609cf 100644 --- a/neutron/plugins/ml2/drivers/type_gre.py +++ b/neutron/plugins/ml2/drivers/type_gre.py @@ -19,7 +19,7 @@ from oslo_log import log from neutron._i18n import _LE from neutron.conf.plugins.ml2.drivers import driver_type -from neutron.db.models.plugins.ml2 import gre_allocation_endpoints as gre_model +from neutron.objects.plugins.ml2 import greallocation as gre_obj from neutron.plugins.common import constants as p_const from neutron.plugins.ml2.drivers import type_tunnel @@ -32,7 +32,7 @@ class GreTypeDriver(type_tunnel.EndpointTunnelTypeDriver): def __init__(self): super(GreTypeDriver, self).__init__( - gre_model.GreAllocation, gre_model.GreEndpoints) + gre_obj.GreAllocation, gre_obj.GreEndpoint) def get_type(self): return p_const.TYPE_GRE diff --git a/neutron/plugins/ml2/drivers/type_tunnel.py b/neutron/plugins/ml2/drivers/type_tunnel.py index 7dd79c042b3..d4cf1002636 100644 --- a/neutron/plugins/ml2/drivers/type_tunnel.py +++ b/neutron/plugins/ml2/drivers/type_tunnel.py @@ -30,6 +30,7 @@ from sqlalchemy import or_ from neutron._i18n import _, _LI, _LW from neutron.common import topics from neutron.db import api as db_api +from neutron.objects import base as base_obj from neutron.plugins.common import constants as p_const from neutron.plugins.common import utils as plugin_utils from neutron.plugins.ml2.drivers import helpers @@ -339,7 +340,10 @@ class EndpointTunnelTypeDriver(ML2TunnelTypeDriver): def __init__(self, segment_model, endpoint_model): super(EndpointTunnelTypeDriver, self).__init__(segment_model) - self.endpoint_model = endpoint_model + if issubclass(endpoint_model, base_obj.NeutronDbObject): + self.endpoint_model = endpoint_model.db_model + else: + self.endpoint_model = endpoint_model self.segmentation_key = next(iter(self.primary_keys)) def get_endpoint_by_host(self, host): diff --git a/neutron/plugins/ml2/drivers/type_vlan.py b/neutron/plugins/ml2/drivers/type_vlan.py index 0a8fbf1bc77..bd6c3423de5 100644 --- a/neutron/plugins/ml2/drivers/type_vlan.py +++ b/neutron/plugins/ml2/drivers/type_vlan.py @@ -25,7 +25,7 @@ from six import moves from neutron._i18n import _, _LE, _LI, _LW from neutron.conf.plugins.ml2.drivers import driver_type from neutron.db import api as db_api -from neutron.db.models.plugins.ml2 import vlanallocation as vlan_alloc_model +from neutron.objects.plugins.ml2 import vlanallocation as vlanalloc from neutron.plugins.common import constants as p_const from neutron.plugins.common import utils as plugin_utils from neutron.plugins.ml2.drivers import helpers @@ -47,7 +47,7 @@ class VlanTypeDriver(helpers.SegmentTypeDriver): """ def __init__(self): - super(VlanTypeDriver, self).__init__(vlan_alloc_model.VlanAllocation) + super(VlanTypeDriver, self).__init__(vlanalloc.VlanAllocation) self._parse_network_vlan_ranges() def _parse_network_vlan_ranges(self): @@ -66,11 +66,11 @@ class VlanTypeDriver(helpers.SegmentTypeDriver): with db_api.context_manager.writer.using(ctx): # get existing allocations for all physical networks allocations = dict() - allocs = ctx.session.query(vlan_alloc_model.VlanAllocation) + allocs = vlanalloc.VlanAllocation.get_objects(ctx) for alloc in allocs: if alloc.physical_network not in allocations: - allocations[alloc.physical_network] = set() - allocations[alloc.physical_network].add(alloc) + allocations[alloc.physical_network] = list() + allocations[alloc.physical_network].append(alloc) # process vlan ranges for each configured physical network for (physical_network, @@ -103,23 +103,22 @@ class VlanTypeDriver(helpers.SegmentTypeDriver): # values to True while our transaction is # open so we don't accidentally delete # allocated segments. If someone has already - # allocated, count will return 0 so we don't - # delete. - count = allocs.filter_by( + # allocated, update_objects will return 0 so we + # don't delete. + if vlanalloc.VlanAllocation.update_objects( + ctx, values={'allocated': False}, allocated=False, vlan_id=alloc.vlan_id, - physical_network=physical_network - ).update({"allocated": False}) - if count: - ctx.session.delete(alloc) + physical_network=physical_network): + alloc.delete() del allocations[physical_network] # add missing allocatable vlans to table for vlan_id in sorted(vlan_ids): - alloc = vlan_alloc_model.VlanAllocation( - physical_network=physical_network, - vlan_id=vlan_id, - allocated=False) - ctx.session.add(alloc) + alloc = vlanalloc.VlanAllocation( + ctx, + physical_network=physical_network, + vlan_id=vlan_id, allocated=False) + alloc.create() # remove from table unallocated vlans for any unconfigured # physical networks @@ -220,21 +219,23 @@ class VlanTypeDriver(helpers.SegmentTypeDriver): ranges = self.network_vlan_ranges.get(physical_network, []) inside = any(lo <= vlan_id <= hi for lo, hi in ranges) + count = False with db_api.context_manager.writer.using(context): - query = (context.session.query(vlan_alloc_model.VlanAllocation). - filter_by(physical_network=physical_network, - vlan_id=vlan_id)) - if inside: - count = query.update({"allocated": False}) - if count: + alloc = vlanalloc.VlanAllocation.get_object( + context, physical_network=physical_network, vlan_id=vlan_id) + if alloc: + if inside and alloc.allocated: + count = True + alloc.allocated = False + alloc.update() LOG.debug("Releasing vlan %(vlan_id)s on physical " "network %(physical_network)s to pool", {'vlan_id': vlan_id, 'physical_network': physical_network}) - else: - count = query.delete() - if count: + else: + count = True + alloc.delete() LOG.debug("Releasing vlan %(vlan_id)s on physical " "network %(physical_network)s outside pool", {'vlan_id': vlan_id, diff --git a/neutron/plugins/ml2/drivers/type_vxlan.py b/neutron/plugins/ml2/drivers/type_vxlan.py index b7233177450..96934e88eae 100644 --- a/neutron/plugins/ml2/drivers/type_vxlan.py +++ b/neutron/plugins/ml2/drivers/type_vxlan.py @@ -19,7 +19,7 @@ from oslo_log import log from neutron._i18n import _LE from neutron.conf.plugins.ml2.drivers import driver_type -from neutron.db.models.plugins.ml2 import vxlanallocation as vxlan_model +from neutron.objects.plugins.ml2 import vxlanallocation as vxlan_obj from neutron.plugins.common import constants as p_const from neutron.plugins.ml2.drivers import type_tunnel @@ -32,7 +32,7 @@ class VxlanTypeDriver(type_tunnel.EndpointTunnelTypeDriver): def __init__(self): super(VxlanTypeDriver, self).__init__( - vxlan_model.VxlanAllocation, vxlan_model.VxlanEndpoints) + vxlan_obj.VxlanAllocation, vxlan_obj.VxlanEndpoint) def get_type(self): return p_const.TYPE_VXLAN diff --git a/neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py b/neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py index 0ef7753d53d..6b39fc56c78 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py +++ b/neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py @@ -20,7 +20,7 @@ from neutron_lib.plugins.ml2 import api from testtools import matchers from neutron.db import api as db_api -from neutron.db.models.plugins.ml2 import vlanallocation as vlan_alloc_model +from neutron.objects.plugins.ml2 import vlanallocation as vlan_alloc_obj from neutron.plugins.common import constants as p_const from neutron.plugins.common import utils as plugin_utils from neutron.plugins.ml2 import config @@ -37,6 +37,7 @@ UPDATED_VLAN_RANGES = { PROVIDER_NET: [], TENANT_NET: [(VLAN_MIN + 5, VLAN_MAX + 5)], } +CORE_PLUGIN = 'ml2' class VlanTypeTest(testlib_api.SqlTestCase): @@ -52,6 +53,7 @@ class VlanTypeTest(testlib_api.SqlTestCase): self.driver._sync_vlan_allocations() self.context = context.Context() self.driver.physnet_mtus = [] + self.setup_coreplugin(CORE_PLUGIN) def test_parse_network_exception_handling(self): with mock.patch.object(plugin_utils, @@ -62,10 +64,10 @@ class VlanTypeTest(testlib_api.SqlTestCase): @db_api.context_manager.reader def _get_allocation(self, context, segment): - return context.session.query( - vlan_alloc_model.VlanAllocation).filter_by( + return vlan_alloc_obj.VlanAllocation.get_object( + context, physical_network=segment[api.PHYSICAL_NETWORK], - vlan_id=segment[api.SEGMENTATION_ID]).first() + vlan_id=segment[api.SEGMENTATION_ID]) def test_partial_segment_is_partial_segment(self): segment = {api.NETWORK_TYPE: p_const.TYPE_VLAN}