diff --git a/neutron/services/segments/plugin.py b/neutron/services/segments/plugin.py index 79526dc8083..fb88d2a7d9d 100644 --- a/neutron/services/segments/plugin.py +++ b/neutron/services/segments/plugin.py @@ -260,10 +260,10 @@ class NovaSegmentNotifier(object): total += int(netaddr.IPAddress(pool['end']) - netaddr.IPAddress(pool['start'])) + 1 if total: - if subnet['gateway_ip']: + if subnet.get('gateway_ip'): total += 1 reserved += 1 - if subnet['enable_dhcp']: + if subnet.get('enable_dhcp'): reserved += 1 return total, reserved @@ -560,7 +560,7 @@ class SegmentHostRoutes(object): context=context, ip_version=netaddr.IPNetwork(subnet['cidr']).version, network_id=subnet['network_id'], - segment_id=subnet['segment_id'], + segment_id=segment_id, host_routes=copy.deepcopy(host_routes), gateway_ip=gateway_ip) if (not host_routes or @@ -572,11 +572,14 @@ class SegmentHostRoutes(object): def host_routes_before_update(self, resource, event, trigger, **kwargs): context = kwargs['context'] subnet, original_subnet = kwargs['request'], kwargs['original_subnet'] - segment_id = subnet.get('segment_id', original_subnet['segment_id']) - gateway_ip = subnet.get('gateway_ip', original_subnet['gateway_ip']) - host_routes = subnet.get('host_routes', original_subnet['host_routes']) - if (segment_id and (host_routes != original_subnet['host_routes'] or - gateway_ip != original_subnet['gateway_ip'])): + orig_segment_id = original_subnet.get('segment_id') + segment_id = subnet.get('segment_id', orig_segment_id) + orig_gateway_ip = original_subnet.get('gateway_ip') + gateway_ip = subnet.get('gateway_ip', orig_gateway_ip) + orig_host_routes = original_subnet.get('host_routes') + host_routes = subnet.get('host_routes', orig_host_routes) + if (segment_id and (host_routes != orig_host_routes or + gateway_ip != orig_gateway_ip)): calc_host_routes = self._calculate_routed_network_host_routes( context=context, ip_version=netaddr.IPNetwork(original_subnet['cidr']).version, @@ -584,8 +587,8 @@ class SegmentHostRoutes(object): segment_id=segment_id, host_routes=copy.deepcopy(host_routes), gateway_ip=gateway_ip, - old_gateway_ip=original_subnet['gateway_ip'] if ( - gateway_ip != original_subnet['gateway_ip']) else None) + old_gateway_ip=orig_gateway_ip if ( + gateway_ip != orig_gateway_ip) else None) if self._host_routes_need_update(host_routes, calc_host_routes): subnet['host_routes'] = calc_host_routes diff --git a/neutron/tests/unit/extensions/test_segment.py b/neutron/tests/unit/extensions/test_segment.py index 7e967711dda..6fd3f1e51fc 100644 --- a/neutron/tests/unit/extensions/test_segment.py +++ b/neutron/tests/unit/extensions/test_segment.py @@ -1701,10 +1701,10 @@ class TestNovaSegmentNotifier(SegmentAwareIpamTestCase): total += int(netaddr.IPAddress(pool['end']) - netaddr.IPAddress(pool['start'])) + 1 if total: - if subnet['gateway_ip']: + if subnet.get('gateway_ip'): total += 1 reserved += 1 - if subnet['enable_dhcp']: + if subnet.get('enable_dhcp'): reserved += 1 return total, reserved