use context manager from neutron-lib

Access to neutron.db.api's context manager is already in neutron-lib
and in fact neutron is already using it as a shim. This patch switches
over context manager access to use neutron-lib's accessors.
Also see https://review.openstack.org/#/c/613122

Change-Id: I13eb3a25a5bd83bb00dfa4a7430324551fea0f2e
changes/25/613125/1
Boden R 4 years ago
parent 00708913cb
commit d29a0baa80
  1. 10
      vmware_nsx/common/sync.py
  2. 4
      vmware_nsx/db/db.py
  3. 10
      vmware_nsx/db/extended_security_group.py
  4. 6
      vmware_nsx/db/extended_security_group_rule.py
  5. 16
      vmware_nsx/db/lsn_db.py
  6. 4
      vmware_nsx/db/maclearning.py
  7. 22
      vmware_nsx/db/networkgw_db.py
  8. 4
      vmware_nsx/db/nsx_portbindings_db.py
  9. 14
      vmware_nsx/db/qos_db.py
  10. 4
      vmware_nsx/dhcp_meta/rpc.py
  11. 8
      vmware_nsx/plugins/common/plugin.py
  12. 20
      vmware_nsx/plugins/dvs/plugin.py
  13. 17
      vmware_nsx/plugins/nsx/plugin.py
  14. 52
      vmware_nsx/plugins/nsx_mh/plugin.py
  15. 24
      vmware_nsx/plugins/nsx_p/plugin.py
  16. 39
      vmware_nsx/plugins/nsx_v/plugin.py
  17. 32
      vmware_nsx/plugins/nsx_v3/plugin.py
  18. 6
      vmware_nsx/shell/admin/plugins/nsxv/resources/securitygroups.py
  19. 6
      vmware_nsx/shell/admin/plugins/nsxv3/resources/securitygroups.py
  20. 8
      vmware_nsx/tests/unit/extensions/test_provider_security_groups.py
  21. 4
      vmware_nsx/tests/unit/extensions/test_secgroup_rule_local_ip_prefix.py
  22. 6
      vmware_nsx/tests/unit/extensions/test_vnic_index.py

@ -18,6 +18,7 @@ import random
from neutron_lib import constants
from neutron_lib import context as n_context
from neutron_lib.db import api as db_api
from neutron_lib import exceptions
from neutron_lib.exceptions import l3 as l3_exc
from oslo_log import log
@ -27,7 +28,6 @@ from oslo_utils import timeutils
import six
from neutron.db import _model_query as model_query
from neutron.db import api as db_api
from neutron.db.models import external_net as external_net_db
from neutron.db.models import l3 as l3_db
from neutron.db import models_v2
@ -302,7 +302,7 @@ class NsxSynchronizer(object):
# do nothing
return
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
try:
network = self._plugin._get_network(context,
neutron_network_data['id'])
@ -384,7 +384,7 @@ class NsxSynchronizer(object):
# do nothing
return
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
try:
router = self._plugin._get_router(context,
neutron_router_data['id'])
@ -436,7 +436,7 @@ class NsxSynchronizer(object):
(models_v2.Network.id ==
external_net_db.ExternalNetwork.network_id))]
if neutron_port_data['network_id'] in ext_networks:
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
neutron_port_data['status'] = constants.PORT_STATUS_ACTIVE
return
@ -479,7 +479,7 @@ class NsxSynchronizer(object):
# do nothing
return
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
try:
port = self._plugin._get_port(context,
neutron_port_data['id'])

