This commit is a preparation step for using pluggable IPAM.
1. Moved get_subnets functionality to db_base_plugin_common to make it
accessible by ipam backends.
2. Reworked update_subnet routine:
- moved db part into update_db_subnet;
Partially-Implements: blueprint neutron-ipam
Change-Id: Idb8f54d9fccaad1137222d156590c37d86aa576b
# a lot of stress on the db. Consider adding a cache layer
return context.session.query(models_v2.Subnet).all()
+ def _get_subnets(self, context, filters=None, fields=None,
+ sorts=None, limit=None, marker=None,
+ page_reverse=False):
+ marker_obj = self._get_marker_obj(context, 'subnet', limit, marker)
+ return self._get_collection(context, models_v2.Subnet,
+ self._make_subnet_dict,
+ filters=filters, fields=fields,
+ sorts=sorts,
+ limit=limit,
+ marker_obj=marker_obj,
+ page_reverse=page_reverse)
+
def _make_network_dict(self, network, fields=None,
process_extensions=True):
res = {'id': network['id'],
dns lease or we support gratuitous DHCP offers
"""
s = subnet['subnet']
- changed_host_routes = False
- changed_dns = False
- changed_allocation_pools = False
db_subnet = self._get_subnet(context, id)
# Fill 'ip_version' and 'allocation_pools' fields with the current
# value since _validate_subnet() expects subnet spec has 'ip_version'
self._validate_gw_out_of_pools(s["gateway_ip"], allocation_pools)
with context.session.begin(subtransactions=True):
- if "dns_nameservers" in s:
- changed_dns = True
- new_dns = self._update_subnet_dns_nameservers(context, id, s)
-
- if "host_routes" in s:
- changed_host_routes = True
- new_routes = self._update_subnet_host_routes(context, id, s)
-
- if "allocation_pools" in s:
- self._validate_allocation_pools(s['allocation_pools'],
- s['cidr'])
- changed_allocation_pools = True
- new_pools = self._update_subnet_allocation_pools(context,
- id, s)
- subnet = self._get_subnet(context, id)
- subnet.update(s)
+ subnet, changes = self._update_db_subnet(context, id, s)
result = self._make_subnet_dict(subnet)
# Keep up with fields that changed
- if changed_dns:
- result['dns_nameservers'] = new_dns
- if changed_host_routes:
- result['host_routes'] = new_routes
- if changed_allocation_pools:
- result['allocation_pools'] = new_pools
+ result.update(changes)
return result
def _subnet_check_ip_allocations(self, context, subnet_id):
def get_subnets(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
- marker_obj = self._get_marker_obj(context, 'subnet', limit, marker)
- return self._get_collection(context, models_v2.Subnet,
- self._make_subnet_dict,
- filters=filters, fields=fields,
- sorts=sorts,
- limit=limit,
- marker_obj=marker_obj,
- page_reverse=page_reverse)
+ return self._get_subnets(context, filters, fields, sorts, limit,
+ marker, page_reverse)
def get_subnets_count(self, context, filters=None):
return self._get_collection_count(context, models_v2.Subnet,
del s["dns_nameservers"]
return new_dns
- def _update_subnet_allocation_pools(self, context, id, s):
+ def _update_subnet_allocation_pools(self, context, subnet_id, s):
context.session.query(models_v2.IPAllocationPool).filter_by(
- subnet_id=id).delete()
+ subnet_id=subnet_id).delete()
new_pools = [models_v2.IPAllocationPool(first_ip=p['start'],
last_ip=p['end'],
- subnet_id=id)
+ subnet_id=subnet_id)
for p in s['allocation_pools']]
context.session.add_all(new_pools)
# Call static method with self to redefine in child
del s['allocation_pools']
return result_pools
+ def _update_db_subnet(self, context, subnet_id, s):
+ changes = {}
+ if "dns_nameservers" in s:
+ changes['dns_nameservers'] = (
+ self._update_subnet_dns_nameservers(context, subnet_id, s))
+
+ if "host_routes" in s:
+ changes['host_routes'] = self._update_subnet_host_routes(
+ context, subnet_id, s)
+
+ if "allocation_pools" in s:
+ self._validate_allocation_pools(s['allocation_pools'],
+ s['cidr'])
+ changes['allocation_pools'] = (
+ self._update_subnet_allocation_pools(context, subnet_id, s))
+
+ subnet = self._get_subnet(context, subnet_id)
+ subnet.update(s)
+ return subnet, changes
+
def _validate_allocation_pools(self, ip_pools, subnet_cidr):
"""Validate IP allocation pools.
raise n_exc.InvalidInput(error_message=msg)
filter = {'network_id': [network_id]}
- subnets = self.get_subnets(context, filters=filter)
+ subnets = self._get_subnets(context, filters=filter)
for subnet in subnets:
if ipam_utils.check_subnet_ip(subnet['cidr'],
fixed['ip_address']):
ips = []
v6_stateless = []
net_id_filter = {'network_id': [p['network_id']]}
- subnets = self.get_subnets(context, filters=net_id_filter)
+ subnets = self._get_subnets(context, filters=net_id_filter)
is_router_port = (
p['device_owner'] in constants.ROUTER_INTERFACE_OWNERS or
p['device_owner'] == constants.DEVICE_OWNER_ROUTER_SNAT)
def _test__allocate_ips_for_port(self, subnets, port, expected):
plugin = db_base_plugin_v2.NeutronDbPluginV2()
with mock.patch.object(db_base_plugin_v2.NeutronDbPluginV2,
- 'get_subnets') as get_subnets:
+ '_get_subnets') as get_subnets:
with mock.patch.object(db_base_plugin_v2.NeutronDbPluginV2,
'_check_unique_ip') as check_unique:
context = mock.Mock()