diff --git a/neutron/db/api.py b/neutron/db/api.py index a601b0553db..9a9016d5ad3 100644 --- a/neutron/db/api.py +++ b/neutron/db/api.py @@ -31,7 +31,6 @@ from pecan import util as p_util import six import sqlalchemy from sqlalchemy import event # noqa -from sqlalchemy import exc as sql_exc from sqlalchemy import orm from sqlalchemy.orm import exc @@ -148,35 +147,6 @@ def autonested_transaction(sess): yield tx -_REGISTERED_SQLA_EVENTS = [] - - -def sqla_listen(*args): - """Wrapper to track subscribers for test teardowns. - - SQLAlchemy has no "unsubscribe all" option for its event listener - framework so we need to keep track of the subscribers by having - them call through here for test teardowns. - """ - event.listen(*args) - _REGISTERED_SQLA_EVENTS.append(args) - - -def sqla_remove(*args): - event.remove(*args) - _REGISTERED_SQLA_EVENTS.remove(args) - - -def sqla_remove_all(): - for args in _REGISTERED_SQLA_EVENTS: - try: - event.remove(*args) - except sql_exc.InvalidRequestError: - # already removed - pass - del _REGISTERED_SQLA_EVENTS[:] - - @event.listens_for(orm.session.Session, "after_flush") def add_to_rel_load_list(session, flush_context=None): # keep track of new items to load relationships on during commit diff --git a/neutron/db/db_base_plugin_v2.py b/neutron/db/db_base_plugin_v2.py index c74c978f9eb..e76938dcaf6 100644 --- a/neutron/db/db_base_plugin_v2.py +++ b/neutron/db/db_base_plugin_v2.py @@ -162,12 +162,13 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon, # NOTE(arosen) These event listeners are here to hook into when # port status changes and notify nova about their change. self.nova_notifier = nova.Notifier.get_instance() - db_api.sqla_listen(models_v2.Port, 'after_insert', - self.nova_notifier.send_port_status) - db_api.sqla_listen(models_v2.Port, 'after_update', - self.nova_notifier.send_port_status) - db_api.sqla_listen(models_v2.Port.status, 'set', - self.nova_notifier.record_port_status_changed) + lib_db_api.sqla_listen(models_v2.Port, 'after_insert', + self.nova_notifier.send_port_status) + lib_db_api.sqla_listen(models_v2.Port, 'after_update', + self.nova_notifier.send_port_status) + lib_db_api.sqla_listen( + models_v2.Port.status, 'set', + self.nova_notifier.record_port_status_changed) @registry.receives(resources.RBAC_POLICY, [events.BEFORE_CREATE, events.BEFORE_UPDATE, diff --git a/neutron/hacking/checks.py b/neutron/hacking/checks.py index 8e872b59330..62effbd9593 100644 --- a/neutron/hacking/checks.py +++ b/neutron/hacking/checks.py @@ -243,6 +243,7 @@ def check_python3_no_filter(logical_line): yield(0, msg) +# TODO(boden): rehome this check to neutron-lib @flake8ext def check_no_sqlalchemy_event_import(logical_line, filename, noqa): """N346 - Use neutron.db.api.sqla_listen instead of sqlalchemy event.""" @@ -256,8 +257,8 @@ def check_no_sqlalchemy_event_import(logical_line, filename, noqa): if kw not in logical_line: return yield (0, "N346: Register sqlalchemy events through " - "neutron.db.api.sqla_listen so they can be cleaned up between " - "unit tests") + "neutron_lib.db.api.sqla_listen so they can be cleaned up " + "between unit tests") def factory(register): diff --git a/neutron/quota/resource.py b/neutron/quota/resource.py index a23c1521e6c..fcd3cddb9be 100644 --- a/neutron/quota/resource.py +++ b/neutron/quota/resource.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +from neutron_lib.db import api as lib_db_api from neutron_lib.plugins import constants from neutron_lib.plugins import directory from oslo_config import cfg @@ -318,19 +319,19 @@ class TrackedResource(BaseResource): self._model_class) def register_events(self): - listen = db_api.sqla_listen + listen = lib_db_api.sqla_listen listen(self._model_class, 'after_insert', self._db_event_handler) listen(self._model_class, 'after_delete', self._db_event_handler) listen(se.Session, 'after_bulk_delete', self._except_bulk_delete) def unregister_events(self): try: - db_api.sqla_remove(self._model_class, 'after_insert', - self._db_event_handler) - db_api.sqla_remove(self._model_class, 'after_delete', - self._db_event_handler) - db_api.sqla_remove(se.Session, 'after_bulk_delete', - self._except_bulk_delete) + lib_db_api.sqla_remove(self._model_class, 'after_insert', + self._db_event_handler) + lib_db_api.sqla_remove(self._model_class, 'after_delete', + self._db_event_handler) + lib_db_api.sqla_remove(se.Session, 'after_bulk_delete', + self._except_bulk_delete) except sql_exc.InvalidRequestError: LOG.warning("No sqlalchemy event for resource %s found", self.name) diff --git a/neutron/services/loki/loki_plugin.py b/neutron/services/loki/loki_plugin.py index c4b86a7d793..491f914b863 100644 --- a/neutron/services/loki/loki_plugin.py +++ b/neutron/services/loki/loki_plugin.py @@ -14,8 +14,7 @@ import random import time -from neutron.db import api as db_api - +from neutron_lib.db import api as db_api from neutron_lib.services import base as service_base from oslo_db import exception as db_exc from oslo_log import log as logging diff --git a/neutron/services/revisions/revision_plugin.py b/neutron/services/revisions/revision_plugin.py index 9115cf5ac9c..fe84bc4cff9 100644 --- a/neutron/services/revisions/revision_plugin.py +++ b/neutron/services/revisions/revision_plugin.py @@ -11,6 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. +from neutron_lib.db import api as db_api from neutron_lib.services import base as service_base from oslo_log import log as logging import sqlalchemy @@ -20,7 +21,6 @@ import webob.exc from neutron._i18n import _ from neutron.db import _resource_extend as resource_extend -from neutron.db import api as db_api from neutron.db import standard_attr LOG = logging.getLogger(__name__) diff --git a/neutron/services/timestamp/timestamp_db.py b/neutron/services/timestamp/timestamp_db.py index 1e6e23736ea..d6604354e90 100644 --- a/neutron/services/timestamp/timestamp_db.py +++ b/neutron/services/timestamp/timestamp_db.py @@ -12,13 +12,13 @@ # License for the specific language governing permissions and limitations # under the License. +from neutron_lib.db import api as db_api from neutron_lib import exceptions as n_exc from oslo_utils import timeutils from sqlalchemy.orm import session as se from neutron.db import _model_query as model_query from neutron.db import _resource_extend as resource_extend -from neutron.db import api as db_api from neutron.db import standard_attr CHANGED_SINCE = 'changed_since' diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 962135a2412..a6821ad75aa 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -27,6 +27,7 @@ import eventlet.timeout import fixtures import mock from neutron_lib.callbacks import manager as registry_manager +from neutron_lib.db import api as db_api from neutron_lib import fixture from oslo_concurrency.fixture import lockutils from oslo_config import cfg @@ -49,7 +50,6 @@ from neutron.conf.agent import common as agent_config from neutron.db import _model_query as model_query from neutron.db import _resource_extend as resource_extend from neutron.db import agentschedulers_db -from neutron.db import api as db_api from neutron import manager from neutron import policy from neutron.quota import resource_registry diff --git a/neutron/tests/unit/db/test_db_base_plugin_v2.py b/neutron/tests/unit/db/test_db_base_plugin_v2.py index 3ff9c0477b3..65e7e2cef87 100644 --- a/neutron/tests/unit/db/test_db_base_plugin_v2.py +++ b/neutron/tests/unit/db/test_db_base_plugin_v2.py @@ -25,6 +25,7 @@ from neutron_lib.callbacks import exceptions from neutron_lib.callbacks import registry from neutron_lib import constants from neutron_lib import context +from neutron_lib.db import api as lib_db_api from neutron_lib import exceptions as lib_exc from neutron_lib import fixture from neutron_lib.plugins import directory @@ -6699,7 +6700,7 @@ class DbOperationBoundMixin(object): self._recorded_statements.append(str(clauseelement)) engine = db_api.context_manager.writer.get_engine() - db_api.sqla_listen(engine, 'after_execute', _event_incrementer) + lib_db_api.sqla_listen(engine, 'after_execute', _event_incrementer) def _get_context(self): if self.admin: diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index c06c67c2053..2dac6f92550 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -29,6 +29,7 @@ from neutron_lib.callbacks import registry from neutron_lib.callbacks import resources from neutron_lib import constants from neutron_lib import context +from neutron_lib.db import api as lib_db_api from neutron_lib import exceptions as exc from neutron_lib import fixture from neutron_lib.plugins import constants as plugin_constants @@ -1282,8 +1283,9 @@ class TestMl2PortsV2(test_plugin.TestPortsV2, Ml2PluginV2TestCase): listener = IPAllocationsGrenade() engine = db_api.context_manager.writer.get_engine() - db_api.sqla_listen(engine, 'before_cursor_execute', listener.execute) - db_api.sqla_listen(engine, 'commit', listener.commit) + lib_db_api.sqla_listen(engine, 'before_cursor_execute', + listener.execute) + lib_db_api.sqla_listen(engine, 'commit', listener.commit) func() # make sure that the grenade went off during the commit self.assertTrue(listener.except_raised) diff --git a/neutron/tests/unit/services/revisions/test_revision_plugin.py b/neutron/tests/unit/services/revisions/test_revision_plugin.py index 6641133ba54..8ae6315785c 100644 --- a/neutron/tests/unit/services/revisions/test_revision_plugin.py +++ b/neutron/tests/unit/services/revisions/test_revision_plugin.py @@ -15,6 +15,7 @@ import netaddr from neutron_lib import context as nctx +from neutron_lib.db import api as db_api from neutron_lib.plugins import constants from neutron_lib.plugins import directory from oslo_config import cfg @@ -23,7 +24,6 @@ from oslo_utils import uuidutils from sqlalchemy.orm import session as se from webob import exc -from neutron.db import api as db_api from neutron.db import models_v2 from neutron.objects import ports as port_obj from neutron.tests.unit.plugins.ml2 import test_plugin