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