Move operation limiter down to TransactionContextWrapper
[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.utils.ActorContext;
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, OperationLimiter limiter) {
34         super(limiter);
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         releaseOperation();
47     }
48
49     @Override
50     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
51         incrementModificationCount();
52         getWriteDelegate().merge(path, data);
53         releaseOperation();
54     }
55
56     @Override
57     public void deleteData(YangInstanceIdentifier path) {
58         incrementModificationCount();
59         getWriteDelegate().delete(path);
60         releaseOperation();
61     }
62
63     @Override
64     public void readData(YangInstanceIdentifier path, final SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
65         Futures.addCallback(getReadDelegate().read(path), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
66             @Override
67             public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
68                 proxyFuture.set(result);
69                 releaseOperation();
70             }
71
72             @Override
73             public void onFailure(final Throwable t) {
74                 proxyFuture.setException(t);
75                 releaseOperation();
76             }
77         });
78     }
79
80     @Override
81     public void dataExists(YangInstanceIdentifier path, final SettableFuture<Boolean> proxyFuture) {
82         Futures.addCallback(getReadDelegate().exists(path), new FutureCallback<Boolean>() {
83             @Override
84             public void onSuccess(final Boolean result) {
85                 proxyFuture.set(result);
86                 releaseOperation();
87             }
88
89             @Override
90             public void onFailure(final Throwable t) {
91                 proxyFuture.setException(t);
92                 releaseOperation();
93             }
94         });
95     }
96
97     private LocalThreePhaseCommitCohort ready() {
98         logModificationCount();
99         acquireOperation();
100         return (LocalThreePhaseCommitCohort) getWriteDelegate().ready();
101     }
102
103     @SuppressWarnings({ "unchecked", "rawtypes" })
104     private <T extends Future> T completeOperation(final ActorContext actorContext, final T operationFuture) {
105         operationFuture.onComplete(getLimiter(), actorContext.getClientDispatcher());
106         return operationFuture;
107     }
108
109     @Override
110     public Future<ActorSelection> readyTransaction() {
111         final LocalThreePhaseCommitCohort cohort = ready();
112         return completeOperation(cohort.getActorContext(), cohort.initiateCoordinatedCommit());
113     }
114
115     @Override
116     public Future<Object> directCommit() {
117         final LocalThreePhaseCommitCohort cohort = ready();
118         return completeOperation(cohort.getActorContext(), cohort.initiateDirectCommit());
119     }
120
121     @Override
122     public boolean supportsDirectCommit() {
123         return true;
124     }
125
126     @Override
127     public void closeTransaction() {
128         txDelegate.close();
129         releaseOperation();
130     }
131 }