Merge "Drop RpcProxy usage from MeteringAgentNotifyAPI"

This commit is contained in:
Jenkins 2014-11-28 22:22:12 +00:00 committed by Gerrit Code Review
commit 2e73eac6a8
2 changed files with 150 additions and 160 deletions

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from oslo import messaging
from neutron.common import constants from neutron.common import constants
from neutron.common import rpc as n_rpc from neutron.common import rpc as n_rpc
from neutron.common import topics from neutron.common import topics
@ -23,13 +25,13 @@ from neutron.plugins.common import constants as service_constants
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class MeteringAgentNotifyAPI(n_rpc.RpcProxy): class MeteringAgentNotifyAPI(object):
"""API for plugin to notify L3 metering agent.""" """API for plugin to notify L3 metering agent."""
BASE_RPC_API_VERSION = '1.0'
def __init__(self, topic=topics.METERING_AGENT): def __init__(self, topic=topics.METERING_AGENT):
super(MeteringAgentNotifyAPI, self).__init__( self.topic = topic
topic=topic, default_version=self.BASE_RPC_API_VERSION) target = messaging.Target(topic=topic, version='1.0')
self.client = n_rpc.get_client(target)
def _agent_notification(self, context, method, routers): def _agent_notification(self, context, method, routers):
"""Notify l3 metering agents hosted by l3 agent hosts.""" """Notify l3 metering agents hosted by l3 agent hosts."""
@ -55,8 +57,8 @@ class MeteringAgentNotifyAPI(n_rpc.RpcProxy):
l3_routers[l3_agent.host] = l3_router l3_routers[l3_agent.host] = l3_router
for host, routers in l3_routers.iteritems(): for host, routers in l3_routers.iteritems():
self.cast(context, self.make_msg(method, routers=routers), cctxt = self.client.prepare(server=host)
topic='%s.%s' % (self.topic, host)) cctxt.cast(context, method, routers=routers)
def _notification_fanout(self, context, method, router_id): def _notification_fanout(self, context, method, router_id):
LOG.debug('Fanout notify metering agent at %(topic)s the message ' LOG.debug('Fanout notify metering agent at %(topic)s the message '
@ -64,9 +66,8 @@ class MeteringAgentNotifyAPI(n_rpc.RpcProxy):
{'topic': self.topic, {'topic': self.topic,
'method': method, 'method': method,
'router_id': router_id}) 'router_id': router_id})
self.fanout_cast( cctxt = self.client.prepare(fanout=True)
context, self.make_msg(method, cctxt.cast(context, method, router_id=router_id)
router_id=router_id))
def _notification(self, context, method, routers): def _notification(self, context, method, routers):
"""Notify all the agents that are hosting the routers.""" """Notify all the agents that are hosting the routers."""
@ -76,7 +77,8 @@ class MeteringAgentNotifyAPI(n_rpc.RpcProxy):
plugin, constants.L3_AGENT_SCHEDULER_EXT_ALIAS): plugin, constants.L3_AGENT_SCHEDULER_EXT_ALIAS):
self._agent_notification(context, method, routers) self._agent_notification(context, method, routers)
else: else:
self.fanout_cast(context, self.make_msg(method, routers=routers)) cctxt = self.client.prepare(fanout=True)
cctxt.cast(context, method, routers=routers)
def router_deleted(self, context, router_id): def router_deleted(self, context, router_id):
self._notification_fanout(context, 'router_deleted', router_id) self._notification_fanout(context, 'router_deleted', router_id)

View File

