summarylogtreecommitdiffstats
path: root/0000-Finish-the-new-DB-engine-facade-migration.patch
blob: 34ee25f71a267b91bf30f0f47fc1e182ac4f5c66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
diff --git a/neutron-17.1.1/doc/source/contributor/internals/objects_usage.rst b/neutron-17.1.1.new/doc/source/contributor/internals/objects_usage.rst
index f829df3..915f4db 100644
--- a/neutron-17.1.1/doc/source/contributor/internals/objects_usage.rst
+++ b/neutron-17.1.1.new/doc/source/contributor/internals/objects_usage.rst
@@ -355,48 +355,6 @@ model, the nullable parameter is by default :code:`True`, while for OVO fields,
 the nullable is set to :code:`False`. Make sure you correctly map database
 column nullability properties to relevant object fields.
 
-Database session activation
----------------------------
-
-By default, all objects use old ``oslo.db`` engine facade. To enable the new
-facade for a particular object, set ``new_facade`` class attribute to ``True``:
-
-.. code-block:: Python
-
-    @obj_base.VersionedObjectRegistry.register
-    class ExampleObject(base.NeutronDbObject):
-        new_facade = True
-
-It will make all OVO actions - ``get_object``, ``update``, ``count`` etc. - to
-use new ``reader.using`` or ``writer.using`` decorators to manage database
-transactions.
-
-Whenever you need to open a new subtransaction in scope of OVO code, use the
-following database session decorators:
-
-.. code-block:: Python
-
-    @obj_base.VersionedObjectRegistry.register
-    class ExampleObject(base.NeutronDbObject):
-
-        @classmethod
-        def get_object(cls, context, **kwargs):
-            with cls.db_context_reader(context):
-                super(ExampleObject,  cls).get_object(context, **kwargs)
-                # fetch more data in the same transaction
-
-        def create(self):
-            with self.db_context_writer(self.obj_context):
-                super(ExampleObject, self).create()
-                # apply more changes in the same transaction
-
-``db_context_reader`` and ``db_context_writer`` decorators abstract the choice
-of engine facade used for particular object from action implementation.
-
-Alternatively, you can call all OVO actions under an active ``reader.using`` /
-``writer.using`` context manager (or ``session.begin``). In this case, OVO will
-pick the appropriate method to open a subtransaction.
-
 Synthetic fields
 ----------------
 :code:`synthetic_fields` is a list of fields, that are not directly backed by
diff --git a/neutron-17.1.1/neutron/db/_utils.py b/neutron-17.1.1.new/neutron/db/_utils.py
index 7d0850b..f067eba 100644
--- a/neutron-17.1.1/neutron/db/_utils.py
+++ b/neutron-17.1.1.new/neutron/db/_utils.py
@@ -31,6 +31,14 @@ def _noop_context_manager():
     yield
 
 
+def context_if_transaction(context, transaction, writer=True):
+    if transaction:
+        return (db_api.CONTEXT_WRITER.using(context) if writer else
+                db_api.CONTEXT_READER.using(context))
+    else:
+        return _noop_context_manager()
+
+
 def safe_creation(context, create_fn, delete_fn, create_bindings,
                   transaction=True):
     '''This function wraps logic of object creation in safe atomic way.
@@ -55,12 +63,11 @@ def safe_creation(context, create_fn, delete_fn, create_bindings,
     :param transaction: if true the whole operation will be wrapped in a
         transaction. if false, no transaction will be used.
     '''
-    cm = (db_api.CONTEXT_WRITER.using(context)
-          if transaction else _noop_context_manager())
-    with cm:
+    with context_if_transaction(context, transaction):
         obj = create_fn()
         try:
-            value = create_bindings(obj['id'])
+            updated_obj, value = create_bindings(obj['id'])
+            obj = updated_obj or obj
         except Exception:
             with excutils.save_and_reraise_exception():
                 try:
diff --git a/neutron-17.1.1/neutron/db/l3_db.py b/neutron-17.1.1.new/neutron/db/l3_db.py
index 8c1140e..7d40672 100644
--- a/neutron-17.1.1/neutron/db/l3_db.py
+++ b/neutron-17.1.1.new/neutron/db/l3_db.py
@@ -192,6 +192,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                    'device_owner': [DEVICE_OWNER_FLOATINGIP]}
         return {p['id'] for p in self._core_plugin.get_ports(context, filters)}
 
+    @db_api.CONTEXT_READER
     def _get_router(self, context, router_id):
         try:
             router = model_query.get_by_id(
@@ -227,7 +228,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
         router['tenant_id'] = tenant_id
         registry.notify(resources.ROUTER, events.BEFORE_CREATE,
                         self, context=context, router=router)
-        with context.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(context):
             # pre-generate id so it will be available when
             # configuring external gw port
             router_db = l3_models.Router(
@@ -245,10 +246,15 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
 
     def _update_gw_for_create_router(self, context, gw_info, router_id):
         if gw_info:
-            router_db = self._get_router(context, router_id)
+            with db_utils.context_if_transaction(
+                    context, not context.session.is_active, writer=False):
+                router_db = self._get_router(context, router_id)
             self._update_router_gw_info(context, router_id,
                                         gw_info, router=router_db)
 
+            return self._get_router(context, router_id), None
+        return None, None
+
     @db_api.retry_if_session_inactive()
     def create_router(self, context, router):
         r = router['router']
@@ -288,9 +294,6 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
         gw_info = r.pop(EXTERNAL_GW_INFO, constants.ATTR_NOT_SPECIFIED)
         original = self.get_router(context, id)
         if gw_info != constants.ATTR_NOT_SPECIFIED:
-            # Update the gateway outside of the DB update since it involves L2
-            # calls that don't make sense to rollback and may cause deadlocks
-            # in a transaction.
             self._update_router_gw_info(context, id, gw_info)
         router_db = self._update_router_db(context, id, r)
         updated = self._make_router_dict(router_db)
@@ -308,6 +311,13 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                      'device_owner': DEVICE_OWNER_ROUTER_GW,
                      'admin_state_up': True,
                      'name': ''}
+
+        if context.session.is_active:
+            # TODO(ralonsoh): ML2 plugin "create_port" should be called outside
+            # a DB transaction. In this case an exception is made but in order
+            # to prevent future errors, this call should be moved outside
+            # the current transaction.
+            context.GUARD_TRANSACTION = False
         gw_port = plugin_utils.create_port(
             self._core_plugin, context.elevated(), {'port': port_data})
 
@@ -316,7 +326,8 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                       network_id)
         with plugin_utils.delete_port_on_error(
                 self._core_plugin, context.elevated(), gw_port['id']):
-            with context.session.begin(subtransactions=True):
+            with db_api.CONTEXT_WRITER.using(context):
+                router = self._get_router(context, router['id'])
                 router.gw_port = self._core_plugin._get_port(
                     context.elevated(), gw_port['id'])
                 router_port = l3_obj.RouterPort(
@@ -325,7 +336,6 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                     port_id=gw_port['id'],
                     port_type=DEVICE_OWNER_ROUTER_GW
                 )
-                context.session.add(router)
                 router_port.create()
 
     def _validate_gw_info(self, context, gw_port, info, ext_ips):
@@ -371,10 +381,14 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
         gw_ips = [x['ip_address'] for x in router.gw_port['fixed_ips']]
         gw_port_id = router.gw_port['id']
         self._delete_router_gw_port_db(context, router)
+        if admin_ctx.session.is_active:
+            # TODO(ralonsoh): ML2 plugin "delete_port" should be called outside
+            # a DB transaction. In this case an exception is made but in order
+            # to prevent future errors, this call should be moved outside
+            # the current transaction.
+            admin_ctx.GUARD_TRANSACTION = False
         self._core_plugin.delete_port(
             admin_ctx, gw_port_id, l3_port_check=False)
-        with context.session.begin(subtransactions=True):
-            context.session.refresh(router)
         # TODO(boden): normalize metadata
         metadata = {'network_id': old_network_id,
                     'new_network_id': new_network_id,
@@ -387,7 +401,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                              resource_id=router_id))
 
     def _delete_router_gw_port_db(self, context, router):
-        with context.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(context):
             router.gw_port = None
             if router not in context.session:
                 context.session.add(router)
@@ -405,10 +419,12 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
 
     def _create_gw_port(self, context, router_id, router, new_network_id,
                         ext_ips):
-        new_valid_gw_port_attachment = (
-            new_network_id and
-            (not router.gw_port or
-             router.gw_port['network_id'] != new_network_id))
+        with db_api.CONTEXT_READER.using(context):
+            router = self._get_router(context, router_id)
+            new_valid_gw_port_attachment = (
+                new_network_id and
+                (not router.gw_port or
+                router.gw_port['network_id'] != new_network_id))
         if new_valid_gw_port_attachment:
             subnets = self._core_plugin.get_subnets_by_network(context,
                                                                new_network_id)
@@ -431,7 +447,9 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
             self._create_router_gw_port(context, router,
                                         new_network_id, ext_ips)
 
-            gw_ips = [x['ip_address'] for x in router.gw_port['fixed_ips']]
+            with db_api.CONTEXT_READER.using(context):
+                router = self._get_router(context, router_id)
+                gw_ips = [x['ip_address'] for x in router.gw_port['fixed_ips']]
 
             registry.publish(resources.ROUTER_GATEWAY,
                              events.AFTER_CREATE,
@@ -445,12 +463,8 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
     def _update_current_gw_port(self, context, router_id, router, ext_ips):
         self._core_plugin.update_port(context.elevated(), router.gw_port['id'],
                                       {'port': {'fixed_ips': ext_ips}})
-        context.session.expire(router.gw_port)
 
     def _update_router_gw_info(self, context, router_id, info, router=None):
-        # TODO(salvatore-orlando): guarantee atomic behavior also across
-        # operations that span beyond the model classes handled by this
-        # class (e.g.: delete_port)
         router = router or self._get_router(context, router_id)
         gw_port = router.gw_port
         ext_ips = info.get('external_fixed_ips') if info else []
@@ -503,27 +517,30 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                          payload=events.DBEventPayload(
                              context, resource_id=id))
         # TODO(nati) Refactor here when we have router insertion model
