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.ListenableFuture;
12 import java.util.concurrent.locks.ReentrantReadWriteLock;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class RequestProcessor {
24 private static final Logger LOG = LoggerFactory.getLogger(RequestProcessor.class);
26 private final DataBroker dataBroker;
27 private ReadWriteTransaction rwTx;
28 private ReadOnlyTransaction readTx;
29 private ReentrantReadWriteLock lock;
33 public RequestProcessor(DataBroker dataBroker) {
34 this.dataBroker = dataBroker;
35 rwTx = dataBroker.newReadWriteTransaction();
36 readTx = dataBroker.newReadOnlyTransaction();
37 lock = new ReentrantReadWriteLock();
38 LOG.info("RequestProcessor instantiated");
42 public <T extends DataObject> ListenableFuture<Optional<T>>
43 read(LogicalDatastoreType store,InstanceIdentifier<T> path) {
45 ListenableFuture<Optional<T>> result = null;
47 LOG.debug("Number of threads in queue to read {}", lock.getQueueLength());
48 result = rwTx.read(store, path);
54 public <T extends DataObject> void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
57 LOG.info("Number of delete requests waiting in queue :{}", lock.getQueueLength());
58 rwTx.delete(store, path);
61 public <T extends DataObject> void put(LogicalDatastoreType store,
62 InstanceIdentifier<T> path, T data, boolean createMissingParents) {
65 LOG.debug("Number of put requests waiting in queue :{}", lock.getQueueLength());
66 rwTx.put(store, path, data, createMissingParents);
69 public <T extends DataObject> void put(LogicalDatastoreType store,
70 InstanceIdentifier<T> path, T data) {
73 LOG.debug("Number of put requests waiting in queue :{}", lock.getQueueLength());
74 rwTx.put(store, path, data);
78 public <T extends DataObject> void merge(LogicalDatastoreType store,
79 InstanceIdentifier<T> path, T data, boolean createMissingParents) {
82 LOG.debug("Number of merge requests waiting in queue :{}", lock.getQueueLength());
83 rwTx.merge(store, path, data, createMissingParents);
86 public <T extends DataObject> void merge(LogicalDatastoreType store,
87 InstanceIdentifier<T> path, T data) {
90 LOG.debug("Number of merge requests waiting in queue :{}", lock.getQueueLength());
91 rwTx.merge(store, path, data);
95 public ListenableFuture<Void> submit() {
97 ListenableFuture<Void> future = null;
98 future = rwTx.submit();
104 public void close() {
108 private void acquireLock() {
109 if (!lock.writeLock().isHeldByCurrentThread()) {
110 lock.writeLock().lock();
111 LOG.debug("Number of write lock requests waiting in queue :{}", lock.getQueueLength());
112 LOG.info("Write Lock acquired by : {}", Thread.currentThread().getName());
115 LOG.debug("Lock already acquired by : {}", Thread.currentThread().getName());
119 private void acquireReadLock() {
120 if (lock.getReadHoldCount() > 0) {
121 LOG.info("Read Lock already acquired by : {}", Thread.currentThread().getName());
123 lock.readLock().lock();
125 LOG.info("Read Lock acquired by : {}", Thread.currentThread().getName());
129 private void releaseLock() {
130 if (lock.writeLock().isHeldByCurrentThread()) {
131 LOG.info("Write Lock released by : {}", Thread.currentThread().getName());
132 lock.writeLock().unlock();
136 private void releaseReadLock() {
137 LOG.info("Read Lock released by : {}", Thread.currentThread().getName());
138 lock.readLock().unlock();
141 private ReadWriteTransaction resetRwTx() {
142 LOG.info("Resetting the read write transaction .....");
143 rwTx = dataBroker.newReadWriteTransaction();