@ -81,10 +81,6 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
self.uuid_patch = mock.patch(uuid, return_value=self.uuid) self.uuid_patch = mock.patch(uuid, return_value=self.uuid)
self.mock_uuid = self.uuid_patch.start() self.mock_uuid = self.uuid_patch.start()
fanout = ('neutron.common.rpc.RpcProxy.fanout_cast')
self.fanout_patch = mock.patch(fanout)
self.mock_fanout = self.fanout_patch.start()
self.tenant_id = 'a7e61382-47b8-4d40-bae3-f95981b5637b' self.tenant_id = 'a7e61382-47b8-4d40-bae3-f95981b5637b'
self.ctx = context.Context('', self.tenant_id, is_admin=True) self.ctx = context.Context('', self.tenant_id, is_admin=True)
self.context_patch = mock.patch('neutron.context.Context', self.context_patch = mock.patch('neutron.context.Context',
@ -93,9 +89,27 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
self.topic = 'metering_agent' self.topic = 'metering_agent'
add = ('neutron.api.rpc.agentnotifiers.' +
'metering_rpc_agent_api.MeteringAgentNotifyAPI' +
'.add_metering_label')
self.add_patch = mock.patch(add)
self.mock_add = self.add_patch.start()
remove = ('neutron.api.rpc.agentnotifiers.' +
'metering_rpc_agent_api.MeteringAgentNotifyAPI' +
'.remove_metering_label')
self.remove_patch = mock.patch(remove)
self.mock_remove = self.remove_patch.start()
update = ('neutron.api.rpc.agentnotifiers.' +
'metering_rpc_agent_api.MeteringAgentNotifyAPI' +
'.update_metering_label_rules')
self.update_patch = mock.patch(update)
self.mock_update = self.update_patch.start()
def test_add_metering_label_rpc_call(self): def test_add_metering_label_rpc_call(self):
second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84' second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84'
expected = {'args': {'routers': [{'status': 'ACTIVE', expected = [{'status': 'ACTIVE',
'name': 'router1', 'name': 'router1',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -103,9 +117,7 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
'_metering_labels': [ '_metering_labels': [
{'rules': [], {'rules': [],
'id': self.uuid}], 'id': self.uuid}],
'id': self.uuid}]}, 'id': self.uuid}]
'namespace': None,
'method': 'add_metering_label'}
tenant_id_2 = '8a268a58-1610-4890-87e0-07abb8231206' tenant_id_2 = '8a268a58-1610-4890-87e0-07abb8231206'
self.mock_uuid.return_value = second_uuid self.mock_uuid.return_value = second_uuid
@ -116,11 +128,11 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
set_context=True): set_context=True):
with self.metering_label(tenant_id=self.tenant_id, with self.metering_label(tenant_id=self.tenant_id,
set_context=True): set_context=True):
self.mock_fanout.assert_called_with(self.ctx, expected) self.mock_add.assert_called_with(self.ctx, expected)
def test_add_metering_label_shared_rpc_call(self): def test_add_metering_label_shared_rpc_call(self):
second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84' second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84'
expected = {'args': {'routers': [{'status': 'ACTIVE', expected = [{'status': 'ACTIVE',
'name': 'router1', 'name': 'router1',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -130,9 +142,7 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
'id': self.uuid}, 'id': self.uuid},
{'rules': [], {'rules': [],
'id': second_uuid}], 'id': second_uuid}],
'id': self.uuid}]}, 'id': self.uuid}]
'namespace': None,
'method': 'add_metering_label'}
tenant_id_2 = '8a268a58-1610-4890-87e0-07abb8231206' tenant_id_2 = '8a268a58-1610-4890-87e0-07abb8231206'
with self.router(name='router1', tenant_id=self.tenant_id, with self.router(name='router1', tenant_id=self.tenant_id,
@ -142,11 +152,10 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
self.mock_uuid.return_value = second_uuid self.mock_uuid.return_value = second_uuid
with self.metering_label(tenant_id=tenant_id_2, shared=True, with self.metering_label(tenant_id=tenant_id_2, shared=True,
set_context=True): set_context=True):
self.mock_fanout.assert_called_with(self.ctx, expected) self.mock_add.assert_called_with(self.ctx, expected)
def test_remove_metering_label_rpc_call(self): def test_remove_metering_label_rpc_call(self):
expected = {'args': expected = [{'status': 'ACTIVE',
{'routers': [{'status': 'ACTIVE',
'name': 'router1', 'name': 'router1',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -154,21 +163,17 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
'_metering_labels': [ '_metering_labels': [
{'rules': [], {'rules': [],
'id': self.uuid}], 'id': self.uuid}],
'id': self.uuid}]}, 'id': self.uuid}]
'namespace': None,
'method': 'add_metering_label'}
with self.router(tenant_id=self.tenant_id, set_context=True): with self.router(tenant_id=self.tenant_id, set_context=True):
with self.metering_label(tenant_id=self.tenant_id, with self.metering_label(tenant_id=self.tenant_id,
set_context=True): set_context=True):
self.mock_fanout.assert_called_with(self.ctx, expected) self.mock_add.assert_called_with(self.ctx, expected)
expected['method'] = 'remove_metering_label' self.mock_remove.assert_called_with(self.ctx, expected)
self.mock_fanout.assert_called_with(self.ctx, expected)
def test_remove_one_metering_label_rpc_call(self): def test_remove_one_metering_label_rpc_call(self):
second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84' second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84'
expected_add = {'args': expected_add = [{'status': 'ACTIVE',
{'routers': [{'status': 'ACTIVE',
'name': 'router1', 'name': 'router1',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -178,11 +183,8 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
'id': self.uuid}, 'id': self.uuid},
{'rules': [], {'rules': [],
'id': second_uuid}], 'id': second_uuid}],
'id': self.uuid}]}, 'id': self.uuid}]
'namespace': None, expected_remove = [{'status': 'ACTIVE',
'method': 'add_metering_label'}
expected_remove = {'args':
{'routers': [{'status': 'ACTIVE',
'name': 'router1', 'name': 'router1',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -190,9 +192,7 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
'_metering_labels': [ '_metering_labels': [
{'rules': [], {'rules': [],
'id': second_uuid}], 'id': second_uuid}],
'id': self.uuid}]}, 'id': self.uuid}]
'namespace': None,
'method': 'remove_metering_label'}
with self.router(tenant_id=self.tenant_id, set_context=True): with self.router(tenant_id=self.tenant_id, set_context=True):
with self.metering_label(tenant_id=self.tenant_id, with self.metering_label(tenant_id=self.tenant_id,
@ -200,14 +200,12 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
self.mock_uuid.return_value = second_uuid self.mock_uuid.return_value = second_uuid
with self.metering_label(tenant_id=self.tenant_id, with self.metering_label(tenant_id=self.tenant_id,
set_context=True): set_context=True):
self.mock_fanout.assert_called_with(self.ctx, expected_add) self.mock_add.assert_called_with(self.ctx, expected_add)
self.mock_fanout.assert_called_with(self.ctx, expected_remove) self.mock_remove.assert_called_with(self.ctx, expected_remove)
def test_update_metering_label_rules_rpc_call(self): def test_update_metering_label_rules_rpc_call(self):
second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84' second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84'
expected_add = {'args': expected_add = [{'status': 'ACTIVE',
{'routers': [
{'status': 'ACTIVE',
'name': 'router1', 'name': 'router1',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -225,13 +223,9 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
'excluded': False, 'excluded': False,
'id': second_uuid}], 'id': second_uuid}],
'id': self.uuid}], 'id': self.uuid}],
'id': self.uuid}]}, 'id': self.uuid}]
'namespace': None,
'method': 'update_metering_label_rules'}
expected_del = {'args': expected_del = [{'status': 'ACTIVE',
{'routers': [
{'status': 'ACTIVE',
'name': 'router1', 'name': 'router1',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -244,9 +238,7 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
'excluded': False, 'excluded': False,
'id': self.uuid}], 'id': self.uuid}],
'id': self.uuid}], 'id': self.uuid}],
'id': self.uuid}]}, 'id': self.uuid}]
'namespace': None,
'method': 'update_metering_label_rules'}
with self.router(tenant_id=self.tenant_id, set_context=True): with self.router(tenant_id=self.tenant_id, set_context=True):
with self.metering_label(tenant_id=self.tenant_id, with self.metering_label(tenant_id=self.tenant_id,
@ -255,9 +247,9 @@ class TestMeteringPlugin(test_db_plugin.NeutronDbPluginV2TestCase,
with self.metering_label_rule(l['id']): with self.metering_label_rule(l['id']):
self.mock_uuid.return_value = second_uuid self.mock_uuid.return_value = second_uuid
with self.metering_label_rule(l['id'], direction='egress'): with self.metering_label_rule(l['id'], direction='egress'):
self.mock_fanout.assert_called_with(self.ctx, self.mock_update.assert_called_with(self.ctx,
expected_add) expected_add)
self.mock_fanout.assert_called_with(self.ctx, self.mock_update.assert_called_with(self.ctx,
expected_del) expected_del)
def test_delete_metering_label_does_not_clear_router_tenant_id(self): def test_delete_metering_label_does_not_clear_router_tenant_id(self):
@ -307,10 +299,6 @@ class TestMeteringPluginL3AgentScheduler(
self.uuid_patch = mock.patch(uuid, return_value=self.uuid) self.uuid_patch = mock.patch(uuid, return_value=self.uuid)
self.mock_uuid = self.uuid_patch.start() self.mock_uuid = self.uuid_patch.start()
cast = 'neutron.common.rpc.RpcProxy.cast'
self.cast_patch = mock.patch(cast)
self.mock_cast = self.cast_patch.start()
self.tenant_id = 'a7e61382-47b8-4d40-bae3-f95981b5637b' self.tenant_id = 'a7e61382-47b8-4d40-bae3-f95981b5637b'
self.ctx = context.Context('', self.tenant_id, is_admin=True) self.ctx = context.Context('', self.tenant_id, is_admin=True)
self.context_patch = mock.patch('neutron.context.Context', self.context_patch = mock.patch('neutron.context.Context',
@ -323,9 +311,21 @@ class TestMeteringPluginL3AgentScheduler(
self.topic = 'metering_agent' self.topic = 'metering_agent'
add = ('neutron.api.rpc.agentnotifiers.' +
'metering_rpc_agent_api.MeteringAgentNotifyAPI' +
'.add_metering_label')
self.add_patch = mock.patch(add)
self.mock_add = self.add_patch.start()
remove = ('neutron.api.rpc.agentnotifiers.' +
'metering_rpc_agent_api.MeteringAgentNotifyAPI' +
'.remove_metering_label')
self.remove_patch = mock.patch(remove)
self.mock_remove = self.remove_patch.start()
def test_add_metering_label_rpc_call(self): def test_add_metering_label_rpc_call(self):
second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84' second_uuid = 'e27fe2df-376e-4ac7-ae13-92f050a21f84'
expected1 = {'args': {'routers': [{'status': 'ACTIVE', expected = [{'status': 'ACTIVE',
'name': 'router1', 'name': 'router1',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -333,10 +333,8 @@ class TestMeteringPluginL3AgentScheduler(
'_metering_labels': [ '_metering_labels': [
{'rules': [], {'rules': [],
'id': second_uuid}], 'id': second_uuid}],
'id': self.uuid}]}, 'id': self.uuid},
'namespace': None, {'status': 'ACTIVE',
'method': 'add_metering_label'}
expected2 = {'args': {'routers': [{'status': 'ACTIVE',
'name': 'router2', 'name': 'router2',
'gw_port_id': None, 'gw_port_id': None,
'admin_state_up': True, 'admin_state_up': True,
@ -344,9 +342,7 @@ class TestMeteringPluginL3AgentScheduler(
'_metering_labels': [ '_metering_labels': [
{'rules': [], {'rules': [],
'id': second_uuid}], 'id': second_uuid}],
'id': second_uuid}]}, 'id': second_uuid}]
'namespace': None,
'method': 'add_metering_label'}
# bind each router to a specific agent # bind each router to a specific agent
agent1 = agents_db.Agent(host='agent1') agent1 = agents_db.Agent(host='agent1')
@ -367,15 +363,7 @@ class TestMeteringPluginL3AgentScheduler(
set_context=True): set_context=True):
with self.metering_label(tenant_id=self.tenant_id, with self.metering_label(tenant_id=self.tenant_id,
set_context=True): set_context=True):
self.mock_add.assert_called_with(self.ctx, expected)
topic1 = "%s.%s" % (self.topic, 'agent1')
topic2 = "%s.%s" % (self.topic, 'agent2')
# check if there is a call per agent
expected = [mock.call(self.ctx, expected1, topic=topic1),
mock.call(self.ctx, expected2, topic=topic2)]
self.mock_cast.assert_has_calls(expected, any_order=True)
class TestMeteringPluginL3AgentSchedulerServicePlugin( class TestMeteringPluginL3AgentSchedulerServicePlugin(