From eedbcdd03b04db7976606b5d12c1919b073f965e Mon Sep 17 00:00:00 2001 From: Boden R Date: Thu, 18 Jul 2019 15:35:32 -0600 Subject: [PATCH] use payloads for ROUTER_GATEWAY events This patch switches the code over to the payload style of callbacks [1] for PORT ROUTER_GATEWAY events for those that are not using them yet. The unit tests are also updated where needed to account for the payload style callbacks and publish() method. Finally the patch normalizes the passing of gateway IPs which are currently referred to as 'gw_ips' and 'gateway_ips' depending on the event; now all events use 'gateway_ips'. [1] https://docs.openstack.org/neutron-lib/latest/contributor/callbacks.html Change-Id: Ibc255de79443e908cc3615a8e1cb108757f80011 --- vmware_nsx/plugins/common/plugin.py | 6 +++--- vmware_nsx/plugins/nsx_v/plugin.py | 17 +++++++++-------- .../services/dynamic_routing/bgp_plugin.py | 14 +++++++------- .../services/vpnaas/nsxv3/ipsec_driver.py | 4 ++-- vmware_nsx/tests/unit/nsx_p/test_plugin.py | 7 +++---- vmware_nsx/tests/unit/nsx_v/test_plugin.py | 7 +++---- vmware_nsx/tests/unit/nsx_v3/test_plugin.py | 7 +++---- 7 files changed, 30 insertions(+), 32 deletions(-) diff --git a/vmware_nsx/plugins/common/plugin.py b/vmware_nsx/plugins/common/plugin.py index a9b577597b..28ff8a969b 100644 --- a/vmware_nsx/plugins/common/plugin.py +++ b/vmware_nsx/plugins/common/plugin.py @@ -449,9 +449,9 @@ class NsxPluginBase(db_base_plugin_v2.NeutronDbPluginV2, # Register the callback -def _validate_network_has_subnet(resource, event, trigger, **kwargs): - network_id = kwargs.get('network_id') - subnets = kwargs.get('subnets') +def _validate_network_has_subnet(resource, event, trigger, payload=None): + network_id = payload.metadata.get('network_id') + subnets = payload.metadata.get('subnets') if not subnets: msg = _('No subnet defined on network %s') % network_id raise n_exc.InvalidInput(error_message=msg) diff --git a/vmware_nsx/plugins/nsx_v/plugin.py b/vmware_nsx/plugins/nsx_v/plugin.py index 0987c1f079..9327fc0d52 100644 --- a/vmware_nsx/plugins/nsx_v/plugin.py +++ b/vmware_nsx/plugins/nsx_v/plugin.py @@ -3760,14 +3760,15 @@ class NsxVPluginV2(addr_pair_db.AllowedAddressPairsMixin, context, router.gw_port['id'], {'port': port_data}) self._extension_manager.process_update_port( context, port_data, updated_port) - registry.notify(resources.ROUTER_GATEWAY, - events.AFTER_UPDATE, - self._update_current_gw_port, - context=context, - router_id=router_id, - router=router, - network_id=router.gw_port.network_id, - updated_port=updated_port) + registry.publish(resources.ROUTER_GATEWAY, + events.AFTER_UPDATE, + self._update_current_gw_port, + payload=events.DBEventPayload( + context, resource_id=router_id, + states=(router,), metadata={ + 'network_id': router.gw_port.network_id, + 'updated_port': updated_port + })) context.session.expire(router.gw_port) def _update_router_gw_info(self, context, router_id, info, diff --git a/vmware_nsx/services/dynamic_routing/bgp_plugin.py b/vmware_nsx/services/dynamic_routing/bgp_plugin.py index 1578c83827..4cf6393ba2 100644 --- a/vmware_nsx/services/dynamic_routing/bgp_plugin.py +++ b/vmware_nsx/services/dynamic_routing/bgp_plugin.py @@ -300,11 +300,11 @@ class NSXBgpPlugin(service_base.ServicePluginBase, bgp_db.BgpDbMixin): driver.withdraw_subnet(context, speaker_id, router_id, subnet_id) - def router_gateway_callback(self, resource, event, trigger, **kwargs): - context = kwargs.get('context') or n_context.get_admin_context() + def router_gateway_callback(self, resource, event, trigger, payload=None): + context = payload.context or n_context.get_admin_context() context = context.elevated() - router_id = kwargs['router_id'] - network_id = kwargs['network_id'] + router_id = payload.resource_id + network_id = payload.metadata['network_id'] speakers = self._bgp_speakers_for_gateway_network(context, network_id) for speaker in speakers: @@ -316,14 +316,14 @@ class NSXBgpPlugin(service_base.ServicePluginBase, bgp_db.BgpDbMixin): if network_id not in speaker['networks']: continue if event == events.AFTER_DELETE: - gw_ips = kwargs['gateway_ips'] + gw_ips = payload.metadata['gateway_ips'] driver.disable_bgp_on_router(context, speaker, router_id, gw_ips[0]) if event == events.AFTER_UPDATE: - updated_port = kwargs['updated_port'] - router = kwargs['router'] + updated_port = payload.metadata['updated_port'] + router = payload.latest_state driver.process_router_gw_port_update( context, speaker, router, updated_port) diff --git a/vmware_nsx/services/vpnaas/nsxv3/ipsec_driver.py b/vmware_nsx/services/vpnaas/nsxv3/ipsec_driver.py index d4ffeae37f..005cf9b65c 100644 --- a/vmware_nsx/services/vpnaas/nsxv3/ipsec_driver.py +++ b/vmware_nsx/services/vpnaas/nsxv3/ipsec_driver.py @@ -339,9 +339,9 @@ class NSXv3IPsecVpnDriver(common_driver.NSXcommonIPsecVpnDriver): self.l3_plugin.delete_port(context, port['id'], force_delete_vpn=True) - def _delete_local_endpoint(self, resource, event, trigger, **kwargs): + def _delete_local_endpoint(self, resource, event, trigger, payload=None): """Upon router deletion / gw removal delete the matching endpoint""" - router_id = kwargs.get('router_id') + router_id = payload.resource_id ctx = n_context.get_admin_context() self._delete_local_endpoint_by_router(ctx, router_id) diff --git a/vmware_nsx/tests/unit/nsx_p/test_plugin.py b/vmware_nsx/tests/unit/nsx_p/test_plugin.py index cc16a05533..e4cfc25f01 100644 --- a/vmware_nsx/tests/unit/nsx_p/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_p/test_plugin.py @@ -1965,15 +1965,14 @@ class NsxPTestL3NatTestCase(NsxPTestL3NatTest, with self.router() as r,\ self._create_l3_ext_network() as ext_net,\ self.subnet(network=ext_net, enable_dhcp=False): - with mock.patch.object(registry, 'notify') as notify: + with mock.patch.object(registry, 'publish') as publish: self._add_external_gateway_to_router( r['router']['id'], ext_net['network']['id']) expected = [mock.call( resources.ROUTER_GATEWAY, events.AFTER_CREATE, mock.ANY, - context=mock.ANY, gw_ips=mock.ANY, - network_id=mock.ANY, router_id=mock.ANY)] - notify.assert_has_calls(expected) + payload=mock.ANY)] + publish.assert_has_calls(expected) def test_router_add_gateway_no_subnet_forbidden(self): with self.router() as r: diff --git a/vmware_nsx/tests/unit/nsx_v/test_plugin.py b/vmware_nsx/tests/unit/nsx_v/test_plugin.py index ae31923951..c559d9a20d 100644 --- a/vmware_nsx/tests/unit/nsx_v/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v/test_plugin.py @@ -2779,15 +2779,14 @@ class L3NatTestCaseBase(test_l3_plugin.L3NatTestCaseMixin): with self.router() as r,\ self._create_l3_ext_network() as ext_net,\ self.subnet(network=ext_net): - with mock.patch.object(registry, 'notify') as notify: + with mock.patch.object(registry, 'publish') as publish: self._add_external_gateway_to_router( r['router']['id'], ext_net['network']['id']) expected = [mock.call( resources.ROUTER_GATEWAY, events.AFTER_CREATE, mock.ANY, - context=mock.ANY, gw_ips=mock.ANY, - network_id=mock.ANY, router_id=mock.ANY)] - notify.assert_has_calls(expected) + payload=mock.ANY)] + publish.assert_has_calls(expected) def test_router_delete_ipv6_slaac_subnet_inuse_returns_409(self): self.skipTest('No DHCP v6 Support yet') diff --git a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py index 260f2d0d20..f1367c47c0 100644 --- a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py @@ -2288,15 +2288,14 @@ class TestL3NatTestCase(L3NatTest, with self.router() as r,\ self._create_l3_ext_network() as ext_net,\ self.subnet(network=ext_net): - with mock.patch.object(registry, 'notify') as notify: + with mock.patch.object(registry, 'publish') as publish: self._add_external_gateway_to_router( r['router']['id'], ext_net['network']['id']) expected = [mock.call( resources.ROUTER_GATEWAY, events.AFTER_CREATE, mock.ANY, - context=mock.ANY, gw_ips=mock.ANY, - network_id=mock.ANY, router_id=mock.ANY)] - notify.assert_has_calls(expected) + payload=mock.ANY)] + publish.assert_has_calls(expected) def test_create_l3_ext_network_with_default_tier0(self): self._test_create_l3_ext_network()