use context manager from neutron-lib

The neutron.db.api.context_manager already references neutron-lib's
context manager; so consumers of it are already using neutron-lib. This
patch switches neutron's references to the context_manager over to
use neutron-lib's directly rather than that in neutron.db.api.

NeutronLibImpact

Change-Id: I97120faeec73690592ed21a5ec3c6202f61e1429
changes/22/613122/1
Boden R 4 years ago
parent 83dffac498
commit e4aa5902f7
  1. 3
      neutron/api/v2/base.py
  2. 4
      neutron/db/address_scope_db.py
  3. 4
      neutron/db/allowedaddresspairs_db.py
  4. 12
      neutron/db/db_base_plugin_common.py
  5. 119
      neutron/db/db_base_plugin_v2.py
  6. 17
      neutron/db/dvr_mac_db.py
  7. 6
      neutron/db/extradhcpopt_db.py
  8. 12
      neutron/db/flavors_db.py
  9. 4
      neutron/db/ipam_backend_mixin.py
  10. 4
      neutron/db/ipam_pluggable_backend.py
  11. 6
      neutron/db/metering/metering_db.py
  12. 6
      neutron/db/network_ip_availability_db.py
  13. 6
      neutron/db/portbindings_db.py
  14. 39
      neutron/db/quota/api.py
  15. 17
      neutron/db/quota/driver.py
  16. 53
      neutron/db/securitygroups_db.py
  17. 12
      neutron/db/segments_db.py
  18. 14
      neutron/ipam/subnet_alloc.py
  19. 11
      neutron/objects/base.py
  20. 4
      neutron/pecan_wsgi/hooks/quota_enforcement.py
  21. 42
      neutron/plugins/ml2/db.py
  22. 4
      neutron/plugins/ml2/drivers/helpers.py
  23. 4
      neutron/plugins/ml2/drivers/type_flat.py
  24. 23
      neutron/plugins/ml2/drivers/type_tunnel.py
  25. 9
      neutron/plugins/ml2/drivers/type_vlan.py
  26. 9
      neutron/plugins/ml2/managers.py
  27. 5
      neutron/plugins/ml2/ovo_rpc.py
  28. 97
      neutron/plugins/ml2/plugin.py
  29. 21
      neutron/quota/resource.py
  30. 4
      neutron/quota/resource_registry.py
  31. 8
      neutron/services/logapi/common/db_api.py
  32. 9
      neutron/services/logapi/logging_plugin.py
  33. 13
      neutron/services/portforwarding/pf_plugin.py
  34. 19
      neutron/services/qos/qos_plugin.py
  35. 17
      neutron/services/segments/db.py
  36. 7
      neutron/services/tag/tag_plugin.py
  37. 37
      neutron/tests/unit/db/test_db_base_plugin_v2.py
  38. 4
      neutron/tests/unit/extensions/test_flavors.py
  39. 10
      neutron/tests/unit/extensions/test_portsecurity.py
  40. 6
      neutron/tests/unit/extensions/test_securitygroup.py
  41. 12
      neutron/tests/unit/ipam/test_subnet_alloc.py
  42. 11
      neutron/tests/unit/objects/test_base.py
  43. 4
      neutron/tests/unit/plugins/ml2/drivers/test_type_vlan.py
  44. 8
      neutron/tests/unit/plugins/ml2/test_db.py
  45. 19
      neutron/tests/unit/plugins/ml2/test_plugin.py
  46. 15
      neutron/tests/unit/testlib_api.py
  47. 4
      neutron/wsgi.py

