From 6a24efcb705d37c945bd5afc407b80b2c756a62f Mon Sep 17 00:00:00 2001 From: Boden R Date: Thu, 23 Aug 2018 11:59:43 -0600 Subject: [PATCH] use payloads for AGENT BEFORE_DELETE callbacks This patch switches BEFORE_DELETE callback events for AGENT resources over to the payload style args use a DBEventPayload object. In addition, the _delete_mac_associated_with_agent is now refactored into it's own method to allow the context parameter to be parsed by retry_if_session_inactive. NeutronLibImpact Change-Id: I0af951f4346f9290c2eba6aad17f772fbf27bb2f --- neutron/db/agents_db.py | 5 +++-- neutron/db/dvr_mac_db.py | 12 +++++++++--- neutron/tests/unit/db/test_dvr_mac_db.py | 10 ++++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/neutron/db/agents_db.py b/neutron/db/agents_db.py index 9552b04d4a0..5cfd949c7d7 100644 --- a/neutron/db/agents_db.py +++ b/neutron/db/agents_db.py @@ -266,8 +266,9 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin): @db_api.retry_if_session_inactive() def delete_agent(self, context, id): agent = self._get_agent(context, id) - registry.notify(resources.AGENT, events.BEFORE_DELETE, self, - context=context, agent=agent) + registry.publish(resources.AGENT, events.BEFORE_DELETE, self, + payload=events.DBEventPayload( + context, states=(agent,), resource_id=id)) agent.delete() @db_api.retry_if_session_inactive() diff --git a/neutron/db/dvr_mac_db.py b/neutron/db/dvr_mac_db.py index 275f5e6fdd8..257703162eb 100644 --- a/neutron/db/dvr_mac_db.py +++ b/neutron/db/dvr_mac_db.py @@ -59,10 +59,8 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase): 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): + def _db_delete_mac_associated_with_agent(context, agent): host = agent['host'] plugin = directory.get_plugin() if [a for a in plugin.get_agents(context, filters={'host': [host]}) @@ -78,6 +76,14 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase): dvr_macs = plugin.get_dvr_mac_address_list(context) plugin.notifier.dvr_mac_address_update(context, dvr_macs) + @staticmethod + @registry.receives(resources.AGENT, [events.BEFORE_DELETE]) + def _delete_mac_associated_with_agent(resource, event, + trigger, payload=None): + + DVRDbMixin._db_delete_mac_associated_with_agent( + payload.context, payload.latest_state) + @db_api.context_manager.reader def _get_dvr_mac_address_by_host(self, context, host): dvr_obj = router.DVRMacAddress.get_object(context, host=host) diff --git a/neutron/tests/unit/db/test_dvr_mac_db.py b/neutron/tests/unit/db/test_dvr_mac_db.py index 274c7cb0dee..5034d6c5fb2 100644 --- a/neutron/tests/unit/db/test_dvr_mac_db.py +++ b/neutron/tests/unit/db/test_dvr_mac_db.py @@ -94,8 +94,9 @@ class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase): agent2 = {'host': 'host_1', 'id': 'a2'} with mock.patch.object(plugin, 'get_agents', return_value=[agent2]): with mock.patch.object(plugin, 'notifier') as notifier: - registry.notify(resources.AGENT, events.BEFORE_DELETE, self, - context=self.ctx, agent=agent1) + registry.publish(resources.AGENT, events.BEFORE_DELETE, self, + payload=events.DBEventPayload( + self.ctx, states=(agent1,))) mac_list = self.mixin.get_dvr_mac_address_list(self.ctx) for mac in mac_list: self.assertIsInstance(mac, dict) @@ -110,8 +111,9 @@ class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase): self._create_dvr_mac_entry('host_2', mac_2) agent = {'host': 'host_1', 'id': 'a1'} with mock.patch.object(plugin, 'notifier') as notifier: - registry.notify(resources.AGENT, events.BEFORE_DELETE, self, - context=self.ctx, agent=agent) + registry.publish(resources.AGENT, events.BEFORE_DELETE, self, + payload=events.DBEventPayload( + self.ctx, states=(agent,))) mac_list = self.mixin.get_dvr_mac_address_list(self.ctx) self.assertEqual(1, len(mac_list)) for mac in mac_list: