*/
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;
/**
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));
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(), Collections.emptyMap()),
+ 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<NormalizedNode<?, ?>>... 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());
}
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());
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) {
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);
+ }
}