Relocate VlanAllocation DB model

This patch will relocate the VlanAllocation db model from
plugins/ml2/drivers to db/models and separate it from mixins for OVO
implementation.

Change-Id: Ib544b22c9e1acaf4025e8cdace694b2000a4da90
Partial-Bug: #1597913
This commit is contained in:
Anindita Das 2016-08-16 21:42:20 +00:00
parent cae38fb392
commit 558c0519e1
4 changed files with 54 additions and 34 deletions

View File

@ -52,7 +52,6 @@ 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_geneve # noqa
from neutron.plugins.ml2.drivers import type_vlan # noqa
from neutron.plugins.ml2 import models as ml2_models # noqa
from neutron.services.auto_allocate import models as aa_models # noqa
from neutron.services.segments import db # noqa

View File

@ -0,0 +1,41 @@
# 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_lib.db import model_base
import sqlalchemy as sa
class VlanAllocation(model_base.BASEV2):
"""Represent allocation state of a vlan_id on a physical network.
If allocated is False, the vlan_id on the physical_network is
available for allocation to a tenant network. If allocated is
True, the vlan_id on the physical_network is in use, either as a
tenant or provider network.
When an allocation is released, if the vlan_id for the
physical_network is inside the pool described by
VlanTypeDriver.network_vlan_ranges, then allocated is set to
False. If it is outside the pool, the record is deleted.
"""
__tablename__ = 'ml2_vlan_allocations'
__table_args__ = (
sa.Index('ix_ml2_vlan_allocations_physical_network_allocated',
'physical_network', 'allocated'),
model_base.BASEV2.__table_args__,)
physical_network = sa.Column(sa.String(64), nullable=False,
primary_key=True)
vlan_id = sa.Column(sa.Integer, nullable=False, primary_key=True,
autoincrement=False)
allocated = sa.Column(sa.Boolean, nullable=False)

View File

@ -15,15 +15,15 @@
import sys
from neutron_lib.db import model_base
from neutron_lib import exceptions as exc
from oslo_config import cfg
from oslo_log import log
from six import moves
import sqlalchemy as sa
from neutron._i18n import _, _LE, _LI, _LW
from neutron.common import _deprecate
from neutron.db import api as db_api
from neutron.db.models.plugins.ml2 import vlanallocation as vlan_alloc_model
from neutron.plugins.common import constants as p_const
from neutron.plugins.common import utils as plugin_utils
from neutron.plugins.ml2 import driver_api as api
@ -43,32 +43,7 @@ vlan_opts = [
cfg.CONF.register_opts(vlan_opts, "ml2_type_vlan")
class VlanAllocation(model_base.BASEV2):
"""Represent allocation state of a vlan_id on a physical network.
If allocated is False, the vlan_id on the physical_network is
available for allocation to a tenant network. If allocated is
True, the vlan_id on the physical_network is in use, either as a
tenant or provider network.
When an allocation is released, if the vlan_id for the
physical_network is inside the pool described by
VlanTypeDriver.network_vlan_ranges, then allocated is set to
False. If it is outside the pool, the record is deleted.
"""
__tablename__ = 'ml2_vlan_allocations'
__table_args__ = (
sa.Index('ix_ml2_vlan_allocations_physical_network_allocated',
'physical_network', 'allocated'),
model_base.BASEV2.__table_args__,)
physical_network = sa.Column(sa.String(64), nullable=False,
primary_key=True)
vlan_id = sa.Column(sa.Integer, nullable=False, primary_key=True,
autoincrement=False)
allocated = sa.Column(sa.Boolean, nullable=False)
_deprecate._moved_global('VlanAllocation', new_module=vlan_alloc_model)
class VlanTypeDriver(helpers.SegmentTypeDriver):
@ -83,7 +58,7 @@ class VlanTypeDriver(helpers.SegmentTypeDriver):
"""
def __init__(self):
super(VlanTypeDriver, self).__init__(VlanAllocation)
super(VlanTypeDriver, self).__init__(vlan_alloc_model.VlanAllocation)
self._parse_network_vlan_ranges()
def _parse_network_vlan_ranges(self):
@ -101,7 +76,7 @@ class VlanTypeDriver(helpers.SegmentTypeDriver):
with session.begin(subtransactions=True):
# get existing allocations for all physical networks
allocations = dict()
allocs = (session.query(VlanAllocation).
allocs = (session.query(vlan_alloc_model.VlanAllocation).
with_lockmode('update'))
for alloc in allocs:
if alloc.physical_network not in allocations:
@ -139,7 +114,8 @@ class VlanTypeDriver(helpers.SegmentTypeDriver):
# add missing allocatable vlans to table
for vlan_id in sorted(vlan_ids):
alloc = VlanAllocation(physical_network=physical_network,
alloc = vlan_alloc_model.VlanAllocation(
physical_network=physical_network,
vlan_id=vlan_id,
allocated=False)
session.add(alloc)
@ -235,7 +211,7 @@ class VlanTypeDriver(helpers.SegmentTypeDriver):
inside = any(lo <= vlan_id <= hi for lo, hi in ranges)
with session.begin(subtransactions=True):
query = (session.query(VlanAllocation).
query = (session.query(vlan_alloc_model.VlanAllocation).
filter_by(physical_network=physical_network,
vlan_id=vlan_id))
if inside:
@ -267,3 +243,6 @@ class VlanTypeDriver(helpers.SegmentTypeDriver):
if physical_network in self.physnet_mtus:
mtu.append(int(self.physnet_mtus[physical_network]))
return min(mtu) if mtu else 0
_deprecate._MovedGlobals()

View File

@ -18,6 +18,7 @@ from neutron_lib import exceptions as exc
from testtools import matchers
import neutron.db.api as db
from neutron.db.models.plugins.ml2 import vlanallocation as vlan_alloc_model
from neutron.plugins.common import constants as p_const
from neutron.plugins.common import utils as plugin_utils
from neutron.plugins.ml2 import config
@ -59,7 +60,7 @@ class VlanTypeTest(testlib_api.SqlTestCase):
self.driver._parse_network_vlan_ranges)
def _get_allocation(self, session, segment):
return session.query(type_vlan.VlanAllocation).filter_by(
return session.query(vlan_alloc_model.VlanAllocation).filter_by(
physical_network=segment[api.PHYSICAL_NETWORK],
vlan_id=segment[api.SEGMENTATION_ID]).first()