Split out TransactionContext classes
[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.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.concurrent.Semaphore;
15 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
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     private final Semaphore operationLimiter;
28
29     public NoOpTransactionContext(Throwable failure, TransactionIdentifier identifier, Semaphore operationLimiter) {
30         super(identifier);
31         this.failure = failure;
32         this.operationLimiter = operationLimiter;
33     }
34
35     @Override
36     public void closeTransaction() {
37         LOG.debug("NoOpTransactionContext {} closeTransaction called", identifier);
38     }
39
40     @Override
41     public Future<ActorSelection> readyTransaction() {
42         LOG.debug("Tx {} readyTransaction called", identifier);
43         operationLimiter.release();
44         return akka.dispatch.Futures.failed(failure);
45     }
46
47     @Override
48     public void deleteData(YangInstanceIdentifier path) {
49         LOG.debug("Tx {} deleteData called path = {}", identifier, path);
50         operationLimiter.release();
51     }
52
53     @Override
54     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
55         LOG.debug("Tx {} mergeData called path = {}", identifier, path);
56         operationLimiter.release();
57     }
58
59     @Override
60     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
61         LOG.debug("Tx {} writeData called path = {}", identifier, path);
62         operationLimiter.release();
63     }
64
65     @Override
66     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readData(
67             YangInstanceIdentifier path) {
68         LOG.debug("Tx {} readData called path = {}", identifier, path);
69         operationLimiter.release();
70         return Futures.immediateFailedCheckedFuture(new ReadFailedException(
71                 "Error reading data for path " + path, failure));
72     }
73
74     @Override
75     public CheckedFuture<Boolean, ReadFailedException> dataExists(
76             YangInstanceIdentifier path) {
77         LOG.debug("Tx {} dataExists called path = {}", identifier, path);
78         operationLimiter.release();
79         return Futures.immediateFailedCheckedFuture(new ReadFailedException(
80                 "Error checking exists for path " + path, failure));
81     }
82 }