Use Empty instead of Void in cohorts
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / CohortEntry.java
1 /*
2  * Copyright (c) 2014 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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import com.google.common.primitives.UnsignedLong;
15 import com.google.common.util.concurrent.FutureCallback;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.SortedSet;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.controller.cluster.datastore.ShardCommitCoordinator.CohortDecorator;
21 import org.opendaylight.controller.cluster.datastore.modification.Modification;
22 import org.opendaylight.yangtools.yang.common.Empty;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
25
26 final class CohortEntry {
27     private final ReadWriteShardDataTreeTransaction transaction;
28     private final TransactionIdentifier transactionId;
29     private final short clientVersion;
30
31     private RuntimeException lastBatchedModificationsException;
32     private int totalBatchedModificationsReceived;
33     private int totalOperationsProcessed;
34     private ShardDataTreeCohort cohort;
35     private boolean doImmediateCommit;
36     private ActorRef replySender;
37     private Shard shard;
38
39     private CohortEntry(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
40         cohort = null;
41         this.transaction = requireNonNull(transaction);
42         transactionId = transaction.getIdentifier();
43         this.clientVersion = clientVersion;
44     }
45
46     private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
47         this.cohort = requireNonNull(cohort);
48         transactionId = cohort.getIdentifier();
49         transaction = null;
50         this.clientVersion = clientVersion;
51     }
52
53     static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
54         return new CohortEntry(transaction, clientVersion);
55     }
56
57     static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
58         return new CohortEntry(cohort, clientVersion);
59     }
60
61     TransactionIdentifier getTransactionId() {
62         return transactionId;
63     }
64
65     short getClientVersion() {
66         return clientVersion;
67     }
68
69     boolean isFailed() {
70         return cohort != null && cohort.isFailed();
71     }
72
73     DataTreeModification getDataTreeModification() {
74         return cohort.getDataTreeModification();
75     }
76
77     ReadWriteShardDataTreeTransaction getTransaction() {
78         return transaction;
79     }
80
81     int getTotalBatchedModificationsReceived() {
82         return totalBatchedModificationsReceived;
83     }
84
85     int getTotalOperationsProcessed() {
86         return totalOperationsProcessed;
87     }
88
89     RuntimeException getLastBatchedModificationsException() {
90         return lastBatchedModificationsException;
91     }
92
93     @SuppressWarnings("checkstyle:IllegalCatch")
94     void applyModifications(final List<Modification> modifications) {
95         totalBatchedModificationsReceived++;
96         if (lastBatchedModificationsException == null) {
97             totalOperationsProcessed += modifications.size();
98             for (Modification modification : modifications) {
99                 try {
100                     modification.apply(transaction.getSnapshot());
101                 } catch (RuntimeException e) {
102                     lastBatchedModificationsException = e;
103                     throw e;
104                 }
105             }
106         }
107     }
108
109     void canCommit(final FutureCallback<Empty> callback) {
110         cohort.canCommit(callback);
111     }
112
113     void preCommit(final FutureCallback<DataTreeCandidate> callback) {
114         cohort.preCommit(callback);
115     }
116
117     void commit(final FutureCallback<UnsignedLong> callback) {
118         cohort.commit(callback);
119     }
120
121     void abort(final FutureCallback<Empty> callback) {
122         cohort.abort(callback);
123     }
124
125     void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
126         checkState(cohort == null, "cohort was already set");
127
128         cohort = transaction.ready(participatingShardNames);
129
130         if (cohortDecorator != null) {
131             // Call the hook for unit tests.
132             cohort = cohortDecorator.decorate(transactionId, cohort);
133         }
134     }
135
136     boolean isSealed() {
137         return cohort != null;
138     }
139
140     Optional<SortedSet<String>> getParticipatingShardNames() {
141         return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
142     }
143
144     boolean isDoImmediateCommit() {
145         return doImmediateCommit;
146     }
147
148     void setDoImmediateCommit(final boolean doImmediateCommit) {
149         this.doImmediateCommit = doImmediateCommit;
150     }
151
152     ActorRef getReplySender() {
153         return replySender;
154     }
155
156     void setReplySender(final ActorRef replySender) {
157         this.replySender = replySender;
158     }
159
160     Shard getShard() {
161         return shard;
162     }
163
164     void setShard(final Shard shard) {
165         this.shard = shard;
166     }
167
168     @Override
169     public String toString() {
170         final StringBuilder builder = new StringBuilder();
171         builder.append("CohortEntry [transactionId=").append(transactionId).append(", doImmediateCommit=")
172                 .append(doImmediateCommit).append("]");
173         return builder.toString();
174     }
175 }