CDS: Implement front-end support for local transactions
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContext.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.SettableFuture;
15 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import scala.concurrent.Future;
20
21 /**
22  * Processes front-end transaction operations locally before being committed to the destination shard.
23  * Instances of this class are used when the destination shard is local to the caller.
24  *
25  * @author Thomas Pantelis
26  */
27 final class LocalTransactionContext extends AbstractTransactionContext {
28     private final DOMStoreReadWriteTransaction delegate;
29
30     LocalTransactionContext(TransactionIdentifier identifier, DOMStoreReadWriteTransaction delegate) {
31         super(identifier);
32         this.delegate = delegate;
33     }
34
35     @Override
36     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
37         delegate.write(path, data);
38     }
39
40     @Override
41     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
42         delegate.merge(path, data);
43     }
44
45     @Override
46     public void deleteData(YangInstanceIdentifier path) {
47         delegate.delete(path);
48     }
49
50     @Override
51     public void readData(YangInstanceIdentifier path, final SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
52         Futures.addCallback(delegate.read(path), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
53             @Override
54             public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
55                 proxyFuture.set(result);
56             }
57
58             @Override
59             public void onFailure(Throwable t) {
60                 proxyFuture.setException(t);
61             }
62         });
63     }
64
65     @Override
66     public void dataExists(YangInstanceIdentifier path, final SettableFuture<Boolean> proxyFuture) {
67         Futures.addCallback(delegate.exists(path), new FutureCallback<Boolean>() {
68             @Override
69             public void onSuccess(Boolean result) {
70                 proxyFuture.set(result);
71             }
72
73             @Override
74             public void onFailure(Throwable t) {
75                 proxyFuture.setException(t);
76             }
77         });
78     }
79
80     private LocalThreePhaseCommitCohort ready() {
81         return (LocalThreePhaseCommitCohort) delegate.ready();
82     }
83
84     @Override
85     public Future<ActorSelection> readyTransaction() {
86         return ready().initiateCoordinatedCommit();
87     }
88
89     @Override
90     public Future<Object> directCommit() {
91         return ready().initiateDirectCommit();
92     }
93
94     @Override
95     public boolean supportsDirectCommit() {
96         return true;
97     }
98
99     @Override
100     public void closeTransaction() {
101         delegate.close();
102     }
103 }