Bug 5129 : Added more locks to avoid concurrent DS modification conflicts
[vpnservice.git] / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / vpnservice / neutronvpn / NeutronvpnUtils.java
index a74a34b62a65303df327f79c57c79bd82a3b2635..71d11541a41a2a365677812be30b170535992a0f 100644 (file)
@@ -235,35 +235,38 @@ public class NeutronvpnUtils {
         return new StringBuilder().append("tap").append(tapId).toString();
     }
 
-    protected static void lockVpnInterface(LockManagerService lockManager, String vpnInterfaceName) {
-        TryLockInput input = new TryLockInputBuilder().setLockName(vpnInterfaceName).setTime(5L).setTimeUnit
+    protected static boolean lock(LockManagerService lockManager, String lockName) {
+        TryLockInput input = new TryLockInputBuilder().setLockName(lockName).setTime(5L).setTimeUnit
                 (TimeUnits.Milliseconds).build();
-        Future<RpcResult<Void>> result = lockManager.tryLock(input);
+        boolean islockAquired = false;
         try {
+            Future<RpcResult<Void>> result = lockManager.tryLock(input);
             if ((result != null) && (result.get().isSuccessful())) {
-                logger.debug("Acquired lock for vpninterface {}", vpnInterfaceName);
+                logger.debug("Acquired lock for {}", lockName);
+                islockAquired = true;
             } else {
-                throw new RuntimeException(String.format("Unable to getLock for vpninterface %s", vpnInterfaceName));
+                throw new RuntimeException(String.format("Unable to acquire lock for  %s", lockName));
             }
         } catch (InterruptedException | ExecutionException e) {
-            logger.error("Unable to getLock for vpninterface {}", vpnInterfaceName);
-            throw new RuntimeException(String.format("Unable to getLock for vpninterface %s", vpnInterfaceName), e
+            logger.error("Unable to acquire lock for  {}", lockName);
+            throw new RuntimeException(String.format("Unable to acquire lock for %s", lockName), e
                     .getCause());
         }
+        return islockAquired;
     }
 
-    protected static void unlockVpnInterface(LockManagerService lockManager, String vpnInterfaceName) {
-        UnlockInput input = new UnlockInputBuilder().setLockName(vpnInterfaceName).build();
-        Future<RpcResult<Void>> result = lockManager.unlock(input);
+    protected static void unlock(LockManagerService lockManager, String lockName) {
+        UnlockInput input = new UnlockInputBuilder().setLockName(lockName).build();
         try {
+            Future<RpcResult<Void>> result = lockManager.unlock(input);
             if ((result != null) && (result.get().isSuccessful())) {
-                logger.debug("Unlocked vpninterface{}", vpnInterfaceName);
+                logger.debug("Unlocked {}", lockName);
             } else {
-                logger.debug("Unable to unlock vpninterface {}", vpnInterfaceName);
+                logger.debug("Unable to unlock {}", lockName);
             }
         } catch (InterruptedException | ExecutionException e) {
-            logger.error("Unable to unlock vpninterface {}", vpnInterfaceName);
-            throw new RuntimeException(String.format("Unable to unlock vpninterface %s", vpnInterfaceName), e
+            logger.error("Unable to unlock {}", lockName);
+            throw new RuntimeException(String.format("Unable to unlock %s", lockName), e
                     .getCause());
         }
     }