Define DataStoreVersions.MAGNESIUM_VERSION
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / BatchedModifications.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.messages;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.util.Optional;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
22 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
23
24 /**
25  * Message used to batch write, merge, delete modification operations to the  ShardTransaction actor.
26  *
27  * @author Thomas Pantelis
28  */
29 public class BatchedModifications extends MutableCompositeModification {
30     private static final long serialVersionUID = 1L;
31
32     private boolean ready;
33     private boolean doCommitOnReady;
34     private int totalMessagesSent;
35     private TransactionIdentifier transactionId;
36
37     private @Nullable SortedSet<String> participatingShardNames;
38
39     public BatchedModifications() {
40     }
41
42     public BatchedModifications(TransactionIdentifier transactionId, short version) {
43         super(version);
44         this.transactionId = requireNonNull(transactionId, "transactionID can't be null");
45     }
46
47     public boolean isReady() {
48         return ready;
49     }
50
51     public void setReady(Optional<SortedSet<String>> possibleParticipatingShardNames) {
52         this.ready = true;
53         this.participatingShardNames = requireNonNull(possibleParticipatingShardNames).orElse(null);
54         Preconditions.checkArgument(this.participatingShardNames == null || this.participatingShardNames.size() > 1);
55     }
56
57     public void setReady() {
58         setReady(Optional.empty());
59     }
60
61     public Optional<SortedSet<String>> getParticipatingShardNames() {
62         return Optional.ofNullable(participatingShardNames);
63     }
64
65     public boolean isDoCommitOnReady() {
66         return doCommitOnReady;
67     }
68
69     public void setDoCommitOnReady(boolean doCommitOnReady) {
70         this.doCommitOnReady = doCommitOnReady;
71     }
72
73     public int getTotalMessagesSent() {
74         return totalMessagesSent;
75     }
76
77     public void setTotalMessagesSent(int totalMessagesSent) {
78         this.totalMessagesSent = totalMessagesSent;
79     }
80
81     public TransactionIdentifier getTransactionId() {
82         return transactionId;
83     }
84
85     @Override
86     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
87         super.readExternal(in);
88         transactionId = TransactionIdentifier.readFrom(in);
89         ready = in.readBoolean();
90         totalMessagesSent = in.readInt();
91         doCommitOnReady = in.readBoolean();
92
93         if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
94             final int count = in.readInt();
95             if (count != 0) {
96                 SortedSet<String> shardNames = new TreeSet<>();
97                 for (int i = 0; i < count; i++) {
98                     shardNames.add((String) in.readObject());
99                 }
100
101                 participatingShardNames = shardNames;
102             }
103         }
104     }
105
106     @Override
107     public void writeExternal(ObjectOutput out) throws IOException {
108         super.writeExternal(out);
109         transactionId.writeTo(out);
110         out.writeBoolean(ready);
111         out.writeInt(totalMessagesSent);
112         out.writeBoolean(doCommitOnReady);
113
114         if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
115             if (participatingShardNames != null) {
116                 out.writeInt(participatingShardNames.size());
117                 for (String shardName: participatingShardNames) {
118                     out.writeObject(shardName);
119                 }
120             } else {
121                 out.writeInt(0);
122             }
123         }
124     }
125
126     @Override
127     public String toString() {
128         return "BatchedModifications [transactionId=" + transactionId
129                 + ", ready=" + isReady()
130                 + ", participatingShardNames=" + participatingShardNames
131                 + ", totalMessagesSent=" + totalMessagesSent
132                 + ", modifications size=" + getModifications().size() + "]";
133     }
134 }