Move byte-based serialization method
[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.Optional;
15 import java.util.SortedSet;
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.cohort = null;
36         this.transaction = Preconditions.checkNotNull(transaction);
37         this.transactionId = transaction.getIdentifier();
38         this.clientVersion = clientVersion;
39     }
40
41     private CohortEntry(final ShardDataTreeCohort cohort, final short clientVersion) {
42         this.cohort = Preconditions.checkNotNull(cohort);
43         this.transactionId = cohort.getIdentifier();
44         this.transaction = null;
45         this.clientVersion = clientVersion;
46     }
47
48     static CohortEntry createOpen(final ReadWriteShardDataTreeTransaction transaction, final short clientVersion) {
49         return new CohortEntry(transaction, clientVersion);
50     }
51
52     static CohortEntry createReady(final ShardDataTreeCohort cohort, final short clientVersion) {
53         return new CohortEntry(cohort, clientVersion);
54     }
55
56     TransactionIdentifier getTransactionId() {
57         return transactionId;
58     }
59
60     short getClientVersion() {
61         return clientVersion;
62     }
63
64     boolean isFailed() {
65         return cohort != null && cohort.isFailed();
66     }
67
68     DataTreeModification getDataTreeModification() {
69         return cohort.getDataTreeModification();
70     }
71
72     ReadWriteShardDataTreeTransaction getTransaction() {
73         return transaction;
74     }
75
76     int getTotalBatchedModificationsReceived() {
77         return totalBatchedModificationsReceived;
78     }
79
80     RuntimeException getLastBatchedModificationsException() {
81         return lastBatchedModificationsException;
82     }
83
84     @SuppressWarnings("checkstyle:IllegalCatch")
85     void applyModifications(final Iterable<Modification> modifications) {
86         totalBatchedModificationsReceived++;
87         if (lastBatchedModificationsException == null) {
88             for (Modification modification : modifications) {
89                 try {
90                     modification.apply(transaction.getSnapshot());
91                 } catch (RuntimeException e) {
92                     lastBatchedModificationsException = e;
93                     throw e;
94                 }
95             }
96         }
97     }
98
99     void canCommit(final FutureCallback<Void> callback) {
100         cohort.canCommit(callback);
101     }
102
103     void preCommit(final FutureCallback<DataTreeCandidate> callback) {
104         cohort.preCommit(callback);
105     }
106
107     void commit(final FutureCallback<UnsignedLong> callback) {
108         cohort.commit(callback);
109     }
110
111     void abort(final FutureCallback<Void> callback) {
112         cohort.abort(callback);
113     }
114
115     void ready(final Optional<SortedSet<String>> participatingShardNames, final CohortDecorator cohortDecorator) {
116         Preconditions.checkState(cohort == null, "cohort was already set");
117
118         cohort = transaction.ready(participatingShardNames);
119
120         if (cohortDecorator != null) {
121             // Call the hook for unit tests.
122             cohort = cohortDecorator.decorate(transactionId, cohort);
123         }
124     }
125
126     boolean isSealed() {
127         return cohort != null;
128     }
129
130     Optional<SortedSet<String>> getParticipatingShardNames() {
131         return cohort != null ? cohort.getParticipatingShardNames() : Optional.empty();
132     }
133
134     boolean isDoImmediateCommit() {
135         return doImmediateCommit;
136     }
137
138     void setDoImmediateCommit(final boolean doImmediateCommit) {
139         this.doImmediateCommit = doImmediateCommit;
140     }
141
142     ActorRef getReplySender() {
143         return replySender;
144     }
145
146     void setReplySender(final ActorRef replySender) {
147         this.replySender = replySender;
148     }
149
150     Shard getShard() {
151         return shard;
152     }
153
154     void setShard(final Shard shard) {
155         this.shard = shard;
156     }
157
158     @Override
159     public String toString() {
160         final StringBuilder builder = new StringBuilder();
161         builder.append("CohortEntry [transactionId=").append(transactionId).append(", doImmediateCommit=")
162                 .append(doImmediateCommit).append("]");
163         return builder.toString();
164     }
165 }