Browse Source

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: I18917dfa59e5dc6c2da4428fafc8228f744c01ed
Boden R 5 months ago
parent
commit
af9b539f9c

+ 4
- 4
networking_bigswitch/plugins/bigswitch/bsn_service_plugin.py View File

@@ -16,8 +16,8 @@
16 16
 from datetime import datetime
17 17
 import eventlet
18 18
 
19
-from neutron.db import api as db
20 19
 from neutron.db import common_db_mixin
20
+from neutron_lib.db import api as db_api
21 21
 from neutron_lib.services import base as service_base
22 22
 from oslo_log import log
23 23
 from oslo_serialization import jsonutils
@@ -278,7 +278,7 @@ class BSNServicePlugin(service_base.ServicePluginBase,
278 278
             context=context, id=id, fields=fields)
279 279
 
280 280
     def create_tenantpolicy(self, context, tenantpolicy):
281
-        with db.context_manager.writer.using(context):
281
+        with db_api.CONTEXT_WRITER.using(context):
282 282
             tenantpolicy_dict = self.tenantpolicy_db_mixin.create_tenantpolicy(
283 283
                 context=context, tenantpolicy=tenantpolicy)
284 284
             self.servers.rest_create_tenantpolicy(
@@ -287,7 +287,7 @@ class BSNServicePlugin(service_base.ServicePluginBase,
287 287
             return tenantpolicy_dict
288 288
 
289 289
     def delete_tenantpolicy(self, context, id):
290
-        with db.context_manager.writer.using(context):
290
+        with db_api.CONTEXT_WRITER.using(context):
291 291
             delete_policy = self.tenantpolicy_db_mixin._get_tenantpolicy(
292 292
                 context, id)
293 293
             self.tenantpolicy_db_mixin.delete_tenantpolicy(
@@ -296,7 +296,7 @@ class BSNServicePlugin(service_base.ServicePluginBase,
296 296
                                                   delete_policy['priority'])
297 297
 
298 298
     def update_tenantpolicy(self, context, id, tenantpolicy):
299
-        with db.context_manager.writer.using(context):
299
+        with db_api.CONTEXT_WRITER.using(context):
300 300
             updated_policy = self.tenantpolicy_db_mixin.update_tenantpolicy(
301 301
                 context=context, servers=self.servers, id=id,
302 302
                 tenantpolicy=tenantpolicy)

+ 9
- 10
networking_bigswitch/plugins/bigswitch/db/tenant_policy_db.py View File

@@ -15,11 +15,10 @@
15 15
 
16 16
 from networking_bigswitch.plugins.bigswitch.i18n import _
17 17
 from networking_bigswitch.plugins.bigswitch.utils import Util
18
-from neutron.db import api as db
19 18
 from neutron.db import common_db_mixin
20 19
 from neutron.db.models import l3 as l3_models
21 20
 from neutron_lib.api import validators
22
-from neutron_lib.db.api import _tag_retriables_as_unretriable
21
+from neutron_lib.db import api as db_api
23 22
 from neutron_lib.db import model_base
24 23
 from neutron_lib import exceptions as n_exc
25 24
 from oslo_config import cfg
@@ -211,7 +210,7 @@ class TenantPolicyDbMixin(common_db_mixin.CommonDbMixin):
211 210
     def get_tenantpolicies(self, context, filters=None, fields=None,
212 211
                            sorts=None, limit=None, marker=None,
213 212
                            page_reverse=False):
214
-        with db.context_manager.reader.using(context):
213
+        with db_api.CONTEXT_READER.using(context):
215 214
             tenantpolicies = \
216 215
                 self._get_collection(context, TenantPolicy,
217 216
                                      self._make_tenantpolicy_dict,
@@ -219,16 +218,16 @@ class TenantPolicyDbMixin(common_db_mixin.CommonDbMixin):
219 218
         return tenantpolicies
220 219
 
221 220
     def get_tenantpolicy(self, context, id, fields=None):
222
-        with db.context_manager.reader.using(context):
221
+        with db_api.CONTEXT_READER.using(context):
223 222
             tenantpolicy = self._get_tenantpolicy(context, id)
224 223
             return self._make_tenantpolicy_dict(tenantpolicy, fields)
225 224
 
226
-    @_tag_retriables_as_unretriable
225
+    @db_api._tag_retriables_as_unretriable
227 226
     def create_tenantpolicy(self, context, tenantpolicy):
228 227
         tenantpolicy_data = tenantpolicy['tenantpolicy']
229 228
         tenantpolicy_data = self._validate_and_cleanse_policy(
230 229
             context, tenantpolicy_data)
231
-        with db.context_manager.writer.using(context):
230
+        with db_api.CONTEXT_WRITER.using(context):
232 231
             router_exists = context.session.query(l3_models.Router).filter_by(
233 232
                 project_id=tenantpolicy_data['project_id']).first()
234 233
             if not router_exists:
@@ -254,7 +253,7 @@ class TenantPolicyDbMixin(common_db_mixin.CommonDbMixin):
254 253
         return self._make_tenantpolicy_dict(tenantpolicy)
255 254
 
256 255
     def delete_tenantpolicy(self, context, id):
257
-        with db.context_manager.writer.using(context):
256
+        with db_api.CONTEXT_WRITER.using(context):
258 257
             tenantpolicy = self._get_tenantpolicy(context, id)
259 258
             context.session.delete(tenantpolicy)
260 259
 
@@ -262,7 +261,7 @@ class TenantPolicyDbMixin(common_db_mixin.CommonDbMixin):
262 261
         tenantpolicy_data = tenantpolicy['tenantpolicy']
263 262
         tenantpolicy_data = self._validate_and_cleanse_policy(
264 263
             context, tenantpolicy_data)
265
-        with db.context_manager.writer.using(context):
264
+        with db_api.CONTEXT_WRITER.using(context):
266 265
             tenantpolicy = self._get_tenantpolicy(context, id)
267 266
             tenantpolicy.update(tenantpolicy_data)
268 267
         return self._make_tenantpolicy_dict(tenantpolicy)
@@ -306,7 +305,7 @@ class TenantPolicyDbMixin(common_db_mixin.CommonDbMixin):
306 305
         return tenant_rule if tenant_rule else default_rule
307 306
 
308 307
     def create_default_policy(self, context, tenant_id, default_policy_dict):
309
-        with db.context_manager.writer.using(context):
308
+        with db_api.CONTEXT_WRITER.using(context):
310 309
             existing_def_rule = (context.session.query(TenantPolicy)
311 310
                                  .filter_by(tenant_id=tenant_id)
312 311
                                  .filter_by(priority=DEFAULT_POLICY_PRIORITY)
@@ -328,7 +327,7 @@ class TenantPolicyDbMixin(common_db_mixin.CommonDbMixin):
328 327
         return self._make_tenantpolicy_dict(default_policy)
329 328
 
330 329
     def remove_default_policy(self, context, tenant_id):
331
-        with db.context_manager.writer.using(context):
330
+        with db_api.CONTEXT_WRITER.using(context):
332 331
             # tenant doesn't have another router, remove all policies
333 332
             LOG.debug("Tenant doesn't have another router after router "
334 333
                       "deletion. Removing all policies under the tenant.")

+ 11
- 11
networking_bigswitch/plugins/bigswitch/l3_router_plugin.py View File

@@ -28,7 +28,6 @@ from oslo_utils import excutils
28 28
 from oslo_utils import uuidutils
29 29
 
30 30
 from neutron.api import extensions as neutron_extensions
31
-from neutron.db import api as db
32 31
 from neutron.db import dns_db
33 32
 from neutron.db import l3_db
34 33
 
@@ -38,6 +37,7 @@ from neutron_lib.callbacks import events
38 37
 from neutron_lib.callbacks import registry
39 38
 from neutron_lib.callbacks import resources
40 39
 from neutron_lib import constants as lib_constants
40
+from neutron_lib.db import api as db_api
41 41
 from neutron_lib import exceptions
42 42
 from neutron_lib.plugins import constants as plugin_constants
43 43
 from neutron_lib.plugins import directory
@@ -165,7 +165,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
165 165
         router = kwargs.get('router')
166 166
         self.txn_cache.add_transaction(router[BSN_TRANSACTION_ID],
167 167
                                        router['id'])
168
-        with db.context_manager.reader.using(context):
168
+        with db_api.CONTEXT_READER.using(context):
169 169
             mapped_router = self._map_tenant_name(router)
170 170
             mapped_router = self._map_state_and_status(mapped_router)
171 171
 
@@ -189,7 +189,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
189 189
         # set default router policies
190 190
         default_policy_dict = self._get_tenant_default_router_policy(tenant_id)
191 191
 
192
-        with db.context_manager.writer.using(context):
192
+        with db_api.CONTEXT_WRITER.using(context):
193 193
             mapped_router = self._map_tenant_name(router)
194 194
             mapped_router = self._map_state_and_status(mapped_router)
195 195
             # populate external tenant_id if it is absent for external network,
@@ -249,7 +249,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
249 249
 
250 250
         orig_router = super(L3RestProxy, self).get_router(context, router_id)
251 251
         tenant_id = orig_router["tenant_id"]
252
-        with db.context_manager.writer.using(context):
252
+        with db_api.CONTEXT_WRITER.using(context):
253 253
             setattr(context, 'GUARD_TRANSACTION', False)
254 254
             new_router = super(L3RestProxy,
255 255
                                self).update_router(context, router_id, router)
@@ -277,7 +277,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
277 277
         tenant_id = orig_router['tenant_id']
278 278
 
279 279
         # remove tenant policies if this was the last router under tenant
280
-        with db.context_manager.writer.using(context):
280
+        with db_api.CONTEXT_WRITER.using(context):
281 281
             upstream_routers = super(L3RestProxy, self).get_routers(
282 282
                 context, filters={"tenant_id": [tenant_id]})
283 283
 
@@ -290,7 +290,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
290 290
     @add_debug_log
291 291
     @log_helper.log_method_call
292 292
     def delete_router(self, context, router_id):
293
-        with db.context_manager.reader.using(context):
293
+        with db_api.CONTEXT_READER.using(context):
294 294
             # Ensure that the router is not used
295 295
             router_filter = {'router_id': [router_id]}
296 296
             fips = self.get_floatingips_count(context.elevated(),
@@ -328,7 +328,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
328 328
         # bookmark for delete in case of transaction rollback
329 329
         self.txn_cache.add_transaction(interface_info[BSN_TRANSACTION_ID],
330 330
                                        subnet_id)
331
-        with db.context_manager.reader.using(context):
331
+        with db_api.CONTEXT_READER.using(context):
332 332
             # we will use the port's subnet id as interface's id
333 333
             intf_details = self._get_router_intf_details(context, port,
334 334
                                                          subnet_id)
@@ -392,7 +392,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
392 392
             msg = _("Either subnet_id or port_id must be specified")
393 393
             raise exceptions.BadRequest(resource='router', msg=msg)
394 394
 
395
-        with db.context_manager.writer.using(context):
395
+        with db_api.CONTEXT_WRITER.using(context):
396 396
             # remove router in DB
397 397
             # TODO(wolverineav): hack until fixed at right place
398 398
             setattr(context, 'GUARD_TRANSACTION', False)
@@ -418,7 +418,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
418 418
     @add_debug_log
419 419
     @log_helper.log_method_call
420 420
     def create_floatingip(self, context, floatingip):
421
-        with db.context_manager.writer.using(context):
421
+        with db_api.CONTEXT_WRITER.using(context):
422 422
             # create floatingip in DB
423 423
             # TODO(wolverineav): hack until fixed at right place
424 424
             setattr(context, 'GUARD_TRANSACTION', False)
@@ -448,7 +448,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
448 448
     @add_debug_log
449 449
     @log_helper.log_method_call
450 450
     def update_floatingip(self, context, id, floatingip):
451
-        with db.context_manager.writer.using(context):
451
+        with db_api.CONTEXT_WRITER.using(context):
452 452
             # update floatingip in DB
453 453
             # TODO(wolverineav): hack until fixed at right place
454 454
             setattr(context, 'GUARD_TRANSACTION', False)
@@ -472,7 +472,7 @@ class L3RestProxy(cplugin.NeutronRestProxyV2Base,
472 472
     @add_debug_log
473 473
     @log_helper.log_method_call
474 474
     def delete_floatingip(self, context, id):
475
-        with db.context_manager.writer.using(context):
475
+        with db_api.CONTEXT_WRITER.using(context):
476 476
             # delete floating IP in DB
477 477
             # TODO(wolverineav): hack until fixed at right place
478 478
             setattr(context, 'GUARD_TRANSACTION', False)

+ 14
- 15
networking_bigswitch/plugins/bigswitch/plugin.py View File

@@ -62,7 +62,6 @@ from neutron.api.rpc.handlers import securitygroups_rpc
62 62
 from neutron.db import agents_db
63 63
 from neutron.db import agentschedulers_db
64 64
 from neutron.db import allowedaddresspairs_db as addr_pair_db
65
-from neutron.db import api as db
66 65
 from neutron.db import db_base_plugin_v2
67 66
 from neutron.db import external_net_db
68 67
 from neutron.db import extradhcpopt_db
@@ -79,7 +78,7 @@ from neutron_lib.api.definitions import l3 as l3_apidef
79 78
 from neutron_lib.api.definitions import portbindings
80 79
 from neutron_lib import constants as const
81 80
 from neutron_lib import context as qcontext
82
-from neutron_lib.db import api as lib_db_api
81
+from neutron_lib.db import api as db_api
83 82
 from neutron_lib import exceptions as lib_exceptions
84 83
 from neutron_lib.plugins import constants as plugin_constants
85 84
 from neutron_lib.plugins import directory
@@ -131,7 +130,7 @@ class SecurityGroupServerRpcMixin(sg_db_rpc.SecurityGroupServerRpcMixin):
131 130
         LOG.debug("get_port_and_sgs() called for port_id %s", port_id)
132 131
         sg_binding_port = sg_db.SecurityGroupPortBinding.port_id
133 132
 
134
-        with db.context_manager.reader.using(context):
133
+        with db_api.CONTEXT_READER.using(context):
135 134
             query = context.session.query(
136 135
                 models_v2.Port,
137 136
                 sg_db.SecurityGroupPortBinding.security_group_id
@@ -455,7 +454,7 @@ class NeutronRestProxyV2Base(db_base_plugin_v2.NeutronDbPluginV2,
455 454
         if context is None:
456 455
             context = qcontext.get_admin_context()
457 456
         # start a sub-transaction to avoid breaking parent transactions
458
-        with db.context_manager.writer.using(context):
457
+        with db_api.CONTEXT_WRITER.using(context):
459 458
             subnets = self._get_subnets_by_network(context,
460 459
                                                    net_id)
461 460
         subnets_details = []
@@ -911,7 +910,7 @@ class NeutronRestProxyV2Base(db_base_plugin_v2.NeutronDbPluginV2,
911 910
     # NOTE(kevinbenton): workaround for eventlet/mysql deadlock
912 911
     @runtime.synchronized('bsn-port-barrier')
913 912
     def _set_port_status(self, port_id, status):
914
-        session = lib_db_api.get_writer_session()
913
+        session = db_api.get_writer_session()
915 914
         try:
916 915
             port = session.query(models_v2.Port).filter_by(id=port_id).one()
917 916
             port['status'] = status
@@ -980,7 +979,7 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
980 979
         self.add_periodic_dhcp_agent_status_check()
981 980
         LOG.debug("NeutronRestProxyV2: initialization done")
982 981
 
983
-    @db.context_manager.writer
982
+    @db_api.CONTEXT_WRITER
984 983
     @add_debug_log
985 984
     def create_network(self, context, network):
986 985
         """Create a network.
@@ -1022,7 +1021,7 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
1022 1021
         # return created network
1023 1022
         return new_net
1024 1023
 
1025
-    @db.context_manager.writer
1024
+    @db_api.CONTEXT_WRITER
1026 1025
     @add_debug_log
1027 1026
     def update_network(self, context, net_id, network):
1028 1027
         """Updates the properties of a particular Virtual Network.
@@ -1057,7 +1056,7 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
1057 1056
         self._send_update_network(new_net, context)
1058 1057
         return new_net
1059 1058
 
1060
-    @db.context_manager.writer
1059
+    @db_api.CONTEXT_WRITER
1061 1060
     @add_debug_log
1062 1061
     def delete_network(self, context, net_id):
1063 1062
         """Delete a network.
@@ -1110,7 +1109,7 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
1110 1109
         :raises: RemoteRestError
1111 1110
         """
1112 1111
         # Update DB in new session so exceptions rollback changes
1113
-        with db.context_manager.writer.using(context):
1112
+        with db_api.CONTEXT_WRITER.using(context):
1114 1113
             self._ensure_default_security_group_on_port(context, port)
1115 1114
             sgids = self._get_security_groups_on_port(context, port)
1116 1115
             # non-router port status is set to pending. it is then updated
@@ -1158,14 +1157,14 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
1158 1157
         return new_port
1159 1158
 
1160 1159
     def get_port(self, context, id, fields=None):
1161
-        with db.context_manager.reader.using(context):
1160
+        with db_api.CONTEXT_READER.using(context):
1162 1161
             port = super(NeutronRestProxyV2, self).get_port(context, id,
1163 1162
                                                             fields)
1164 1163
             self._extend_port_dict_binding(context, port)
1165 1164
         return self._fields(port, fields)
1166 1165
 
1167 1166
     def get_ports(self, context, filters=None, fields=None):
1168
-        with db.context_manager.reader.using(context):
1167
+        with db_api.CONTEXT_READER.using(context):
1169 1168
             ports = super(NeutronRestProxyV2, self).get_ports(context, filters,
1170 1169
                                                               fields)
1171 1170
             for port in ports:
@@ -1204,7 +1203,7 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
1204 1203
 
1205 1204
         # Validate Args
1206 1205
         orig_port = super(NeutronRestProxyV2, self).get_port(context, port_id)
1207
-        with db.context_manager.writer.using(context):
1206
+        with db_api.CONTEXT_WRITER.using(context):
1208 1207
             # Update DB
1209 1208
             new_port = super(NeutronRestProxyV2,
1210 1209
                              self).update_port(context, port_id, port)
@@ -1267,7 +1266,7 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
1267 1266
         # and l3-router.  If so, we should prevent deletion.
1268 1267
         if l3_port_check and self.l3_plugin:
1269 1268
             self.l3_plugin.prevent_l3_port_deletion(context, port_id)
1270
-        with db.context_manager.writer.using(context):
1269
+        with db_api.CONTEXT_WRITER.using(context):
1271 1270
             if self.l3_plugin:
1272 1271
                 router_ids = self.l3_plugin.disassociate_floatingips(
1273 1272
                     context, port_id, do_notify=False)
@@ -1301,7 +1300,7 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
1301 1300
     def update_subnet(self, context, id, subnet):
1302 1301
         self._warn_on_state_status(subnet['subnet'])
1303 1302
 
1304
-        with db.context_manager.writer.using(context):
1303
+        with db_api.CONTEXT_WRITER.using(context):
1305 1304
             # update subnet in DB
1306 1305
             new_subnet = super(NeutronRestProxyV2,
1307 1306
                                self).update_subnet(context, id, subnet)
@@ -1316,7 +1315,7 @@ class NeutronRestProxyV2(NeutronRestProxyV2Base,
1316 1315
     def delete_subnet(self, context, id):
1317 1316
         orig_subnet = super(NeutronRestProxyV2, self).get_subnet(context, id)
1318 1317
         net_id = orig_subnet['network_id']
1319
-        with db.context_manager.writer.using(context):
1318
+        with db_api.CONTEXT_WRITER.using(context):
1320 1319
             # delete subnet in DB
1321 1320
             super(NeutronRestProxyV2, self).delete_subnet(context, id)
1322 1321
             orig_net = super(NeutronRestProxyV2, self).get_network(context,

Loading…
Cancel
Save