Merge "DHCP RPC: Separate local from non-local subnets"

This commit is contained in:
Jenkins 2017-06-20 06:37:12 +00:00 committed by Gerrit Code Review
commit 97b97739fe
2 changed files with 32 additions and 4 deletions

View File

@ -157,6 +157,7 @@ class DhcpRpcCallback(object):
segment_ext.SegmentPluginBase.get_plugin_type())
seg_subnets = [subnet for subnet in subnets
if subnet.get('segment_id')]
nonlocal_subnets = []
if seg_plug and seg_subnets:
host_segment_ids = seg_plug.get_segments_by_hosts(context, [host])
# Gather the ids of all the subnets that are on a segment that
@ -170,14 +171,19 @@ class DhcpRpcCallback(object):
# segments as the host. Do this only for the networks that are
# routed because we want non-routed networks to work as
# before.
nonlocal_subnets = [subnet for subnet in seg_subnets
if subnet['id'] not in seg_subnet_ids]
subnets = [subnet for subnet in subnets
if subnet['network_id'] not in routed_net_ids or
subnet['id'] in seg_subnet_ids]
grouped_subnets = self._group_by_network_id(subnets)
grouped_nonlocal_subnets = self._group_by_network_id(nonlocal_subnets)
grouped_ports = self._group_by_network_id(ports)
for network in networks:
network['subnets'] = grouped_subnets.get(network['id'], [])
network['non_local_subnets'] = (
grouped_nonlocal_subnets.get(network['id'], []))
network['ports'] = grouped_ports.get(network['id'], [])
return networks
@ -200,6 +206,7 @@ class DhcpRpcCallback(object):
subnets = plugin.get_subnets(context, filters=filters)
seg_plug = directory.get_plugin(
segment_ext.SegmentPluginBase.get_plugin_type())
nonlocal_subnets = []
if seg_plug and subnets:
seg_subnets = [subnet for subnet in subnets
if subnet.get('segment_id')]
@ -212,12 +219,16 @@ class DhcpRpcCallback(object):
# host is not mapped to any segments and this is a routed
# network, then this host shouldn't have even been scheduled
# to.
nonlocal_subnets = [subnet for subnet in seg_subnets
if subnet['segment_id'] not in segment_ids]
subnets = [subnet for subnet in seg_subnets
if subnet['segment_id'] in segment_ids]
# NOTE(kevinbenton): we sort these because the agent builds tags
# based on position in the list and has to restart the process if
# the order changes.
network['subnets'] = sorted(subnets, key=operator.itemgetter('id'))
network['non_local_subnets'] = sorted(nonlocal_subnets,
key=operator.itemgetter('id'))
network['ports'] = plugin.get_ports(context, filters=filters)
return network

View File

@ -64,8 +64,14 @@ class TestDhcpRpcCallback(base.BaseTestCase):
self.plugin.get_subnets.return_value = [subnet]
networks = self.callbacks.get_active_networks_info(mock.Mock(),
host='host')
expected = [{'id': 'a', 'subnets': [], 'ports': [port]},
{'id': 'b', 'subnets': [subnet], 'ports': []}]
expected = [{'id': 'a',
'non_local_subnets': [],
'subnets': [],
'ports': [port]},
{'id': 'b',
'non_local_subnets': [],
'subnets': [subnet],
'ports': []}]
self.assertEqual(expected, networks)
def test_get_active_networks_info_with_routed_networks(self):
@ -81,8 +87,14 @@ class TestDhcpRpcCallback(base.BaseTestCase):
self.plugin.get_subnets.return_value = subnets
networks = self.callbacks.get_active_networks_info(mock.Mock(),
host='host')
expected = [{'id': 'a', 'subnets': [subnets[1]], 'ports': [port]},
{'id': 'b', 'subnets': [subnets[0]], 'ports': []}]
expected = [{'id': 'a',
'non_local_subnets': [],
'subnets': [subnets[1]],
'ports': [port]},
{'id': 'b',
'non_local_subnets': [subnets[2]],
'subnets': [subnets[0]],
'ports': []}]
self.assertEqual(expected, networks)
def _test__port_action_with_failures(self, exc=None, action=None):
@ -182,6 +194,7 @@ class TestDhcpRpcCallback(base.BaseTestCase):
subnet_retval = [dict(id='a'), dict(id='c'), dict(id='b')]
else:
subnet_retval = [dict(id='c', segment_id='1'),
dict(id='b', segment_id='2'),
dict(id='a', segment_id='1')]
port_retval = mock.Mock()
@ -195,12 +208,16 @@ class TestDhcpRpcCallback(base.BaseTestCase):
retval = self.callbacks.get_network_info(mock.Mock(), network_id='a')
self.assertEqual(retval, network_retval)
sorted_nonlocal_subnet_retval = []
if not routed_network:
sorted_subnet_retval = [dict(id='a'), dict(id='b'), dict(id='c')]
else:
sorted_subnet_retval = [dict(id='a', segment_id='1'),
dict(id='c', segment_id='1')]
sorted_nonlocal_subnet_retval = [dict(id='b', segment_id='2')]
self.assertEqual(retval['subnets'], sorted_subnet_retval)
self.assertEqual(retval['non_local_subnets'],
sorted_nonlocal_subnet_retval)
self.assertEqual(retval['ports'], port_retval)
def test_get_network_info(self):