fa9d97a1fddbed8bb7d5100a8c6ddb9779cf0365
[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.util.concurrent.SettableFuture;
12 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
13 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
15 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
16 import org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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, TransactionIdentifier identifier) {
28         super(identifier);
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         return akka.dispatch.Futures.failed(failure);
46     }
47
48     @Override
49     public Future<ActorSelection> readyTransaction() {
50         LOG.debug("Tx {} readyTransaction called, failure: {}", getIdentifier(), failure);
51         return akka.dispatch.Futures.failed(failure);
52     }
53
54     @Override
55     public void executeModification(AbstractModification modification) {
56         LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(), modification.getClass().getSimpleName(),
57                 modification.getPath());
58     }
59
60     @Override
61     public <T> void executeRead(AbstractRead<T> readCmd, SettableFuture<T> proxyFuture) {
62         LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(),
63                 readCmd.getPath());
64
65         final Throwable t;
66         if (failure instanceof NoShardLeaderException) {
67             t = new DataStoreUnavailableException(failure.getMessage(), failure);
68         } else {
69             t = failure;
70         }
71         proxyFuture.setException(new ReadFailedException("Error executeRead " + readCmd.getClass().getSimpleName()
72                 + " for path " + readCmd.getPath(), t));
73     }
74 }