implements DeviceTransaction.commit() 82/83882/2
authorguillaume.lambert <guillaume.lambert@orange.com>
Thu, 22 Aug 2019 08:54:53 +0000 (10:54 +0200)
committerguillaume.lambert <guillaume.lambert@orange.com>
Thu, 22 Aug 2019 10:26:34 +0000 (12:26 +0200)
instead of submit which is now deprecated

Co-authored-by: Martial COULIBALY <martial.coulibaly@gfi.fr>
Signed-off-by: guillaume.lambert <guillaume.lambert@orange.com>
Change-Id: I7b37bcf123c8d548660076b441d670feaa233150

common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransaction.java

index 5aa093fd89b71420f5f0faeb7a21e7b2c29ba385..3ee2964ce641e8398aaf9ca122bf5c138864402c 100644 (file)
@@ -9,19 +9,21 @@
 package org.opendaylight.transportpce.common.device;
 
 import com.google.common.base.Optional;
+import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
-
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import javax.annotation.Nullable;
-
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -134,6 +136,35 @@ public class DeviceTransaction {
         return future;
     }
 
+    public FluentFuture<? extends @NonNull CommitInfo> commit(long timeout, TimeUnit timeUnit) {
+        if (wasSubmittedOrCancelled.get()) {
+            String msg = "Transaction was already submitted or canceled!";
+            LOG.error(msg);
+            return FluentFutures.immediateFailedFluentFuture(new IllegalStateException(msg));
+        }
+
+        LOG.debug("Transaction committed. Lock: {}", deviceLock);
+        wasSubmittedOrCancelled.set(true);
+        FluentFuture<? extends @NonNull CommitInfo> future =
+                rwTx.commit().withTimeout(timeout, timeUnit, scheduledExecutorService);
+
+        future.addCallback(new FutureCallback<CommitInfo>() {
+            @Override
+            public void onSuccess(@Nullable CommitInfo result) {
+                LOG.debug("Transaction with lock {} successfully committed:", deviceLock, result);
+                afterClose();
+            }
+
+            @Override
+            public void onFailure(Throwable throwable) {
+                LOG.error("Device transaction commit failed or submit took longer than {} {}! Unlocking device.",
+                    timeout, timeUnit, throwable);
+                afterClose();
+            }
+        }, scheduledExecutorService);
+        return future;
+    }
+
     /**
      * Returns state of transaction.
      * @return true if transaction was closed; otherwise false