This patch modifies ML2 DB get_port_binding_host method so that it
reuses the existing context session to do the database query
rather than creating a new database session.
Note that there are other methods in ML2 DB that do not re-use
the caller's session (get_port_from_device_mac() and
get_sg_ids_grouped_by_port()). These will be modified using
a separate bug (https://bugs.launchpad.net/neutron/+bug/
1441205).
Change-Id: I8aafb0a70f40f9306ccc366e5db6860c92c48cce
Closes-Bug: #
1440183
def dvr_deletens_if_no_port(self, context, port_id):
"""Delete the DVR namespace if no dvr serviced port exists."""
router_ids = self.get_dvr_routers_by_portid(context, port_id)
- port_host = ml2_db.get_port_binding_host(port_id)
+ port_host = ml2_db.get_port_binding_host(context.session, port_id)
if not router_ids:
LOG.debug('No namespaces available for this DVR port %(port)s '
'on host %(host)s', {'port': port_id,
return port_dict
-def get_port_binding_host(port_id):
- session = db_api.get_session()
- with session.begin(subtransactions=True):
- try:
+def get_port_binding_host(session, port_id):
+ try:
+ with session.begin(subtransactions=True):
query = (session.query(models.PortBinding).
filter(models.PortBinding.port_id.startswith(port_id)).
one())
- except exc.NoResultFound:
- LOG.debug("No binding found for port %(port_id)s",
- {'port_id': port_id})
- return
- except exc.MultipleResultsFound:
- LOG.error(_LE("Multiple ports have port_id starting with %s"),
- port_id)
- return
+ except exc.NoResultFound:
+ LOG.debug("No binding found for port %(port_id)s",
+ {'port_id': port_id})
+ return
+ except exc.MultipleResultsFound:
+ LOG.error(_LE("Multiple ports have port_id starting with %s"),
+ port_id)
+ return
return query.host
LOG.debug("No binding found for DVR port %s", port['id'])
return False
else:
- port_host = db.get_port_binding_host(port_id)
+ port_host = db.get_port_binding_host(context.session, port_id)
return (port_host == host)
def get_ports_from_devices(self, devices):
self._setup_neutron_port(network_id, port_id)
self._setup_neutron_portbinding(port_id, vif_type, host)
- port_host = ml2_db.get_port_binding_host(port_id)
+ port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
self.assertEqual(host, port_host)
def test_get_port_binding_host_multiple_results_found(self):
self._setup_neutron_port(network_id, port_id_two)
self._setup_neutron_portbinding(port_id_two, vif_type, host)
- port_host = ml2_db.get_port_binding_host(port_id)
+ port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
self.assertIsNone(port_host)
def test_get_port_binding_host_result_not_found(self):
port_id = uuidutils.generate_uuid()
- port_host = ml2_db.get_port_binding_host(port_id)
+ port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
self.assertIsNone(port_host)
def test_get_port(self):