-        router = self._ensure_router_not_in_use(context, id)
-        original = self._make_router_dict(router)
-        self._delete_current_gw_port(context, id, router, None)
-        with context.session.begin(subtransactions=True):
-            context.session.refresh(router)
-
-        router_ports = router.attached_ports
-        for rp in router_ports:
-            self._core_plugin.delete_port(context.elevated(),
-                                          rp.port.id,
-                                          l3_port_check=False)
-        with context.session.begin(subtransactions=True):
-            context.session.refresh(router)
+        with db_api.CONTEXT_WRITER.using(context):
+            router = self._ensure_router_not_in_use(context, id)
+            original = self._make_router_dict(router)
+            self._delete_current_gw_port(context, id, router, None)
+
+            # TODO(ralonsoh): move this section (port deletion) out of the DB
+            # transaction.
+            router_ports_ids = (rp.port.id for rp in router.attached_ports)
+            if context.session.is_active:
+                context.GUARD_TRANSACTION = False
+            for rp_id in router_ports_ids:
+                self._core_plugin.delete_port(context.elevated(), rp_id,
+                                              l3_port_check=False)
+
+            router = self._get_router(context, id)
             registry.notify(resources.ROUTER, events.PRECOMMIT_DELETE,
                             self, context=context, router_db=router,
                             router_id=id)
             # we bump the revision even though we are about to delete to throw
-            # staledataerror if something snuck in with a new interface
+            # staledataerror if something stuck in with a new interface
             router.bump_revision()
             context.session.flush()
             context.session.delete(router)
+
         registry.notify(resources.ROUTER, events.AFTER_DELETE, self,
                         context=context, router_id=id, original=original)
 
@@ -670,7 +687,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                 raise n_exc.BadRequest(resource='router', msg=msg)
 
     def _validate_router_port_info(self, context, router, port_id):
-        with db_api.autonested_transaction(context.session):
+        with db_api.CONTEXT_READER.using(context):
             # check again within transaction to mitigate race
             port = self._check_router_port(context, port_id, router.id)
 
@@ -876,8 +893,6 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                         new_interface=new_router_intf,
                         interface_info=interface_info)
 
-        with context.session.begin(subtransactions=True):
-            context.session.refresh(router)
         return self._make_router_interface_info(
             router.id, port['tenant_id'], port['id'], port['network_id'],
             subnets[-1]['id'], [subnet['id'] for subnet in subnets])
@@ -1021,8 +1036,6 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                         port=port,
                         router_id=router_id,
                         interface_info=interface_info)
