Use payloads for ROUTER AFTER_ callbacks

This patch switches over to callback payloads for ROUTER
AFTER_CREATE, AFTER_UPDATE and AFTER_DELETE events.

Change-Id: Ie818ffbb1a291faa80501157b46ff6671d5c26ba
changes/03/803003/8
Nurmatov Mamatisa 2 years ago committed by Mamatisa Nurmatov
parent 98695ed845
commit ef83719da2

@ -553,7 +553,11 @@ class L3NATAgent(ha.AgentMixin,
LOG.debug("Router info %s delete action done, "
"and it was removed from cache.", router_id)
registry.notify(resources.ROUTER, events.AFTER_DELETE, self, router=ri)
registry.publish(resources.ROUTER, events.AFTER_DELETE, self,
payload=events.DBEventPayload(
self.context,
resource_id=router_id,
states=(ri,)))
def init_extension_manager(self, connection):
l3_ext_manager.register_opts(self.conf)
@ -642,7 +646,13 @@ class L3NATAgent(ha.AgentMixin,
ri = self.router_info[router['id']]
ri.router = router
ri.process()
registry.notify(resources.ROUTER, events.AFTER_CREATE, self, router=ri)
registry.publish(resources.ROUTER, events.AFTER_CREATE, self,
payload=events.DBEventPayload(
self.context,
resource_id=router['id'],
states=(ri,)))
self.l3_ext_manager.add_router(self.context, router)
def _process_updated_router(self, router):
@ -698,8 +708,11 @@ class L3NATAgent(ha.AgentMixin,
states=(ri,)))
ri.process()
registry.notify(
resources.ROUTER, events.AFTER_UPDATE, self, router=ri)
registry.publish(resources.ROUTER, events.AFTER_UPDATE, self,
payload=events.DBEventPayload(
self.context,
resource_id=router['id'],
states=(None, ri)))
self.l3_ext_manager.update_router(self.context, router)
def _resync_router(self, router_update,

@ -353,8 +353,8 @@ class PrefixDelegation(object):
@runtime.synchronized("l3-agent-pd")
def remove_router(resource, event, l3_agent, **kwargs):
router_id = kwargs['router'].router_id
def remove_router(resource, event, l3_agent, payload):
router_id = payload.resource_id
router = l3_agent.pd.routers.get(router_id)
l3_agent.pd.delete_router_pd(router)
del l3_agent.pd.routers[router_id]['subnets']
@ -384,8 +384,8 @@ def add_router(resource, event, l3_agent, payload):
@runtime.synchronized("l3-agent-pd")
def update_router(resource, event, l3_agent, **kwargs):
updated_router = kwargs['router']
def update_router(resource, event, l3_agent, payload):
updated_router = payload.latest_state
router = l3_agent.pd.routers.get(updated_router.router_id)
if not router:
LOG.exception("Router to be updated is not in internal routers "

@ -305,8 +305,8 @@ class MetadataDriver(object):
default_cmd_callback=callback)
def after_router_added(resource, event, l3_agent, **kwargs):
router = kwargs['router']
def after_router_added(resource, event, l3_agent, payload):
router = payload.latest_state
proxy = l3_agent.metadata_driver
apply_metadata_nat_rules(router, proxy)
if not isinstance(router, ha_router.HaRouter):
@ -322,8 +322,8 @@ def after_router_added(resource, event, l3_agent, **kwargs):
**spawn_kwargs)
def after_router_updated(resource, event, l3_agent, **kwargs):
router = kwargs['router']
def after_router_updated(resource, event, l3_agent, payload):
router = payload.latest_state
proxy = l3_agent.metadata_driver
if (not proxy.monitors.get(router.router_id) and
not isinstance(router, ha_router.HaRouter)):

@ -280,10 +280,14 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
delete, update_gw,
transaction=False)
new_router = self._make_router_dict(router_db)
registry.notify(resources.ROUTER, events.AFTER_CREATE, self,
context=context, router_id=router_db.id,
router=new_router, request_attrs=r,
router_db=router_db)
registry.publish(resources.ROUTER, events.AFTER_CREATE, self,
payload=events.DBEventPayload(
context,
resource_id=router_db.id,
metadata={'request_attrs': r,
'router_db': router_db},
states=(new_router,)))
return new_router
def _update_router_db(self, context, router_id, data):
@ -309,9 +313,14 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
self._update_router_gw_info(context, id, gw_info)
router_db = self._update_router_db(context, id, r)
updated = self._make_router_dict(router_db)
registry.notify(resources.ROUTER, events.AFTER_UPDATE, self,
context=context, router_id=id, old_router=original,
router=updated, request_attrs=r, router_db=router_db)
registry.publish(resources.ROUTER, events.AFTER_UPDATE, self,
payload=events.DBEventPayload(
context,
resource_id=id,
metadata={'request_attrs': r,
'router_db': router_db},
states=(original, updated)))
return updated
def _create_router_gw_port(self, context, router, network_id, ext_ips):
@ -582,8 +591,10 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
context.session.flush()
context.session.delete(router)
registry.notify(resources.ROUTER, events.AFTER_DELETE, self,
context=context, router_id=id, original=original)
registry.publish(resources.ROUTER, events.AFTER_DELETE, self,
payload=events.DBEventPayload(
context, resource_id=id,
states=(original,)))
@db_api.retry_if_session_inactive()
def get_router(self, context, id, fields=None):

