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