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
8 package org.opendaylight.transportpce.common.network;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.locks.ReentrantReadWriteLock;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
30 public class RequestProcessor {
32 private static final Logger LOG = LoggerFactory.getLogger(RequestProcessor.class);
34 private final DataBroker dataBroker;
35 private ReadWriteTransaction rwTx;
36 private ReadOnlyTransaction readTx;
37 private ReentrantReadWriteLock lock;
41 public RequestProcessor(DataBroker dataBroker) {
42 this.dataBroker = dataBroker;
43 rwTx = dataBroker.newReadWriteTransaction();
44 readTx = dataBroker.newReadOnlyTransaction();
45 lock = new ReentrantReadWriteLock();
46 LOG.info("RequestProcessor instantiated");
50 public <T extends DataObject> CheckedFuture<Optional<T>,
51 ReadFailedException> read(LogicalDatastoreType store,InstanceIdentifier<T> path) {
53 CheckedFuture<Optional<T>, ReadFailedException> result = null;
55 LOG.debug("Number of threads in queue to read " + lock.getQueueLength());
56 result = rwTx.read(store, path);
62 public <T extends DataObject> void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
65 LOG.info("Number of delete requests waiting in queue :" + lock.getQueueLength());
66 rwTx.delete(store, path);
69 public <T extends DataObject> void put(LogicalDatastoreType store,
70 InstanceIdentifier<T> path, T data, boolean createMissingParents) {
73 LOG.debug("Number of put requests waiting in queue :" + lock.getQueueLength());
74 rwTx.put(store, path, data, createMissingParents);
77 public <T extends DataObject> void put(LogicalDatastoreType store,
78 InstanceIdentifier<T> path, T data) {
81 LOG.debug("Number of put requests waiting in queue :" + lock.getQueueLength());
82 rwTx.put(store, path, data);
86 public <T extends DataObject> void merge(LogicalDatastoreType store,
87 InstanceIdentifier<T> path, T data, boolean createMissingParents) {
90 LOG.debug("Number of merge requests waiting in queue :" + lock.getQueueLength());
91 rwTx.merge(store, path, data, createMissingParents);
94 public <T extends DataObject> void merge(LogicalDatastoreType store,
95 InstanceIdentifier<T> path, T data) {
98 LOG.debug("Number of merge requests waiting in queue :" + lock.getQueueLength());
99 rwTx.merge(store, path, data);
102 public ListenableFuture<Void> submit() {
104 ListenableFuture<Void> future = null;
105 future = rwTx.submit();
111 public void close() {
115 private void acquireLock() {
116 if (!lock.writeLock().isHeldByCurrentThread()) {
117 lock.writeLock().lock();
118 LOG.debug("Number of write lock requests waiting in queue :" + lock.getQueueLength());
119 LOG.info("Write Lock acquired by : " + Thread.currentThread().getName());
122 LOG.debug("Lock already acquired by : " + Thread.currentThread().getName());
126 private void acquireReadLock() {
127 if (lock.getReadHoldCount() > 0) {
128 LOG.info("Read Lock already acquired by : " + Thread.currentThread().getName());
130 lock.readLock().lock();
132 LOG.info("Read Lock acquired by : " + Thread.currentThread().getName());
136 private void releaseLock() {
137 if (lock.writeLock().isHeldByCurrentThread()) {
138 LOG.info("Write Lock released by : " + Thread.currentThread().getName());
139 lock.writeLock().unlock();
143 private void releaseReadLock() {
144 LOG.info("Read Lock released by : " + Thread.currentThread().getName());
145 lock.readLock().unlock();
148 private ReadWriteTransaction resetRwTx() {
149 LOG.info("Resetting the read write transaction .....");
150 rwTx = dataBroker.newReadWriteTransaction();