@ -209,11 +209,12 @@ class DVRResourceOperationHandler(object):
@registry.receives(resources.ROUTER, [events.AFTER_UPDATE],
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)
def _delete_distributed_port_bindings_after_change(self, resource, event,
trigger, context,
router_id, router,
request_attrs,
router_db, **kwargs):
old_router = kwargs['old_router']
trigger, payload):
context = payload.context
router = payload.latest_state
router_db = payload.metadata['router_db']
old_router = payload.states[0] if (len(payload.states) > 1) else None
if (old_router and old_router['distributed'] and not
router['distributed']):
self._core_plugin.delete_distributed_port_bindings_by_router_id(
@ -222,12 +223,15 @@ class DVRResourceOperationHandler(object):
@registry.receives(resources.ROUTER, [events.AFTER_UPDATE],
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)
def _delete_snat_interfaces_after_change(self, resource, event, trigger,
context, router_id, router,
request_attrs, router_db,
**kwargs):
payload):
context = payload.context
router = payload.latest_state
router_db = payload.metadata['router_db']
if (router.get(l3_apidef.EXTERNAL_GW_INFO) and
not router['distributed']):
old_router = kwargs['old_router']
old_router = (payload.states[0] if (len(payload.states) > 1)
else None)
if old_router and old_router['distributed']:
self.delete_csnat_router_interface_ports(
context.elevated(), router_db)
@ -236,9 +240,11 @@ class DVRResourceOperationHandler(object):
[events.AFTER_CREATE, events.AFTER_UPDATE],
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)
def _create_snat_interfaces_after_change(self, resource, event, trigger,
context, router_id, router,
request_attrs, router_db,
**kwargs):
payload):
context = payload.context
router = payload.latest_state
router_db = payload.metadata['router_db']
if (not router.get(l3_apidef.EXTERNAL_GW_INFO) or
not router['distributed']):
# we don't care if it's not distributed or not attached to an
@ -247,7 +253,8 @@ class DVRResourceOperationHandler(object):
if event == events.AFTER_UPDATE:
# after an update, we check to see if it was a migration or a
# gateway attachment
old_router = kwargs['old_router']
old_router = (payload.states[0] if (len(payload.states) > 1)
else None)
do_create = (not old_router['distributed'] or
not old_router.get(l3_apidef.EXTERNAL_GW_INFO))
if not do_create:

