X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fmessages%2FDatastoreSnapshotListTest.java;h=8c8e329e14df9c4e8e27eb9f217260f331cb08b2;hp=72009015ade866b929476ba0fd92f74c9fc67916;hb=95c296a7c1e8e186a88a0a0dc82e080b2185db33;hpb=2f77e92af7a68b4a97dbfb709c6cc9b11a49878a diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/messages/DatastoreSnapshotListTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/messages/DatastoreSnapshotListTest.java index 72009015ad..8c8e329e14 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/messages/DatastoreSnapshotListTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/messages/DatastoreSnapshotListTest.java @@ -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,21 @@ 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()); + + ShardManagerSnapshot legacyShardManagerSnapshot = newLegacyShardManagerSnapshot("config-one", "config-two"); 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())))); + SerializationUtils.serialize(legacyShardManagerSnapshot), + 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,22 +74,29 @@ 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), + new org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot( + legacyShardManagerSnapshot.getShardList()), + Optional.of(legacyConfigRoot1), Optional.of(legacyConfigRoot2)); + assertDatastoreSnapshotEquals(legacyOperSnapshot, cloned.get(1), + (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)null, + Optional.empty()); } + @SuppressWarnings("unchecked") private void assertDatastoreSnapshotEquals(DatastoreSnapshot legacy, - org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot actual) { + org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot actual, + org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot expShardMgrSnapshot, + Optional>... shardRoots) throws IOException { assertEquals("Type", legacy.getType(), actual.getType()); if (legacy.getShardManagerSnapshot() == null) { assertNull("Expected null ShardManagerSnapshot", actual.getShardManagerSnapshot()); } else { - ShardManagerSnapshot legacyShardManagerSnapshot = - (ShardManagerSnapshot) SerializationUtils.deserialize(legacy.getShardManagerSnapshot()); - ShardManagerSnapshot actualShardManagerSnapshot = - (ShardManagerSnapshot) SerializationUtils.deserialize(actual.getShardManagerSnapshot()); - assertEquals("ShardManagerSnapshot", legacyShardManagerSnapshot.getShardList(), + org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot actualShardManagerSnapshot = + (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) + SerializationUtils.deserialize(legacy.getShardManagerSnapshot()); + assertEquals("ShardManagerSnapshot", expShardMgrSnapshot.getShardList(), actualShardManagerSnapshot.getShardList()); } @@ -91,11 +108,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> 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 +121,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 +141,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.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); + } }