]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Recycle IPs used by 'gateway' ports
authorSalvatore Orlando <salv.orlando@gmail.com>
Tue, 21 May 2013 18:05:00 +0000 (20:05 +0200)
committerSalvatore Orlando <salv.orlando@gmail.com>
Tue, 21 May 2013 18:06:09 +0000 (20:06 +0200)
Bug 1182602

Previous code simply removed IP allocation for these ports which
should never have an IP. This patch triggers IP recycling so the
IP addresses can be reused by other ports.

Change-Id: I594e02d5bbc78b219eab07e595cde713d6450ffe

quantum/plugins/nicira/nicira_networkgw_db.py
quantum/tests/unit/nicira/test_networkgw.py

index cc9d51b31536f476a0054829089f2466394c2bd0..f50a0759dc160e911dcfe3f6de9c288445bd8ca3 100644 (file)
@@ -26,7 +26,6 @@ from webob import exc as web_exc
 from quantum.api.v2 import attributes
 from quantum.api.v2 import base
 from quantum.common import exceptions
-from quantum.db import db_base_plugin_v2
 from quantum.db import model_base
 from quantum.db import models_v2
 from quantum.openstack.common import log as logging
@@ -267,7 +266,7 @@ class NetworkGatewayMixin(nvp_networkgw.NetworkGatewayPluginBase):
                                                   network_mapping_info):
                 raise GatewayConnectionInUse(mapping=network_mapping_info,
                                              gateway_id=network_gateway_id)
-            # TODO(salvatore-orlando): This will give the port a fixed_ip,
+            # TODO(salvatore-orlando): Creating a port will give it an IP,
             # but we actually do not need any. Instead of wasting an IP we
             # should have a way to say a port shall not be associated with
             # any subnet
@@ -313,12 +312,11 @@ class NetworkGatewayMixin(nvp_networkgw.NetworkGatewayPluginBase):
             gw_db.network_connections.append(
                 NetworkConnection(**network_mapping_info))
             port_id = port['id']
-            # now deallocate the ip from the port
+            # now deallocate and recycle ip from the port
             for fixed_ip in port.get('fixed_ips', []):
-                db_base_plugin_v2.QuantumDbPluginV2._delete_ip_allocation(
-                    context, network_id,
-                    fixed_ip['subnet_id'],
-                    fixed_ip['ip_address'])
+                self._recycle_ip(context, network_id,
+                                 fixed_ip['subnet_id'],
+                                 fixed_ip['ip_address'])
             LOG.debug(_("Ensured no Ip addresses are configured on port %s"),
                       port_id)
             return {'connection_info':
index 6a59f7a820b668f488f048e5b61fe3acfbec0bf1..c1fd33c92203136f625dfe8a1365ea0b68b82021 100644 (file)
@@ -462,6 +462,35 @@ class NetworkGatewayDbTestCase(test_db_plugin.QuantumDbPluginV2TestCase):
                                      'vlan', 555,
                                      expected_status=exc.HTTPBadRequest.code)
 
+    def test_connect_network_does_not_waste_ips(self):
+        # Ensure address is immediately recycled
+        cfg.CONF.set_override('dhcp_lease_duration', -1)
+        with self._network_gateway() as gw:
+            with self.network() as net:
+                with self.subnet(network=net) as sub:
+                    with self.port(subnet=sub) as port_1:
+                        expected_ips = port_1['port']['fixed_ips']
+                    # port_1 has now been deleted
+                    body = self._gateway_action('connect',
+                                                gw[self.resource]['id'],
+                                                net['network']['id'],
+                                                'flat')
+                    gw_port_id = body['connection_info']['port_id']
+                    gw_port_body = self._show('ports', gw_port_id)
+                    self.assertEqual(gw[self.resource]['id'],
+                                     gw_port_body['port']['device_id'])
+                    self.assertEqual([], gw_port_body['port']['fixed_ips'])
+                    # Verify a new port gets same address as port_1
+                    # This will confirm the gateway port did not waste an ip
+                    with self.port(subnet=sub) as port_2:
+                        self.assertEqual(expected_ips,
+                                         port_2['port']['fixed_ips'])
+                    # Clean up - otherwise delete will fail
+                    self._gateway_action('disconnect',
+                                         gw[self.resource]['id'],
+                                         net['network']['id'],
+                                         'flat')
+
     def test_disconnect_network_ambiguous_returns_409(self):
         with self._network_gateway() as gw:
             with self.network() as net_1: