Bug 7521: Convert DatastoreSnapshot.ShardSnapshot to store Snapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / DatastoreSnapshotList.java
index ca137d73dbc6f7a5853940f896614e7a161e4dc1..8400e0c3e236fb2eaf6f8be67932c48b01e1064e 100644 (file)
@@ -7,8 +7,15 @@
  */
 package org.opendaylight.controller.cluster.datastore.messages;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
 import java.util.ArrayList;
 import java.util.List;
+import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
+import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
+import org.opendaylight.controller.cluster.raft.Snapshot;
+import org.opendaylight.controller.cluster.raft.persisted.EmptyState;
 
 /**
  * Stores a list of DatastoreSnapshot instances.
@@ -26,7 +33,7 @@ public class DatastoreSnapshotList extends ArrayList<DatastoreSnapshot> {
         super(snapshots);
     }
 
-    private Object readResolve() {
+    private Object readResolve() throws IOException, ClassNotFoundException {
         List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot> snapshots =
                 new ArrayList<>(size());
         for (DatastoreSnapshot legacy: this) {
@@ -37,15 +44,32 @@ public class DatastoreSnapshotList extends ArrayList<DatastoreSnapshot> {
         return new org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList(snapshots);
     }
 
-    private List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot> fromLegacy(
-            List<DatastoreSnapshot.ShardSnapshot> from) {
+    private static List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot>
+            fromLegacy(List<DatastoreSnapshot.ShardSnapshot> from) throws IOException, ClassNotFoundException {
         List<org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot> snapshots =
                 new ArrayList<>(from.size());
         for (DatastoreSnapshot.ShardSnapshot legacy: from) {
             snapshots.add(new org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot(
-                    legacy.getName(), legacy.getSnapshot()));
+                    legacy.getName(), deserialize(legacy.getSnapshot())));
         }
 
         return snapshots;
     }
+
+    private static org.opendaylight.controller.cluster.raft.persisted.Snapshot deserialize(byte[] bytes)
+            throws IOException, ClassNotFoundException {
+        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
+            Snapshot legacy = (Snapshot) ois.readObject();
+
+            org.opendaylight.controller.cluster.raft.persisted.Snapshot.State state = EmptyState.INSTANCE;
+            if (legacy.getState().length > 0) {
+                state = new ShardSnapshotState(ShardDataTreeSnapshot.deserializePreCarbon(legacy.getState()));
+            }
+
+            return org.opendaylight.controller.cluster.raft.persisted.Snapshot.create(
+                    state, legacy.getUnAppliedEntries(), legacy.getLastIndex(),
+                    legacy.getLastTerm(), legacy.getLastAppliedIndex(), legacy.getLastAppliedTerm(),
+                    legacy.getElectionTerm(), legacy.getElectionVotedFor(), legacy.getServerConfiguration());
+        }
+    }
 }