BUG-5280: add FrontendMetadata
[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     void applyModifications(final Iterable<Modification> modifications) {
84         totalBatchedModificationsReceived++;
85         if(lastBatchedModificationsException == null) {
86             for (Modification modification : modifications) {
87                     try {
88                         modification.apply(transaction.getSnapshot());
89                     } catch (RuntimeException e) {
90                         lastBatchedModificationsException = e;
91                         throw e;
92                     }
93             }
94         }
95     }
96
97     void canCommit(final FutureCallback<Void> callback) {
98         cohort.canCommit(callback);
99     }
100
101     void preCommit(final FutureCallback<DataTreeCandidate> callback) {
102         cohort.preCommit(callback);
103     }
104
105     void commit(final FutureCallback<UnsignedLong> callback) {
106         cohort.commit(callback);
107     }
108
109     void abort() throws InterruptedException, ExecutionException, TimeoutException {
110         cohort.abort().get();
111     }
112
113     void ready(final CohortDecorator cohortDecorator) {
114         Preconditions.checkState(cohort == null, "cohort was already set");
115
116         cohort = transaction.ready();
117
118         if(cohortDecorator != null) {
119             // Call the hook for unit tests.
120             cohort = cohortDecorator.decorate(transactionID, cohort);
121         }
122     }
123
124     boolean isDoImmediateCommit() {
125         return doImmediateCommit;
126     }
127
128     void setDoImmediateCommit(final boolean doImmediateCommit) {
129         this.doImmediateCommit = doImmediateCommit;
130     }
131
132     ActorRef getReplySender() {
133         return replySender;
134     }
135
136     void setReplySender(final ActorRef replySender) {
137         this.replySender = replySender;
138     }
139
140     Shard getShard() {
141         return shard;
142     }
143
144     void setShard(final Shard shard) {
145         this.shard = shard;
146     }
147
148     @Override
149     public String toString() {
150         final StringBuilder builder = new StringBuilder();
151         builder.append("CohortEntry [transactionID=").append(transactionID).append(", doImmediateCommit=")
152                 .append(doImmediateCommit).append("]");
153         return builder.toString();
154     }
155 }