Remove common module deprecated methods
[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
65     public <T extends DataObject> void put(LogicalDatastoreType store,
66         InstanceIdentifier<T> path, T data) {
67
68         acquireLock();
69         LOG.debug("Number of put requests waiting in queue :{}", lock.getQueueLength());
70         rwTx.put(store, path, data);
71     }
72
73
74     public <T extends DataObject> void merge(LogicalDatastoreType store,
75         InstanceIdentifier<T> path, T data) {
76
77         acquireLock();
78         LOG.debug("Number of merge requests waiting in queue :{}", lock.getQueueLength());
79         rwTx.merge(store, path, data);
80     }
81
82     public FluentFuture<? extends @NonNull CommitInfo> commit() {
83         acquireLock();
84         FluentFuture<? extends @NonNull CommitInfo> future = null;
85         future = rwTx.commit();
86         releaseLock();
87         resetRwTx();
88         return future;
89     }
90
91     public void close() {
92         releaseLock();
93     }
94
95     private void acquireLock() {
96         if (!lock.writeLock().isHeldByCurrentThread()) {
97             lock.writeLock().lock();
98             LOG.debug("Number of write lock requests waiting in queue :{}", lock.getQueueLength());
99             LOG.info("Write Lock acquired by : {}", Thread.currentThread().getName());
100             rwTx = resetRwTx();
101         } else {
102             LOG.debug("Lock already acquired by : {}", Thread.currentThread().getName());
103         }
104     }
105
106     private void acquireReadLock() {
107         if (lock.getReadHoldCount() > 0) {
108             LOG.info("Read Lock already acquired by : {}", Thread.currentThread().getName());
109         } else {
110             lock.readLock().lock();
111             rwTx = resetRwTx();
112             LOG.info("Read Lock acquired by : {}", Thread.currentThread().getName());
113         }
114     }
115
116     private void releaseLock() {
117         if (lock.writeLock().isHeldByCurrentThread()) {
118             LOG.info("Write Lock released by : {}", Thread.currentThread().getName());
119             lock.writeLock().unlock();
120         }
121     }
122
123     private void releaseReadLock() {
124         LOG.info("Read Lock released by : {}", Thread.currentThread().getName());
125         lock.readLock().unlock();
126     }
127
128     private ReadWriteTransaction resetRwTx() {
129         LOG.info("Resetting the read write transaction .....");
130         rwTx = dataBroker.newReadWriteTransaction();
131         return rwTx;
132     }
133 }