@ -33,7 +33,6 @@ from neutron.api.v2 import resource as wsgi_resource
from neutron.common import constants as n_const
from neutron.common import exceptions as n_exc
from neutron.common import rpc as n_rpc
from neutron.db import api as ndb_api
from neutron import policy
from neutron import quota
from neutron.quota import resource_registry
@ -493,7 +492,7 @@ class Controller(object):
def notify(create_result):
# Ensure usage trackers for all resources affected by this API
# operation are marked as dirty
with ndb_api.context_manager.writer.using(request.context):
with db_api.CONTEXT_WRITER.using(request.context):
# Commit the reservation(s)
for reservation in reservations:
quota.QUOTAS.commit_reservation(

@ -15,13 +15,13 @@
from neutron_lib.api.definitions import address_scope as apidef
from neutron_lib.api.definitions import network as net_def
from neutron_lib import constants
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as db_utils
from neutron_lib.exceptions import address_scope as api_err
from oslo_utils import uuidutils
from neutron._i18n import _
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.extensions import address_scope as ext_address_scope
from neutron.objects import address_scope as obj_addr_scope
from neutron.objects import base as base_obj
@ -113,7 +113,7 @@ class AddressScopeDbMixin(ext_address_scope.AddressScopePluginBase):
return obj_addr_scope.AddressScope.count(context, **filters)
def delete_address_scope(self, context, id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
if subnetpool_obj.SubnetPool.get_objects(context,
address_scope_id=id):
raise api_err.AddressScopeInUse(address_scope_id=id)

@ -16,13 +16,13 @@
from neutron_lib.api.definitions import allowedaddresspairs as addr_apidef
from neutron_lib.api.definitions import port as port_def
from neutron_lib.api import validators
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as db_utils
from neutron_lib.exceptions import allowedaddresspairs as addr_exc
from neutron_lib.objects import exceptions
from neutron.common import utils
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.objects.port.extensions import (allowedaddresspairs
as obj_addr_pair)
@ -36,7 +36,7 @@ class AllowedAddressPairsMixin(object):
if not validators.is_attr_set(allowed_address_pairs):
return []
try:
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
for address_pair in allowed_address_pairs:
# use port.mac_address if no mac address in address pair
if 'mac_address' not in address_pair:

@ -21,6 +21,7 @@ from neutron_lib.api.definitions import subnet as subnet_def
from neutron_lib.api.definitions import subnetpool as subnetpool_def
from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as db_utils
from neutron_lib import exceptions as n_exc
from neutron_lib.utils import net
@ -32,7 +33,6 @@ from neutron.common import constants as n_const
from neutron.common import exceptions
from neutron.db import _model_query as model_query
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.db import common_db_mixin
from neutron.db import models_v2
from neutron.objects import base as base_obj
@ -91,7 +91,7 @@ class DbBasePluginCommon(common_db_mixin.CommonDbMixin):
def _generate_mac():
return net.get_random_mac(cfg.CONF.base_mac.split(':'))
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def _is_mac_in_use(self, context, network_id, mac_address):
return port_obj.Port.objects_exist(context, network_id=network_id,
mac_address=mac_address)
@ -110,7 +110,7 @@ class DbBasePluginCommon(common_db_mixin.CommonDbMixin):
subnet_id=subnet_id)
@staticmethod
@db_api.context_manager.writer
@db_api.CONTEXT_WRITER
def _store_ip_allocation(context, ip_address, network_id, subnet_id,
port_id):
LOG.debug("Allocated IP %(ip_address)s "
@ -263,11 +263,11 @@ class DbBasePluginCommon(common_db_mixin.CommonDbMixin):
context, network_id=network_id,
device_owner=constants.DEVICE_OWNER_ROUTER_GW)
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def _get_subnets_by_network(self, context, network_id):
return subnet_obj.Subnet.get_objects(context, network_id=network_id)
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def _get_subnets_by_subnetpool(self, context, subnetpool_id):
return subnet_obj.Subnet.get_objects(context,
subnetpool_id=subnetpool_id)
@ -279,7 +279,7 @@ class DbBasePluginCommon(common_db_mixin.CommonDbMixin):
filters = filters or {}
# TODO(ihrachys) remove explicit reader usage when subnet OVO switches
# to engine facade by default
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
return subnet_obj.Subnet.get_objects(context, _pager=pager,
validate_filters=False,
**filters)

@ -26,7 +26,7 @@ from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
from neutron_lib import constants
from neutron_lib import context as ctx
from neutron_lib.db import api as lib_db_api
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as ndb_utils
from neutron_lib import exceptions as exc
from neutron_lib.exceptions import l3 as l3_exc
@ -48,7 +48,6 @@ from neutron.common import ipv6_utils
from neutron.common import utils
from neutron.db import _model_query as model_query
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.db import db_base_plugin_common
from neutron.db import ipam_pluggable_backend
from neutron.db import models_v2
@ -166,18 +165,18 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
# NOTE(arosen) These event listeners are here to hook into when
# port status changes and notify nova about their change.
self.nova_notifier = nova.Notifier.get_instance()
lib_db_api.sqla_listen(models_v2.Port, 'after_insert',
self.nova_notifier.send_port_status)
lib_db_api.sqla_listen(models_v2.Port, 'after_update',
self.nova_notifier.send_port_status)
lib_db_api.sqla_listen(
db_api.sqla_listen(models_v2.Port, 'after_insert',
self.nova_notifier.send_port_status)
db_api.sqla_listen(models_v2.Port, 'after_update',
self.nova_notifier.send_port_status)
db_api.sqla_listen(
models_v2.Port.status, 'set',
self.nova_notifier.record_port_status_changed)
@registry.receives(resources.RBAC_POLICY, [events.BEFORE_CREATE,
events.BEFORE_UPDATE,
events.BEFORE_DELETE])
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def validate_network_rbac_policy_change(self, resource, event, trigger,
context, object_type, policy,
**kwargs):
@ -369,7 +368,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
collection = "%ss" % resource
items = request_items[collection]
try:
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
for item in items:
obj_creator = getattr(self, 'create_%s' % resource)
objects.append(obj_creator(context, item))
@ -380,11 +379,11 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
{'resource': resource, 'item': item})
return objects
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_network_bulk(self, context, networks):
return self._create_bulk('network', context, networks)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_network(self, context, network):
"""Handle creation of a single network."""
net_db = self.create_network_db(context, network)
@ -394,7 +393,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
def create_network_db(self, context, network):
# single request processing
n = network['network']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
args = {'tenant_id': n['tenant_id'],
'id': n.get('id') or uuidutils.generate_uuid(),
'name': n['name'],
@ -411,10 +410,10 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
context.session.add(network)
return network
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def update_network(self, context, id, network):
n = network['network']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
network = self._get_network(context, id)
# validate 'shared' parameter
if 'shared' in n:
@ -459,13 +458,13 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
if non_auto_ports.count():
raise exc.NetworkInUse(net_id=net_id)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def delete_network(self, context, id):
registry.publish(resources.NETWORK, events.BEFORE_DELETE, self,
payload=events.DBEventPayload(
context, resource_id=id))
self._ensure_network_not_in_use(context, id)
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
auto_delete_port_ids = [p.id for p in context.session.query(
models_v2.Port.id).filter_by(network_id=id).filter(
models_v2.Port.device_owner.in_(AUTO_DELETE_PORT_OWNERS))]
@ -478,12 +477,12 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
"The port has already been deleted.", port_id)
# clean up subnets
subnets = self._get_subnets_by_network(context, id)
with lib_db_api.exc_to_retry(os_db_exc.DBReferenceError):
with db_api.exc_to_retry(os_db_exc.DBReferenceError):
# retry reference errors so we can check the port type and
# cleanup if a network-owned port snuck in without failing
for subnet in subnets:
self._delete_subnet(context, subnet)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
network_db = self._get_network(context, id)
network = self._make_network_dict(network_db, context=context)
registry.notify(resources.NETWORK, events.PRECOMMIT_DELETE,
@ -497,12 +496,12 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
registry.notify(resources.NETWORK, events.AFTER_DELETE,
self, context=context, network=network)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_network(self, context, id, fields=None):
network = self._get_network(context, id)
return self._make_network_dict(network, fields, context=context)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def _get_networks(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
@ -518,7 +517,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
marker_obj=marker_obj,
page_reverse=page_reverse)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_networks(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
@ -531,12 +530,12 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
limit=limit, marker=marker, page_reverse=page_reverse)
]
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_networks_count(self, context, filters=None):
return model_query.get_collection_count(context, models_v2.Network,
filters=filters)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_subnet_bulk(self, context, subnets):
return self._create_bulk('subnet', context, subnets)
@ -608,7 +607,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
# a subnet-update and a router-interface-add operation are
# executed concurrently
if cur_subnet and not ipv6_utils.is_ipv6_pd_enabled(s):
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
# TODO(electrocucaracha): Look a solution for Join in OVO
ipal = models_v2.IPAllocation
alloc_qry = context.session.query(ipal.port_id)
@ -717,7 +716,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
external_gateway_info}}
l3plugin.update_router(context, router_id, info)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def _create_subnet_postcommit(self, context, result, network, ipam_subnet):
if hasattr(network, 'external') and network.external:
self._update_router_gw_ports(context,
@ -780,7 +779,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
msg = _('No default subnetpool found for IPv%s') % ip_version
raise exc.BadRequest(resource='subnets', msg=msg)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_subnet(self, context, subnet):
result, net, ipam_sub = self._create_subnet_precommit(context, subnet)
self._create_subnet_postcommit(context, result, net, ipam_sub)
@ -828,7 +827,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
raise exc.BadRequest(resource='subnets', msg=msg)
self._validate_subnet(context, s)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
network = self._get_network(context,
subnet['subnet']['network_id'])
subnet, ipam_subnet = self.ipam.allocate_subnet(context,
@ -849,7 +848,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
'end': str(netaddr.IPAddress(p.last, subnet['ip_version']))}
for p in allocation_pools]
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def update_subnet(self, context, id, subnet):
"""Update the subnet with new info.
@ -917,7 +916,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
registry.notify(resources.SUBNET, events.BEFORE_UPDATE,
self, **kwargs)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
subnet, changes = self.ipam.update_db_subnet(context, id, s,
db_pools)
return self._make_subnet_dict(subnet, context=context), orig
@ -972,13 +971,13 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
**kwargs)
return result
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def _subnet_get_user_allocation(self, context, subnet_id):
"""Check if there are any user ports on subnet and return first."""
return port_obj.IPAllocation.get_alloc_by_subnet_id(
context, subnet_id, AUTO_DELETE_PORT_OWNERS)
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def _subnet_check_ip_allocations_internal_router_ports(self, context,
subnet_id):
# Do not delete the subnet if IP allocations for internal
@ -990,7 +989,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
"cannot delete", subnet_id)
raise exc.SubnetInUse(subnet_id=subnet_id)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def _remove_subnet_from_port(self, context, sub_id, port_id, auto_subnet):
try:
fixed = [f for f in self.get_port(context, port_id)['fixed_ips']
@ -1018,7 +1017,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
'subnet': id})
raise exc.SubnetInUse(subnet_id=id)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def _remove_subnet_ip_allocations_from_ports(self, context, id):
# Do not allow a subnet to be deleted if a router is attached to it
self._subnet_check_ip_allocations_internal_router_ports(
@ -1036,7 +1035,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
self._remove_subnet_from_port(context, id, port_id,
auto_subnet=is_auto_addr_subnet)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def delete_subnet(self, context, id):
LOG.debug("Deleting subnet %s", id)
# Make sure the subnet isn't used by other resources
@ -1046,8 +1045,8 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
self._delete_subnet(context, subnet)
def _delete_subnet(self, context, subnet):
with lib_db_api.exc_to_retry(sql_exc.IntegrityError), \
db_api.context_manager.writer.using(context):
with db_api.exc_to_retry(sql_exc.IntegrityError), \
db_api.CONTEXT_WRITER.using(context):
registry.notify(resources.SUBNET, events.PRECOMMIT_DELETE,
self, context=context, subnet_id=subnet.id)
subnet.delete()
@ -1057,12 +1056,12 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
registry.notify(resources.SUBNET, events.AFTER_DELETE,
self, context=context, subnet=subnet.to_dict())
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_subnet(self, context, id, fields=None):
subnet_obj = self._get_subnet_object(context, id)
return self._make_subnet_dict(subnet_obj, fields, context=context)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_subnets(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
@ -1073,13 +1072,13 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
for subnet_object in subnet_objs
]
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_subnets_count(self, context, filters=None):
filters = filters or {}
return subnet_obj.Subnet.count(context, validate_filters=False,
**filters)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_subnets_by_network(self, context, network_id):
return [self._make_subnet_dict(subnet_obj) for subnet_obj in
self._get_subnets_by_network(context, network_id)]
@ -1153,7 +1152,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
"been set. Only one default may exist per IP family")
raise exc.InvalidInput(error_message=msg)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_subnetpool(self, context, subnetpool):
sp = subnetpool['subnetpool']
sp_reader = subnet_alloc.SubnetPoolReader(sp)
@ -1182,11 +1181,11 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
return self._make_subnetpool_dict(subnetpool.db_obj)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def update_subnetpool(self, context, id, subnetpool):
new_sp = subnetpool['subnetpool']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
orig_sp = self._get_subnetpool(context, id=id)
updated = _update_subnetpool_dict(orig_sp, new_sp)
reader = subnet_alloc.SubnetPoolReader(updated)
@ -1220,12 +1219,12 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
updated, orig_sp.db_obj)
return updated
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_subnetpool(self, context, id, fields=None):
subnetpool = self._get_subnetpool(context, id)
return self._make_subnetpool_dict(subnetpool.db_obj, fields)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_subnetpools(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
@ -1238,7 +1237,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
for pool in subnetpools
]
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_default_subnetpool(self, context, ip_version):
"""Retrieve the default subnetpool for the given IP version."""
filters = {'is_default': True,
@ -1247,9 +1246,9 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
if subnetpool:
return subnetpool[0]
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def delete_subnetpool(self, context, id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
subnetpool = self._get_subnetpool(context, id=id)
if subnet_obj.Subnet.objects_exist(context, subnetpool_id=id):
reason = _("Subnet pool has existing allocations")
@ -1264,7 +1263,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
op=_("mac address update"), port_id=id,
device_owner=device_owner)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_port_bulk(self, context, ports):
return self._create_bulk('port', context, ports)
@ -1281,7 +1280,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
context.session.add(db_port)
return db_port
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_port(self, context, port):
db_port = self.create_port_db(context, port)
return self._make_port_dict(db_port, process_extensions=False)
@ -1306,7 +1305,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
description=p.get('description'))
if p.get('mac_address') is not constants.ATTR_NOT_SPECIFIED:
port_data['mac_address'] = p.get('mac_address')
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# Ensure that the network exists.
self._get_network(context, network_id)
@ -1345,11 +1344,11 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
self._check_mac_addr_update(context, db_port,
new_mac, current_owner)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def update_port(self, context, id, port):
new_port = port['port']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
db_port = self._get_port(context, id)
new_mac = new_port.get('mac_address')
self._validate_port_for_update(context, db_port, new_port, new_mac)
@ -1380,13 +1379,13 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
raise os_db_exc.RetryRequest(e)
return self._make_port_dict(db_port)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def delete_port(self, context, id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
self.ipam.delete_port(context, id)
def delete_ports_by_device_id(self, context, device_id, network_id=None):
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
query = (context.session.query(models_v2.Port.id)
.enable_eagerloads(False)
.filter(models_v2.Port.device_id == device_id))
@ -1402,8 +1401,8 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
"The port has already been deleted.",
port_id)
@lib_db_api.retry_if_session_inactive()
@db_api.context_manager.reader
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_READER
def get_port(self, context, id, fields=None):
port = self._get_port(context, id)
return self._make_port_dict(port, fields)
@ -1427,7 +1426,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
Port.fixed_ips.any(IPAllocation.subnet_id.in_(subnet_ids)))
return query
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_ports(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
@ -1442,7 +1441,7 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
items.reverse()
return items
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_ports_count(self, context, filters=None):
return self._get_ports_query(context, filters).count()

@ -20,7 +20,7 @@ from neutron_lib.callbacks import events
from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
from neutron_lib import constants
from neutron_lib.db import api as lib_db_api
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as n_exc
from neutron_lib.exceptions import dvr as dvr_exc
from neutron_lib.objects import exceptions
@ -33,7 +33,6 @@ from sqlalchemy import or_
from neutron.common import utils
from neutron.conf.db import dvr_mac_db
from neutron.db import api as db_api
from neutron.db import models_v2
from neutron.extensions import dvr as ext_dvr
from neutron.objects import router
@ -60,7 +59,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
return self._plugin
@staticmethod
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def _db_delete_mac_associated_with_agent(context, agent):
host = agent['host']
plugin = directory.get_plugin()
@ -85,7 +84,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
DVRDbMixin._db_delete_mac_associated_with_agent(
payload.context, payload.latest_state)
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def _get_dvr_mac_address_by_host(self, context, host):
dvr_obj = router.DVRMacAddress.get_object(context, host=host)
if not dvr_obj:
@ -93,9 +92,9 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
return self._make_dvr_mac_address_dict(dvr_obj)
@utils.transaction_guard
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def _create_dvr_mac_address_retry(self, context, host, base_mac):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
mac_address = net.get_random_mac(base_mac)
dvr_mac_binding = router.DVRMacAddress(
context, host=host, mac_address=netaddr.EUI(mac_address))
@ -120,7 +119,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
db_api.MAX_RETRIES)
raise n_exc.HostMacAddressGenerationFailure(host=host)
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def get_dvr_mac_address_list(self, context):
return [
dvr_mac.to_dict()
@ -142,7 +141,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
'mac_address': str(dvr_mac_entry['mac_address'])}
@log_helpers.log_method_call
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_ports_on_host_by_subnet(self, context, host, subnet):
"""Returns DVR serviced ports on a given subnet in the input host
@ -172,7 +171,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
return ports
@log_helpers.log_method_call
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_subnet_for_dvr(self, context, subnet, fixed_ips=None):
if fixed_ips:
subnet_data = fixed_ips[0]['subnet_id']

