3ee2964ce641e8398aaf9ca122bf5c138864402c
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / device / DeviceTransaction.java
1 /*
2  * Copyright © 2017 Orange, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.transportpce.common.device;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 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;
22 import org.eclipse.jdt.annotation.NonNull;
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.mdsal.common.api.CommitInfo;
26 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Represents read-write transaction on netconf device.
34  * This transaction can be obtained by {@link DeviceTransactionManager}.
35  *
36  * <p>
37  * WARNING: Only one transaction can be opened at the same time on device!
38  * It's important to close (cancel/submit) transaction when work is done with it
39  * (so others can access the device).
40  * </p>
41  */
42 public class DeviceTransaction {
43
44     private static final Logger LOG = LoggerFactory.getLogger(DeviceTransaction.class);
45
46     private final ReadWriteTransaction rwTx;
47     private final CountDownLatch deviceLock;
48     private final ScheduledExecutorService scheduledExecutorService;
49     private final AtomicBoolean wasSubmittedOrCancelled = new AtomicBoolean(false);
50
51     DeviceTransaction(ReadWriteTransaction rwTx, CountDownLatch deviceLock) {
52         this.rwTx = rwTx;
53         this.deviceLock = deviceLock;
54         this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
55         LOG.debug("Device transaction created. Lock: {}", deviceLock);
56     }
57
58     public <T extends DataObject> ListenableFuture<Optional<T>> read(LogicalDatastoreType store,
59             InstanceIdentifier<T> path) {
60         return rwTx.read(store, path);
61     }
62
63     public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
64         rwTx.put(store, path, data);
65     }
66
67     public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path, T data,
68             boolean createMissingParents) {
69         rwTx.put(store, path, data, createMissingParents);
70     }
71
72     public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
73         rwTx.merge(store, path, data);
74     }
75
76     public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data,
77             boolean createMissingParents) {
78         rwTx.merge(store, path, data, createMissingParents);
79     }
80
81     public void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
82         rwTx.delete(store, path);
83     }
84
85     /**
86      * Cancels transaction and unlocks it.
87      * @return true if cancel was successful.
88      */
89     public boolean cancel() {
90         if (wasSubmittedOrCancelled.get()) {
91             LOG.warn("Transaction was already submitted or canceled!");
92             return false;
93         }
94
95         LOG.debug("Transaction cancelled. Lock: {}", deviceLock);
96         wasSubmittedOrCancelled.set(true);
97         afterClose();
98         return rwTx.cancel();
99     }
100
101     /**
102      * Submits data changed in transaction to device with defined timeout to submit. If time from timeout runs out then
103      * submit will be interrupted and device will be unlocked.
104      *
105      * @param timeout a timeout
106      * @param timeUnit a time unit
107      * @return ListenableFuture which indicates when is submit completed.
108      */
109     @Deprecated
110     public ListenableFuture<Void> submit(long timeout, TimeUnit timeUnit) {
111         if (wasSubmittedOrCancelled.get()) {
112             String msg = "Transaction was already submitted or canceled!";
113             LOG.error(msg);
114             return Futures.immediateFailedFuture(new IllegalStateException(msg));
115         }
116
117         LOG.debug("Transaction submitted. Lock: {}", deviceLock);
118         wasSubmittedOrCancelled.set(true);
119         ListenableFuture<Void> future =
120                 Futures.withTimeout(rwTx.submit(), timeout, timeUnit, scheduledExecutorService);
121
122         Futures.addCallback(future, new FutureCallback<Void>() {
123             @Override
124             public void onSuccess(@Nullable Void result) {
125                 LOG.debug("Transaction with lock {} successfully submitted.", deviceLock);
126                 afterClose();
127             }
128
129             @Override
130             public void onFailure(Throwable throwable) {
131                 LOG.error("Device transaction submit failed or submit took longer than {} {}! Unlocking device.",
132                     timeout, timeUnit, throwable);
133                 afterClose();
134             }
135         }, scheduledExecutorService);
136         return future;
137     }
138
139     public FluentFuture<? extends @NonNull CommitInfo> commit(long timeout, TimeUnit timeUnit) {
140         if (wasSubmittedOrCancelled.get()) {
141             String msg = "Transaction was already submitted or canceled!";
142             LOG.error(msg);
143             return FluentFutures.immediateFailedFluentFuture(new IllegalStateException(msg));
144         }
145
146         LOG.debug("Transaction committed. Lock: {}", deviceLock);
147         wasSubmittedOrCancelled.set(true);
148         FluentFuture<? extends @NonNull CommitInfo> future =
149                 rwTx.commit().withTimeout(timeout, timeUnit, scheduledExecutorService);
150
151         future.addCallback(new FutureCallback<CommitInfo>() {
152             @Override
153             public void onSuccess(@Nullable CommitInfo result) {
154                 LOG.debug("Transaction with lock {} successfully committed:", deviceLock, result);
155                 afterClose();
156             }
157
158             @Override
159             public void onFailure(Throwable throwable) {
160                 LOG.error("Device transaction commit failed or submit took longer than {} {}! Unlocking device.",
161                     timeout, timeUnit, throwable);
162                 afterClose();
163             }
164         }, scheduledExecutorService);
165         return future;
166     }
167
168     /**
169      * Returns state of transaction.
170      * @return true if transaction was closed; otherwise false
171      */
172     public AtomicBoolean wasSubmittedOrCancelled() {
173         return wasSubmittedOrCancelled;
174     }
175
176     private void afterClose() {
177         scheduledExecutorService.shutdown();
178         deviceLock.countDown();
179     }
180 }