Define DataStoreVersions.MAGNESIUM_VERSION
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / BatchedModifications.java
index 670641f6ac1ee392e2c57a249f82afc54f65f90a..77d2687ccba2a723dd5041891c8c1d8b4dbde566 100644 (file)
@@ -7,6 +7,18 @@
  */
 package org.opendaylight.controller.cluster.datastore.messages;
 
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Optional;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
+import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
 
 /**
@@ -14,18 +26,109 @@ import org.opendaylight.controller.cluster.datastore.modification.MutableComposi
  *
  * @author Thomas Pantelis
  */
-public class BatchedModifications extends MutableCompositeModification implements SerializableMessage {
+public class BatchedModifications extends MutableCompositeModification {
     private static final long serialVersionUID = 1L;
 
+    private boolean ready;
+    private boolean doCommitOnReady;
+    private int totalMessagesSent;
+    private TransactionIdentifier transactionId;
+
+    private @Nullable SortedSet<String> participatingShardNames;
+
     public BatchedModifications() {
     }
 
-    public BatchedModifications(short version) {
+    public BatchedModifications(TransactionIdentifier transactionId, short version) {
         super(version);
+        this.transactionId = requireNonNull(transactionId, "transactionID can't be null");
+    }
+
+    public boolean isReady() {
+        return ready;
+    }
+
+    public void setReady(Optional<SortedSet<String>> possibleParticipatingShardNames) {
+        this.ready = true;
+        this.participatingShardNames = requireNonNull(possibleParticipatingShardNames).orElse(null);
+        Preconditions.checkArgument(this.participatingShardNames == null || this.participatingShardNames.size() > 1);
+    }
+
+    public void setReady() {
+        setReady(Optional.empty());
+    }
+
+    public Optional<SortedSet<String>> getParticipatingShardNames() {
+        return Optional.ofNullable(participatingShardNames);
+    }
+
+    public boolean isDoCommitOnReady() {
+        return doCommitOnReady;
+    }
+
+    public void setDoCommitOnReady(boolean doCommitOnReady) {
+        this.doCommitOnReady = doCommitOnReady;
+    }
+
+    public int getTotalMessagesSent() {
+        return totalMessagesSent;
+    }
+
+    public void setTotalMessagesSent(int totalMessagesSent) {
+        this.totalMessagesSent = totalMessagesSent;
+    }
+
+    public TransactionIdentifier getTransactionId() {
+        return transactionId;
+    }
+
+    @Override
+    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        super.readExternal(in);
+        transactionId = TransactionIdentifier.readFrom(in);
+        ready = in.readBoolean();
+        totalMessagesSent = in.readInt();
+        doCommitOnReady = in.readBoolean();
+
+        if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
+            final int count = in.readInt();
+            if (count != 0) {
+                SortedSet<String> shardNames = new TreeSet<>();
+                for (int i = 0; i < count; i++) {
+                    shardNames.add((String) in.readObject());
+                }
+
+                participatingShardNames = shardNames;
+            }
+        }
+    }
+
+    @Override
+    public void writeExternal(ObjectOutput out) throws IOException {
+        super.writeExternal(out);
+        transactionId.writeTo(out);
+        out.writeBoolean(ready);
+        out.writeInt(totalMessagesSent);
+        out.writeBoolean(doCommitOnReady);
+
+        if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
+            if (participatingShardNames != null) {
+                out.writeInt(participatingShardNames.size());
+                for (String shardName: participatingShardNames) {
+                    out.writeObject(shardName);
+                }
+            } else {
+                out.writeInt(0);
+            }
+        }
     }
 
     @Override
-    public Object toSerializable() {
-        return this;
+    public String toString() {
+        return "BatchedModifications [transactionId=" + transactionId
+                + ", ready=" + isReady()
+                + ", participatingShardNames=" + participatingShardNames
+                + ", totalMessagesSent=" + totalMessagesSent
+                + ", modifications size=" + getModifications().size() + "]";
     }
 }