]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Refactor awkward logic in setup_dhcp_port
authorCarl Baldwin <carl.baldwin@hp.com>
Thu, 4 Jun 2015 22:25:44 +0000 (22:25 +0000)
committerCarl Baldwin <carl.baldwin@hp.com>
Fri, 5 Jun 2015 20:18:36 +0000 (20:18 +0000)
I noticed this logic as I was reviewing another patch set [1].  I
didn't like removing subnet ids from dhcp_enabled_subnet_ids and I
wasn't too keen on the ips_need_removal semantics that were kind of
forced by the existing structure of the code.  I hope you find this
alternative much clearer.  I like straight-forward code with less
indentation that doesn't use awkward booleans like ips_needs_removal.

[1] https://review.openstack.org/#/c/157697/6

Change-Id: I8bd3d6924a855ea08f8096e66bd3bfbb165a4da3

neutron/agent/linux/dhcp.py

index 70453d265f7362972be1f1c7c041ed8f9e78f391..ae6fd43fadbf93d508a75a0a8dacd2a5ea92aee5 100644 (file)
@@ -849,33 +849,28 @@ class DeviceManager(object):
         """Create/update DHCP port for the host if needed and return port."""
 
         device_id = self.get_device_id(network)
-        subnets = {}
-        dhcp_enabled_subnet_ids = []
-        for subnet in network.subnets:
-            if subnet.enable_dhcp:
-                dhcp_enabled_subnet_ids.append(subnet.id)
-                subnets[subnet.id] = subnet
+        subnets = {subnet.id: subnet for subnet in network.subnets
+                   if subnet.enable_dhcp}
 
         dhcp_port = None
         for port in network.ports:
             port_device_id = getattr(port, 'device_id', None)
             if port_device_id == device_id:
+                dhcp_enabled_subnet_ids = set(subnets)
                 port_fixed_ips = []
-                ips_needs_removal = False
                 for fixed_ip in port.fixed_ips:
                     if fixed_ip.subnet_id in dhcp_enabled_subnet_ids:
                         port_fixed_ips.append(
                             {'subnet_id': fixed_ip.subnet_id,
                              'ip_address': fixed_ip.ip_address})
-                        dhcp_enabled_subnet_ids.remove(fixed_ip.subnet_id)
-                    else:
-                        ips_needs_removal = True
 
-                # If there are dhcp_enabled_subnet_ids here that means that
-                # we need to add those to the port and call update.
-                if dhcp_enabled_subnet_ids or ips_needs_removal:
+                port_subnet_ids = set(ip.subnet_id for ip in port.fixed_ips)
+                # If there is a new dhcp enabled subnet or a port that is no
+                # longer on a dhcp enabled subnet, we need to call update.
+                if dhcp_enabled_subnet_ids != port_subnet_ids:
                     port_fixed_ips.extend(
-                        [dict(subnet_id=s) for s in dhcp_enabled_subnet_ids])
+                        dict(subnet_id=s)
+                        for s in dhcp_enabled_subnet_ids - port_subnet_ids)
                     dhcp_port = self.plugin.update_dhcp_port(
                         port.id, {'port': {'network_id': network.id,
                                            'fixed_ips': port_fixed_ips}})
@@ -911,7 +906,7 @@ class DeviceManager(object):
                 device_id=device_id,
                 network_id=network.id,
                 tenant_id=network.tenant_id,
-                fixed_ips=[dict(subnet_id=s) for s in dhcp_enabled_subnet_ids])
+                fixed_ips=[dict(subnet_id=s) for s in subnets])
             dhcp_port = self.plugin.create_dhcp_port({'port': port_dict})
 
         if not dhcp_port: