Bug 7521: Convert DatastoreSnapshot.ShardSnapshot to store Snapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / messages / DatastoreSnapshotListTest.java
index 72009015ade866b929476ba0fd92f74c9fc67916..989ed46ca38a34b06f5ce1b5be86ff392a16d7bb 100644 (file)
@@ -7,31 +7,38 @@
  */
 package org.opendaylight.controller.cluster.datastore.messages;
 
-import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
 import java.math.BigInteger;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Optional;
 import org.apache.commons.lang.SerializationUtils;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.datastore.AbstractShardTest;
 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
+import org.opendaylight.controller.cluster.datastore.persisted.PayloadVersion;
+import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
+import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerSnapshot;
 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
 import org.opendaylight.controller.cluster.raft.Snapshot;
+import org.opendaylight.controller.cluster.raft.persisted.EmptyState;
 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
-import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
 
 /**
@@ -43,18 +50,20 @@ import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFac
 public class DatastoreSnapshotListTest {
     @Test
     public void testSerialization() throws Exception {
+        NormalizedNode<?, ?> legacyConfigRoot1 = toRootNode(CarsModel.BASE_PATH,
+                CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima",
+                        BigInteger.valueOf(20000L)),CarsModel.newCarEntry("sportage",
+                            BigInteger.valueOf(30000L)))));
+
+        NormalizedNode<?, ?> legacyConfigRoot2 = toRootNode(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
+
         DatastoreSnapshot legacyConfigSnapshot = new DatastoreSnapshot("config",
                 SerializationUtils.serialize(newLegacyShardManagerSnapshot("config-one", "config-two")),
-                Arrays.asList(newLegacyShardSnapshot("config-one", newLegacySnapshot(CarsModel.BASE_PATH,
-                        CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima",
-                            BigInteger.valueOf(20000L)),CarsModel.newCarEntry("sportage",
-                                BigInteger.valueOf(30000L)))))),
-                    newLegacyShardSnapshot("config-two", newLegacySnapshot(PeopleModel.BASE_PATH,
-                            PeopleModel.emptyContainer()))));
+                Arrays.asList(newLegacyShardSnapshot("config-one", newLegacySnapshot(legacyConfigRoot1)),
+                    newLegacyShardSnapshot("config-two", newLegacySnapshot(legacyConfigRoot2))));
 
         DatastoreSnapshot legacyOperSnapshot = new DatastoreSnapshot("oper",
-                null, Arrays.asList(newLegacyShardSnapshot("oper-one", newLegacySnapshot(TestModel.TEST_PATH,
-                        ImmutableNodes.containerNode(TestModel.TEST_QNAME)))));
+                null, Arrays.asList(newLegacyShardSnapshot("oper-one", newLegacySnapshot(null))));
 
         DatastoreSnapshotList legacy = new DatastoreSnapshotList(Arrays.asList(legacyConfigSnapshot,
                 legacyOperSnapshot));
@@ -64,12 +73,14 @@ public class DatastoreSnapshotListTest {
                 SerializationUtils.clone(legacy);
 
         assertEquals("DatastoreSnapshotList size", 2, cloned.size());
-        assertDatastoreSnapshotEquals(legacyConfigSnapshot, cloned.get(0));
-        assertDatastoreSnapshotEquals(legacyOperSnapshot, cloned.get(1));
+        assertDatastoreSnapshotEquals(legacyConfigSnapshot, cloned.get(0), Optional.of(legacyConfigRoot1),
+                Optional.of(legacyConfigRoot2));
+        assertDatastoreSnapshotEquals(legacyOperSnapshot, cloned.get(1), Optional.empty());
     }
 
     private void assertDatastoreSnapshotEquals(DatastoreSnapshot legacy,
-            org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot actual) {
+            org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot actual,
+            Optional<NormalizedNode<?, ?>>... shardRoots) throws IOException {
         assertEquals("Type", legacy.getType(), actual.getType());
 
         if (legacy.getShardManagerSnapshot() == null) {
@@ -91,11 +102,12 @@ public class DatastoreSnapshotListTest {
                 actualShardSnapshot = actual.getShardSnapshots().get(i);
             assertEquals("Shard name", legacyShardSnapshot.getName(), actualShardSnapshot.getName());
             assertSnapshotEquals((Snapshot) SerializationUtils.deserialize(legacyShardSnapshot.getSnapshot()),
-                    (Snapshot) SerializationUtils.deserialize(actualShardSnapshot.getSnapshot()));
+                    shardRoots[i], actualShardSnapshot.getSnapshot());
         }
     }
 
-    private static void assertSnapshotEquals(Snapshot expected, Snapshot actual) {
+    private static void assertSnapshotEquals(Snapshot expected, Optional<NormalizedNode<?, ?>> expRoot,
+            org.opendaylight.controller.cluster.raft.persisted.Snapshot actual) throws IOException {
         assertEquals("lastIndex", expected.getLastIndex(), actual.getLastIndex());
         assertEquals("lastTerm", expected.getLastTerm(), actual.getLastTerm());
         assertEquals("lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
@@ -103,7 +115,15 @@ public class DatastoreSnapshotListTest {
         assertEquals("unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries());
         assertEquals("electionTerm", expected.getElectionTerm(), actual.getElectionTerm());
         assertEquals("electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor());
-        assertArrayEquals("state", expected.getState(), actual.getState());
+
+        if (expRoot.isPresent()) {
+            ShardDataTreeSnapshot actualSnapshot = ((ShardSnapshotState)actual.getState()).getSnapshot();
+            assertEquals("ShardDataTreeSnapshot type", MetadataShardDataTreeSnapshot.class, actualSnapshot.getClass());
+            assertTrue("Expected root node present", actualSnapshot.getRootNode().isPresent());
+            assertEquals("Root node", expRoot.get(), actualSnapshot.getRootNode().get());
+        } else {
+            assertEquals("State type", EmptyState.class, actual.getState().getClass());
+        }
     }
 
     private static ShardManagerSnapshot newLegacyShardManagerSnapshot(String... shards) {
@@ -115,16 +135,28 @@ public class DatastoreSnapshotListTest {
         return new DatastoreSnapshot.ShardSnapshot(name, SerializationUtils.serialize(snapshot));
     }
 
-    private static Snapshot newLegacySnapshot(YangInstanceIdentifier path, NormalizedNode<?, ?> node)
+    private static Snapshot newLegacySnapshot(NormalizedNode<?, ?> root)
             throws Exception {
-        DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
-        dataTree.setSchemaContext(SchemaContextHelper.full());
-        AbstractShardTest.writeToStore(dataTree, path, node);
-        NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
-
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        new MetadataShardDataTreeSnapshot(root).serialize(bos);
+        if (root != null) {
+            MetadataShardDataTreeSnapshot snapshot = new MetadataShardDataTreeSnapshot(root);
+            try (final DataOutputStream dos = new DataOutputStream(bos)) {
+                PayloadVersion.BORON.writeTo(dos);
+                try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
+                    oos.writeObject(snapshot);
+                }
+            }
+        }
+
         return Snapshot.create(bos.toByteArray(), Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1,
                 "member-1", null);
     }
+
+    private static NormalizedNode<?, ?> toRootNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node)
+            throws DataValidationFailedException {
+        DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
+        dataTree.setSchemaContext(SchemaContextHelper.full());
+        AbstractShardTest.writeToStore(dataTree, path, node);
+        return AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
+    }
 }