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