From 019690702456da9606800a4faee14015bec6136e Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Fri, 21 Nov 2014 20:56:04 +0000 Subject: [PATCH] Drop RpcProxy usage from mlnx plugin This patch drops usage of the RpcProxy compatibility class from the mlnx plugin. The equivalent oslo.messaging APIs are now used instead. Note that one test case was dropped, but only because it did not provide any actual additional test coverage. Also note that the base rpc version is set to '1.0' instead of '1.1' as it was in the original code. Setting the base to X.0 is the right pattern. Methods that require a newer version should specify it in their implementation. According to the API history comment here, the methods in this class were not the ones changed in '1.1', so they don't specify '1.1' as a requirement. Part of blueprint drop-rpc-compat. Change-Id: Ia8174b3e3415aa1e8588d908117e79a604e63881 --- neutron/plugins/mlnx/agent_notify_api.py | 23 +++--- neutron/tests/unit/mlnx/test_rpcapi.py | 93 ++++-------------------- 2 files changed, 26 insertions(+), 90 deletions(-) diff --git a/neutron/plugins/mlnx/agent_notify_api.py b/neutron/plugins/mlnx/agent_notify_api.py index b17ad7e6b..28cb9b746 100644 --- a/neutron/plugins/mlnx/agent_notify_api.py +++ b/neutron/plugins/mlnx/agent_notify_api.py @@ -12,7 +12,9 @@ # implied. # See the License for the specific language governing permissions and # limitations under the License. + from oslo.config import cfg +from oslo import messaging from neutron.agent import securitygroups_rpc as sg_rpc from neutron.common import rpc as n_rpc @@ -22,8 +24,7 @@ from neutron.openstack.common import log as logging LOG = logging.getLogger(__name__) -class AgentNotifierApi(n_rpc.RpcProxy, - sg_rpc.SecurityGroupAgentRpcApiMixin): +class AgentNotifierApi(sg_rpc.SecurityGroupAgentRpcApiMixin): """Agent side of the Embedded Switch RPC API. API version history: @@ -31,11 +32,7 @@ class AgentNotifierApi(n_rpc.RpcProxy, 1.1 - Added get_active_networks_info, create_dhcp_port, and update_dhcp_port methods. """ - BASE_RPC_API_VERSION = '1.1' - def __init__(self, topic): - super(AgentNotifierApi, self).__init__( - topic=topic, default_version=self.BASE_RPC_API_VERSION) self.topic = topic self.topic_network_delete = topics.get_topic_name(topic, topics.NETWORK, @@ -43,13 +40,14 @@ class AgentNotifierApi(n_rpc.RpcProxy, self.topic_port_update = topics.get_topic_name(topic, topics.PORT, topics.UPDATE) + target = messaging.Target(topic=topic, version='1.0') + self.client = n_rpc.get_client(target) def network_delete(self, context, network_id): LOG.debug(_("Sending delete network message")) - self.fanout_cast(context, - self.make_msg('network_delete', - network_id=network_id), - topic=self.topic_network_delete) + cctxt = self.client.prepare(topic=self.topic_network_delete, + fanout=True) + cctxt.cast(context, 'network_delete', network_id=network_id) def port_update(self, context, port, physical_network, network_type, vlan_id): @@ -60,6 +58,5 @@ class AgentNotifierApi(n_rpc.RpcProxy, 'segmentation_id': vlan_id} if cfg.CONF.AGENT.rpc_support_old_agents: kwargs['vlan_id'] = vlan_id - msg = self.make_msg('port_update', **kwargs) - self.fanout_cast(context, msg, - topic=self.topic_port_update) + cctxt = self.client.prepare(topic=self.topic_port_update, fanout=True) + cctxt.cast(context, 'port_update', **kwargs) diff --git a/neutron/tests/unit/mlnx/test_rpcapi.py b/neutron/tests/unit/mlnx/test_rpcapi.py index 4a39cb6ec..7f1880d8b 100644 --- a/neutron/tests/unit/mlnx/test_rpcapi.py +++ b/neutron/tests/unit/mlnx/test_rpcapi.py @@ -20,7 +20,6 @@ Unit Tests for Mellanox RPC (major reuse of linuxbridge rpc unit tests) import contextlib import mock -import fixtures from oslo.config import cfg from neutron.agent import rpc as agent_rpc @@ -32,47 +31,11 @@ from neutron.tests import base class rpcApiTestCase(base.BaseTestCase): - def _test_mlnx_api_legacy(self, rpcapi, topic, method, rpc_method, - expected_msg=None, **kwargs): - # NOTE(russellb) This method can be removed once the AgentNotifierApi - # has been converted to no longer use the RpcProxy class. - ctxt = context.RequestContext('fake_user', 'fake_project') - expected_retval = 'foo' if rpc_method == 'call' else None - expected_kwargs = {} - if topic: - expected_kwargs['topic'] = topic - if 'version' in kwargs: - expected_kwargs['version'] = kwargs.pop('version') - if not expected_msg: - expected_msg = rpcapi.make_msg(method, **kwargs) - - self.fake_args = None - self.fake_kwargs = None - - def _fake_rpc_method(*args, **kwargs): - self.fake_args = args - self.fake_kwargs = kwargs - if expected_retval: - return expected_retval - - self.useFixture(fixtures.MonkeyPatch( - 'neutron.common.rpc.RpcProxy.' + rpc_method, - _fake_rpc_method)) - - retval = getattr(rpcapi, method)(ctxt, **kwargs) - - self.assertEqual(expected_retval, retval) - expected_args = [ctxt, expected_msg] - - # skip the first argument which is 'self' - for arg, expected_arg in zip(self.fake_args[1:], expected_args): - self.assertEqual(expected_arg, arg) - self.assertEqual(expected_kwargs, self.fake_kwargs) - def _test_mlnx_api(self, rpcapi, topic, method, rpc_method, **kwargs): ctxt = context.RequestContext('fake_user', 'fake_project') expected_retval = 'foo' if rpc_method == 'call' else None expected_version = kwargs.pop('version', None) + fanout = kwargs.pop('fanout', False) with contextlib.nested( mock.patch.object(rpcapi.client, rpc_method), @@ -87,77 +50,53 @@ class rpcApiTestCase(base.BaseTestCase): prepare_args = {} if expected_version: prepare_args['version'] = expected_version + if fanout: + prepare_args['fanout'] = True + if topic: + prepare_args['topic'] = topic prepare_mock.assert_called_once_with(**prepare_args) + if method == 'port_update': + kwargs['segmentation_id'] = kwargs['vlan_id'] + if not cfg.CONF.AGENT.rpc_support_old_agents: + del kwargs['vlan_id'] + self.assertEqual(retval, expected_retval) rpc_mock.assert_called_once_with(ctxt, method, **kwargs) def test_delete_network(self): rpcapi = agent_notify_api.AgentNotifierApi(topics.AGENT) - self._test_mlnx_api_legacy( + self._test_mlnx_api( rpcapi, topics.get_topic_name(topics.AGENT, topics.NETWORK, topics.DELETE), - 'network_delete', rpc_method='fanout_cast', + 'network_delete', rpc_method='cast', fanout=True, network_id='fake_request_spec') def test_port_update(self): cfg.CONF.set_override('rpc_support_old_agents', False, 'AGENT') rpcapi = agent_notify_api.AgentNotifierApi(topics.AGENT) - expected_msg = rpcapi.make_msg('port_update', - port='fake_port', - network_type='vlan', - physical_network='fake_net', - segmentation_id='fake_vlan_id') - self._test_mlnx_api_legacy( + self._test_mlnx_api( rpcapi, topics.get_topic_name(topics.AGENT, topics.PORT, topics.UPDATE), - 'port_update', rpc_method='fanout_cast', - expected_msg=expected_msg, + 'port_update', rpc_method='cast', fanout=True, port='fake_port', network_type='vlan', physical_network='fake_net', vlan_id='fake_vlan_id') - def test_port_update_ib(self): - cfg.CONF.set_override('rpc_support_old_agents', False, 'AGENT') - rpcapi = agent_notify_api.AgentNotifierApi(topics.AGENT) - expected_msg = rpcapi.make_msg('port_update', - port='fake_port', - network_type='ib', - physical_network='fake_net', - segmentation_id='fake_vlan_id') - self._test_mlnx_api_legacy( - rpcapi, - topics.get_topic_name(topics.AGENT, - topics.PORT, - topics.UPDATE), - 'port_update', rpc_method='fanout_cast', - expected_msg=expected_msg, - port='fake_port', - network_type='ib', - physical_network='fake_net', - vlan_id='fake_vlan_id') - def test_port_update_old_agent(self): cfg.CONF.set_override('rpc_support_old_agents', True, 'AGENT') rpcapi = agent_notify_api.AgentNotifierApi(topics.AGENT) - expected_msg = rpcapi.make_msg('port_update', - port='fake_port', - network_type='vlan', - physical_network='fake_net', - segmentation_id='fake_vlan_id', - vlan_id='fake_vlan_id') - self._test_mlnx_api_legacy( + self._test_mlnx_api( rpcapi, topics.get_topic_name(topics.AGENT, topics.PORT, topics.UPDATE), - 'port_update', rpc_method='fanout_cast', - expected_msg=expected_msg, + 'port_update', rpc_method='cast', fanout=True, port='fake_port', network_type='vlan', physical_network='fake_net', -- 2.45.2