From a2e915977a26278b272020b5c0dbd17f3e793004 Mon Sep 17 00:00:00 2001 From: sindhudevale Date: Thu, 11 Aug 2016 20:53:48 +0000 Subject: [PATCH] Relocate Flat Allocation DB model Since there would be a circular import issue once we try to implement an object relying on the model, and adopt it in tree for database accessing code, this patch prepares the ground for this work by moving the model out of database accessing modules into neutron/db/models. Change-Id: I7876d28d35c5c4b9167520733e1a1b08dabc6951 Partial-Bug: #1597913 --- neutron/db/migration/models/head.py | 1 - neutron/db/models/plugins/__init__.py | 0 neutron/db/models/plugins/ml2/__init__.py | 0 .../db/models/plugins/ml2/flatallocation.py | 28 +++++++++++++++++ neutron/plugins/ml2/drivers/type_flat.py | 30 ++++++++----------- .../plugins/ml2/drivers/test_type_flat.py | 3 +- 6 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 neutron/db/models/plugins/__init__.py create mode 100644 neutron/db/models/plugins/ml2/__init__.py create mode 100644 neutron/db/models/plugins/ml2/flatallocation.py diff --git a/neutron/db/migration/models/head.py b/neutron/db/migration/models/head.py index ad038f115f8..ee5c7529f61 100644 --- a/neutron/db/migration/models/head.py +++ b/neutron/db/migration/models/head.py @@ -52,7 +52,6 @@ from neutron.db import segments_db # noqa from neutron.db import servicetype_db # noqa from neutron.db import tag_db # noqa from neutron.ipam.drivers.neutrondb_ipam import db_models # noqa -from neutron.plugins.ml2.drivers import type_flat # noqa from neutron.plugins.ml2.drivers import type_geneve # noqa from neutron.plugins.ml2.drivers import type_gre # noqa from neutron.plugins.ml2.drivers import type_vlan # noqa diff --git a/neutron/db/models/plugins/__init__.py b/neutron/db/models/plugins/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/neutron/db/models/plugins/ml2/__init__.py b/neutron/db/models/plugins/ml2/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/neutron/db/models/plugins/ml2/flatallocation.py b/neutron/db/models/plugins/ml2/flatallocation.py new file mode 100644 index 00000000000..341eaadce85 --- /dev/null +++ b/neutron/db/models/plugins/ml2/flatallocation.py @@ -0,0 +1,28 @@ +# 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 sqlalchemy as sa + +from neutron.db import model_base + + +class FlatAllocation(model_base.BASEV2): + """Represent persistent allocation state of a physical network. + + If a record exists for a physical network, then that physical + network has been allocated as a flat network. + """ + + __tablename__ = 'ml2_flat_allocations' + + physical_network = sa.Column(sa.String(64), nullable=False, + primary_key=True) diff --git a/neutron/plugins/ml2/drivers/type_flat.py b/neutron/plugins/ml2/drivers/type_flat.py index d39d4e38d48..4958b3e2216 100644 --- a/neutron/plugins/ml2/drivers/type_flat.py +++ b/neutron/plugins/ml2/drivers/type_flat.py @@ -13,16 +13,18 @@ # License for the specific language governing permissions and limitations # under the License. +import sys + from neutron_lib import exceptions as exc from oslo_config import cfg from oslo_db import exception as db_exc from oslo_log import log import six -import sqlalchemy as sa from neutron._i18n import _, _LI, _LW +from neutron.common import _deprecate from neutron.common import exceptions as n_exc -from neutron.db import model_base +from neutron.db.models.plugins.ml2 import flatallocation as type_flat_model from neutron.plugins.common import constants as p_const from neutron.plugins.ml2 import driver_api as api from neutron.plugins.ml2.drivers import helpers @@ -41,19 +43,6 @@ flat_opts = [ cfg.CONF.register_opts(flat_opts, "ml2_type_flat") -class FlatAllocation(model_base.BASEV2): - """Represent persistent allocation state of a physical network. - - If a record exists for a physical network, then that physical - network has been allocated as a flat network. - """ - - __tablename__ = 'ml2_flat_allocations' - - physical_network = sa.Column(sa.String(64), nullable=False, - primary_key=True) - - class FlatTypeDriver(helpers.BaseTypeDriver): """Manage state for flat networks with ML2. @@ -114,7 +103,8 @@ class FlatTypeDriver(helpers.BaseTypeDriver): try: LOG.debug("Reserving flat network on physical " "network %s", physical_network) - alloc = FlatAllocation(physical_network=physical_network) + alloc = type_flat_model.FlatAllocation( + physical_network=physical_network) alloc.save(session) except db_exc.DBDuplicateEntry: raise n_exc.FlatNetworkInUse( @@ -129,7 +119,7 @@ class FlatTypeDriver(helpers.BaseTypeDriver): def release_segment(self, session, segment): physical_network = segment[api.PHYSICAL_NETWORK] with session.begin(subtransactions=True): - count = (session.query(FlatAllocation). + count = (session.query(type_flat_model.FlatAllocation). filter_by(physical_network=physical_network). delete()) if count: @@ -147,3 +137,9 @@ class FlatTypeDriver(helpers.BaseTypeDriver): if physical_network in self.physnet_mtus: mtu.append(int(self.physnet_mtus[physical_network])) return min(mtu) if mtu else 0 + + +# WARNING: THESE MUST BE THE LAST TWO LINES IN THIS MODULE +_OLD_REF = sys.modules[__name__] +sys.modules[__name__] = _deprecate._DeprecateSubset(globals(), type_flat_model) +# WARNING: THESE MUST BE THE LAST TWO LINES IN THIS MODULE diff --git a/neutron/tests/unit/plugins/ml2/drivers/test_type_flat.py b/neutron/tests/unit/plugins/ml2/drivers/test_type_flat.py index a51fd6e2f88..42e40a53398 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/test_type_flat.py +++ b/neutron/tests/unit/plugins/ml2/drivers/test_type_flat.py @@ -17,6 +17,7 @@ from neutron_lib import exceptions as exc from neutron.common import exceptions as n_exc import neutron.db.api as db +from neutron.db.models.plugins.ml2 import flatallocation as type_flat_model from neutron.plugins.common import constants as p_const from neutron.plugins.ml2 import config from neutron.plugins.ml2 import driver_api as api @@ -39,7 +40,7 @@ class FlatTypeTest(testlib_api.SqlTestCase): self.driver.physnet_mtus = [] def _get_allocation(self, session, segment): - return session.query(type_flat.FlatAllocation).filter_by( + return session.query(type_flat_model.FlatAllocation).filter_by( physical_network=segment[api.PHYSICAL_NETWORK]).first() def test_is_partial_segment(self):