vpnmanager: use datastore-constrained transactions
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / VpnFootprintService.java
index e9452abbc3a0b270f3bf4ac95ac9caab69f515d2..34e26c20074c1f4436e9aa3024ee842ac77a43b6 100644 (file)
@@ -8,6 +8,9 @@
 
 package org.opendaylight.netvirt.vpnmanager;
 
+import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
+import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
+
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
@@ -25,8 +28,6 @@ import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
@@ -125,8 +126,6 @@ public class VpnFootprintService implements IVpnFootprintService {
         }
     }
 
-    // Allow deprecated TransactionRunner calls for now
-    @SuppressWarnings("ForbidCertainMethod")
     private void createOrUpdateVpnToDpnListForInterfaceName(Uint32 vpnId, String primaryRd, Uint64 dpnId,
             String intfName, String vpnName) {
         AtomicBoolean newDpnOnVpn = new AtomicBoolean(false);
@@ -137,10 +136,10 @@ public class VpnFootprintService implements IVpnFootprintService {
         final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnName);
         lock.lock();
         try {
-            ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+            ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
                 InstanceIdentifier<VpnToDpnList> id = VpnHelper.getVpnToDpnListIdentifier(primaryRd, dpnId);
                 VpnInterfaces vpnInterface = new VpnInterfacesBuilder().setInterfaceName(intfName).build();
-                Optional<VpnToDpnList> dpnInVpn = tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet();
+                Optional<VpnToDpnList> dpnInVpn = tx.read(id).get();
                 if (dpnInVpn.isPresent()) {
                     VpnToDpnList vpnToDpnList = dpnInVpn.get();
                     List<VpnInterfaces> vpnInterfaces = new ArrayList<>(vpnToDpnList.nonnullVpnInterfaces());
@@ -148,8 +147,7 @@ public class VpnFootprintService implements IVpnFootprintService {
                     VpnToDpnListBuilder vpnToDpnListBuilder = new VpnToDpnListBuilder(vpnToDpnList);
                     vpnToDpnListBuilder.setDpnState(VpnToDpnList.DpnState.Active).setVpnInterfaces(vpnInterfaces);
 
-                    tx.put(LogicalDatastoreType.OPERATIONAL, id, vpnToDpnListBuilder.build(),
-                        WriteTransaction.CREATE_MISSING_PARENTS);
+                    tx.put(id, vpnToDpnListBuilder.build(), CREATE_MISSING_PARENTS);
                     /*
                      * If earlier state was inactive, it is considered new DPN coming back to the
                      * same VPN
@@ -165,8 +163,7 @@ public class VpnFootprintService implements IVpnFootprintService {
                     VpnToDpnListBuilder vpnToDpnListBuilder = new VpnToDpnListBuilder().setDpnId(dpnId);
                     vpnToDpnListBuilder.setDpnState(VpnToDpnList.DpnState.Active).setVpnInterfaces(vpnInterfaces);
 
-                    tx.put(LogicalDatastoreType.OPERATIONAL, id, vpnToDpnListBuilder.build(),
-                        WriteTransaction.CREATE_MISSING_PARENTS);
+                    tx.put(id, vpnToDpnListBuilder.build(), CREATE_MISSING_PARENTS);
                     newDpnOnVpn.set(true);
                     LOG.debug("createOrUpdateVpnToDpnList: Creating vpn footprint for vpn {} vpnId {} interface {}"
                             + " on dpn {}", vpnName, vpnId, intfName, dpnId);
@@ -198,8 +195,6 @@ public class VpnFootprintService implements IVpnFootprintService {
         }
     }
 
-    // Allow deprecated TransactionRunner calls for now
-    @SuppressWarnings("ForbidCertainMethod")
     private void createOrUpdateVpnToDpnListForIPAddress(Uint32 vpnId, String primaryRd, Uint64 dpnId,
             ImmutablePair<IpAddresses.IpAddressSource, String> ipAddressSourceValuePair, String vpnName) {
         AtomicBoolean newDpnOnVpn = new AtomicBoolean(false);
@@ -210,13 +205,13 @@ public class VpnFootprintService implements IVpnFootprintService {
         final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnName);
         lock.lock();
         try {
-            ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+            ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
                 InstanceIdentifier<VpnToDpnList> id = VpnHelper.getVpnToDpnListIdentifier(primaryRd, dpnId);
                 IpAddressesBuilder ipAddressesBldr = new IpAddressesBuilder()
                         .setIpAddressSource(ipAddressSourceValuePair.getKey());
                 ipAddressesBldr.withKey(new IpAddressesKey(ipAddressSourceValuePair.getValue()));
                 ipAddressesBldr.setIpAddress(ipAddressSourceValuePair.getValue());
-                Optional<VpnToDpnList> dpnInVpn = tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet();
+                Optional<VpnToDpnList> dpnInVpn = tx.read(id).get();
                 if (dpnInVpn.isPresent()) {
                     VpnToDpnList vpnToDpnList = dpnInVpn.get();
                     List<IpAddresses> ipAddresses = new ArrayList<>(vpnToDpnList.nonnullIpAddresses());
@@ -224,7 +219,7 @@ public class VpnFootprintService implements IVpnFootprintService {
                     VpnToDpnListBuilder vpnToDpnListBuilder = new VpnToDpnListBuilder(vpnToDpnList);
                     vpnToDpnListBuilder.setDpnState(VpnToDpnList.DpnState.Active).setIpAddresses(ipAddresses);
 
-                    tx.put(LogicalDatastoreType.OPERATIONAL, id, vpnToDpnListBuilder.build(), true);
+                    tx.put(id, vpnToDpnListBuilder.build(), CREATE_MISSING_PARENTS);
                     /*
                      * If earlier state was inactive, it is considered new DPN coming back to the
                      * same VPN
@@ -237,7 +232,7 @@ public class VpnFootprintService implements IVpnFootprintService {
                     ipAddresses.add(ipAddressesBldr.build());
                     VpnToDpnListBuilder vpnToDpnListBuilder = new VpnToDpnListBuilder().setDpnId(dpnId);
                     vpnToDpnListBuilder.setDpnState(VpnToDpnList.DpnState.Active).setIpAddresses(ipAddresses);
-                    tx.put(LogicalDatastoreType.OPERATIONAL, id, vpnToDpnListBuilder.build(), true);
+                    tx.put(id, vpnToDpnListBuilder.build(), CREATE_MISSING_PARENTS);
                     newDpnOnVpn.set(true);
                 }
 
@@ -260,8 +255,6 @@ public class VpnFootprintService implements IVpnFootprintService {
         }
     }
 
-    // Allow deprecated TransactionRunner calls for now
-    @SuppressWarnings("ForbidCertainMethod")
     private void removeOrUpdateVpnToDpnListForInterfaceName(Uint32 vpnId, String rd, Uint64 dpnId, String intfName,
             String vpnName) {
         AtomicBoolean lastDpnOnVpn = new AtomicBoolean(false);
@@ -273,10 +266,9 @@ public class VpnFootprintService implements IVpnFootprintService {
         lock.lock();
         try {
             try {
-                ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+                ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
                     InstanceIdentifier<VpnToDpnList> id = VpnHelper.getVpnToDpnListIdentifier(rd, dpnId);
-                    Optional<VpnToDpnList> dpnInVpnOpt = tx.read(LogicalDatastoreType.OPERATIONAL, id)
-                            .checkedGet();
+                    Optional<VpnToDpnList> dpnInVpnOpt = tx.read(id).get();
                     if (!dpnInVpnOpt.isPresent()) {
                         LOG.error("removeOrUpdateVpnToDpnList: Could not find DpnToVpn map for VPN=[name={}"
                                 + " rd={} id={}] and dpnId={}", vpnName, rd, id, dpnId);
@@ -305,12 +297,10 @@ public class VpnFootprintService implements IVpnFootprintService {
                             }
                             LOG.debug("removeOrUpdateVpnToDpnList: Removing vpn footprint for vpn {} vpnId {} "
                                     + "interface {}, on dpn {}", vpnName, vpnName, intfName, dpnId);
-                            tx.put(LogicalDatastoreType.OPERATIONAL, id, dpnInVpnBuilder.build(),
-                                    WriteTransaction.CREATE_MISSING_PARENTS);
+                            tx.put(id, dpnInVpnBuilder.build(), CREATE_MISSING_PARENTS);
 
                         } else {
-                            tx.delete(LogicalDatastoreType.OPERATIONAL,
-                                    id.child(VpnInterfaces.class, new VpnInterfacesKey(intfName)));
+                            tx.delete(id.child(VpnInterfaces.class, new VpnInterfacesKey(intfName)));
                             LOG.debug("removeOrUpdateVpnToDpnList: Updating vpn footprint for vpn {} vpnId {} "
                                     + "interface {}, on dpn {}", vpnName, vpnName, intfName, dpnId);
                         }
@@ -337,8 +327,6 @@ public class VpnFootprintService implements IVpnFootprintService {
         }
     }
 
-    // Allow deprecated TransactionRunner calls for now
-    @SuppressWarnings("ForbidCertainMethod")
     private void removeOrUpdateVpnToDpnListForIpAddress(Uint32 vpnId, String rd, Uint64 dpnId,
             ImmutablePair<IpAddresses.IpAddressSource, String> ipAddressSourceValuePair, String vpnName) {
         AtomicBoolean lastDpnOnVpn = new AtomicBoolean(false);
@@ -349,11 +337,10 @@ public class VpnFootprintService implements IVpnFootprintService {
         final ReentrantLock lock = JvmGlobalLocks.getLockForString(vpnName);
         lock.lock();
         try {
-            ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+            ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
                 InstanceIdentifier<VpnToDpnList> id = VpnHelper.getVpnToDpnListIdentifier(rd, dpnId);
 
-                Optional<VpnToDpnList> dpnInVpnOpt = tx.read(LogicalDatastoreType.OPERATIONAL, id)
-                        .checkedGet();
+                Optional<VpnToDpnList> dpnInVpnOpt = tx.read(id).get();
                 if (!dpnInVpnOpt.isPresent()) {
                     LOG.error("removeOrUpdateVpnToDpnList: Could not find DpnToVpn map for VPN=[name={} "
                             + "rd={} id={}] and dpnId={}", vpnName, rd, id, dpnId);
@@ -382,11 +369,10 @@ public class VpnFootprintService implements IVpnFootprintService {
                             LOG.warn("ip addresses are empty but vpn interfaces are present for the vpn {} in "
                                     + "dpn {}", vpnName, dpnId);
                         }
-                        tx.put(LogicalDatastoreType.OPERATIONAL, id, dpnInVpnBuilder.build(), true);
+                        tx.put(id, dpnInVpnBuilder.build(), CREATE_MISSING_PARENTS);
 
                     } else {
-                        tx.delete(LogicalDatastoreType.OPERATIONAL, id.child(IpAddresses.class,
-                            new IpAddressesKey(ipAddressSourceValuePair.getValue())));
+                        tx.delete(id.child(IpAddresses.class, new IpAddressesKey(ipAddressSourceValuePair.getValue())));
                     }
                 }