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