197cd9fa83c00b569b25d59d982328774fb44436
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / NoOpTransactionContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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.controller.cluster.datastore;
9
10 import akka.actor.ActorSelection;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.util.concurrent.Semaphore;
14 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
15 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import scala.concurrent.Future;
21
22 final class NoOpTransactionContext extends AbstractTransactionContext {
23     private static final Logger LOG = LoggerFactory.getLogger(NoOpTransactionContext.class);
24
25     private final Throwable failure;
26     private final Semaphore operationLimiter;
27
28     public NoOpTransactionContext(Throwable failure, TransactionIdentifier identifier, Semaphore operationLimiter) {
29         super(identifier);
30         this.failure = failure;
31         this.operationLimiter = operationLimiter;
32     }
33
34     @Override
35     public void closeTransaction() {
36         LOG.debug("NoOpTransactionContext {} closeTransaction called", getIdentifier());
37     }
38
39     @Override
40     public boolean supportsDirectCommit() {
41         return true;
42     }
43
44     @Override
45     public Future<Object> directCommit() {
46         LOG.debug("Tx {} directCommit called, failure: {}", getIdentifier(), failure);
47         operationLimiter.release();
48         return akka.dispatch.Futures.failed(failure);
49     }
50
51     @Override
52     public Future<ActorSelection> readyTransaction() {
53         LOG.debug("Tx {} readyTransaction called, failure: {}", getIdentifier(), failure);
54         operationLimiter.release();
55         return akka.dispatch.Futures.failed(failure);
56     }
57
58     @Override
59     public void deleteData(YangInstanceIdentifier path) {
60         LOG.debug("Tx {} deleteData called path = {}", getIdentifier(), path);
61         operationLimiter.release();
62     }
63
64     @Override
65     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
66         LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
67         operationLimiter.release();
68     }
69
70     @Override
71     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
72         LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
73         operationLimiter.release();
74     }
75
76     @Override
77     public void readData(final YangInstanceIdentifier path, SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
78         LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
79         operationLimiter.release();
80         proxyFuture.setException(new ReadFailedException("Error reading data for path " + path, failure));
81     }
82
83     @Override
84     public void dataExists(YangInstanceIdentifier path, SettableFuture<Boolean> proxyFuture) {
85         LOG.debug("Tx {} dataExists called path = {}", getIdentifier(), path);
86         operationLimiter.release();
87         proxyFuture.setException(new ReadFailedException("Error checking exists for path " + path, failure));
88     }
89 }