diff --git a/quantum/db/db_base_plugin_v2.py b/quantum/db/db_base_plugin_v2.py index 4cdfe9b4e4..ed5bf238ff 100644 --- a/quantum/db/db_base_plugin_v2.py +++ b/quantum/db/db_base_plugin_v2.py @@ -693,12 +693,14 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2): first_ip = net.first + 1 last_ip = net.last - 1 gw_ip = int(netaddr.IPAddress(subnet['gateway_ip'] or net.last)) - - if gw_ip > first_ip: + # Use the gw_ip to find a point for splitting allocation pools + # for this subnet + split_ip = min(max(gw_ip, net.first), net.last) + if split_ip > first_ip: pools.append({'start': str(netaddr.IPAddress(first_ip)), - 'end': str(netaddr.IPAddress(gw_ip - 1))}) - if gw_ip < last_ip: - pools.append({'start': str(netaddr.IPAddress(gw_ip + 1)), + 'end': str(netaddr.IPAddress(split_ip - 1))}) + if split_ip < last_ip: + pools.append({'start': str(netaddr.IPAddress(split_ip + 1)), 'end': str(netaddr.IPAddress(last_ip))}) # return auto-generated pools # no need to check for their validity diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index 0fcfcf732f..974bff768b 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -1663,7 +1663,7 @@ class TestNetworksV2(QuantumDbPluginV2TestCase): class TestSubnetsV2(QuantumDbPluginV2TestCase): - def _test_create_subnet(self, network=None, **kwargs): + def _test_create_subnet(self, network=None, expected=None, **kwargs): keys = kwargs.copy() keys.setdefault('cidr', '10.0.0.0/24') keys.setdefault('ip_version', 4) @@ -1673,6 +1673,11 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase): for k in keys: self.assertIn(k, subnet['subnet']) self.assertEquals(subnet['subnet'][k], keys[k]) + # verify the configured validations are correct + if expected: + for k in expected: + self.assertIn(k, subnet['subnet']) + self.assertEquals(subnet['subnet'][k], expected[k]) return subnet def test_create_subnet(self): @@ -1871,6 +1876,36 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase): self.assertEquals(subnet['subnet']['allocation_pools'], allocation_pools) + def test_create_subnet_gw_values(self): + # Gateway not in subnet + gateway = '100.0.0.1' + cidr = '10.0.0.0/24' + allocation_pools = [{'start': '10.0.0.1', + 'end': '10.0.0.254'}] + expected = {'gateway_ip': gateway, + 'cidr': cidr, + 'allocation_pools': allocation_pools} + subnet = self._test_create_subnet(expected=expected, + gateway_ip=gateway) + # Gateway is last IP in range + gateway = '10.0.0.254' + allocation_pools = [{'start': '10.0.0.1', + 'end': '10.0.0.253'}] + expected = {'gateway_ip': gateway, + 'cidr': cidr, + 'allocation_pools': allocation_pools} + subnet = self._test_create_subnet(expected=expected, + gateway_ip=gateway) + # Gateway is first in subnet + gateway = '10.0.0.1' + allocation_pools = [{'start': '10.0.0.2', + 'end': '10.0.0.254'}] + expected = {'gateway_ip': gateway, + 'cidr': cidr, + 'allocation_pools': allocation_pools} + subnet = self._test_create_subnet(expected=expected, + gateway_ip=gateway) + def test_create_subnet_with_allocation_pool(self): gateway_ip = '10.0.0.1' cidr = '10.0.0.0/24'