2 * Copyright © 2017 Orange, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.transportpce.common.device;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.ScheduledExecutorService;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.atomic.AtomicBoolean;
21 import javax.annotation.Nullable;
23 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Represents read-write transaction on netconf device.
32 * This transaction can be obtained by {@link DeviceTransactionManager}.
35 * WARNING: Only one transaction can be opened at the same time on device!
36 * It's important to close (cancel/submit) transaction when work is done with it
37 * (so others can access the device).
40 public class DeviceTransaction {
42 private static final Logger LOG = LoggerFactory.getLogger(DeviceTransaction.class);
44 private final ReadWriteTransaction rwTx;
45 private final CountDownLatch deviceLock;
46 private final ScheduledExecutorService scheduledExecutorService;
47 private final AtomicBoolean wasSubmittedOrCancelled = new AtomicBoolean(false);
49 DeviceTransaction(ReadWriteTransaction rwTx, CountDownLatch deviceLock) {
51 this.deviceLock = deviceLock;
52 this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
53 LOG.debug("Device transaction created. Lock: {}", deviceLock);
56 public <T extends DataObject> ListenableFuture<Optional<T>> read(LogicalDatastoreType store,
57 InstanceIdentifier<T> path) {
58 return rwTx.read(store, path);
61 public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
62 rwTx.put(store, path, data);
65 public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path, T data,
66 boolean createMissingParents) {
67 rwTx.put(store, path, data, createMissingParents);
70 public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
71 rwTx.merge(store, path, data);
74 public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data,
75 boolean createMissingParents) {
76 rwTx.merge(store, path, data, createMissingParents);
79 public void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
80 rwTx.delete(store, path);
84 * Cancels transaction and unlocks it.
85 * @return true if cancel was successful.
87 public boolean cancel() {
88 if (wasSubmittedOrCancelled.get()) {
89 LOG.warn("Transaction was already submitted or canceled!");
93 LOG.debug("Transaction cancelled. Lock: {}", deviceLock);
94 wasSubmittedOrCancelled.set(true);
100 * Submits data changed in transaction to device with defined timeout to submit. If time from timeout runs out then
101 * submit will be interrupted and device will be unlocked.
103 * @param timeout a timeout
104 * @param timeUnit a time unit
105 * @return ListenableFuture which indicates when is submit completed.
107 public ListenableFuture<Void> submit(long timeout, TimeUnit timeUnit) {
108 if (wasSubmittedOrCancelled.get()) {
109 String msg = "Transaction was already submitted or canceled!";
111 return Futures.immediateFailedFuture(new IllegalStateException(msg));
114 LOG.debug("Transaction submitted. Lock: {}", deviceLock);
115 wasSubmittedOrCancelled.set(true);
116 ListenableFuture<Void> future =
117 Futures.withTimeout(rwTx.submit(), timeout, timeUnit, scheduledExecutorService);
119 Futures.addCallback(future, new FutureCallback<Void>() {
121 public void onSuccess(@Nullable Void result) {
122 LOG.debug("Transaction with lock {} successfully submitted.", deviceLock);
127 public void onFailure(Throwable throwable) {
128 LOG.error("Device transaction submit failed or submit took longer than {} {}!"
129 + " Unlocking device.", timeout, timeUnit, throwable);
132 }, scheduledExecutorService);
137 * Returns state of transaction.
138 * @return true if transaction was closed; otherwise false
140 public AtomicBoolean wasSubmittedOrCancelled() {
141 return wasSubmittedOrCancelled;
144 private void afterClose() {
145 scheduledExecutorService.shutdown();
146 deviceLock.countDown();