450716800066a291d00e2b22b71a4c6306dfadaf
[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
15 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
16 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
17 import org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.Future;
24
25 final class NoOpTransactionContext extends AbstractTransactionContext {
26     private static final Logger LOG = LoggerFactory.getLogger(NoOpTransactionContext.class);
27
28     private final Throwable failure;
29     private final Semaphore operationLimiter;
30
31     public NoOpTransactionContext(Throwable failure, TransactionIdentifier identifier, Semaphore operationLimiter) {
32         super(identifier);
33         this.failure = failure;
34         this.operationLimiter = operationLimiter;
35     }
36
37     @Override
38     public void closeTransaction() {
39         LOG.debug("NoOpTransactionContext {} closeTransaction called", getIdentifier());
40     }
41
42     @Override
43     public boolean supportsDirectCommit() {
44         return true;
45     }
46
47     @Override
48     public Future<Object> directCommit() {
49         LOG.debug("Tx {} directCommit called, failure: {}", getIdentifier(), failure);
50         operationLimiter.release();
51         return akka.dispatch.Futures.failed(failure);
52     }
53
54     @Override
55     public Future<ActorSelection> readyTransaction() {
56         LOG.debug("Tx {} readyTransaction called, failure: {}", getIdentifier(), failure);
57         operationLimiter.release();
58         return akka.dispatch.Futures.failed(failure);
59     }
60
61     @Override
62     public void deleteData(YangInstanceIdentifier path) {
63         LOG.debug("Tx {} deleteData called path = {}", getIdentifier(), path);
64         operationLimiter.release();
65     }
66
67     @Override
68     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
69         LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
70         operationLimiter.release();
71     }
72
73     @Override
74     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
75         LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
76         operationLimiter.release();
77     }
78
79     @Override
80     public void readData(final YangInstanceIdentifier path, SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
81         LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
82         operationLimiter.release();
83         Throwable t;
84         if(failure instanceof NoShardLeaderException) {
85             t = new DataStoreUnavailableException(failure.getMessage(), failure);
86         } else {
87             t = failure;
88         }
89         proxyFuture.setException(new ReadFailedException("Error reading data for path " + path, t));
90     }
91
92     @Override
93     public void dataExists(YangInstanceIdentifier path, SettableFuture<Boolean> proxyFuture) {
94         LOG.debug("Tx {} dataExists called path = {}", getIdentifier(), path);
95         operationLimiter.release();
96         proxyFuture.setException(new ReadFailedException("Error checking exists for path " + path, failure));
97     }
98 }