@ -15,9 +15,9 @@
from neutron_lib.api.definitions import extra_dhcp_opt as edo_ext
from neutron_lib.api.definitions import port as port_def
from neutron_lib.db import api as db_api
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.objects.port.extensions import extra_dhcp_opt as obj_extra_dhcp
@ -41,7 +41,7 @@ class ExtraDhcpOptMixin(object):
extra_dhcp_opts):
if not extra_dhcp_opts:
return port
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
for dopt in extra_dhcp_opts:
if self._is_valid_opt_value(dopt['opt_name'],
dopt['opt_value']):
@ -79,7 +79,7 @@ class ExtraDhcpOptMixin(object):
context, port_id=id)
# if there are currently no dhcp_options associated to
# this port, Then just insert the new ones and be done.
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
for upd_rec in dopts:
for opt in opts:
if (opt['opt_name'] == upd_rec['opt_name'] and

@ -12,12 +12,12 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as db_utils
from neutron_lib.exceptions import flavors as flav_exc
from oslo_db import exception as db_exc
from oslo_log import log as logging
from neutron.db import api as db_api
from neutron.db import common_db_mixin
from neutron.db import servicetype_db as sdb
from neutron.objects import base as base_obj
@ -101,7 +101,7 @@ class FlavorsDbMixin(common_db_mixin.CommonDbMixin):
return self._make_flavor_dict(obj)
def update_flavor(self, context, flavor_id, flavor):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
self._ensure_flavor_not_in_use(context, flavor_id)
fl_obj = self._get_flavor(context, flavor_id)
fl_obj.update_fields(flavor['flavor'])
@ -118,7 +118,7 @@ class FlavorsDbMixin(common_db_mixin.CommonDbMixin):
# flavors so for now we just capture the foreign key violation
# to detect if it's in use.
try:
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
self._ensure_flavor_not_in_use(context, flavor_id)
self._get_flavor(context, flavor_id).delete()
except db_exc.DBReferenceError:
@ -136,7 +136,7 @@ class FlavorsDbMixin(common_db_mixin.CommonDbMixin):
def create_flavor_service_profile(self, context,
service_profile, flavor_id):
sp = service_profile['service_profile']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
if obj_flavor.FlavorServiceProfileBinding.objects_exist(
context, service_profile_id=sp['id'], flavor_id=flavor_id):
raise flav_exc.FlavorServiceProfileBindingExists(
@ -189,7 +189,7 @@ class FlavorsDbMixin(common_db_mixin.CommonDbMixin):
if sp.get('driver'):
self._validate_driver(context, sp['driver'])
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
self._ensure_service_profile_not_in_use(context,
service_profile_id)
sp_obj = self._get_service_profile(context, service_profile_id)
@ -202,7 +202,7 @@ class FlavorsDbMixin(common_db_mixin.CommonDbMixin):
return self._make_service_profile_dict(sp_db, fields)
def delete_service_profile(self, context, sp_id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
self._ensure_service_profile_not_in_use(context, sp_id)
self._get_service_profile(context, sp_id).delete()

@ -22,6 +22,7 @@ from neutron_lib.api.definitions import ip_allocation as ipalloc_apidef
from neutron_lib.api.definitions import portbindings
from neutron_lib.api import validators
from neutron_lib import constants as const
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as db_utils
from neutron_lib import exceptions as exc
from oslo_config import cfg
@ -32,7 +33,6 @@ from neutron._i18n import _
from neutron.common import exceptions as n_exc
from neutron.common import ipv6_utils
from neutron.common import utils as common_utils
from neutron.db import api as db_api
from neutron.db import db_base_plugin_common
from neutron.db import models_v2
from neutron.extensions import segment
@ -159,7 +159,7 @@ class IpamBackendMixin(db_base_plugin_common.DbBasePluginCommon):
del s["dns_nameservers"]
return new_dns_addr_list
@db_api.context_manager.writer
@db_api.CONTEXT_WRITER
def _update_subnet_allocation_pools(self, context, subnet_id, s):
subnet_obj.IPAllocationPool.delete_objects(context,
subnet_id=subnet_id)

@ -18,6 +18,7 @@ import copy
import netaddr
from neutron_lib.api.definitions import portbindings
from neutron_lib import constants
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as n_exc
from oslo_db import exception as db_exc
from oslo_log import log as logging
@ -25,7 +26,6 @@ from oslo_utils import excutils
from sqlalchemy import and_
from neutron.common import ipv6_utils
from neutron.db import api as db_api
from neutron.db import ipam_backend_mixin
from neutron.db import models_v2
from neutron.ipam import driver
@ -322,7 +322,7 @@ class IpamPluggableBackend(ipam_backend_mixin.IpamBackendMixin):
original=changes.original,
remove=removed)
@db_api.context_manager.writer
@db_api.CONTEXT_WRITER
def save_allocation_pools(self, context, subnet, allocation_pools):
for pool in allocation_pools:
first_ip = str(netaddr.IPAddress(pool.first, pool.version))

@ -13,6 +13,7 @@
# under the License.
import netaddr
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as db_utils
from neutron_lib.exceptions import metering as metering_exc
from oslo_db import exception as db_exc
@ -20,7 +21,6 @@ from oslo_utils import uuidutils
from neutron.api.rpc.agentnotifiers import metering_rpc_agent_api
from neutron.common import constants
from neutron.db import api as db_api
from neutron.db import common_db_mixin as base_db
from neutron.db import l3_dvr_db
from neutron.extensions import metering
@ -131,7 +131,7 @@ class MeteringDbMixin(metering.MeteringPluginBase,
def create_metering_label_rule(self, context, metering_label_rule):
m = metering_label_rule['metering_label_rule']
try:
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
label_id = m['metering_label_id']
ip_prefix = m['remote_ip_prefix']
direction = m['direction']
@ -151,7 +151,7 @@ class MeteringDbMixin(metering.MeteringPluginBase,
return self._make_metering_label_rule_dict(rule)
def delete_metering_label_rule(self, context, rule_id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
rule = self._get_metering_label_rule(context, rule_id)
rule.delete()

@ -14,10 +14,10 @@
# limitations under the License.
import netaddr
from neutron_lib.db import api as db_api
import six
from sqlalchemy import func
from neutron.db import api as db_api
import neutron.db.models_v2 as mod
NETWORK_ID = 'network_id'
@ -86,7 +86,7 @@ class IpAvailabilityMixin(object):
return net_ip_availabilities
@classmethod
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def _build_network_used_ip_query(cls, context, filters):
# Generate a query to gather network/subnet/used_ips.
# Ensure query is tolerant of missing child table data (outerjoins)
@ -103,7 +103,7 @@ class IpAvailabilityMixin(object):
return cls._adjust_query_for_filters(query, filters)
@classmethod
@db_api.context_manager.reader
@db_api.CONTEXT_READER
def _build_total_ips_query(cls, context, filters):
query = context.session.query()
query = query.add_columns(*cls.total_ips_columns)

@ -16,11 +16,11 @@
from neutron_lib.api.definitions import port as port_def
from neutron_lib.api.definitions import portbindings
from neutron_lib.api import validators
from neutron_lib.db import api as db_api
from neutron_lib.plugins import directory
from neutron.db import _model_query as model_query
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.db.models import portbinding as pmodels
from neutron.db import models_v2
from neutron.db import portbindings_base
@ -71,7 +71,7 @@ class PortBindingMixin(portbindings_base.PortBindingBaseMixin):
host = port_data.get(portbindings.HOST_ID)
host_set = validators.is_attr_set(host)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
bind_port = context.session.query(
pmodels.PortBindingPort).filter_by(port_id=port['id']).first()
if host_set:
@ -85,7 +85,7 @@ class PortBindingMixin(portbindings_base.PortBindingBaseMixin):
self._extend_port_dict_binding_host(port, host)
def get_port_host(self, context, port_id):
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
bind_port = (
context.session.query(pmodels.PortBindingPort.host).
filter_by(port_id=port_id).

@ -15,9 +15,8 @@
import collections
import datetime
from neutron_lib.db import api as lib_db_api
from neutron_lib.db import api as db_api
from neutron.db import api as db_api
from neutron.objects import quota as quota_obj
@ -37,7 +36,7 @@ class ReservationInfo(collections.namedtuple(
"""Information about a resource reservation."""
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_quota_usage_by_resource_and_tenant(context, resource, tenant_id):
"""Return usage info for a given resource and tenant.
@ -55,7 +54,7 @@ def get_quota_usage_by_resource_and_tenant(context, resource, tenant_id):
result.dirty)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_quota_usage_by_resource(context, resource):
objs = quota_obj.QuotaUsage.get_objects(context, resource=resource)
return [QuotaUsageInfo(item.resource,
@ -64,7 +63,7 @@ def get_quota_usage_by_resource(context, resource):
item.dirty) for item in objs]
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_quota_usage_by_tenant_id(context, tenant_id):
objs = quota_obj.QuotaUsage.get_objects(context, project_id=tenant_id)
return [QuotaUsageInfo(item.resource,
@ -73,7 +72,7 @@ def get_quota_usage_by_tenant_id(context, tenant_id):
item.dirty) for item in objs]
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def set_quota_usage(context, resource, tenant_id,
in_use=None, delta=False):
"""Set resource quota usage.
@ -87,7 +86,7 @@ def set_quota_usage(context, resource, tenant_id,
:param delta: Specifies whether in_use is an absolute number
or a delta (default to False)
"""
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
usage_data = quota_obj.QuotaUsage.get_object(
context, resource=resource, project_id=tenant_id)
if not usage_data:
@ -107,8 +106,8 @@ def set_quota_usage(context, resource, tenant_id,
usage_data.in_use, usage_data.dirty)
@lib_db_api.retry_if_session_inactive()
@db_api.context_manager.writer
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_WRITER
def set_quota_usage_dirty(context, resource, tenant_id, dirty=True):
"""Set quota usage dirty bit for a given resource and tenant.
@ -126,8 +125,8 @@ def set_quota_usage_dirty(context, resource, tenant_id, dirty=True):
return 0
@lib_db_api.retry_if_session_inactive()
@db_api.context_manager.writer
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_WRITER
def set_resources_quota_usage_dirty(context, resources, tenant_id, dirty=True):
"""Set quota usage dirty bit for a given tenant and multiple resources.
@ -147,8 +146,8 @@ def set_resources_quota_usage_dirty(context, resources, tenant_id, dirty=True):
return len(objs)
@lib_db_api.retry_if_session_inactive()
@db_api.context_manager.writer
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_WRITER
def set_all_quota_usage_dirty(context, resource, dirty=True):
"""Set the dirty bit on quota usage for all tenants.
@ -165,7 +164,7 @@ def set_all_quota_usage_dirty(context, resource, dirty=True):
return len(objs)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_reservation(context, tenant_id, deltas, expiration=None):
# This method is usually called from within another transaction.
# Consider using begin_nested
@ -185,7 +184,7 @@ def create_reservation(context, tenant_id, deltas, expiration=None):
for delta in reserv_obj.resource_deltas))
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_reservation(context, reservation_id):
reserv_obj = quota_obj.Reservation.get_object(context, id=reservation_id)
if not reserv_obj:
@ -197,8 +196,8 @@ def get_reservation(context, reservation_id):
for delta in reserv_obj.resource_deltas))
@lib_db_api.retry_if_session_inactive()
@db_api.context_manager.writer
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_WRITER
def remove_reservation(context, reservation_id, set_dirty=False):
reservation = quota_obj.Reservation.get_object(context, id=reservation_id)
if not reservation:
@ -214,7 +213,7 @@ def remove_reservation(context, reservation_id, set_dirty=False):
return 1
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_reservations_for_resources(context, tenant_id, resources,
expired=False):
"""Retrieve total amount of reservations for specified resources.
@ -233,7 +232,7 @@ def get_reservations_for_resources(context, tenant_id, resources,
context, utcnow(), tenant_id, resources, expired)
@lib_db_api.retry_if_session_inactive()
@db_api.context_manager.writer
@db_api.retry_if_session_inactive()
@db_api.CONTEXT_WRITER
def remove_expired_reservations(context, tenant_id=None):
return quota_obj.Reservation.delete_expired(context, utcnow(), tenant_id)

@ -14,14 +14,13 @@
# under the License.
from neutron_lib.api import attributes
from neutron_lib.db import api as lib_db_api
from neutron_lib.db import api as db_api
from neutron_lib import exceptions
from neutron_lib.plugins import constants
from neutron_lib.plugins import directory
from oslo_log import log
from neutron.common import exceptions as n_exc
from neutron.db import api as db_api
from neutron.db.quota import api as quota_api
from neutron.objects import quota as quota_obj
from neutron.quota import resource as res
@ -53,7 +52,7 @@ class DbQuotaDriver(object):
for key, resource in resources.items())
@staticmethod
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_tenant_quotas(context, resources, tenant_id):
"""Given a list of resources, retrieve the quotas for the given
tenant. If no limits are found for the specified tenant, the operation
@ -77,7 +76,7 @@ class DbQuotaDriver(object):
return tenant_quota
@staticmethod
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_detailed_tenant_quotas(context, resources, tenant_id):
"""Given a list of resources and a sepecific tenant, retrieve
the detailed quotas (limit, used, reserved).
@ -112,7 +111,7 @@ class DbQuotaDriver(object):
return tenant_quota_ext
@staticmethod
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def delete_tenant_quota(context, tenant_id):
"""Delete the quota entries for a given tenant_id.
@ -126,7 +125,7 @@ class DbQuotaDriver(object):
raise n_exc.TenantQuotaNotFound(tenant_id=tenant_id)
@staticmethod
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def get_all_quotas(context, resources):
"""Given a list of resources, retrieve the quotas for the all tenants.
@ -159,7 +158,7 @@ class DbQuotaDriver(object):
return list(all_tenant_quotas.values())
@staticmethod
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def update_quota_limit(context, tenant_id, resource, limit):
tenant_quotas = quota_obj.Quota.get_objects(
context, project_id=tenant_id, resource=resource)
@ -194,7 +193,7 @@ class DbQuotaDriver(object):
quota_api.remove_expired_reservations(
context, tenant_id=tenant_id)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def make_reservation(self, context, tenant_id, resources, deltas, plugin):