Move operation limiter down to TransactionContextWrapper
[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 org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
14 import org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException;
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
27     public NoOpTransactionContext(Throwable failure, OperationLimiter limiter) {
28         super(limiter);
29         this.failure = failure;
30     }
31
32     @Override
33     public void closeTransaction() {
34         LOG.debug("NoOpTransactionContext {} closeTransaction called", getIdentifier());
35     }
36
37     @Override
38     public boolean supportsDirectCommit() {
39         return true;
40     }
41
42     @Override
43     public Future<Object> directCommit() {
44         LOG.debug("Tx {} directCommit called, failure: {}", getIdentifier(), failure);
45         releaseOperation();
46         return akka.dispatch.Futures.failed(failure);
47     }
48
49     @Override
50     public Future<ActorSelection> readyTransaction() {
51         LOG.debug("Tx {} readyTransaction called, failure: {}", getIdentifier(), failure);
52         releaseOperation();
53         return akka.dispatch.Futures.failed(failure);
54     }
55
56     @Override
57     public void deleteData(YangInstanceIdentifier path) {
58         LOG.debug("Tx {} deleteData called path = {}", getIdentifier(), path);
59         releaseOperation();
60     }
61
62     @Override
63     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
64         LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
65         releaseOperation();
66     }
67
68     @Override
69     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
70         LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
71         releaseOperation();
72     }
73
74     @Override
75     public void readData(final YangInstanceIdentifier path, SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
76         LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
77         releaseOperation();
78
79         final Throwable t;
80         if (failure instanceof NoShardLeaderException) {
81             t = new DataStoreUnavailableException(failure.getMessage(), failure);
82         } else {
83             t = failure;
84         }
85         proxyFuture.setException(new ReadFailedException("Error reading data for path " + path, t));
86     }
87
88     @Override
89     public void dataExists(YangInstanceIdentifier path, SettableFuture<Boolean> proxyFuture) {
90         LOG.debug("Tx {} dataExists called path = {}", getIdentifier(), path);
91         releaseOperation();
92         proxyFuture.setException(new ReadFailedException("Error checking exists for path " + path, failure));
93     }
94 }