-        with context.session.begin(subtransactions=True):
-            context.session.refresh(router)
         return self._make_router_interface_info(router_id, port['tenant_id'],
                                                 port['id'], port['network_id'],
                                                 subnets[0]['id'],
@@ -1322,7 +1335,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
         with plugin_utils.delete_port_on_error(
                 self._core_plugin, context.elevated(),
                 external_port['id']),\
-                context.session.begin(subtransactions=True):
+                db_api.CONTEXT_WRITER.using(context):
             # Ensure IPv4 addresses are allocated on external port
             external_ipv4_ips = self._port_ipv4_fixed_ips(external_port)
             if not external_ipv4_ips:
@@ -1400,7 +1413,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
             raise e.errors[0].error
 
         fip = floatingip['floatingip']
-        with context.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(context):
             floatingip_obj = self._get_floatingip(context, id)
             old_floatingip = self._make_floatingip_dict(floatingip_obj)
             old_fixed_port_id = floatingip_obj.fixed_port_id
@@ -1589,7 +1602,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
                           This parameter is ignored.
         @return: set of router-ids that require notification updates
         """
-        with context.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(context):
             floating_ip_objs = l3_obj.FloatingIP.get_objects(
                 context, fixed_port_id=port_id)
             router_ids = {fip.router_id for fip in floating_ip_objs}
@@ -1829,7 +1842,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
     def _get_router_info_list(self, context, router_ids=None, active=None,
                               device_owners=None):
         """Query routers and their related floating_ips, interfaces."""
-        with context.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(context):
             routers = self._get_sync_routers(context,
                                              router_ids=router_ids,
                                              active=active)
diff --git a/neutron-17.1.1/neutron/db/l3_gateway_ip_qos.py b/neutron-17.1.1.new/neutron/db/l3_gateway_ip_qos.py
index 3fa7bfa..190b7a9 100644
--- a/neutron-17.1.1/neutron/db/l3_gateway_ip_qos.py
+++ b/neutron-17.1.1.new/neutron/db/l3_gateway_ip_qos.py
@@ -18,6 +18,7 @@
 from neutron_lib.api.definitions import l3 as l3_apidef
 from neutron_lib.api.definitions import qos_gateway_ip
 from neutron_lib.api import extensions
+from neutron_lib.db import api as db_api
 from neutron_lib.db import resource_extend
 from neutron_lib.services.qos import constants as qos_consts
 from oslo_log import log as logging
@@ -65,9 +66,10 @@ class L3_gw_ip_qos_dbonly_mixin(l3_gwmode_db.L3_NAT_dbonly_mixin):
                        self)._update_router_gw_info(
             context, router_id, info, router)
 
-        if self._is_gw_ip_qos_supported and router.gw_port:
-            self._update_router_gw_qos_policy(context, router_id,
-                                              info, router)
+        with db_api.CONTEXT_WRITER.using(context):
+            if self._is_gw_ip_qos_supported and router.gw_port:
+                self._update_router_gw_qos_policy(context, router_id,
+                                                  info, router)
 
         return router
 
@@ -94,9 +96,6 @@ class L3_gw_ip_qos_dbonly_mixin(l3_gwmode_db.L3_NAT_dbonly_mixin):
                                           router_id,
                                           old_qos_policy_id)
 
-        with context.session.begin(subtransactions=True):
-            context.session.refresh(router)
-
         if new_qos_policy_id:
             self._create_gw_ip_qos_db(
                 context, router_id, new_qos_policy_id)
diff --git a/neutron-17.1.1/neutron/db/l3_gwmode_db.py b/neutron-17.1.1.new/neutron/db/l3_gwmode_db.py
index fabfcdd..04d8d4f 100644
--- a/neutron-17.1.1/neutron/db/l3_gwmode_db.py
+++ b/neutron-17.1.1.new/neutron/db/l3_gwmode_db.py
@@ -17,6 +17,7 @@ from neutron_lib.api.definitions import l3 as l3_apidef
 from neutron_lib.callbacks import events
 from neutron_lib.callbacks import registry
 from neutron_lib.callbacks import resources
+from neutron_lib.db import api as db_api
 from neutron_lib.db import resource_extend
 from oslo_config import cfg
 import sqlalchemy as sa
@@ -56,10 +57,9 @@ class L3_NAT_dbonly_mixin(l3_db.L3_NAT_dbonly_mixin):
             })
 
     def _update_router_gw_info(self, context, router_id, info, router=None):
-        # Load the router only if necessary
-        if not router:
+        with db_api.CONTEXT_WRITER.using(context):
+            # Always load the router inside the DB context.
             router = self._get_router(context, router_id)
-        with context.session.begin(subtransactions=True):
             old_router = self._make_router_dict(router)
             router.enable_snat = self._get_enable_snat(info)
             router_body = {l3_apidef.ROUTER:
@@ -75,7 +75,7 @@ class L3_NAT_dbonly_mixin(l3_db.L3_NAT_dbonly_mixin):
             context, router_id, info, router=router)
         # Returning the router might come back useful if this
         # method is overridden in child classes
-        return router
+        return self._get_router(context, router_id)
 
     @staticmethod
     def _get_enable_snat(info):
diff --git a/neutron-17.1.1/neutron/db/l3_hamode_db.py b/neutron-17.1.1.new/neutron/db/l3_hamode_db.py
index 423797d..7d06e71 100644
--- a/neutron-17.1.1/neutron/db/l3_hamode_db.py
+++ b/neutron-17.1.1.new/neutron/db/l3_hamode_db.py
@@ -143,7 +143,10 @@ class L3_HA_NAT_db_mixin(l3_dvr_db.L3_NAT_with_dvr_db_mixin,
                         context, network_id=network_id, vr_id=vr_id)
                     allocation.create()
 
-                    router_db.extra_attributes.ha_vr_id = allocation.vr_id
+                    router_db.extra_attributes.update(
+                        {'ha_vr_id': allocation.vr_id})
+                    context.session.add(router_db.extra_attributes)
+
                     LOG.debug(
                         "Router %(router_id)s has been allocated a ha_vr_id "
                         "%(ha_vr_id)d.",
@@ -200,7 +203,7 @@ class L3_HA_NAT_db_mixin(l3_dvr_db.L3_NAT_with_dvr_db_mixin,
             # and the process is started over where the existing
             # network will be selected.
             raise db_exc.DBDuplicateEntry(columns=['tenant_id'])
-        return ha_network
+        return None, ha_network
 
     def _add_ha_network_settings(self, network):
         if cfg.CONF.l3_ha_network_type:
@@ -267,7 +270,7 @@ class L3_HA_NAT_db_mixin(l3_dvr_db.L3_NAT_with_dvr_db_mixin,
                 context, port_id=port_id, router_id=router_id)
             portbinding.create()
 
-            return portbinding
+            return None, portbinding
         except db_exc.DBReferenceError as e:
             with excutils.save_and_reraise_exception() as ctxt:
                 if isinstance(e.inner_exception, sql_exc.IntegrityError):
diff --git a/neutron-17.1.1/neutron/db/ovn_revision_numbers_db.py b/neutron-17.1.1.new/neutron/db/ovn_revision_numbers_db.py
index ba2ba1b..370283c 100644
--- a/neutron-17.1.1/neutron/db/ovn_revision_numbers_db.py
+++ b/neutron-17.1.1.new/neutron/db/ovn_revision_numbers_db.py
@@ -104,7 +104,7 @@ def create_initial_revision(context, resource_uuid, resource_type,
     LOG.debug('create_initial_revision uuid=%s, type=%s, rev=%s',
               resource_uuid, resource_type, revision_number)
     db_func = context.session.merge if may_exist else context.session.add
-    with context.session.begin(subtransactions=True):
+    with db_api.CONTEXT_WRITER.using(context):
         std_attr_id = _get_standard_attr_id(
             context, resource_uuid, resource_type)
         row = ovn_models.OVNRevisionNumbers(
@@ -116,7 +116,7 @@ def create_initial_revision(context, resource_uuid, resource_type,
 @db_api.retry_if_session_inactive()
 def delete_revision(context, resource_uuid, resource_type):
     LOG.debug('delete_revision(%s)', resource_uuid)
-    with context.session.begin(subtransactions=True):
+    with db_api.CONTEXT_WRITER.using(context):
         row = context.session.query(ovn_models.OVNRevisionNumbers).filter_by(
             resource_uuid=resource_uuid,
             resource_type=resource_type).one_or_none()
@@ -136,7 +136,7 @@ def _ensure_revision_row_exist(context, resource, resource_type):
     # deal with objects that already existed before the sync work. I believe
     # that we can remove this method after few development cycles. Or,
     # if we decide to make a migration script as well.
-    with context.session.begin(subtransactions=True):
+    with db_api.CONTEXT_READER.using(context):
         if not context.session.query(ovn_models.OVNRevisionNumbers).filter_by(
                 resource_uuid=resource['id'],
                 resource_type=resource_type).one_or_none():
@@ -151,7 +151,7 @@ def _ensure_revision_row_exist(context, resource, resource_type):
 @db_api.retry_if_session_inactive()
 def get_revision_row(context, resource_uuid):
     try:
-        with context.session.begin(subtransactions=True):
+        with db_api.CONTEXT_READER.using(context):
             return context.session.query(
                 ovn_models.OVNRevisionNumbers).filter_by(
                 resource_uuid=resource_uuid).one()
@@ -162,7 +162,7 @@ def get_revision_row(context, resource_uuid):
 @db_api.retry_if_session_inactive()
 def bump_revision(context, resource, resource_type):
     revision_number = ovn_utils.get_revision_number(resource, resource_type)
-    with context.session.begin(subtransactions=True):
+    with db_api.CONTEXT_WRITER.using(context):
         _ensure_revision_row_exist(context, resource, resource_type)
         std_attr_id = _get_standard_attr_id(
             context, resource['id'], resource_type)
@@ -195,7 +195,7 @@ def get_inconsistent_resources(context):
                          whens=MAINTENANCE_CREATE_UPDATE_TYPE_ORDER)
     time_ = (timeutils.utcnow() -
              datetime.timedelta(seconds=INCONSISTENCIES_OLDER_THAN))
-    with context.session.begin(subtransactions=True):
+    with db_api.CONTEXT_READER.using(context):
         query = context.session.query(ovn_models.OVNRevisionNumbers).join(
             standard_attr.StandardAttribute,
             ovn_models.OVNRevisionNumbers.standard_attr_id ==
@@ -224,6 +224,6 @@ def get_deleted_resources(context):
     """
     sort_order = sa.case(value=ovn_models.OVNRevisionNumbers.resource_type,
                          whens=MAINTENANCE_DELETE_TYPE_ORDER)
-    with context.session.begin(subtransactions=True):
+    with db_api.CONTEXT_READER.using(context):
         return context.session.query(ovn_models.OVNRevisionNumbers).filter_by(
             standard_attr_id=None).order_by(sort_order).all()
diff --git a/neutron-17.1.1/neutron/db/securitygroups_db.py b/neutron-17.1.1.new/neutron/db/securitygroups_db.py
index 5db2777..a4fe240 100644
--- a/neutron-17.1.1/neutron/db/securitygroups_db.py
+++ b/neutron-17.1.1.new/neutron/db/securitygroups_db.py
@@ -110,18 +110,18 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase,
                 return self.get_security_group(context, existing_def_sg_id)
 
         with db_api.CONTEXT_WRITER.using(context):
-            sg = sg_obj.SecurityGroup(
-                context, id=s.get('id') or uuidutils.generate_uuid(),
-                description=s['description'], project_id=tenant_id,
-                name=s['name'], is_default=default_sg, stateful=stateful)
-            sg.create()
-
             delta = len(ext_sg.sg_supported_ethertypes)
             delta = delta * 2 if default_sg else delta
             reservation = quota.QUOTAS.make_reservation(
                 context, tenant_id, {'security_group_rule': delta},
                 self)
 
+            sg = sg_obj.SecurityGroup(
+                context, id=s.get('id') or uuidutils.generate_uuid(),
+                description=s['description'], project_id=tenant_id,
+                name=s['name'], is_default=default_sg, stateful=stateful)
+            sg.create()
+
             for ethertype in ext_sg.sg_supported_ethertypes:
                 if default_sg:
                     # Allow intercommunication
@@ -736,6 +736,17 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase,
                 raise ext_sg.SecurityGroupRuleParameterConflict(
                     ethertype=rule['ethertype'], cidr=input_prefix)
 
+    @db_api.retry_if_session_inactive()
+    def get_security_group_rules_count(self, context, filters=None):
+        filters = filters if filters else {}
+        if not filters and context.project_id and not context.is_admin:
+            rule_ids = sg_obj.SecurityGroupRule.get_security_group_rule_ids(
+                context.project_id)
+            filters = {'id': rule_ids}
+
+        return sg_obj.SecurityGroupRule.count(context_lib.get_admin_context(),
+                                              **filters)
+
     @db_api.retry_if_session_inactive()
     def get_security_group_rules(self, context, filters=None, fields=None,
                                  sorts=None, limit=None, marker=None,
diff --git a/neutron-17.1.1/neutron/objects/base.py b/neutron-17.1.1.new/neutron/objects/base.py
index 08e15ff..41a24ee 100644
--- a/neutron-17.1.1/neutron/objects/base.py
+++ b/neutron-17.1.1.new/neutron/objects/base.py
@@ -405,9 +405,6 @@ class NeutronDbObject(NeutronObject, metaclass=DeclarativeObject):
     # should be overridden for all rbac aware objects
     rbac_db_cls = None
 
-    # whether to use new engine facade for the object
-    new_facade = False
-
     primary_keys = ['id']
 
     # 'unique_keys' is a list of unique keys that can be used with get_object
@@ -571,16 +568,12 @@ class NeutronDbObject(NeutronObject, metaclass=DeclarativeObject):
     @classmethod
     def db_context_writer(cls, context):
         """Return read-write session activation decorator."""
-        if cls.new_facade or cls._use_db_facade(context):
-            return db_api.CONTEXT_WRITER.using(context)
-        return db_api.autonested_transaction(context.session)
+        return db_api.CONTEXT_WRITER.using(context)
 
     @classmethod
     def db_context_reader(cls, context):
         """Return read-only session activation decorator."""
-        if cls.new_facade or cls._use_db_facade(context):
-            return db_api.CONTEXT_READER.using(context)
-        return db_api.autonested_transaction(context.session)
+        return db_api.CONTEXT_READER.using(context)
 
     @classmethod
     def get_object(cls, context, fields=None, **kwargs):
diff --git a/neutron-17.1.1/neutron/objects/l3_hamode.py b/neutron-17.1.1.new/neutron/objects/l3_hamode.py
index 63dd2a4..814686e 100644
--- a/neutron-17.1.1/neutron/objects/l3_hamode.py
+++ b/neutron-17.1.1.new/neutron/objects/l3_hamode.py
@@ -37,7 +37,7 @@ class L3HARouterAgentPortBinding(base.NeutronDbObject):
     }
 
     primary_keys = ['port_id']
-    fields_no_update = ['router_id', 'port_id', 'l3_agent_id']
+    fields_no_update = ['router_id', 'port_id']
 
     @classmethod
     def get_l3ha_filter_host_router(cls, context, router_ids, host):
diff --git a/neutron-17.1.1/neutron/objects/router.py b/neutron-17.1.1.new/neutron/objects/router.py
index a1f727f..2ed0917 100644
--- a/neutron-17.1.1/neutron/objects/router.py
+++ b/neutron-17.1.1.new/neutron/objects/router.py
@@ -363,7 +363,6 @@ class DvrFipGatewayPortAgentBinding(base.NeutronDbObject):
     VERSION = '1.0'
 
     db_model = dvr_models.DvrFipGatewayPortAgentBinding
-    new_facade = True
 
     primary_keys = ['network_id', 'agent_id']
 
diff --git a/neutron-17.1.1/neutron/objects/stdattrs.py b/neutron-17.1.1.new/neutron/objects/stdattrs.py
index d7a509b..b2aa9ca 100644
--- a/neutron-17.1.1/neutron/objects/stdattrs.py
+++ b/neutron-17.1.1.new/neutron/objects/stdattrs.py
@@ -24,8 +24,6 @@ class StandardAttribute(base.NeutronDbObject):
     # Version 1.0: Initial version
     VERSION = '1.0'
 
-    new_facade = True
-
     db_model = standard_attr.StandardAttribute
 
     fields = {
diff --git a/neutron-17.1.1/neutron/objects/subnet.py b/neutron-17.1.1.new/neutron/objects/subnet.py
index 540c6e5..41231fc 100644
--- a/neutron-17.1.1/neutron/objects/subnet.py
+++ b/neutron-17.1.1.new/neutron/objects/subnet.py
@@ -69,8 +69,6 @@ class Route(base.NeutronDbObject):
     # Version 1.0: Initial version
     VERSION = '1.0'
 
-    new_facade = True
-
     db_model = models_v2.SubnetRoute
 
     primary_keys = ['destination', 'nexthop', 'subnet_id']
@@ -196,7 +194,6 @@ class Subnet(base.NeutronDbObject):
     VERSION = '1.1'
 
     db_model = models_v2.Subnet
-    new_facade = True
 
     fields = {
         'id': common_types.UUIDField(),
@@ -500,7 +497,6 @@ class NetworkSubnetLock(base.NeutronDbObject):
     VERSION = '1.0'
 
     db_model = models_v2.NetworkSubnetLock
-    new_facade = True
     primary_keys = ['network_id']
 
     fields = {
diff --git a/neutron-17.1.1/neutron/objects/trunk.py b/neutron-17.1.1.new/neutron/objects/trunk.py
index a16bdf5..774a188 100644
--- a/neutron-17.1.1/neutron/objects/trunk.py
+++ b/neutron-17.1.1.new/neutron/objects/trunk.py
@@ -30,7 +30,6 @@ class SubPort(base.NeutronDbObject):
     VERSION = '1.0'
 
     db_model = models.SubPort
-    new_facade = True
 
     primary_keys = ['port_id']
     foreign_keys = {'Trunk': {'trunk_id': 'id'}}
@@ -89,7 +88,6 @@ class Trunk(base.NeutronDbObject):
     VERSION = '1.1'
 
     db_model = models.Trunk
-    new_facade = True
 
     fields = {
         'admin_state_up': obj_fields.BooleanField(),
diff --git a/neutron-17.1.1/neutron/scheduler/l3_agent_scheduler.py b/neutron-17.1.1.new/neutron/scheduler/l3_agent_scheduler.py
index 42ec78d..98ab0bb 100644
--- a/neutron-17.1.1/neutron/scheduler/l3_agent_scheduler.py
+++ b/neutron-17.1.1.new/neutron/scheduler/l3_agent_scheduler.py
@@ -30,6 +30,7 @@ from oslo_log import log as logging
 from neutron.common import utils
 from neutron.conf.db import l3_hamode_db
 from neutron.db.models import l3agent as rb_model
+from neutron.objects import l3_hamode as l3_hamode_obj
 from neutron.objects import l3agent as rb_obj
 
 
@@ -284,10 +285,12 @@ class L3Scheduler(object, metaclass=abc.ABCMeta):
             port_binding = utils.create_object_with_dependency(
                 creator, dep_getter, dep_creator,
                 dep_id_attr, dep_deleter)[0]
-            # NOTE(ralonsoh): to be migrated to the new facade that can't be
-            # used with "create_object_with_dependency".
-            with lib_db_api.autonested_transaction(context.session):
+            with lib_db_api.CONTEXT_WRITER.using(context):
+                port_binding = (
+                    l3_hamode_obj.L3HARouterAgentPortBinding.get_object(
+                        context, port_id=port_binding['port_id']))
                 port_binding.l3_agent_id = agent['id']
+                port_binding.update()
         except db_exc.DBDuplicateEntry:
             LOG.debug("Router %(router)s already scheduled for agent "
                       "%(agent)s", {'router': router_id,
diff --git a/neutron-17.1.1/neutron/services/portforwarding/pf_plugin.py b/neutron-17.1.1.new/neutron/services/portforwarding/pf_plugin.py
index 9e7ca86..f118e66 100644
--- a/neutron-17.1.1/neutron/services/portforwarding/pf_plugin.py
+++ b/neutron-17.1.1.new/neutron/services/portforwarding/pf_plugin.py
@@ -383,13 +383,15 @@ class PortForwardingPlugin(fip_pf.PortForwardingPluginBase):
                 raise lib_exc.BadRequest(resource=apidef.RESOURCE_NAME,
                                          msg=message)
 
-            if self._rpc_notifications_required:
-                self.push_api.push(context, [pf_obj], rpc_events.CREATED)
-            registry.notify(pf_consts.PORT_FORWARDING, events.AFTER_CREATE,
-                            self,
-                            payload=[callbacks.PortForwardingPayload(context,
-                                current_pf=pf_obj)])
-            return pf_obj
+        registry.notify(pf_consts.PORT_FORWARDING, events.AFTER_CREATE,
+                        self,
+                        payload=[callbacks.PortForwardingPayload(context,
+                            current_pf=pf_obj)])
+
+        if self._rpc_notifications_required:
+            self.push_api.push(context, [pf_obj], rpc_events.CREATED)
+
+        return pf_obj
 
     @db_base_plugin_common.convert_result_to_dict
     def update_floatingip_port_forwarding(self, context, id, floatingip_id,
diff --git a/neutron-17.1.1/neutron/services/qos/qos_plugin.py b/neutron-17.1.1.new/neutron/services/qos/qos_plugin.py
index cfd3950..6f274db 100644
--- a/neutron-17.1.1/neutron/services/qos/qos_plugin.py
+++ b/neutron-17.1.1.new/neutron/services/qos/qos_plugin.py
@@ -549,7 +549,7 @@ class QoSPlugin(qos.QoSPluginBase):
         return rule
 
     def _get_policy_id(self, context, rule_cls, rule_id):
-        with db_api.autonested_transaction(context.session):
+        with db_api.CONTEXT_READER.using(context):
             rule_object = rule_cls.get_object(context, id=rule_id)
             if not rule_object:
                 raise qos_exc.QosRuleNotFound(policy_id="", rule_id=rule_id)
diff --git a/neutron-17.1.1/neutron/tests/fullstack/test_dhcp_agent.py b/neutron-17.1.1.new/neutron/tests/fullstack/test_dhcp_agent.py
index d8e16b4..a11a8ac 100644
--- a/neutron-17.1.1/neutron/tests/fullstack/test_dhcp_agent.py
+++ b/neutron-17.1.1.new/neutron/tests/fullstack/test_dhcp_agent.py
@@ -229,6 +229,9 @@ class TestDhcpAgentHARaceCondition(BaseDhcpAgentTest):
         self._simulate_concurrent_requests_process_and_raise(funcs, args)
 
     def test_dhcp_agent_ha_with_race_condition(self):
+        # NOTE(ralonsoh): the concurrent creation in the same thread could
+        # fail because the context and the session is the same for all DB
+        # calls.
         network_dhcp_agents = self.client.list_dhcp_agent_hosting_networks(
             self.network['id'])['agents']
         self.assertEqual(1, len(network_dhcp_agents))
diff --git a/neutron-17.1.1/neutron/tests/functional/services/portforwarding/test_port_forwarding.py b/neutron-17.1.1.new/neutron/tests/functional/services/portforwarding/test_port_forwarding.py
index d8b4541..38c65a0 100644
--- a/neutron-17.1.1/neutron/tests/functional/services/portforwarding/test_port_forwarding.py
+++ b/neutron-17.1.1.new/neutron/tests/functional/services/portforwarding/test_port_forwarding.py
@@ -14,7 +14,6 @@ from unittest import mock
 
 from neutron_lib.api.definitions import fip_pf_description as ext_apidef
 from neutron_lib.api.definitions import floating_ip_port_forwarding as apidef
-from neutron_lib.callbacks import exceptions as c_exc
 from neutron_lib import exceptions as lib_exc
 from neutron_lib.exceptions import l3 as lib_l3_exc
 from neutron_lib.plugins import constants as plugin_constants
@@ -387,70 +386,6 @@ class PortForwardingTestCase(PortForwardingTestCaseBase):
                           self.pf_plugin.delete_floatingip_port_forwarding,
                           self.context, res['id'], uuidutils.generate_uuid())
 
-    def test_concurrent_create_port_forwarding_delete_fip(self):
-
-        func1 = self.pf_plugin.create_floatingip_port_forwarding
-        func2 = self._delete_floatingip
-        funcs = [func1, func2]
-        args_list = [(self.context, self.fip['id'], self.port_forwarding),
-                     (self.fip['id'],)]
-        self.assertRaises(c_exc.CallbackFailure,
-                          self._simulate_concurrent_requests_process_and_raise,
-                          funcs, args_list)
-
-        port_forwardings = self.pf_plugin.get_floatingip_port_forwardings(
-            self.context, floatingip_id=self.fip['id'], fields=['id'])
-        self.pf_plugin.delete_floatingip_port_forwarding(
-            self.context, port_forwardings[0][apidef.ID],
-            floatingip_id=self.fip['id'])
-
-        funcs.reverse()
-        args_list.reverse()
-        self.assertRaises(lib_l3_exc.FloatingIPNotFound,
-                          self._simulate_concurrent_requests_process_and_raise,
-                          funcs, args_list)
-
-    def test_concurrent_create_port_forwarding_update_fip(self):
-        newport = self._create_port(self.fmt, self.net['id']).json['port']
-        func1 = self.pf_plugin.create_floatingip_port_forwarding
-        func2 = self._update_floatingip
-        funcs = [func1, func2]
-        args_list = [(self.context, self.fip['id'], self.port_forwarding),
-                     (self.fip['id'], {'port_id': newport['id']})]
-        self.assertRaises(c_exc.CallbackFailure,
-                          self._simulate_concurrent_requests_process_and_raise,
-                          funcs, args_list)
-
-        funcs.reverse()
-        args_list.reverse()
-        self.assertRaises(c_exc.CallbackFailure,
-                          self._simulate_concurrent_requests_process_and_raise,
-                          funcs, args_list)
-
-    def test_concurrent_create_port_forwarding_update_port(self):
-        new_ip = self._find_ip_address(
-            self.subnet,
-            exclude=self._get_network_port_ips(),
-            is_random=True)
-        funcs = [self.pf_plugin.create_floatingip_port_forwarding,
-                 self._update_port]
-        args_list = [(self.context, self.fip['id'], self.port_forwarding),
-                     (self.port['id'], {
-                         'fixed_ips': [{'subnet_id': self.subnet['id'],
-                                        'ip_address': new_ip}]})]
-        self._simulate_concurrent_requests_process_and_raise(funcs, args_list)
-        self.assertEqual([], self.pf_plugin.get_floatingip_port_forwardings(
-            self.context, floatingip_id=self.fip['id']))
-
-    def test_concurrent_create_port_forwarding_delete_port(self):
-        funcs = [self.pf_plugin.create_floatingip_port_forwarding,
-                 self._delete_port]
-        args_list = [(self.context, self.fip['id'], self.port_forwarding),
-                     (self.port['id'],)]
-        self._simulate_concurrent_requests_process_and_raise(funcs, args_list)
-        self.assertEqual([], self.pf_plugin.get_floatingip_port_forwardings(
-            self.context, floatingip_id=self.fip['id']))
-
     def test_create_floatingip_port_forwarding_port_in_use(self):
         res = self.pf_plugin.create_floatingip_port_forwarding(
             self.context, self.fip['id'], self.port_forwarding)
diff --git a/neutron-17.1.1/neutron/tests/unit/db/test_l3_db.py b/neutron-17.1.1.new/neutron/tests/unit/db/test_l3_db.py
index eeff600..70cb493 100644
--- a/neutron-17.1.1/neutron/tests/unit/db/test_l3_db.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/db/test_l3_db.py
@@ -21,6 +21,7 @@ from neutron_lib.callbacks import registry
 from neutron_lib.callbacks import resources
 from neutron_lib import constants as n_const
 from neutron_lib import context
+from neutron_lib.db import api as db_api
 from neutron_lib import exceptions as n_exc
 from neutron_lib.plugins import constants as plugin_constants
 from neutron_lib.plugins import directory
@@ -39,9 +40,11 @@ from neutron.tests import base
 from neutron.tests.unit.db import test_db_base_plugin_v2
 
 
-class TestL3_NAT_dbonly_mixin(base.BaseTestCase):
-    def setUp(self):
-        super(TestL3_NAT_dbonly_mixin, self).setUp()
+class TestL3_NAT_dbonly_mixin(
+        test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
+
+    def setUp(self, *args, **kwargs):
+        super(TestL3_NAT_dbonly_mixin, self).setUp(*args, **kwargs)
         self.db = l3_db.L3_NAT_dbonly_mixin()
 
     def test__each_port_having_fixed_ips_none(self):
@@ -289,6 +292,9 @@ class TestL3_NAT_dbonly_mixin(base.BaseTestCase):
                 **kwargs)
 
     def test__create_gw_port(self):
+        # NOTE(slaweq): this test is probably wrong
+        # returing dict as gw_port breaks test later in L334 in
+        # neutron.db.l3_db file
         router_id = '2afb8434-7380-43a2-913f-ba3a5ad5f349'
         router = l3_models.Router(id=router_id)
         new_network_id = 'net-id'
@@ -298,37 +304,42 @@ class TestL3_NAT_dbonly_mixin(base.BaseTestCase):
                    'id': '8742d007-6f05-4b7e-abdb-11818f608959'}
         ctx = context.get_admin_context()
 
-        with mock.patch.object(directory, 'get_plugin') as get_p, \
-                mock.patch.object(get_p(), 'get_subnets_by_network',
-                                  return_value=mock.ANY), \
-                mock.patch.object(get_p(), '_get_port',
-                                  return_value=gw_port), \
-                mock.patch.object(l3_db.L3_NAT_dbonly_mixin,
-                                  '_check_for_dup_router_subnets') as cfdrs,\
-                mock.patch.object(plugin_utils, 'create_port',
-                                  return_value=gw_port), \
-                mock.patch.object(ctx.session, 'add'), \
-                mock.patch.object(base_obj.NeutronDbObject, 'create'), \
-                mock.patch.object(l3_db.registry, 'publish') as mock_notify:
-
-            self.db._create_gw_port(ctx, router_id=router_id,
-                                    router=router,
-                                    new_network_id=new_network_id,
-                                    ext_ips=ext_ips)
-
-            expected_gw_ips = ['1.1.1.1']
-
-            self.assertTrue(cfdrs.called)
-            mock_notify.assert_called_with(
-                resources.ROUTER_GATEWAY, events.AFTER_CREATE,
-                self.db._create_gw_port, payload=mock.ANY)
-            cb_payload = mock_notify.mock_calls[1][2]['payload']
-            self.assertEqual(ctx, cb_payload.context)
-            self.assertEqual(expected_gw_ips,
-                             cb_payload.metadata.get('gateway_ips'))
-            self.assertEqual(new_network_id,
-                             cb_payload.metadata.get('network_id'))
-            self.assertEqual(router_id, cb_payload.resource_id)
+        with db_api.CONTEXT_WRITER.using(ctx):
+            with mock.patch.object(directory, 'get_plugin') as get_p, \
+                    mock.patch.object(get_p(), 'get_subnets_by_network',
+                                      return_value=mock.ANY), \
+                    mock.patch.object(get_p(), '_get_port',
+                                      return_value=gw_port), \
+                    mock.patch.object(l3_db.L3_NAT_dbonly_mixin,
+                                      '_check_for_dup_router_subnets') as \
+                    cfdrs, \
+                    mock.patch.object(plugin_utils, 'create_port',
+                                      return_value=gw_port), \
+                    mock.patch.object(ctx.session, 'add'), \
+                    mock.patch.object(base_obj.NeutronDbObject, 'create'), \
+                    mock.patch.object(l3_db.registry, 'publish') as \
+                    mock_notify, \
+                    mock.patch.object(l3_db.L3_NAT_dbonly_mixin, '_get_router',
+                                      return_value=router):
+
+                self.db._create_gw_port(ctx, router_id=router_id,
+                                        router=router,
+                                        new_network_id=new_network_id,
+                                        ext_ips=ext_ips)
+
+                expected_gw_ips = ['1.1.1.1']
+
+                self.assertTrue(cfdrs.called)
+                mock_notify.assert_called_with(
+                    resources.ROUTER_GATEWAY, events.AFTER_CREATE,
+                    self.db._create_gw_port, payload=mock.ANY)
+                cb_payload = mock_notify.mock_calls[1][2]['payload']
+                self.assertEqual(ctx, cb_payload.context)
+                self.assertEqual(expected_gw_ips,
+                                 cb_payload.metadata.get('gateway_ips'))
+                self.assertEqual(new_network_id,
+                                 cb_payload.metadata.get('network_id'))
+                self.assertEqual(router_id, cb_payload.resource_id)
 
 
 class L3_NAT_db_mixin(base.BaseTestCase):
@@ -428,20 +439,20 @@ class L3TestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
         l3_obj.Router.get_object(self.ctx, id=self.router['id']).delete()
 
     def create_router(self, router):
-        with self.ctx.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(self.ctx):
             return self.mixin.create_router(self.ctx, router)
 
     def create_port(self, net_id, port_info):
-        with self.ctx.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(self.ctx):
             return self._make_port(self.fmt, net_id, **port_info)
 
     def create_network(self, name=None, **kwargs):
         name = name or 'network1'
-        with self.ctx.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(self.ctx):
             return self._make_network(self.fmt, name, True, **kwargs)
 
     def create_subnet(self, network, gateway, cidr, **kwargs):
-        with self.ctx.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(self.ctx):
             return self._make_subnet(self.fmt, network, gateway, cidr,
                                      **kwargs)
 
diff --git a/neutron-17.1.1/neutron/tests/unit/db/test_l3_dvr_db.py b/neutron-17.1.1.new/neutron/tests/unit/db/test_l3_dvr_db.py
index 89a3894..21699e4 100644
--- a/neutron-17.1.1/neutron/tests/unit/db/test_l3_dvr_db.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/db/test_l3_dvr_db.py
@@ -21,6 +21,7 @@ from neutron_lib.callbacks import registry
 from neutron_lib.callbacks import resources
 from neutron_lib import constants as const
 from neutron_lib import 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 neutron_lib.objects import exceptions as o_exc
@@ -60,11 +61,11 @@ class L3DvrTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
         directory.add_plugin(plugin_constants.L3, self.mixin)
 
     def _create_router(self, router):
-        with self.ctx.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(self.ctx):
             return self.mixin._create_router_db(self.ctx, router, 'foo_tenant')
 
     def create_port(self, net_id, port_info):
-        with self.ctx.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(self.ctx):
             return self._create_port(self.fmt, net_id, **port_info)
 
     def _test__create_router_db(self, expected=False, distributed=None):
@@ -463,20 +464,20 @@ class L3DvrTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
             'admin_state_up': True,
             'distributed': True
         }
-        router = self._create_router(router_db)
-        if gw_port:
-            with self.subnet(cidr='10.10.10.0/24') as subnet:
-                port_dict = {
-                    'device_id': router.id,
-                    'device_owner': const.DEVICE_OWNER_ROUTER_GW,
-                    'admin_state_up': True,
-                    'fixed_ips': [{'subnet_id': subnet['subnet']['id'],
-                                   'ip_address': '10.10.10.100'}]
-                }
-            net_id = subnet['subnet']['network_id']
-            port_res = self.create_port(net_id, port_dict)
-            port_res_dict = self.deserialize(self.fmt, port_res)
-            with self.ctx.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            router = self._create_router(router_db)
+            if gw_port:
+                with self.subnet(cidr='10.10.10.0/24') as subnet:
+                    port_dict = {
+                        'device_id': router.id,
+                        'device_owner': const.DEVICE_OWNER_ROUTER_GW,
+                        'admin_state_up': True,
+                        'fixed_ips': [{'subnet_id': subnet['subnet']['id'],
+                                       'ip_address': '10.10.10.100'}]
+                    }
+                net_id = subnet['subnet']['network_id']
+                port_res = self.create_port(net_id, port_dict)
+                port_res_dict = self.deserialize(self.fmt, port_res)
                 port_db = self.ctx.session.query(models_v2.Port).filter_by(
                     id=port_res_dict['port']['id']).one()
                 router.gw_port = port_db
@@ -487,9 +488,8 @@ class L3DvrTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
                 )
                 self.ctx.session.add(router)
                 self.ctx.session.add(router_port)
-
-        else:
-            net_id = None
+            else:
+                net_id = None
 
         plugin = mock.Mock()
         directory.add_plugin(plugin_constants.CORE, plugin)
@@ -1132,6 +1132,10 @@ class L3DvrTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
             mock_notify.assert_called_once_with(
                 'router', 'before_update', self.mixin, **kwargs)
 
+    def _assert_mock_called_with_router(self, mock_fn, router_id):
+        router = mock_fn.call_args[1].get('router_db')
+        self.assertEqual(router_id, router.id)
+
     def test__validate_router_migration_notify_advanced_services_mocked(self):
         # call test with admin_state_down_before_update ENABLED
         self._test__validate_router_migration_notify_advanced_services()
@@ -1152,9 +1156,16 @@ class L3DvrTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
             interface_info = {'subnet_id': sub['subnet']['id']}
             self.mixin.add_router_interface(self.ctx, router_db.id,
                                             interface_info)
-            mock_notify.assert_called_once_with(self.ctx, router_db=router_db,
+            # NOTE(slaweq): here we are just checking if mock_notify was called
+            # with kwargs which we are expecting, but we can't check exactly if
+            # router_db was object which we are expecting and because of that
+            # below we are checking if router_db used as argument in
+            # mock_notify call is the has same id as the one which we are
+            # expecting
+            mock_notify.assert_called_once_with(self.ctx, router_db=mock.ANY,
                                                 port=mock.ANY,
                                                 interface_info=interface_info)
+            self._assert_mock_called_with_router(mock_notify, router_db.id)
 
     def test_validate_add_router_interface_by_port_notify_advanced_services(
             self):
@@ -1169,9 +1180,16 @@ class L3DvrTestCase(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
             interface_info = {'port_id': port['port']['id']}
             self.mixin.add_router_interface(self.ctx, router_db.id,
                                             interface_info)
-            mock_notify.assert_called_once_with(self.ctx, router_db=router_db,
+            # NOTE(slaweq): here we are just checking if mock_notify was called
+            # with kwargs which we are expecting, but we can't check exactly if
+            # router_db was object which we are expecting and because of that
+            # below we are checking if router_db used as argument in
+            # mock_notify call is the has same id as the one which we are
+            # expecting.
+            mock_notify.assert_called_once_with(self.ctx, router_db=mock.ANY,
                                                 port=mock.ANY,
                                                 interface_info=interface_info)
+            self._assert_mock_called_with_router(mock_notify, router_db.id)
 
     def test__generate_arp_table_and_notify_agent(self):
         fixed_ip = {
diff --git a/neutron-17.1.1/neutron/tests/unit/db/test_ovn_revision_numbers_db.py b/neutron-17.1.1.new/neutron/tests/unit/db/test_ovn_revision_numbers_db.py
index d1692c8..1056f21 100644
--- a/neutron-17.1.1/neutron/tests/unit/db/test_ovn_revision_numbers_db.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/db/test_ovn_revision_numbers_db.py
@@ -54,57 +54,72 @@ class TestRevisionNumber(test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
     def _create_initial_revision(self, resource_uuid, resource_type,
                                  revision_number=ovn_rn_db.INITIAL_REV_NUM,
                                  may_exist=False):
-        with self.ctx.session.begin(subtransactions=True):
-            ovn_rn_db.create_initial_revision(
-                self.ctx, resource_uuid, resource_type,
-                revision_number=revision_number, may_exist=may_exist)
+        ovn_rn_db.create_initial_revision(
+            self.ctx, resource_uuid, resource_type,
+            revision_number=revision_number, may_exist=may_exist)
 
     def test_bump_revision(self):
-        self._create_initial_revision(self.net['id'], ovn_rn_db.TYPE_NETWORKS)
-        self.net['revision_number'] = 123
-        ovn_rn_db.bump_revision(self.ctx, self.net,
-                                ovn_rn_db.TYPE_NETWORKS)
-        row = ovn_rn_db.get_revision_row(self.ctx, self.net['id'])
-        self.assertEqual(123, row.revision_number)
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            self._create_initial_revision(self.net['id'],
+                                          ovn_rn_db.TYPE_NETWORKS)
+            self.net['revision_number'] = 123
+            ovn_rn_db.bump_revision(self.ctx, self.net,
+                                    ovn_rn_db.TYPE_NETWORKS)
+            row = ovn_rn_db.get_revision_row(self.ctx, self.net['id'])
+            self.assertEqual(123, row.revision_number)
 
     def test_bump_older_revision(self):
-        self._create_initial_revision(self.net['id'], ovn_rn_db.TYPE_NETWORKS,
-                                      revision_number=124)
-        self.net['revision_number'] = 1
-        ovn_rn_db.bump_revision(self.ctx, self.net,
-                                ovn_rn_db.TYPE_NETWORKS)
-        row = ovn_rn_db.get_revision_row(self.ctx, self.net['id'])
-        self.assertEqual(124, row.revision_number)
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            self._create_initial_revision(
+                self.net['id'], ovn_rn_db.TYPE_NETWORKS,
+                revision_number=124)
+            self.net['revision_number'] = 1
+            ovn_rn_db.bump_revision(self.ctx, self.net,
+                                    ovn_rn_db.TYPE_NETWORKS)
+            row = ovn_rn_db.get_revision_row(self.ctx, self.net['id'])
+            self.assertEqual(124, row.revision_number)
 
     @mock.patch.object(ovn_rn_db.LOG, 'warning')
     def test_bump_revision_row_not_found(self, mock_log):
-        self.net['revision_number'] = 123
-        ovn_rn_db.bump_revision(self.ctx, self.net, ovn_rn_db.TYPE_NETWORKS)
-        # Assert the revision number wasn't bumped
-        row = ovn_rn_db.get_revision_row(self.ctx, self.net['id'])
-        self.assertEqual(123, row.revision_number)
-        self.assertIn('No revision row found for', mock_log.call_args[0][0])
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            self.net['revision_number'] = 123
+            ovn_rn_db.bump_revision(self.ctx, self.net,
+                                    ovn_rn_db.TYPE_NETWORKS)
+            # Assert the revision number wasn't bumped
+            row = ovn_rn_db.get_revision_row(self.ctx, self.net['id'])
+            self.assertEqual(123, row.revision_number)
+            self.assertIn('No revision row found for',
+                          mock_log.call_args[0][0])
 
     def test_delete_revision(self):
-        self._create_initial_revision(self.net['id'], ovn_rn_db.TYPE_NETWORKS)
-        ovn_rn_db.delete_revision(self.ctx, self.net['id'],
-                                  ovn_rn_db.TYPE_NETWORKS)
-        row = ovn_rn_db.get_revision_row(self.ctx, self.net['id'])
-        self.assertIsNone(row)
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            self._create_initial_revision(self.net['id'],
+                                          ovn_rn_db.TYPE_NETWORKS)
+            ovn_rn_db.delete_revision(self.ctx, self.net['id'],
+                                      ovn_rn_db.TYPE_NETWORKS)
+            row = ovn_rn_db.get_revision_row(self.ctx, self.net['id'])
+            self.assertIsNone(row)
 
     def test_create_initial_revision_may_exist_duplicated_entry(self):
-        args = (self.net['id'], ovn_rn_db.TYPE_NETWORKS)
-        self._create_initial_revision(*args)
-
-        # Assert DBDuplicateEntry is raised when may_exist is False (default)
-        self.assertRaises(db_exc.DBDuplicateEntry,
-                          self._create_initial_revision, *args)
-
         try:
-            self._create_initial_revision(*args, may_exist=True)
-        except db_exc.DBDuplicateEntry:
-            self.fail("create_initial_revision shouldn't raise "
-                      "DBDuplicateEntry when may_exist is True")
+            with db_api.CONTEXT_WRITER.using(self.ctx):
+                args = (self.net['id'], ovn_rn_db.TYPE_NETWORKS)
+                self._create_initial_revision(*args)
+                # DBDuplicateEntry is raised when may_exist is False (default)
+                self._create_initial_revision(*args)
+        except Exception as exc:
+            if type(exc) is not db_exc.DBDuplicateEntry:
+                self.fail("create_initial_revision with the same parameters "
+                          "should have raisen a DBDuplicateEntry exception")
+
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            args = (self.net['id'], ovn_rn_db.TYPE_NETWORKS)
+            self._create_initial_revision(*args)
+            try:
+                self._create_initial_revision(*args, may_exist=True)
+            except db_exc.DBDuplicateEntry:
+                self.fail("create_initial_revision shouldn't raise "
+                          "DBDuplicateEntry when may_exist is True")
 
 
 class TestMaintenancePlugin(test_securitygroup.SecurityGroupTestPlugin,
@@ -149,7 +164,7 @@ class TestRevisionNumberMaintenance(test_securitygroup.SecurityGroupsTestCase,
     def _create_initial_revision(self, resource_uuid, resource_type,
                                  revision_number=ovn_rn_db.INITIAL_REV_NUM,
                                  may_exist=False):
-        with self.ctx.session.begin(subtransactions=True):
+        with db_api.CONTEXT_WRITER.using(self.ctx):
             ovn_rn_db.create_initial_revision(
                 self.ctx, resource_uuid, resource_type,
                 revision_number=revision_number, may_exist=may_exist)
diff --git a/neutron-17.1.1/neutron/tests/unit/extensions/test_l3.py b/neutron-17.1.1.new/neutron/tests/unit/extensions/test_l3.py
index 2557d2d..bbb41c2 100644
--- a/neutron-17.1.1/neutron/tests/unit/extensions/test_l3.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/extensions/test_l3.py
@@ -821,8 +821,8 @@ class L3NatTestCaseBase(L3NatTestCaseMixin):
 
     def test_router_update_gateway(self):
         with self.router() as r:
-            with self.subnet() as s1:
-                with self.subnet() as s2:
+            with self.subnet(cidr='10.51.0.0/24') as s1:
+                with self.subnet(cidr='10.52.0.0/24') as s2:
                     self._set_net_external(s1['subnet']['network_id'])
                     self._add_external_gateway_to_router(
                         r['router']['id'],
diff --git a/neutron-17.1.1/neutron/tests/unit/extensions/test_portsecurity.py b/neutron-17.1.1.new/neutron/tests/unit/extensions/test_portsecurity.py
index 0b48333..e9a97d2 100644
--- a/neutron-17.1.1/neutron/tests/unit/extensions/test_portsecurity.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/extensions/test_portsecurity.py
@@ -14,6 +14,7 @@
 # limitations under the License.
 
 import copy
+from unittest import mock
 
 from neutron_lib.api.definitions import port_security as psec
 from neutron_lib.api import validators
@@ -28,6 +29,7 @@ from neutron.db import db_base_plugin_v2
 from neutron.db import portsecurity_db
 from neutron.db import securitygroups_db
 from neutron.extensions import securitygroup as ext_sg
+from neutron import quota
 from neutron.tests.unit.db import test_db_base_plugin_v2
 from neutron.tests.unit.extensions import test_securitygroup
 
@@ -65,9 +67,9 @@ class PortSecurityTestPlugin(db_base_plugin_v2.NeutronDbPluginV2,
     supported_extension_aliases = ["security-group", psec.ALIAS]
 
     def create_network(self, context, network):
-        tenant_id = network['network'].get('tenant_id')
-        self._ensure_default_security_group(context, tenant_id)
         with db_api.CONTEXT_WRITER.using(context):
+            tenant_id = network['network'].get('tenant_id')
+            self._ensure_default_security_group(context, tenant_id)
             neutron_db = super(PortSecurityTestPlugin, self).create_network(
                 context, network)
             neutron_db.update(network['network'])
@@ -177,6 +179,14 @@ class PortSecurityDBTestCase(PortSecurityTestCase):
 
 
 class TestPortSecurity(PortSecurityDBTestCase):
+
+    def setUp(self, plugin=None, service_plugins=None):
+        super().setUp(plugin)
+        make_res = mock.patch.object(quota.QuotaEngine, 'make_reservation')
+        commit_res = mock.patch.object(quota.QuotaEngine, 'commit_reservation')
+        self.mock_quota_make_res = make_res.start()
+        self.mock_quota_commit_res = commit_res.start()
+
     def test_create_network_with_portsecurity_mac(self):
         res = self._create_network('json', 'net1', True)
         net = self.deserialize('json', res)
diff --git a/neutron-17.1.1/neutron/tests/unit/objects/db/test_api.py b/neutron-17.1.1.new/neutron/tests/unit/objects/db/test_api.py
index 4d2b498..d34fc9d 100644
--- a/neutron-17.1.1/neutron/tests/unit/objects/db/test_api.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/objects/db/test_api.py
@@ -99,56 +99,61 @@ class CRUDScenarioTestCase(testlib_api.SqlTestCase):
         self.obj_cls = network.Network
         self.ctxt = context.get_admin_context()
 
+    def _compare_objs(self, obj1, obj2):
+        for field in (field for field in self.obj_cls.fields if
+                      field not in ('updated_at', 'created_at')):
+            self.assertEqual(getattr(obj1, field, None),
+                             getattr(obj2, field, None))
+
     def test_get_object_with_None_value_in_filters(self):
         obj = api.create_object(self.obj_cls, self.ctxt, {'name': 'foo'})
         new_obj = api.get_object(
             self.obj_cls, self.ctxt, name='foo', status=None)
-        self.assertEqual(obj, new_obj)
+        self._compare_objs(obj, new_obj)
 
     def test_get_objects_with_None_value_in_filters(self):
         obj = api.create_object(self.obj_cls, self.ctxt, {'name': 'foo'})
         new_objs = api.get_objects(
             self.obj_cls, self.ctxt, name='foo', status=None)
-        self.assertEqual(obj, new_objs[0])
+        self._compare_objs(obj, new_objs[0])
 
     def test_get_objects_with_string_matching_filters_contains(self):
         obj1 = api.create_object(
             self.obj_cls, self.ctxt, {'name': 'obj_con_1'})
         obj2 = api.create_object(
             self.obj_cls, self.ctxt, {'name': 'obj_con_2'})
-        obj3 = api.create_object(
-            self.obj_cls, self.ctxt, {'name': 'obj_3'})
+        api.create_object(self.obj_cls, self.ctxt, {'name': 'obj_3'})
 
         objs = api.get_objects(
-            self.obj_cls, self.ctxt, name=obj_utils.StringContains('con'))
+            self.obj_cls, self.ctxt, name=obj_utils.StringContains('con'),
+            _pager=base.Pager(sorts=[('name', True)]))
         self.assertEqual(2, len(objs))
-        self.assertIn(obj1, objs)
-        self.assertIn(obj2, objs)
-        self.assertNotIn(obj3, objs)
+        self._compare_objs(obj1, objs[0])
+        self._compare_objs(obj2, objs[1])
 
     def test_get_objects_with_string_matching_filters_starts(self):
         obj1 = api.create_object(self.obj_cls, self.ctxt, {'name': 'pre_obj1'})
         obj2 = api.create_object(self.obj_cls, self.ctxt, {'name': 'pre_obj2'})
-        obj3 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj_3'})
+        api.create_object(self.obj_cls, self.ctxt, {'name': 'obj_3'})
 
         objs = api.get_objects(
-            self.obj_cls, self.ctxt, name=obj_utils.StringStarts('pre'))
+            self.obj_cls, self.ctxt, name=obj_utils.StringStarts('pre'),
+            _pager=base.Pager(sorts=[('name', True)]))
         self.assertEqual(2, len(objs))
-        self.assertIn(obj1, objs)
-        self.assertIn(obj2, objs)
-        self.assertNotIn(obj3, objs)
+        self._compare_objs(obj1, objs[0])
+        self._compare_objs(obj2, objs[1])
 
     def test_get_objects_with_string_matching_filters_ends(self):
         obj1 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj1_end'})
         obj2 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj2_end'})
-        obj3 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj_3'})
+        api.create_object(self.obj_cls, self.ctxt, {'name': 'obj_3'})
 
         objs = api.get_objects(
-            self.obj_cls, self.ctxt, name=obj_utils.StringEnds('end'))
+            self.obj_cls, self.ctxt, name=obj_utils.StringEnds('end'),
+            _pager=base.Pager(sorts=[('name', True)]))
         self.assertEqual(2, len(objs))
-        self.assertIn(obj1, objs)
-        self.assertIn(obj2, objs)
-        self.assertNotIn(obj3, objs)
+        self._compare_objs(obj1, objs[0])
+        self._compare_objs(obj2, objs[1])
 
     def test_get_values_with_None_value_in_filters(self):
         api.create_object(self.obj_cls, self.ctxt, {'name': 'foo'})
@@ -201,15 +206,14 @@ class CRUDScenarioTestCase(testlib_api.SqlTestCase):
         obj = api.create_object(self.obj_cls, self.ctxt, {'name': 'foo'})
 
         new_obj = api.get_object(self.obj_cls, self.ctxt, id=obj.id)
-        self.assertEqual(obj, new_obj)
+        self._compare_objs(obj, new_obj)
 
-        obj = new_obj
+        obj.name = 'bar'
         api.update_object(self.obj_cls, self.ctxt, {'name': 'bar'}, id=obj.id)
 
         new_obj = api.get_object(self.obj_cls, self.ctxt, id=obj.id)
-        self.assertEqual(obj, new_obj)
+        self._compare_objs(obj, new_obj)
 
-        obj = new_obj
         api.delete_object(self.obj_cls, self.ctxt, id=obj.id)
 
         new_obj = api.get_object(self.obj_cls, self.ctxt, id=obj.id)
diff --git a/neutron-17.1.1/neutron/tests/unit/objects/test_base.py b/neutron-17.1.1.new/neutron/tests/unit/objects/test_base.py
index c66ce65..ef1e1a0 100644
--- a/neutron-17.1.1/neutron/tests/unit/objects/test_base.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/objects/test_base.py
@@ -100,8 +100,6 @@ class FakeSmallNeutronObjectNewEngineFacade(base.NeutronDbObject):
 
     db_model = ObjectFieldsModel
 
-    new_facade = True
-
     primary_keys = ['field1']
 
     foreign_keys = {
@@ -1793,7 +1791,7 @@ class BaseDbObjectTestCase(_BaseObjectTestCase,
 
     def test_get_objects_single_transaction(self):
         with mock.patch(self._get_ro_txn_exit_func_name()) as mock_exit:
-            with db_api.autonested_transaction(self.context.session):
+            with db_api.CONTEXT_READER.using(self.context):
                 self._test_class.get_objects(self.context)
         self.assertEqual(1, mock_exit.call_count)
 
@@ -1808,7 +1806,7 @@ class BaseDbObjectTestCase(_BaseObjectTestCase,
         obj.create()
 
         with mock.patch(self._get_ro_txn_exit_func_name()) as mock_exit:
-            with db_api.autonested_transaction(self.context.session):
+            with db_api.CONTEXT_READER.using(self.context):
                 obj = self._test_class.get_object(self.context,
                                                   **obj._get_composite_keys())
         self.assertEqual(1, mock_exit.call_count)
diff --git a/neutron-17.1.1/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/extensions/test_qos.py b/neutron-17.1.1.new/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/extensions/test_qos.py
index 28886d4..3e3b860 100644
--- a/neutron-17.1.1/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/extensions/test_qos.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/extensions/test_qos.py
@@ -17,6 +17,7 @@ from unittest import mock
 import netaddr
 from neutron_lib import constants
 from neutron_lib import context
+from neutron_lib.db import api as db_api
 from neutron_lib.services.qos import constants as qos_constants
 from oslo_config import cfg
 from oslo_utils import uuidutils
diff --git a/neutron-17.1.1/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_maintenance.py b/neutron-17.1.1.new/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_maintenance.py
index 03121f5..27601ce 100644
--- a/neutron-17.1.1/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_maintenance.py
+++ b/neutron-17.1.1.new/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_maintenance.py
@@ -17,6 +17,7 @@ from unittest import mock
 
 from futurist import periodics
 from neutron_lib import context
+from neutron_lib.db import api as db_api
 from oslo_config import cfg
 
 from neutron.common.ovn import constants
@@ -139,39 +140,40 @@ class TestDBInconsistenciesPeriodics(testlib_api.SqlTestCaseLight,
                                                      never_again=False)
 
     def _test_fix_create_update_network(self, ovn_rev, neutron_rev):
-        self.net['revision_number'] = neutron_rev
-
-        # Create an entry to the revision_numbers table and assert the
-        # initial revision_number for our test object is the expected
-        ovn_revision_numbers_db.create_initial_revision(
-            self.ctx, self.net['id'], constants.TYPE_NETWORKS,
-            revision_number=ovn_rev)
-        row = ovn_revision_numbers_db.get_revision_row(self.ctx,
-                                                       self.net['id'])
-        self.assertEqual(ovn_rev, row.revision_number)
-
-        if ovn_rev < 0:
-            self.fake_ovn_client._nb_idl.get_lswitch.return_value = None
-        else:
-            fake_ls = mock.Mock(external_ids={
-                constants.OVN_REV_NUM_EXT_ID_KEY: ovn_rev})
-            self.fake_ovn_client._nb_idl.get_lswitch.return_value = fake_ls
-
-        self.fake_ovn_client._plugin.get_network.return_value = self.net
-        self.periodic._fix_create_update(self.ctx, row)
-
-        # Since the revision number was < 0, make sure create_network()
-        # is invoked with the latest version of the object in the neutron
-        # database
-        if ovn_rev < 0:
-            self.fake_ovn_client.create_network.assert_called_once_with(
-                self.ctx, self.net)
-        # If the revision number is > 0 it means that the object already
-        # exist and we just need to update to match the latest in the
-        # neutron database so, update_network() should be called.
-        else:
-            self.fake_ovn_client.update_network.assert_called_once_with(
-                self.ctx, self.net)
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            self.net['revision_number'] = neutron_rev
+
+            # Create an entry to the revision_numbers table and assert the
+            # initial revision_number for our test object is the expected
+            ovn_revision_numbers_db.create_initial_revision(
+                self.ctx, self.net['id'], constants.TYPE_NETWORKS,
+                revision_number=ovn_rev)
+            row = ovn_revision_numbers_db.get_revision_row(self.ctx,
+                                                           self.net['id'])
+            self.assertEqual(ovn_rev, row.revision_number)
+
+            if ovn_rev < 0:
+                self.fake_ovn_client._nb_idl.get_lswitch.return_value = None
+            else:
+                fake_ls = mock.Mock(external_ids={
+                    constants.OVN_REV_NUM_EXT_ID_KEY: ovn_rev})
+                self.fake_ovn_client._nb_idl.get_lswitch.return_value = fake_ls
+
+            self.fake_ovn_client._plugin.get_network.return_value = self.net
+            self.periodic._fix_create_update(self.ctx, row)
+
+            # Since the revision number was < 0, make sure create_network()
+            # is invoked with the latest version of the object in the neutron
+            # database
+            if ovn_rev < 0:
+                self.fake_ovn_client.create_network.assert_called_once_with(
+                    self.ctx, self.net)
+            # If the revision number is > 0 it means that the object already
+            # exist and we just need to update to match the latest in the
+            # neutron database so, update_network() should be called.
+            else:
+                self.fake_ovn_client.update_network.assert_called_once_with(
+                    self.ctx, self.net)
 
     def test_fix_network_create(self):
         self._test_fix_create_update_network(ovn_rev=-1, neutron_rev=2)
@@ -180,40 +182,41 @@ class TestDBInconsistenciesPeriodics(testlib_api.SqlTestCaseLight,
         self._test_fix_create_update_network(ovn_rev=5, neutron_rev=7)
 
     def _test_fix_create_update_port(self, ovn_rev, neutron_rev):
-        self.port['revision_number'] = neutron_rev
-
-        # Create an entry to the revision_numbers table and assert the
-        # initial revision_number for our test object is the expected
-        ovn_revision_numbers_db.create_initial_revision(
-            self.ctx, self.port['id'], constants.TYPE_PORTS,
-            revision_number=ovn_rev)
-        row = ovn_revision_numbers_db.get_revision_row(self.ctx,
-                                                       self.port['id'])
-        self.assertEqual(ovn_rev, row.revision_number)
-
-        if ovn_rev < 0:
-            self.fake_ovn_client._nb_idl.get_lswitch_port.return_value = None
-        else:
-            fake_lsp = mock.Mock(external_ids={
-                constants.OVN_REV_NUM_EXT_ID_KEY: ovn_rev})
-            self.fake_ovn_client._nb_idl.get_lswitch_port.return_value = (
-                fake_lsp)
-
-        self.fake_ovn_client._plugin.get_port.return_value = self.port
-        self.periodic._fix_create_update(self.ctx, row)
-
-        # Since the revision number was < 0, make sure create_port()
-        # is invoked with the latest version of the object in the neutron
-        # database
-        if ovn_rev < 0:
-            self.fake_ovn_client.create_port.assert_called_once_with(
-                self.ctx, self.port)
-        # If the revision number is > 0 it means that the object already
-        # exist and we just need to update to match the latest in the
-        # neutron database so, update_port() should be called.
-        else:
-            self.fake_ovn_client.update_port.assert_called_once_with(
-                self.ctx, self.port)
+        _nb_idl = self.fake_ovn_client._nb_idl
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            self.port['revision_number'] = neutron_rev
+
+            # Create an entry to the revision_numbers table and assert the
+            # initial revision_number for our test object is the expected
+            ovn_revision_numbers_db.create_initial_revision(
+                self.ctx, self.port['id'], constants.TYPE_PORTS,
+                revision_number=ovn_rev)
+            row = ovn_revision_numbers_db.get_revision_row(self.ctx,
+                                                           self.port['id'])
+            self.assertEqual(ovn_rev, row.revision_number)
+
+            if ovn_rev < 0:
+                _nb_idl.get_lswitch_port.return_value = None
+            else:
+                fake_lsp = mock.Mock(external_ids={
+                    constants.OVN_REV_NUM_EXT_ID_KEY: ovn_rev})
+                _nb_idl.get_lswitch_port.return_value = fake_lsp
+
+            self.fake_ovn_client._plugin.get_port.return_value = self.port
+            self.periodic._fix_create_update(self.ctx, row)
+
+            # Since the revision number was < 0, make sure create_port()
+            # is invoked with the latest version of the object in the neutron
+            # database
+            if ovn_rev < 0:
+                self.fake_ovn_client.create_port.assert_called_once_with(
+                    self.ctx, self.port)
+            # If the revision number is > 0 it means that the object already
+            # exist and we just need to update to match the latest in the
+            # neutron database so, update_port() should be called.
+            else:
+                self.fake_ovn_client.update_port.assert_called_once_with(
+                    self.ctx, self.port)
 
     def test_fix_port_create(self):
         self._test_fix_create_update_port(ovn_rev=-1, neutron_rev=2)
@@ -223,14 +226,16 @@ class TestDBInconsistenciesPeriodics(testlib_api.SqlTestCaseLight,
 
     @mock.patch.object(ovn_revision_numbers_db, 'bump_revision')
     def _test_fix_security_group_create(self, mock_bump, revision_number):
-        sg_name = utils.ovn_addrset_name('fake_id', 'ip4')
-        sg = self._make_security_group(self.fmt, sg_name, '')['security_group']
-
-        ovn_revision_numbers_db.create_initial_revision(
-            self.ctx, sg['id'], constants.TYPE_SECURITY_GROUPS,
-            revision_number=revision_number)
-        row = ovn_revision_numbers_db.get_revision_row(self.ctx, sg['id'])
-        self.assertEqual(revision_number, row.revision_number)
+        with db_api.CONTEXT_WRITER.using(self.ctx):
+            sg_name = utils.ovn_addrset_name('fake_id', 'ip4')
+            sg = self._make_security_group(
+                self.fmt, sg_name, '')['security_group']
+
+            ovn_revision_numbers_db.create_initial_revision(
+                self.ctx, sg['id'], constants.TYPE_SECURITY_GROUPS,
+                revision_number=revision_number)
+            row = ovn_revision_numbers_db.get_revision_row(self.ctx, sg['id'])
+            self.assertEqual(revision_number, row.revision_number)
 
         if revision_number < 0:
             self.fake_ovn_client._nb_idl.get_address_set.return_value = None