fix Databroker deprecated warnings
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / network / RequestProcessor.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 package org.opendaylight.transportpce.common.network;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Optional;
13 import java.util.concurrent.locks.ReentrantReadWriteLock;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.ReadTransaction;
17 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.mdsal.common.api.CommitInfo;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class RequestProcessor {
26
27     private static final Logger LOG = LoggerFactory.getLogger(RequestProcessor.class);
28
29     private final DataBroker dataBroker;
30     private ReadWriteTransaction rwTx;
31     private ReadTransaction readTx;
32     private ReentrantReadWriteLock lock;
33
34
35
36     public RequestProcessor(DataBroker dataBroker) {
37         this.dataBroker = dataBroker;
38         rwTx = dataBroker.newReadWriteTransaction();
39         readTx = dataBroker.newReadOnlyTransaction();
40         lock = new ReentrantReadWriteLock();
41         LOG.info("RequestProcessor instantiated");
42
43     }
44
45     public <T extends DataObject> ListenableFuture<Optional<T>>
46          read(LogicalDatastoreType store,InstanceIdentifier<T> path) {
47
48         ListenableFuture<Optional<T>> result = null;
49         acquireReadLock();
50         LOG.debug("Number of threads in queue to read {}", lock.getQueueLength());
51         result = rwTx.read(store, path);
52
53         releaseReadLock();
54         return result;
55     }
56
57     public <T extends DataObject> void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
58
59         acquireLock();
60         LOG.info("Number of delete requests waiting in queue :{}", lock.getQueueLength());
61         rwTx.delete(store, path);
62     }
63
64     public <T extends DataObject> void put(LogicalDatastoreType store,
65         InstanceIdentifier<T> path, T data, boolean createMissingParents) {
66
67         acquireLock();
68         LOG.debug("Number of put requests waiting in queue :{}", lock.getQueueLength());
69         rwTx.put(store, path, data, createMissingParents);
70     }
71
72     public <T extends DataObject> void put(LogicalDatastoreType store,
73         InstanceIdentifier<T> path, T data) {
74
75         acquireLock();
76         LOG.debug("Number of put requests waiting in queue :{}", lock.getQueueLength());
77         rwTx.put(store, path, data);
78     }
79
80
81     public <T extends DataObject> void merge(LogicalDatastoreType store,
82         InstanceIdentifier<T> path, T data, boolean createMissingParents) {
83
84         acquireLock();
85         LOG.debug("Number of merge requests waiting in queue :{}", lock.getQueueLength());
86         rwTx.merge(store, path, data, createMissingParents);
87     }
88
89     public <T extends DataObject> void merge(LogicalDatastoreType store,
90         InstanceIdentifier<T> path, T data) {
91
92         acquireLock();
93         LOG.debug("Number of merge requests waiting in queue :{}", lock.getQueueLength());
94         rwTx.merge(store, path, data);
95     }
96
97     public FluentFuture<? extends @NonNull CommitInfo> commit() {
98         acquireLock();
99         FluentFuture<? extends @NonNull CommitInfo> future = null;
100         future = rwTx.commit();
101         releaseLock();
102         resetRwTx();
103         return future;
104     }
105
106     public void close() {
107         releaseLock();
108     }
109
110     private void acquireLock() {
111         if (!lock.writeLock().isHeldByCurrentThread()) {
112             lock.writeLock().lock();
113             LOG.debug("Number of write lock requests waiting in queue :{}", lock.getQueueLength());
114             LOG.info("Write Lock acquired by : {}", Thread.currentThread().getName());
115             rwTx = resetRwTx();
116         } else {
117             LOG.debug("Lock already acquired by : {}", Thread.currentThread().getName());
118         }
119     }
120
121     private void acquireReadLock() {
122         if (lock.getReadHoldCount() > 0) {
123             LOG.info("Read Lock already acquired by : {}", Thread.currentThread().getName());
124         } else {
125             lock.readLock().lock();
126             rwTx = resetRwTx();
127             LOG.info("Read Lock acquired by : {}", Thread.currentThread().getName());
128         }
129     }
130
131     private void releaseLock() {
132         if (lock.writeLock().isHeldByCurrentThread()) {
133             LOG.info("Write Lock released by : {}", Thread.currentThread().getName());
134             lock.writeLock().unlock();
135         }
136     }
137
138     private void releaseReadLock() {
139         LOG.info("Read Lock released by : {}", Thread.currentThread().getName());
140         lock.readLock().unlock();
141     }
142
143     private ReadWriteTransaction resetRwTx() {
144         LOG.info("Resetting the read write transaction .....");
145         rwTx = dataBroker.newReadWriteTransaction();
146         return rwTx;
147     }
148 }