diff --git a/neutron/db/ipam_backend_mixin.py b/neutron/db/ipam_backend_mixin.py index 39dc546e747..221bd5c8755 100644 --- a/neutron/db/ipam_backend_mixin.py +++ b/neutron/db/ipam_backend_mixin.py @@ -33,6 +33,7 @@ from neutron.common import exceptions as n_exc from neutron.common import ipv6_utils from neutron.common import utils as common_utils from neutron.db import db_base_plugin_common +from neutron.db.models import segment as segment_model from neutron.db.models import subnet_service_type as sst_model from neutron.db import models_v2 from neutron.db import segments_db @@ -42,7 +43,6 @@ from neutron.extensions import segment from neutron.ipam import exceptions as ipam_exceptions from neutron.ipam import utils as ipam_utils from neutron.objects import subnet as subnet_obj -from neutron.services.segments import db as segment_svc_db from neutron.services.segments import exceptions as segment_exc LOG = logging.getLogger(__name__) @@ -612,7 +612,7 @@ class IpamBackendMixin(db_base_plugin_common.DbBasePluginCommon): *cannot* reach are excluded. """ Subnet = models_v2.Subnet - SegmentHostMapping = segment_svc_db.SegmentHostMapping + SegmentHostMapping = segment_model.SegmentHostMapping # A host has been provided. Consider these two scenarios # 1. Not a routed network: subnets are not on segments diff --git a/neutron/db/migration/models/head.py b/neutron/db/migration/models/head.py index 1ead9bdd4e3..93731f64fb9 100644 --- a/neutron/db/migration/models/head.py +++ b/neutron/db/migration/models/head.py @@ -45,7 +45,6 @@ from neutron.db import tag_db # noqa from neutron.ipam.drivers.neutrondb_ipam import db_models # 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 from neutron.services.trunk import models as trunk_models # noqa diff --git a/neutron/db/models/segment.py b/neutron/db/models/segment.py new file mode 100644 index 00000000000..3c50a1843f8 --- /dev/null +++ b/neutron/db/models/segment.py @@ -0,0 +1,43 @@ +# Copyright 2016 Hewlett Packard Enterprise Development, LP +# +# All Rights Reserved. +# +# 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 +from sqlalchemy import orm + +from neutron.db import segments_db as db + + +class SegmentHostMapping(model_base.BASEV2): + + segment_id = sa.Column(sa.String(36), + sa.ForeignKey('networksegments.id', + ondelete="CASCADE"), + primary_key=True, + index=True, + nullable=False) + host = sa.Column(sa.String(255), + primary_key=True, + index=True, + nullable=False) + + # Add a relationship to the NetworkSegment model in order to instruct + # SQLAlchemy to eagerly load this association + network_segment = orm.relationship( + db.NetworkSegment, backref=orm.backref("segment_host_mapping", + lazy='joined', + cascade='delete')) diff --git a/neutron/scheduler/dhcp_agent_scheduler.py b/neutron/scheduler/dhcp_agent_scheduler.py index 9a9afced69d..c65dccbe43d 100644 --- a/neutron/scheduler/dhcp_agent_scheduler.py +++ b/neutron/scheduler/dhcp_agent_scheduler.py @@ -26,11 +26,11 @@ from sqlalchemy import sql from neutron._i18n import _LI, _LW from neutron.db import agents_db from neutron.db import api as db_api +from neutron.db.models import segment as segment_model from neutron.db.network_dhcp_agent_binding import models as ndab_model from neutron.extensions import availability_zone as az_ext from neutron.scheduler import base_resource_filter from neutron.scheduler import base_scheduler -from neutron.services.segments import db as segments_db LOG = logging.getLogger(__name__) @@ -66,8 +66,8 @@ class AutoScheduler(object): dhcp_agents = query.all() query = context.session.query( - segments_db.SegmentHostMapping.segment_id) - query = query.filter(segments_db.SegmentHostMapping.host == host) + segment_model.SegmentHostMapping.segment_id) + query = query.filter(segment_model.SegmentHostMapping.host == host) segments_on_host = {s.segment_id for s in query} for dhcp_agent in dhcp_agents: diff --git a/neutron/services/segments/db.py b/neutron/services/segments/db.py index da0d4c06143..a7d63e0d695 100644 --- a/neutron/services/segments/db.py +++ b/neutron/services/segments/db.py @@ -18,45 +18,25 @@ import functools from neutron_lib import constants -from neutron_lib.db import model_base from neutron_lib import exceptions as n_exc from oslo_db import exception as db_exc from oslo_log import helpers as log_helpers from oslo_utils import uuidutils -import sqlalchemy as sa -from sqlalchemy import orm from sqlalchemy.orm import exc from neutron.callbacks import events from neutron.callbacks import registry from neutron.callbacks import resources +from neutron.common import _deprecate from neutron.db import api as db_api from neutron.db import common_db_mixin +from neutron.db.models import segment as segment_model from neutron.db import segments_db as db from neutron.extensions import segment as extension from neutron import manager from neutron.services.segments import exceptions - -class SegmentHostMapping(model_base.BASEV2): - - segment_id = sa.Column(sa.String(36), - sa.ForeignKey('networksegments.id', - ondelete="CASCADE"), - primary_key=True, - index=True, - nullable=False) - host = sa.Column(sa.String(255), - primary_key=True, - index=True, - nullable=False) - - # Add a relationship to the NetworkSegment model in order to instruct - # SQLAlchemy to eagerly load this association - network_segment = orm.relationship( - db.NetworkSegment, backref=orm.backref("segment_host_mapping", - lazy='joined', - cascade='delete')) +_deprecate._moved_global('SegmentHostMapping', new_module=segment_model) class SegmentDbMixin(common_db_mixin.CommonDbMixin): @@ -179,8 +159,9 @@ class SegmentDbMixin(common_db_mixin.CommonDbMixin): def get_segments_by_hosts(self, context, hosts): if not hosts: return [] - query = context.session.query(SegmentHostMapping).filter( - SegmentHostMapping.host.in_(hosts)) + query = context.session.query( + segment_model.SegmentHostMapping).filter( + segment_model.SegmentHostMapping.host.in_(hosts)) return list({mapping.segment_id for mapping in query}) @log_helpers.log_method_call @@ -211,16 +192,17 @@ class SegmentDbMixin(common_db_mixin.CommonDbMixin): def update_segment_host_mapping(context, host, current_segment_ids): with context.session.begin(subtransactions=True): segments_host_query = context.session.query( - SegmentHostMapping).filter_by(host=host) + segment_model.SegmentHostMapping).filter_by(host=host) previous_segment_ids = { seg_host['segment_id'] for seg_host in segments_host_query} for segment_id in current_segment_ids - previous_segment_ids: - context.session.add(SegmentHostMapping(segment_id=segment_id, - host=host)) + context.session.add(segment_model.SegmentHostMapping( + segment_id=segment_id, + host=host)) stale_segment_ids = previous_segment_ids - current_segment_ids if stale_segment_ids: segments_host_query.filter( - SegmentHostMapping.segment_id.in_( + segment_model.SegmentHostMapping.segment_id.in_( stale_segment_ids)).delete(synchronize_session=False) @@ -230,7 +212,7 @@ def get_hosts_mapped_with_segments(context): L2 providers can use this method to get an overview of SegmentHostMapping, and then delete the stale SegmentHostMapping. """ - query = context.session.query(SegmentHostMapping.host) + query = context.session.query(segment_model.SegmentHostMapping.host) return {row.host for row in query} @@ -269,8 +251,9 @@ def map_segment_to_hosts(context, segment_id, hosts): """Map segment to a collection of hosts.""" with db_api.autonested_transaction(context.session): for host in hosts: - context.session.add(SegmentHostMapping(segment_id=segment_id, - host=host)) + context.session.add( + segment_model.SegmentHostMapping(segment_id=segment_id, + host=host)) def _update_segment_host_mapping_for_agent(resource, event, trigger, @@ -341,3 +324,6 @@ def subscribe(): events.PRECOMMIT_DELETE) subscribe() + + +_deprecate._MovedGlobals() diff --git a/neutron/tests/unit/extensions/test_segment.py b/neutron/tests/unit/extensions/test_segment.py index 3e353570d85..6c6e046d6c4 100644 --- a/neutron/tests/unit/extensions/test_segment.py +++ b/neutron/tests/unit/extensions/test_segment.py @@ -28,6 +28,7 @@ from neutron import context from neutron.db import agents_db from neutron.db import agentschedulers_db from neutron.db import db_base_plugin_v2 +from neutron.db.models import segment as segment_model from neutron.db import portbindings_db from neutron.db import segments_db from neutron.extensions import ip_allocation @@ -471,7 +472,7 @@ class HostSegmentMappingTestCase(SegmentTestCase): def _get_segments_for_host(self, host): ctx = context.get_admin_context() segments_host_list = ctx.session.query( - db.SegmentHostMapping).filter_by(host=host) + segment_model.SegmentHostMapping).filter_by(host=host) return {seg_host['segment_id']: seg_host for seg_host in segments_host_list} @@ -788,7 +789,7 @@ class TestSegmentAwareIpam(SegmentTestCase): ctx = context.get_admin_context() with ctx.session.begin(subtransactions=True): for segment_id, host in mappings: - record = db.SegmentHostMapping( + record = segment_model.SegmentHostMapping( segment_id=segment_id, host=host) ctx.session.add(record)