From 53de19cc3cfd65b8650287ca49b2933494aa1e57 Mon Sep 17 00:00:00 2001 From: jufeng Date: Fri, 29 Sep 2017 16:38:24 +0800 Subject: [PATCH] Fix unnecessary security_groups_member_updated notification notify_sg_on_port_change will cause unnecessary notification to neutron-l2-agents when port update, for example like port's name updating will also notify. notify_sg_on_port_change will only be needed when port's Security Group update or port IP change. Change-Id: I5439adf2c4b7dcf832241201fd949f6930e65fdf Closes-Bug: #1720322 (cherry picked from commit 530d97141acb63dfe02566bda0a7e78fb058b6b0) --- neutron/db/securitygroups_rpc_base.py | 7 ++++++- neutron/plugins/ml2/plugin.py | 2 -- neutron/tests/unit/plugins/ml2/test_plugin.py | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/neutron/db/securitygroups_rpc_base.py b/neutron/db/securitygroups_rpc_base.py index 704a405d76c..f1da678b1e7 100644 --- a/neutron/db/securitygroups_rpc_base.py +++ b/neutron/db/securitygroups_rpc_base.py @@ -45,7 +45,12 @@ class SecurityGroupServerNotifierRpcMixin(sg_db.SecurityGroupDbMixin): def notify_sg_on_port_change(self, resource, event, trigger, context, port, *args, **kwargs): """Trigger notification to other SG members on port changes.""" - self.notify_security_groups_member_updated(context, port) + if event == events.AFTER_UPDATE: + original_port = kwargs.get('original_port') + self.check_and_notify_security_group_member_changed( + context, original_port, port) + else: + self.notify_security_groups_member_updated(context, port) def create_security_group_rule(self, context, security_group_rule): rule = super(SecurityGroupServerNotifierRpcMixin, diff --git a/neutron/plugins/ml2/plugin.py b/neutron/plugins/ml2/plugin.py index 5d927936b86..31ad010a061 100644 --- a/neutron/plugins/ml2/plugin.py +++ b/neutron/plugins/ml2/plugin.py @@ -1487,8 +1487,6 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2, LOG.error("mechanism_manager.update_port_postcommit " "failed for port %s", id) - self.check_and_notify_security_group_member_changed( - context, original_port, updated_port) need_port_update_notify |= self.is_security_group_member_updated( context, original_port, updated_port) diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index 2893bebb15d..3f042085c08 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -1027,6 +1027,18 @@ class TestMl2PortsV2(test_plugin.TestPortsV2, Ml2PluginV2TestCase): plugin.update_port(ctx, port['port']['id'], port) self.assertTrue(sg_member_update.called) + def test_update_port_name_do_not_notify_sg(self): + ctx = context.get_admin_context() + plugin = directory.get_plugin() + port_name = "port_name" + with self.port(name=port_name) as port,\ + mock.patch.object( + plugin.notifier, + 'security_groups_member_updated') as sg_member_update: + port['port']['name'] = 'new_port_name' + plugin.update_port(ctx, port['port']['id'], port) + self.assertFalse(sg_member_update.called) + def test_update_port_status_with_network(self): registry.clear() # don't care about callback behavior ctx = context.get_admin_context()