From b8b8788d23de525cfd5f446d5902435957ffa472 Mon Sep 17 00:00:00 2001 From: elajkat Date: Mon, 30 Aug 2021 09:17:15 +0200 Subject: [PATCH] Use payload for PORT and FLOATING_IP events * Updated callback functions port_callback, floatingip_update_callback to include payload * Fixed unit test cases Change-Id: I84956d86ea45039a8841b2b1f4f02bf373efcef5 Signed-off-by: Manu B --- .../services/bgp/bgp_plugin.py | 23 +++++++++++-------- .../unit/services/bgp/test_bgp_plugin.py | 12 ++++++---- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/neutron_dynamic_routing/services/bgp/bgp_plugin.py b/neutron_dynamic_routing/services/bgp/bgp_plugin.py index be325d4a..b726a44e 100644 --- a/neutron_dynamic_routing/services/bgp/bgp_plugin.py +++ b/neutron_dynamic_routing/services/bgp/bgp_plugin.py @@ -77,6 +77,9 @@ class BgpPlugin(service_base.ServicePluginBase, self.conn.consume_in_threads() def _register_callbacks(self): + registry.subscribe(self.floatingip_update_callback, + resources.FLOATING_IP, + events.AFTER_CREATE) registry.subscribe(self.floatingip_update_callback, resources.FLOATING_IP, events.AFTER_UPDATE) @@ -224,18 +227,18 @@ class BgpPlugin(service_base.ServicePluginBase, return super(BgpPlugin, self).get_advertised_routes(context, bgp_speaker_id) - def floatingip_update_callback(self, resource, event, trigger, **kwargs): - if event != events.AFTER_UPDATE: + def floatingip_update_callback(self, resource, event, trigger, payload): + if event not in [events.AFTER_CREATE, events.AFTER_UPDATE]: return - ctx = context.get_admin_context() - new_router_id = kwargs['router_id'] - last_router_id = kwargs.get('last_known_router_id') - floating_ip_address = kwargs['floating_ip_address'] + fip = payload.latest_state + new_router_id = fip['router_id'] + last_router_id = fip.get('last_known_router_id') + floating_ip_address = fip['floating_ip_address'] dest = str(floating_ip_address) + '/32' bgp_speakers = self._bgp_speakers_for_gw_network_by_family( ctx, - kwargs['floating_network_id'], + fip['floating_network_id'], n_const.IP_VERSION_4) if last_router_id and new_router_id != last_router_id: @@ -348,12 +351,12 @@ class BgpPlugin(service_base.ServicePluginBase, self.stop_route_advertisements(ctx, self._bgp_rpc, speaker.id, routes) - def port_callback(self, resource, event, trigger, **kwargs): + def port_callback(self, resource, event, trigger, payload): if event != events.AFTER_UPDATE: return - original_port = kwargs['original_port'] - updated_port = kwargs['port'] + original_port = payload.states[0] + updated_port = payload.latest_state if not updated_port.get('fixed_ips'): return diff --git a/neutron_dynamic_routing/tests/unit/services/bgp/test_bgp_plugin.py b/neutron_dynamic_routing/tests/unit/services/bgp/test_bgp_plugin.py index b2cb64a4..f5bdeee3 100644 --- a/neutron_dynamic_routing/tests/unit/services/bgp/test_bgp_plugin.py +++ b/neutron_dynamic_routing/tests/unit/services/bgp/test_bgp_plugin.py @@ -56,6 +56,8 @@ class TestBgpPlugin(base.BaseTestCase): expected_calls = [ mock.call(plugin.bgp_drscheduler.schedule_bgp_speaker_callback, dr_resources.BGP_SPEAKER, events.AFTER_CREATE), + mock.call(plugin.floatingip_update_callback, + resources.FLOATING_IP, events.AFTER_CREATE), mock.call(plugin.floatingip_update_callback, resources.FLOATING_IP, events.AFTER_UPDATE), mock.call(plugin.router_interface_callback, @@ -92,9 +94,9 @@ class TestBgpPlugin(base.BaseTestCase): payload=payload) def test_floatingip_update_callback(self): - kwargs = {'floating_ip_address': netaddr.IPAddress('10.10.10.10'), - 'last_known_router_id': 'old-router-id', - 'router_id': '', 'floating_network_id': 'a-b-c-d-e'} + fip = {'floating_ip_address': netaddr.IPAddress('10.10.10.10'), + 'last_known_router_id': 'old-router-id', + 'router_id': '', 'floating_network_id': 'a-b-c-d-e'} test_context = 'test_context' @@ -108,7 +110,9 @@ class TestBgpPlugin(base.BaseTestCase): get_bgp.return_value = [bgp_speaker] self.plugin.floatingip_update_callback( - test_context, events.AFTER_UPDATE, None, **kwargs) + test_context, events.AFTER_UPDATE, None, + payload=events.DBEventPayload( + test_context, states=(fip,))) get_bgp.assert_called_once_with(self.fake_admin_ctx, 'a-b-c-d-e',