Add some bulk lookup methods to ML2 for RPC handling

This adds three methods to make working with bulk port
DB lookups easier in ML2:

* partial_port_ids_to_full_ids: takes short port IDs map to
  full port IDs. This will allow us to eliminate many LIKE
  queries and do one just once for all ports on an RPC call.
* get_port_db_objects: Takes a list of port IDs and returns
  a map to port DB objects. This allows us to get access to
  sqla obejcts for a bunch of ports without a custom
  session.query call.
* get_network_contexts: Takes a list of network_ids and does
  a bulk construction of NetworkContext objects and returns
  them as a map of network_id to NetworkContext to avoid
  expensive net lookups when constructing lots of PortContext

Partial-Bug: #1665215
Change-Id: I330eefbf429bd62f2a7e8ebadf7037da15c86815
Kevin Benton 6 years ago
parent 604e598a7d
commit 323eb7f2e1

@ -292,6 +292,46 @@ def get_distributed_port_bindings(context, port_id):
return bindings
def partial_port_ids_to_full_ids(context, partial_ids):
"""Takes a list of the start of port IDs and returns full IDs.
Returns dictionary of partial IDs to full IDs if a single match
is found.
result = {}
to_full_query = (context.session.query(
for p in partial_ids])))
candidates = [match[0] for match in to_full_query]
for partial_id in partial_ids:
matching = [c for c in candidates if c.startswith(partial_id)]
if len(matching) == 1:
result[partial_id] = matching[0]
if len(matching) < 1:"No ports have port_id starting with %s", partial_id)
elif len(matching) > 1:
LOG.error(_LE("Multiple ports have port_id starting with %s"),
return result
def get_port_db_objects(context, port_ids):
"""Takes a list of port_ids and returns matching port db objects.
return format is a dictionary keyed by passed in IDs with db objects
for values or None if the port was not present.
port_qry = (context.session.query(models_v2.Port).
result = {p: None for p in port_ids}
for port in port_qry:
result[] = port
return result
def is_dhcp_active_on_any_subnet(context, subnet_ids):
if not subnet_ids:

@ -884,6 +884,23 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
return [db_utils.resource_fields(net, fields) for net in nets]
def get_network_contexts(self, context, network_ids):
"""Return a map of network_id to NetworkContext for network_ids."""
net_filters = {'id': list(set(network_ids))}
nets_by_netid = {
n['id']: n for n in self.get_networks(context,
segments_by_netid = segments_db.get_networks_segments(
context, nets_by_netid.keys())
netctxs_by_netid = {
net_id: driver_context.NetworkContext(
self, context, nets_by_netid[net_id],
for net_id in nets_by_netid.keys()
return netctxs_by_netid
def delete_network(self, context, id):
# the only purpose of this override is to protect this from being