@ -23,8 +23,6 @@ from oslo_log import log as logging
from oslo_utils import excutils
from oslo_utils import uuidutils
import neutron.db.api as db
from vmware_nsx.common import exceptions as nsx_exc
from vmware_nsx.db import nsx_models
@ -440,7 +438,7 @@ def delete_port_mirror_session_mapping(session, tf_id):
filter_by(tap_flow_id=tf_id).delete())
@db.context_manager.writer
@db_api.CONTEXT_WRITER
def save_sg_mappings(context, sg_id, nsgroup_id, section_id):
context.session.add(
nsx_models.NeutronNsxFirewallSectionMapping(neutron_id=sg_id,

@ -21,7 +21,6 @@ from sqlalchemy.orm import exc
from sqlalchemy import sql
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.db.models import securitygroup as securitygroups_db
from neutron.extensions import securitygroup as ext_sg
from neutron_lib.api.definitions import port as port_def
@ -30,6 +29,7 @@ from neutron_lib.callbacks import events
from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
from neutron_lib import constants as n_constants
from neutron_lib.db import api as db_api
from neutron_lib.db import model_base
from neutron_lib.objects import registry as obj_reg
from neutron_lib.utils import helpers
@ -97,7 +97,7 @@ class ExtendedSecurityGroupPropertiesMixin(object):
if not default_sg:
self._ensure_default_security_group(context, tenant_id)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
sg = obj_reg.new_instance(
'SecurityGroup', context,
id=s.get('id') or uuidutils.generate_uuid(),
@ -123,7 +123,7 @@ class ExtendedSecurityGroupPropertiesMixin(object):
default_sg=False):
self._validate_security_group_properties_create(
context, sg_req, default_sg)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
properties = NsxExtendedSecurityGroupProperties(
security_group_id=sg_res['id'],
logging=sg_req.get(sg_logging.LOGGING, False),
@ -135,7 +135,7 @@ class ExtendedSecurityGroupPropertiesMixin(object):
sg_res[sg_policy.POLICY] = sg_req.get(sg_policy.POLICY)
def _get_security_group_properties(self, context, security_group_id):
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
try:
prop = context.session.query(
NsxExtendedSecurityGroupProperties).filter_by(
@ -152,7 +152,7 @@ class ExtendedSecurityGroupPropertiesMixin(object):
(sg_policy.POLICY in sg_req and
(sg_req[sg_policy.POLICY] !=
sg_res.get(sg_policy.POLICY)))):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
prop = context.session.query(
NsxExtendedSecurityGroupProperties).filter_by(
security_group_id=sg_res['id']).one()

@ -19,10 +19,10 @@ from sqlalchemy import orm
from sqlalchemy.orm import exc
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.db.models import securitygroup
from neutron.extensions import securitygroup as ext_sg
from neutron_lib.api import validators
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as nexception
from vmware_nsx._i18n import _
from vmware_nsx.extensions import secgroup_rule_local_ip_prefix as ext_local_ip
@ -75,7 +75,7 @@ class ExtendedSecurityGroupRuleMixin(object):
rule_req.get(ext_local_ip.LOCAL_IP_PREFIX)):
return
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
properties = NsxExtendedSecurityGroupRuleProperties(
rule_id=rule_res['id'],
local_ip_prefix=rule_req[ext_local_ip.LOCAL_IP_PREFIX])
@ -93,7 +93,7 @@ class ExtendedSecurityGroupRuleMixin(object):
sg_rule_res[ext_local_ip.LOCAL_IP_PREFIX] = None
def _get_security_group_rule_local_ip(self, context, rule_id):
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
try:
prop = context.session.query(
NsxExtendedSecurityGroupRuleProperties).filter_by(

@ -15,6 +15,7 @@
# under the License.
#
from neutron_lib.db import api as db_api
from oslo_db import exception as d_exc
from oslo_log import log as logging
from sqlalchemy import orm
@ -23,27 +24,26 @@ from vmware_nsx._i18n import _
from vmware_nsx.common import exceptions as p_exc
from vmware_nsx.db import nsx_models
from neutron.db import api as db_api
LOG = logging.getLogger(__name__)
def lsn_add(context, network_id, lsn_id):
"""Add Logical Service Node information to persistent datastore."""
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
lsn = nsx_models.Lsn(network_id, lsn_id)
context.session.add(lsn)
def lsn_remove(context, lsn_id):
"""Remove Logical Service Node information from datastore given its id."""
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
context.session.query(nsx_models.Lsn).filter_by(lsn_id=lsn_id).delete()
def lsn_remove_for_network(context, network_id):
"""Remove information about the Logical Service Node given its network."""
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
context.session.query(nsx_models.Lsn).filter_by(
net_id=network_id).delete()
@ -65,14 +65,14 @@ def lsn_get_for_network(context, network_id, raise_on_err=True):
def lsn_port_add_for_lsn(context, lsn_port_id, subnet_id, mac, lsn_id):
"""Add Logical Service Node Port information to persistent datastore."""
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
lsn_port = nsx_models.LsnPort(lsn_port_id, subnet_id, mac, lsn_id)
context.session.add(lsn_port)
def lsn_port_get_for_subnet(context, subnet_id, raise_on_err=True):
"""Return Logical Service Node Port information given its subnet id."""
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
try:
return (context.session.query(nsx_models.LsnPort).
filter_by(sub_id=subnet_id).one())
@ -85,7 +85,7 @@ def lsn_port_get_for_subnet(context, subnet_id, raise_on_err=True):
def lsn_port_get_for_mac(context, mac_address, raise_on_err=True):
"""Return Logical Service Node Port information given its mac address."""
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
try:
return (context.session.query(nsx_models.LsnPort).
filter_by(mac_addr=mac_address).one())
@ -98,6 +98,6 @@ def lsn_port_get_for_mac(context, mac_address, raise_on_err=True):
def lsn_port_remove(context, lsn_port_id):
"""Remove Logical Service Node port from the given Logical Service Node."""
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
(context.session.query(nsx_models.LsnPort).
filter_by(lsn_port_id=lsn_port_id).delete())

@ -17,9 +17,9 @@ from sqlalchemy.orm import exc
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_lib.api.definitions import port as port_def
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as db_utils
from oslo_log import log as logging
@ -59,7 +59,7 @@ class MacLearningDbMixin(object):
mac.MAC_LEARNING: enabled})
def _create_mac_learning_state(self, context, port):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
enabled = port[mac.MAC_LEARNING]
state = nsx_models.MacLearningState(
port_id=port['id'],

@ -15,8 +15,8 @@
from sqlalchemy.orm import exc as sa_orm_exc
from neutron.db import _model_query as model_query
from neutron.db import api as db_api
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
from neutron_lib.plugins import utils
@ -180,12 +180,12 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
return query.one() if only_one else query.all()
def _unset_default_network_gateways(self, context):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
context.session.query(nsx_models.NetworkGateway).update(
{nsx_models.NetworkGateway.default: False})
def _set_default_network_gateway(self, context, gw_id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
gw = (context.session.query(nsx_models.NetworkGateway).
filter_by(id=gw_id).one())
gw['default'] = True
@ -220,7 +220,7 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
validate_device_list=True):
gw_data = network_gateway[self.gateway_resource]
tenant_id = gw_data['tenant_id']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
gw_db = nsx_models.NetworkGateway(
id=gw_data.get('id', uuidutils.generate_uuid()),
tenant_id=tenant_id,
@ -238,7 +238,7 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
def update_network_gateway(self, context, id, network_gateway):
gw_data = network_gateway[self.gateway_resource]
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
gw_db = self._get_network_gateway(context, id)
if gw_db.default:
raise NetworkGatewayUnchangeable(gateway_id=id)
@ -253,7 +253,7 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
return self._make_network_gateway_dict(gw_db, fields)
def delete_network_gateway(self, context, id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
gw_db = self._get_network_gateway(context, id)
if gw_db.network_connections:
raise GatewayInUse(gateway_id=id)
@ -281,7 +281,7 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
"'%(network_gateway_id)s'",
{'network_id': network_id,
'network_gateway_id': network_gateway_id})
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
gw_db = self._get_network_gateway(context, network_gateway_id)
tenant_id = gw_db['tenant_id']
if context.is_admin and not tenant_id:
@ -358,7 +358,7 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
"'%(network_gateway_id)s'",
{'network_id': network_id,
'network_gateway_id': network_gateway_id})
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# Uniquely identify connection, otherwise raise
try:
net_connection = self._retrieve_gateway_connections(
@ -442,7 +442,7 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
initial_status=STATUS_UNKNOWN):
device_data = gateway_device[self.device_resource]
tenant_id = device_data['tenant_id']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
device_db = nsx_models.NetworkGatewayDevice(
id=device_data.get('id', uuidutils.generate_uuid()),
tenant_id=tenant_id,
@ -457,7 +457,7 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
def update_gateway_device(self, context, gateway_device_id,
gateway_device, include_nsx_id=False):
device_data = gateway_device[self.device_resource]
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
device_db = self._get_gateway_device(context, gateway_device_id)
# Ensure there is something to update before doing it
if any([device_db[k] != device_data[k] for k in device_data]):
@ -468,7 +468,7 @@ class NetworkGatewayMixin(networkgw.NetworkGatewayPluginBase):
device_db, include_nsx_id=include_nsx_id)
def delete_gateway_device(self, context, device_id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# A gateway device should not be deleted
# if it is used in any network gateway service
if self._is_device_in_use(context, device_id):

@ -21,12 +21,12 @@ from neutron_lib.api.definitions import portbindings as pbin
from neutron_lib.api.definitions import provider_net as pnet
from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib.db import api as db_api
from neutron_lib import exceptions
from neutron_lib.plugins import directory
from neutron_lib.plugins import utils as p_utils
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.db import portbindings_db as pbin_db
from neutron.plugins.ml2 import models as pbin_model
from vmware_nsx._i18n import _
@ -112,7 +112,7 @@ class NsxPortBindingMixin(pbin_db.PortBindingMixin):
elif network.get(pnet.NETWORK_TYPE) == c_utils.NsxVNetworkTypes.VLAN:
vif_details[pbin.VIF_DETAILS_VLAN] = network[pnet.SEGMENTATION_ID]
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
port_binding = context.session.query(
pbin_model.PortBinding).filter_by(port_id=port_id).first()

@ -17,11 +17,11 @@ from sqlalchemy.orm import exc
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 models_v2
from neutron_lib.api.definitions import network as net_def
from neutron_lib.api.definitions import port as port_def
from neutron_lib.db import api as db_api
from neutron_lib.db import utils as db_utils
from oslo_log import log
@ -39,7 +39,7 @@ class QoSDbMixin(qos.QueuePluginBase):
def create_qos_queue(self, context, qos_queue):
q = qos_queue['qos_queue']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
qos_queue = nsx_models.QoSQueue(
id=q.get('id', uuidutils.generate_uuid()),
name=q.get('name'),
@ -75,7 +75,7 @@ class QoSDbMixin(qos.QueuePluginBase):
page_reverse=page_reverse)
def delete_qos_queue(self, context, queue_id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
qos_queue = self._get_qos_queue(context, queue_id)
context.session.delete(qos_queue)
@ -83,7 +83,7 @@ class QoSDbMixin(qos.QueuePluginBase):
port_data[qos.QUEUE] = queue_id
if not queue_id:
return
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
context.session.add(nsx_models.PortQueueMapping(
port_id=port_data['id'],
queue_id=queue_id))
@ -105,14 +105,14 @@ class QoSDbMixin(qos.QueuePluginBase):
# did not already have a queue on it. There is no need to check
# if there is one before deleting if we return here.
return
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
context.session.delete(binding)
def _process_network_queue_mapping(self, context, net_data, queue_id):
net_data[qos.QUEUE] = queue_id
if not queue_id:
return
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
context.session.add(
nsx_models.NetworkQueueMapping(network_id=net_data['id'],
queue_id=queue_id))
@ -126,7 +126,7 @@ class QoSDbMixin(qos.QueuePluginBase):
def _delete_network_queue_mapping(self, context, network_id):
query = self._model_query(context, nsx_models.NetworkQueueMapping)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
binding = query.filter_by(network_id=network_id).first()
if binding:
context.session.delete(binding)

@ -15,13 +15,13 @@
#
from neutron_lib import constants as const
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as ntn_exc
from oslo_config import cfg
from oslo_db import exception as db_exc
from oslo_log import log as logging
from neutron.api.rpc.agentnotifiers import dhcp_rpc_agent_api
from neutron.db import api as db_api
from neutron.db import db_base_plugin_v2
from neutron.db import models_v2
@ -159,7 +159,7 @@ def _find_dhcp_disabled_subnet_by_port(plugin, context, ports):
# of the vmware plugin (and base db neutron plugin) to engine
# facade to avoid cross transaction session cache reuse but such
# change wouldn't happen overnight.
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
subnet = plugin.get_subnet(context, fixed_ip['subnet_id'])
if not subnet['enable_dhcp']:
return subnet

@ -18,7 +18,6 @@ from oslo_log import log as logging
from neutron.db import _resource_extend as resource_extend
from neutron.db import address_scope_db
from neutron.db import api as db_api
from neutron.db import db_base_plugin_v2
from neutron.db import l3_attrs_db
from neutron.db import l3_db
@ -38,6 +37,7 @@ from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
from neutron_lib import constants
from neutron_lib import context as n_context
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as n_exc
from neutron_lib.exceptions import allowedaddresspairs as addr_exc
from neutron_lib.plugins import directory
@ -70,7 +70,7 @@ class NsxPluginBase(db_base_plugin_v2.NeutronDbPluginV2,
ctx = n_context.get_admin_context()
# get the core plugin as this is a static method with no 'self'
plugin = directory.get_plugin()
with db_api.context_manager.writer.using(ctx):
with db_api.CONTEXT_WRITER.using(ctx):
plugin._extension_manager.extend_network_dict(
ctx.session, netdb, result)
@ -80,7 +80,7 @@ class NsxPluginBase(db_base_plugin_v2.NeutronDbPluginV2,
ctx = n_context.get_admin_context()
# get the core plugin as this is a static method with no 'self'
plugin = directory.get_plugin()
with db_api.context_manager.writer.using(ctx):
with db_api.CONTEXT_WRITER.using(ctx):
plugin._extension_manager.extend_port_dict(
ctx.session, portdb, result)
@ -90,7 +90,7 @@ class NsxPluginBase(db_base_plugin_v2.NeutronDbPluginV2,
ctx = n_context.get_admin_context()
# get the core plugin as this is a static method with no 'self'
plugin = directory.get_plugin()
with db_api.context_manager.writer.using(ctx):
with db_api.CONTEXT_WRITER.using(ctx):
plugin._extension_manager.extend_subnet_dict(
ctx.session, subnetdb, result)

@ -27,7 +27,6 @@ from neutron.api import extensions as neutron_extensions
from neutron.db import _resource_extend as resource_extend
from neutron.db import agentschedulers_db
from neutron.db import allowedaddresspairs_db as addr_pair_db
from neutron.db import api as db_api
from neutron.db import dns_db
from neutron.db import external_net_db
from neutron.db import l3_db
@ -46,6 +45,7 @@ from neutron_lib.api.definitions import provider_net as pnet
from neutron_lib.api.definitions import vlantransparent as vlan_apidef
from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as n_exc
from neutron_lib.plugins import utils
@ -212,7 +212,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
LOG.warning('One or more hosts may not be configured')
try:
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
new_net = super(NsxDvsV2, self).create_network(context,
network)
self._extension_manager.process_create_network(
@ -293,7 +293,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
network = self._get_network(context, id)
dvs_id = self._dvs_get_id(network)
bindings = nsx_db.get_network_bindings(context.session, id)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
nsx_db.delete_network_bindings(context.session, id)
super(NsxDvsV2, self).delete_network(context, id)
try:
@ -308,7 +308,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
self._dvs_delete_network(context, id)
def _dvs_get_network(self, context, id, fields=None):
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
# goto to the plugin DB and fetch the network
network = self._get_network(context, id)
# Don't do field selection here otherwise we won't be able
@ -325,7 +325,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
sorts=None, limit=None, marker=None,
page_reverse=False):
filters = filters or {}
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
networks = (
super(NsxDvsV2, self).get_networks(
context, filters, fields, sorts,
@ -340,7 +340,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
net_attrs = network['network']
providernet._raise_if_updates_provider_attributes(net_attrs)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
net_res = super(NsxDvsV2, self).update_network(context, id,
network)
self._extension_manager.process_update_network(context, net_attrs,
@ -374,7 +374,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
# shared network that is not owned by the tenant.
port_data = port['port']
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# First we allocate port in neutron database
neutron_db = super(NsxDvsV2, self).create_port(context, port)
self._extension_manager.process_create_port(
@ -439,7 +439,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
port)
has_addr_pairs = self._check_update_has_allowed_address_pairs(port)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
ret_port = super(NsxDvsV2, self).update_port(
context, id, port)
# Save current mac learning state to check whether it's
@ -497,7 +497,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
"""
neutron_db_port = self.get_port(context, id)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# metadata_dhcp_host_route
self.handle_port_metadata_access(
context, neutron_db_port, is_delete=True)
@ -509,7 +509,7 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
sorts=None, limit=None, marker=None,
page_reverse=False):
filters = filters or {}
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
ports = (
super(NsxDvsV2, self).get_ports(
context, filters, fields, sorts,

@ -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 context as n_context
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 db_utils
from neutron_lib.plugins import constants as plugin_constants
from neutron_lib.plugins import directory
@ -33,7 +33,6 @@ from neutron.db import _resource_extend as resource_extend
from neutron.db import agents_db
from neutron.db import agentschedulers_db
from neutron.db import allowedaddresspairs_db as addr_pair_db
from neutron.db import api as db_api
from neutron.db.availability_zone import router as router_az_db
from neutron.db import external_net_db
from neutron.db import extradhcpopt_db
@ -364,7 +363,7 @@ class NsxTVDPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
p = self._get_plugin_from_project(context, tenant_id)
return p.create_network(context, network)
@lib_db_api.retry_if_session_inactive()
@db_api.retry_if_session_inactive()
def create_network_bulk(self, context, networks):
#Implement create bulk so that the plugin calculation will be done once
objects = []
@ -378,7 +377,7 @@ class NsxTVDPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
# create all networks one by one
try:
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
for item in items:
objects.append(p.create_network(context, item))
except Exception:
@ -426,7 +425,7 @@ class NsxTVDPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
req_p = self._get_plugin_for_request(context, filters,
keys=['shared'])
filters = filters or {}
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
networks = (
super(NsxTVDPlugin, self).get_networks(
context, filters, fields, sorts,
@ -485,7 +484,7 @@ class NsxTVDPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
'network_id',
'fixed_ips'])
filters = filters or {}
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
ports = (
super(NsxTVDPlugin, self).get_ports(
context, filters, fields, sorts,
@ -823,7 +822,7 @@ class NsxTVDPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
# get the core plugin as this is a static method with no 'self'
plugin = directory.get_plugin()
p = plugin._get_plugin_from_project(ctx, netdb['tenant_id'])
with db_api.context_manager.writer.using(ctx):
with db_api.CONTEXT_WRITER.using(ctx):
p._extension_manager.extend_network_dict(
ctx.session, netdb, result)
@ -834,7 +833,7 @@ class NsxTVDPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
# get the core plugin as this is a static method with no 'self'
plugin = directory.get_plugin()
p = plugin._get_plugin_from_project(ctx, portdb['tenant_id'])
with db_api.context_manager.writer.using(ctx):
with db_api.CONTEXT_WRITER.using(ctx):
p._extension_manager.extend_port_dict(
ctx.session, portdb, result)
@ -845,7 +844,7 @@ class NsxTVDPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
# get the core plugin as this is a static method with no 'self'
plugin = directory.get_plugin()
p = plugin._get_plugin_from_project(ctx, subnetdb['tenant_id'])
with db_api.context_manager.writer.using(ctx):
with db_api.CONTEXT_WRITER.using(ctx):
p._extension_manager.extend_subnet_dict(
ctx.session, subnetdb, result)

@ -43,7 +43,6 @@ from neutron.db import _model_query as model_query
from neutron.db import _resource_extend as resource_extend
from neutron.db import agentschedulers_db
from neutron.db import allowedaddresspairs_db as addr_pair_db
from neutron.db import api as db_api
from neutron.db import db_base_plugin_v2
from neutron.db import dns_db
from neutron.db import external_net_db
@ -68,6 +67,7 @@ from neutron_lib.api.definitions import extraroute as xroute_apidef
from neutron_lib.api.definitions import multiprovidernet as mpnet_apidef
from neutron_lib.api.definitions import portbindings as pbin
from neutron_lib.api.definitions import provider_net as pnet
from neutron_lib.db import api as db_api
from neutron_lib.exceptions import extraroute as xroute_exc
from neutron_lib.exceptions import multiprovidernet as mpnet_exc
from neutron_lib.plugins import utils
@ -965,7 +965,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
transport_zone_config,
shared=net_data.get(constants.SHARED))
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
new_net = super(NsxPluginV2, self).create_network(context,
network)
# Process port security extension
@ -1037,7 +1037,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
LOG.debug("Delete network complete for network: %s", id)
def get_network(self, context, id, fields=None):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# goto to the plugin DB and fetch the network
network = self._get_network(context, id)
if (self.nsx_sync_opts.always_read_status or
@ -1057,7 +1057,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
sorts=None, limit=None, marker=None,
page_reverse=False):
filters = filters or {}
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
networks = (
super(NsxPluginV2, self).get_networks(
context, filters, fields, sorts,
@ -1073,7 +1073,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
if network["network"].get("admin_state_up") is False:
raise NotImplementedError(_("admin_state_up=False networks "
"are not supported."))
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
net = super(NsxPluginV2, self).update_network(context, id, network)
if psec.PORTSECURITY in network['network']:
self._process_network_port_security_update(
@ -1117,7 +1117,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
dhcp_opts = port_data.get(edo_ext.EXTRADHCPOPTS, [])
# Set port status as 'DOWN'. This will be updated by backend sync.
port_data['status'] = constants.PORT_STATUS_DOWN
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# First we allocate port in neutron database
neutron_db = super(NsxPluginV2, self).create_port(context, port)
neutron_port_id = neutron_db['id']
@ -1188,7 +1188,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
LOG.warning("Logical switch for network %s was not "
"found in NSX.", port_data['network_id'])
# Put port in error on neutron DB
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
port = self._get_port(context, neutron_port_id)
port_data['status'] = constants.PORT_STATUS_ERROR
port['status'] = port_data['status']
@ -1198,7 +1198,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
with excutils.save_and_reraise_exception():
LOG.error("Unable to create port or set port "
"attachment in NSX.")
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
self.ipam.delete_port(context, neutron_port_id)
# this extra lookup is necessary to get the
# latest db model for the extension functions
@ -1215,7 +1215,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
port)
has_addr_pairs = self._check_update_has_allowed_address_pairs(port)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
ret_port = super(NsxPluginV2, self).update_port(
context, id, port)
@ -1363,7 +1363,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
port_delete_func(context, neutron_db_port)
self.disassociate_floatingips(context, id)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
queue = self._get_port_queue_bindings(context, {'port_id': [id]})
# metadata_dhcp_host_route
self.handle_port_metadata_access(
@ -1376,7 +1376,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
context, neutron_db_port, action='delete_port')
def get_port(self, context, id, fields=None):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
if (self.nsx_sync_opts.always_read_status or
fields and 'status' in fields):
# Perform explicit state synchronization
@ -1388,7 +1388,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
return super(NsxPluginV2, self).get_port(context, id, fields)
def get_router(self, context, id, fields=None):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
if (self.nsx_sync_opts.always_read_status or
fields and 'status' in fields):
db_router = self._get_router(context, id)
@ -1489,10 +1489,10 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
lrouter = self._create_lrouter(context, r, nexthop)
# TODO(salv-orlando): Deal with backend object removal in case
# of db failures
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# Transaction nesting is needed to avoid foreign key violations
# when processing the distributed router binding
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
router_db = l3_db_models.Router(
id=neutron_router_id,
tenant_id=tenant_id,
@ -1579,7 +1579,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
# object is not found in the underlying backend
except n_exc.NotFound:
# Put the router in ERROR status
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
router_db = self._get_router(context, router_id)
router_db['status'] = constants.NET_STATUS_ERROR
raise nsx_exc.NsxPluginException(
@ -1611,7 +1611,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
routerlib.delete_lrouter(self.cluster, nsx_router_id)
def delete_router(self, context, router_id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# NOTE(salv-orlando): These checks will be repeated anyway when
# calling the superclass. This is wasteful, but is the simplest
# way of ensuring a consistent removal of the router both in
@ -2095,7 +2095,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
"""
# Ensure the default gateway in the config file is in sync with the db
self._ensure_default_network_gateway()
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
try:
super(NsxPluginV2, self).delete_network_gateway(
context, gateway_id)
@ -2161,7 +2161,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
new_status=None, is_create=False):
LOG.error("Rolling back database changes for gateway device %s "
"because of an error in the NSX backend", device_id)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
query = model_query.query_with_hooks(
context, nsx_models.NetworkGatewayDevice).filter(
nsx_models.NetworkGatewayDevice.id == device_id)
@ -2197,7 +2197,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
nsx_res['uuid'])
# set NSX GW device in neutron database and update status
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
query = model_query.query_with_hooks(
context, nsx_models.NetworkGatewayDevice).filter(
nsx_models.NetworkGatewayDevice.id == neutron_id)
@ -2236,7 +2236,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
device_status = nsx_utils.get_nsx_device_status(self.cluster,
nsx_id)
# update status
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
query = model_query.query_with_hooks(
context, nsx_models.NetworkGatewayDevice).filter(
nsx_models.NetworkGatewayDevice.id == neutron_id)
@ -2271,7 +2271,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
device_status = nsx_utils.get_nsx_device_status(self.cluster, nsx_id)
# TODO(salv-orlando): Asynchronous sync for gateway device status
# Update status in database
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
query = model_query.query_with_hooks(
context, nsx_models.NetworkGatewayDevice).filter(
nsx_models.NetworkGatewayDevice.id == device_id)
@ -2292,7 +2292,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
nsx_statuses = nsx_utils.get_nsx_device_statuses(self.cluster,
tenant_id)
# Update statuses in database
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
for device in devices:
new_status = nsx_statuses.get(device['nsx_id'])
if new_status:
@ -2367,7 +2367,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
neutron_id = str(uuidutils.generate_uuid())
nsx_secgroup = secgrouplib.create_security_profile(
self.cluster, tenant_id, neutron_id, s)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
s['id'] = neutron_id
sec_group = super(NsxPluginV2, self).create_security_group(
context, security_group, default_sg)
@ -2404,7 +2404,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
:param security_group_id: security group rule to remove.
"""
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
security_group = super(NsxPluginV2, self).get_security_group(
context, security_group_id)
if not security_group:
@ -2476,7 +2476,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
# TODO(arosen) is there anyway we could avoid having the update of
# the security group rules in nsx outside of this transaction?
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
security_group_id = self._validate_security_group_rules(
context, security_group_rules)
# Check to make sure security group exists
@ -2506,7 +2506,7 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
"""Delete a security group rule
:param sgrid: security group id to remove.
"""
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# determine security profile id
security_group_rule = (
super(NsxPluginV2, self).get_security_group_rule(

@ -24,7 +24,6 @@ import webob.exc
from neutron.db import _resource_extend as resource_extend
from neutron.db import agentschedulers_db
from neutron.db import allowedaddresspairs_db as addr_pair_db
from neutron.db import api as db_api
from neutron.db import dns_db
from neutron.db import external_net_db
from neutron.db import extradhcpopt_db
@ -51,6 +50,7 @@ from neutron_lib.callbacks import events
from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
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 n_exc
@ -292,7 +292,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
self._validate_external_net_create(net_data)
# Create the neutron network
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# Create network in Neutron
created_net = super(NsxPolicyPlugin, self).create_network(
context, network)
@ -322,7 +322,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
return created_net
def delete_network(self, context, network_id):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
self._process_l3_delete(context, network_id)
super(NsxPolicyPlugin, self).delete_network(
context, network_id)
@ -354,7 +354,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
return updated_net
def get_network(self, context, id, fields=None):
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
# Get network from Neutron database
network = self._get_network(context, id)
# Don't do field selection here otherwise we won't be able to add
@ -367,7 +367,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
page_reverse=False):
# Get networks from Neutron database
filters = filters or {}
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
networks = (
super(NsxPolicyPlugin, self).get_networks(
context, filters, fields, sorts,
@ -460,7 +460,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
self._validate_max_ips_per_port(port_data.get('fixed_ips', []),
port_data.get('device_owner'))
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
is_external_net = self._network_is_external(
context, port_data['network_id'])
if is_external_net:
@ -514,7 +514,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
pass
def update_port(self, context, id, port):
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
# get the original port, and keep it honest as it is later used
# for notifications
original_port = super(NsxPolicyPlugin, self).get_port(context, id)
@ -572,7 +572,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
page_reverse=False):
filters = filters or {}
self._update_filters_with_sec_group(context, filters)
with db_api.context_manager.reader.using(context):
with db_api.CONTEXT_READER.using(context):
ports = (
super(NsxPolicyPlugin, self).get_ports(
context, filters, fields, sorts,
@ -601,7 +601,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
def create_router(self, context, router):
r = router['router']
gw_info = self._extract_external_gw(context, router, is_extract=True)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
router = super(NsxPolicyPlugin, self).create_router(
context, router)
router_db = self._get_router(context, router['id'])
@ -1041,7 +1041,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
self._create_project_domain(project_id)
# create the Neutron SG
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
if secgroup.get(provider_sg.PROVIDER) is True:
secgroup_db = self.create_provider_security_group(
context, security_group)
@ -1086,7 +1086,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
sg_data = security_group['security_group']
# update the neutron security group
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
secgroup_res = super(NsxPolicyPlugin, self).update_security_group(
context, sg_id, security_group)
self._process_security_group_properties_update(
@ -1150,7 +1150,7 @@ class NsxPolicyPlugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
sg_id = example_rule['security_group_id']
self._prevent_non_admin_edit_provider_sg(context, sg_id)
with db_api.context_manager.writer.using(context):
with db_api.CONTEXT_WRITER.using(context):
rules_db = (super(NsxPolicyPlugin,
self).create_security_group_rule_bulk_native(
context, security_group_rules))

@ -38,7 +38,7 @@ from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
from neutron_lib import constants
from neutron_lib import context as n_context
from neutron_lib.db import api as lib_db_api
from neutron_lib.db import api as db_api
from neutron_lib.db import constants as db_const
from neutron_lib.db import utils as db_utils