@ -393,8 +393,11 @@ class L3_HA_NAT_db_mixin(l3_dvr_db.L3_NAT_with_dvr_db_mixin,
@registry.receives(resources.ROUTER, [events.AFTER_CREATE],
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)
def _after_router_create(self, resource, event, trigger, context,
router_id, router, router_db, **kwargs):
def _after_router_create(self, resource, event, trigger, payload):
context = payload.context
router_id = payload.resource_id
router = payload.latest_state
router_db = payload.metadata['router_db']
if not router['ha']:
return
try:
@ -460,10 +463,14 @@ class L3_HA_NAT_db_mixin(l3_dvr_db.L3_NAT_with_dvr_db_mixin,
@registry.receives(resources.ROUTER, [events.AFTER_UPDATE],
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)
def _reconfigure_ha_resources(self, resource, event, trigger, context,
router_id, old_router, router, router_db,
**kwargs):
def _reconfigure_ha_resources(self, resource, event, trigger, payload):
"""Event handler to react to changes after HA flag has been updated."""
context = payload.context
router_id = payload.resource_id
old_router = payload.states[0] if (len(payload.states) > 1) else None
router = payload.latest_state
router_db = payload.metadata['router_db']
ha_changed = old_router['ha'] != router['ha']
if not ha_changed:
return
@ -529,9 +536,16 @@ class L3_HA_NAT_db_mixin(l3_dvr_db.L3_NAT_with_dvr_db_mixin,
@registry.receives(resources.ROUTER, [events.AFTER_DELETE],
priority_group.PRIORITY_ROUTER_EXTENDED_ATTRIBUTE)
def _cleanup_ha_network_handler(self, resource, event, trigger, payload):
return self._cleanup_ha_network(event,
payload.context,
payload.resource_id,
payload.latest_state)
@db_api.retry_if_session_inactive()
def _cleanup_ha_network(self, resource, event, trigger, context,
router_id, original, **kwargs):
def _cleanup_ha_network(self, event, context,
router_id, original):
"""Event handler to attempt HA network deletion after router delete."""
if not original['ha']:
return

@ -66,11 +66,11 @@ class L3AgentTestCase(framework.L3AgentTestFramework):
expected_calls = [
mock.call('router', 'before_create', self.agent, payload=mock.ANY),
mock.call('router', 'after_create', self.agent, router=router),
mock.call('router', 'after_create', self.agent, payload=mock.ANY),
mock.call('router', 'before_update', self.agent, payload=mock.ANY),
mock.call('router', 'after_update', self.agent, router=router),
mock.call('router', 'after_update', self.agent, payload=mock.ANY),
mock.call('router', 'before_delete', self.agent, payload=mock.ANY),
mock.call('router', 'after_delete', self.agent, router=router)]
mock.call('router', 'after_delete', self.agent, payload=mock.ANY)]
event_handler.assert_has_calls(expected_calls)
def test_agent_notifications_for_router_events(self):

@ -32,7 +32,11 @@ class TestPrefixDelegation(tests_base.DietTestCase):
router_id = 1
l3_agent.pd.routers = {router_id:
pd.get_router_entry(None, True)}
pd.remove_router(None, None, l3_agent, router=FakeRouter(router_id))
pd.remove_router(None, None, l3_agent,
payload=events.DBEventPayload(
mock.ANY,
resource_id=router_id,
states=(FakeRouter(router_id),)))
self.assertTrue(l3_agent.pd.delete_router_pd.called)
self.assertEqual({}, l3_agent.pd.routers)
@ -46,7 +50,11 @@ class TestPrefixDelegation(tests_base.DietTestCase):
# clear namespace name, update entry
pd_router['ns_name'] = None
pd.update_router(None, None, l3_agent, router=router)
pd.update_router(None, None, l3_agent,
payload=events.DBEventPayload(
mock.ANY,
resource_id=router.router_id,
states=(router,)))
pd_router = l3_agent.pd.routers.get(router.router_id)
self.assertEqual(ns_name, pd_router.get('ns_name'))
@ -99,7 +107,12 @@ class TestPrefixDelegation(tests_base.DietTestCase):
router.router_id = '1'
with mock.patch.object(pd.LOG, 'exception') as log:
pd.update_router(None, None, l3_agent, router=router)
pd.update_router(None, None, l3_agent,
payload=events.DBEventPayload(
mock.ANY,
resource_id=router.router_id,
states=(router,)))
self.assertTrue(log.called)
def test_remove_stale_ri_ifname(self):

@ -109,7 +109,12 @@ class TestMetadataDriverProcess(base.BaseTestCase):
agent.router_info[router_id] = ri
agent._process_updated_router(router)
f.assert_called_once_with(
'router', 'after_update', agent, router=ri)
'router', 'after_update', agent,
payload=mock.ANY)
payload = f.call_args_list[0][1]['payload']
self.assertEqual(ri, payload.latest_state)
self.assertEqual(router_id, payload.resource_id)
def test_after_router_updated_should_not_call_add_metadata_rules(self):
with mock.patch.object(iptables_manager.IptablesTable,

@ -69,8 +69,8 @@ class TestExtraRouteDb(testlib_api.SqlTestCase):
with mock.patch.object(self._plugin, '_validate_routes'):
updated_router = self._plugin.update_router(ctx, router_id,
update_request)
mock_cb.assert_called_with('router', events.PRECOMMIT_UPDATE,
self._plugin, payload=mock.ANY)
mock_cb.assert_any_call('router', events.PRECOMMIT_UPDATE,
self._plugin, payload=mock.ANY)
self.assertCountEqual(updated_router['routes'], routes)
got_router = self._plugin.get_router(ctx, router_id)
self.assertCountEqual(got_router['routes'], routes)

@ -1352,8 +1352,8 @@ class L3HAUserTestCase(L3HATestFramework):
router = self._create_router(ctx=self.user_ctx)
with mock.patch.object(registry, 'publish') as mock_cb:
self._update_router(router['id'], ctx=self.user_ctx)
mock_cb.assert_called_with('router', events.PRECOMMIT_UPDATE,
self.plugin, payload=mock.ANY)
mock_cb.assert_any_call('router', events.PRECOMMIT_UPDATE,
self.plugin, payload=mock.ANY)
def test_delete_router(self):
router = self._create_router(ctx=self.user_ctx)

Loading…
Cancel
Save