From: guillaume.lambert Date: Thu, 22 Aug 2019 08:54:53 +0000 (+0200) Subject: implements DeviceTransaction.commit() X-Git-Tag: 0.4.0~14 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F82%2F83882%2F2;p=transportpce.git implements DeviceTransaction.commit() instead of submit which is now deprecated Co-authored-by: Martial COULIBALY Signed-off-by: guillaume.lambert Change-Id: I7b37bcf123c8d548660076b441d670feaa233150 --- diff --git a/common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransaction.java b/common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransaction.java index 5aa093fd8..3ee2964ce 100644 --- a/common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransaction.java +++ b/common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransaction.java @@ -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 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 future = + rwTx.commit().withTimeout(timeout, timeUnit, scheduledExecutorService); + + future.addCallback(new FutureCallback() { + @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