Use registry.receives decorator in neutron.db.dvr_mac_db

Switch to registry.receives decorator in neutron.db.dvr_mac_db.

Change-Id: I7f1426adabe1d97b8df11d561454972354c23b14
Partial-Bug: #1668262
This commit is contained in:
Cedric Brandily 2017-02-28 18:45:56 +01:00
parent 792afbfab2
commit e3cd8e9bdf
1 changed files with 26 additions and 31 deletions

View File

@ -54,40 +54,10 @@ dvr_mac_address_opts = [
cfg.CONF.register_opts(dvr_mac_address_opts)
@db_api.retry_if_session_inactive()
def _delete_mac_associated_with_agent(resource, event, trigger, context, agent,
**kwargs):
host = agent['host']
plugin = directory.get_plugin()
if [a for a in plugin.get_agents(context, filters={'host': [host]})
if a['id'] != agent['id']]:
# there are still agents on this host, don't mess with the mac entry
# until they are all deleted.
return
try:
with db_api.context_manager.writer.using(context):
entry = (context.session.query(
dvr_models.DistributedVirtualRouterMacAddress).
filter(
dvr_models.DistributedVirtualRouterMacAddress.host ==
host).
one())
context.session.delete(entry)
except exc.NoResultFound:
return
# notify remaining agents so they cleanup flows
dvr_macs = plugin.get_dvr_mac_address_list(context)
plugin.notifier.dvr_mac_address_update(context, dvr_macs)
@registry.has_registry_receivers
class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
"""Mixin class to add dvr mac address to db_plugin_base_v2."""
def __new__(cls, *args, **kwargs):
registry.subscribe(_delete_mac_associated_with_agent,
resources.AGENT, events.BEFORE_DELETE)
return super(DVRDbMixin, cls).__new__(cls)
@property
def plugin(self):
try:
@ -98,6 +68,31 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
self._plugin = directory.get_plugin()
return self._plugin
@staticmethod
@registry.receives(resources.AGENT, [events.BEFORE_DELETE])
@db_api.retry_if_session_inactive()
def _delete_mac_associated_with_agent(resource, event, trigger, context,
agent, **kwargs):
host = agent['host']
plugin = directory.get_plugin()
if [a for a in plugin.get_agents(context, filters={'host': [host]})
if a['id'] != agent['id']]:
# there are still agents on this host, don't mess with the mac
# entry until they are all deleted.
return
try:
with db_api.context_manager.writer.using(context):
entry = (context.session.
query(dvr_models.DistributedVirtualRouterMacAddress).
filter_by(host=host).
one())
context.session.delete(entry)
except exc.NoResultFound:
return
# notify remaining agents so they cleanup flows
dvr_macs = plugin.get_dvr_mac_address_list(context)
plugin.notifier.dvr_mac_address_update(context, dvr_macs)
@db_api.context_manager.reader
def _get_dvr_mac_address_by_host(self, context, host):
try: