]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
MidoNet plugin, clean up dhcp entries correctly
authorRossella Sblendido <rossella@midokura.com>
Mon, 30 Sep 2013 16:09:59 +0000 (16:09 +0000)
committerRossella Sblendido <rossella@midokura.com>
Fri, 4 Oct 2013 08:35:52 +0000 (08:35 +0000)
When a subnet gets deleted, remove the proper dhcp entry, not
always the first one.

Change-Id: I4f152eed5a7dd408bda866cb304838989bdc363c
Solves-bug: #1233259

neutron/plugins/midonet/midonet_lib.py
neutron/plugins/midonet/plugin.py
neutron/tests/unit/midonet/test_midonet_lib.py

index 6b96e3812fbec82caf5c38d476d6abf7842ece3a..8f40b3986d6170d3f79102a3776e1d6fc7dcb9dc 100644 (file)
@@ -187,18 +187,24 @@ class MidoClient:
         self.remove_dhcp_host(bridge, net_util.subnet_str(cidr), ip, mac)
 
     @handle_api_error
-    def delete_dhcp(self, bridge):
+    def delete_dhcp(self, bridge, cidr):
         """Delete a DHCP entry
 
         :param bridge: bridge to remove DHCP from
+        :param cidr: subnet represented as x.x.x.x/y
         """
-        LOG.debug(_("MidoClient.delete_dhcp called: bridge=%(bridge)s, "),
-                  {'bridge': bridge})
-        dhcp = bridge.get_dhcp_subnets()
-        if not dhcp:
+        LOG.debug(_("MidoClient.delete_dhcp called: bridge=%(bridge)s, "
+                    "cidr=%(cidr)s"),
+                  {'bridge': bridge, 'cidr': cidr})
+        dhcp_subnets = bridge.get_dhcp_subnets()
+        net_addr, net_len = net_util.net_addr(cidr)
+        if not dhcp_subnets:
             raise MidonetApiException(
                 msg=_("Tried to delete non-existent DHCP"))
-        dhcp[0].delete()
+        for dhcp in dhcp_subnets:
+            if dhcp.get_subnet_prefix() == net_addr:
+                dhcp.delete()
+                break
 
     @handle_api_error
     def delete_port(self, id, delete_chains=False):
index cd12ec5ce7faf9c601b25f5a7542dcc8892c77d9..a1790d488d3bd014fb35d4ab0e5de0c28f862510 100644 (file)
@@ -38,6 +38,7 @@ from neutron.db import external_net_db
 from neutron.db import l3_db
 from neutron.db import models_v2
 from neutron.db import securitygroups_db
+from neutron.extensions import external_net as ext_net
 from neutron.extensions import securitygroup as ext_sg
 from neutron.openstack.common import excutils
 from neutron.openstack.common import log as logging
@@ -412,16 +413,19 @@ class MidonetPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
         net = super(MidonetPluginV2, self).get_network(context,
                                                        subnet['network_id'],
                                                        fields=None)
-        bridge = self.client.get_bridge(subnet['network_id'])
-        self.client.delete_dhcp(bridge)
+        session = context.session
+        with session.begin(subtransactions=True):
+
+            super(MidonetPluginV2, self).delete_subnet(context, id)
+            bridge = self.client.get_bridge(subnet['network_id'])
+            self.client.delete_dhcp(bridge, subnet['cidr'])
 
-        # If the network is external, clean up routes, links, ports.
-        if net['router:external']:
-            self._unlink_bridge_from_gw_router(bridge,
-                                               self._get_provider_router())
+            # If the network is external, clean up routes, links, ports
+            if net[ext_net.EXTERNAL]:
+                self._unlink_bridge_from_gw_router(
+                    bridge, self._get_provider_router())
 
-        super(MidonetPluginV2, self).delete_subnet(context, id)
-        LOG.debug(_("MidonetPluginV2.delete_subnet exiting"))
+            LOG.debug(_("MidonetPluginV2.delete_subnet exiting"))
 
     def create_network(self, context, network):
         """Create Neutron network.
index 0fe95f99f185641a9bfa691c1239559964fcd767..1406cdf045b56d69ee65d6f4ebb72cff83239a33 100644 (file)
@@ -95,6 +95,18 @@ class MidoClientTestCase(testtools.TestCase):
 
         bridge.assert_has_call(dhcp_call)
 
+    def test_delete_dhcp(self):
+
+        bridge = mock.Mock()
+        subnet = mock.Mock()
+        subnet.get_subnet_prefix.return_value = "10.0.0.0"
+        subnets = mock.MagicMock(return_value=[subnet])
+        bridge.get_dhcp_subnets.side_effect = subnets
+        self.client.delete_dhcp(bridge, "10.0.0.0/24")
+        bridge.assert_has_calls(mock.call.get_dhcp_subnets)
+        subnet.assert_has_calls([mock.call.get_subnet_prefix(),
+                                mock.call.delete()])
+
     def test_add_dhcp_host(self):
 
         bridge = mock.Mock()