DHCP: add method to get static routes

Provide a class method build_static_routes that will prepare
the static routes. This will be used by the plugin to update
the subnets host routes if necessary.

Change-Id: I6adcc997c72e0dd3871df319871c7e78942d2cf6
This commit is contained in:
Gary Kotton 2017-07-14 19:29:26 +03:00 committed by garyk
parent ab15170ac0
commit 514c398ee9
2 changed files with 71 additions and 20 deletions

View File

@ -86,3 +86,51 @@ class TestNativeDhcp(nsxlib_testcase.NsxLibTestCase):
self.assertEqual(nsxlib_testcase.DNS_DOMAIN, result['domain_name'])
self.assertEqual(nsxlib_testcase.DNS_NAMESERVERS,
result['dns_nameservers'])
def test_build_static_routes(self):
gateway_ip = '2.2.2.2'
cidr = '5.5.0.0/24'
host_routes = [{'nexthop': '81.0.200.254',
'destination': '91.255.255.0/24'}]
static_routes, gateway_ip = self.handler.build_static_routes(
gateway_ip, cidr, host_routes)
expected = [{'network': '5.5.0.0/24', 'next_hop': '0.0.0.0'},
{'network': '91.255.255.0/24', 'next_hop': '81.0.200.254'},
{'network': '0.0.0.0/0', 'next_hop': '2.2.2.2'}]
self.assertEqual(expected, static_routes)
self.assertEqual('2.2.2.2', gateway_ip)
def test_build_static_routes_gw_none(self):
gateway_ip = None
cidr = '5.5.0.0/24'
host_routes = [{'nexthop': '81.0.200.254',
'destination': '91.255.255.0/24'}]
static_routes, gateway_ip = self.handler.build_static_routes(
gateway_ip, cidr, host_routes)
expected = [{'network': '5.5.0.0/24', 'next_hop': '0.0.0.0'},
{'network': '91.255.255.0/24', 'next_hop': '81.0.200.254'}]
self.assertEqual(expected, static_routes)
self.assertIsNone(gateway_ip)
def test_build_static_routes_no_host_routes(self):
gateway_ip = '2.2.2.2'
cidr = '5.5.0.0/24'
host_routes = []
static_routes, gateway_ip = self.handler.build_static_routes(
gateway_ip, cidr, host_routes)
expected = [{'network': '5.5.0.0/24', 'next_hop': '0.0.0.0'},
{'network': '0.0.0.0/0', 'next_hop': '2.2.2.2'}]
self.assertEqual(expected, static_routes)
self.assertEqual('2.2.2.2', gateway_ip)
def test_build_static_routes_gw_none_host_route_any(self):
gateway_ip = None
cidr = '5.5.0.0/24'
host_routes = [{'nexthop': '81.0.200.254',
'destination': '0.0.0.0/0'}]
static_routes, gateway_ip = self.handler.build_static_routes(
gateway_ip, cidr, host_routes)
expected = [{'network': '5.5.0.0/24', 'next_hop': '0.0.0.0'},
{'network': '0.0.0.0/0', 'next_hop': '81.0.200.254'}]
self.assertEqual(expected, static_routes)
self.assertEqual('81.0.200.254', gateway_ip)

View File

@ -22,6 +22,26 @@ from vmware_nsxlib.v3 import utils
class NsxLibNativeDhcp(utils.NsxLibApiBase):
def build_static_routes(self, gateway_ip, cidr, host_routes):
# The following code is based on _generate_opts_per_subnet() in
# neutron/agent/linux/dhcp.py. It prepares DHCP options for a subnet.
# Add route for directly connected network.
static_routes = [{'network': cidr, 'next_hop': '0.0.0.0'}]
# Copy routes from subnet host_routes attribute.
for hr in host_routes:
if hr['destination'] == constants.IPv4_ANY:
if not gateway_ip:
gateway_ip = hr['nexthop']
else:
static_routes.append({'network': hr['destination'],
'next_hop': hr['nexthop']})
# If gateway_ip is defined, add default route via this gateway.
if gateway_ip:
static_routes.append({'network': constants.IPv4_ANY,
'next_hop': gateway_ip})
return static_routes, gateway_ip
def build_server_config(self, network, subnet, port, tags,
default_dns_nameservers=None,
default_dns_domain=None):
@ -38,26 +58,9 @@ class NsxLibNativeDhcp(utils.NsxLibApiBase):
gateway_ip = subnet['gateway_ip']
if not validators.is_attr_set(gateway_ip):
gateway_ip = None
# The following code is based on _generate_opts_per_subnet() in
# neutron/agent/linux/dhcp.py. It prepares DHCP options for a subnet.
# Add route for directly connected network.
host_routes = [{'network': subnet['cidr'], 'next_hop': '0.0.0.0'}]
# Copy routes from subnet host_routes attribute.
for hr in subnet['host_routes']:
if hr['destination'] == constants.IPv4_ANY:
if not gateway_ip:
gateway_ip = hr['nexthop']
else:
host_routes.append({'network': hr['destination'],
'next_hop': hr['nexthop']})
# If gateway_ip is defined, add default route via this gateway.
if gateway_ip:
host_routes.append({'network': constants.IPv4_ANY,
'next_hop': gateway_ip})
options = {'option121': {'static_routes': host_routes}}
static_routes, gateway_ip = self.build_static_routes(
gateway_ip, subnet['cidr'], subnet['host_routes'])
options = {'option121': {'static_routes': static_routes}}
name = utils.get_name_and_uuid(network['name'] or 'dhcpserver',
network['id'])
dns_domain = network.get('dns_domain')