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%2FDatastoreSnapshotRestoreTest.java;h=67af9bb3487ff3ca31f886eacfee98e7d87c483c;hp=7adc93c8f3d3bec2f9d9d3089b4f4fda82ed2975;hb=95c296a7c1e8e186a88a0a0dc82e080b2185db33;hpb=600e07c2adb213b614cad127070f1e7ff074b42b diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestoreTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestoreTest.java index 7adc93c8f3..67af9bb348 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestoreTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestoreTest.java @@ -7,23 +7,38 @@ */ package org.opendaylight.controller.cluster.datastore; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.FileOutputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; import org.apache.commons.lang3.SerializationUtils; import org.junit.After; import org.junit.Test; -import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot; -import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot; -import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshotList; +import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList; +import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState; +import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot; +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.TreeType; +import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; +import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; + /** * Unit tests for DatastoreSnapshotRestore. @@ -45,52 +60,83 @@ public class DatastoreSnapshotRestoreTest { public void test() throws Exception { assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs()); - List shardSnapshots = new ArrayList<>(); - shardSnapshots.add(new ShardSnapshot("cars", new byte[]{1,2})); - shardSnapshots.add(new ShardSnapshot("people", new byte[]{3,4})); - DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config", null, shardSnapshots ); + final DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config", + newShardManagerSnapshot("config-one", "config-two"), + Arrays.asList(new DatastoreSnapshot.ShardSnapshot("config-one", newSnapshot(CarsModel.BASE_PATH, + CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", + BigInteger.valueOf(20000L)),CarsModel.newCarEntry("sportage", + BigInteger.valueOf(30000L)))))), + new DatastoreSnapshot.ShardSnapshot("config-two", newSnapshot(PeopleModel.BASE_PATH, + PeopleModel.emptyContainer())))); - shardSnapshots = new ArrayList<>(); - shardSnapshots.add(new ShardSnapshot("cars", new byte[]{5,6})); - shardSnapshots.add(new ShardSnapshot("people", new byte[]{7,8})); - shardSnapshots.add(new ShardSnapshot("bikes", new byte[]{9,0})); - DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper", null, shardSnapshots ); + DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper", + null, Arrays.asList(new DatastoreSnapshot.ShardSnapshot("oper-one", newSnapshot(TestModel.TEST_PATH, + ImmutableNodes.containerNode(TestModel.TEST_QNAME))))); - DatastoreSnapshotList snapshotList = new DatastoreSnapshotList(); - snapshotList.add(configSnapshot); - snapshotList.add(operSnapshot); + DatastoreSnapshotList snapshotList = new DatastoreSnapshotList(Arrays.asList(configSnapshot, operSnapshot)); - File backupFile = new File(restoreDirectoryFile, "backup"); - try(FileOutputStream fos = new FileOutputStream(backupFile)) { + try (FileOutputStream fos = new FileOutputStream(backupFile)) { SerializationUtils.serialize(snapshotList, fos); } - DatastoreSnapshotRestore.createInstance(restoreDirectoryPath); + DatastoreSnapshotRestore instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath); - verifySnapshot(configSnapshot, DatastoreSnapshotRestore.instance().getAndRemove("config")); - verifySnapshot(operSnapshot, DatastoreSnapshotRestore.instance().getAndRemove("oper")); + assertDatastoreSnapshotEquals(configSnapshot, instance.getAndRemove("config")); + assertDatastoreSnapshotEquals(operSnapshot, instance.getAndRemove("oper")); - assertNull("DatastoreSnapshot was not removed", DatastoreSnapshotRestore.instance().getAndRemove("config")); + assertNull("DatastoreSnapshot was not removed", instance.getAndRemove("config")); assertFalse(backupFile + " was not deleted", backupFile.exists()); - DatastoreSnapshotRestore.removeInstance(); - DatastoreSnapshotRestore.createInstance("target/does-not-exist"); - assertNull("Expected null DatastoreSnapshot", DatastoreSnapshotRestore.instance().getAndRemove("config")); - assertNull("Expected null DatastoreSnapshot", DatastoreSnapshotRestore.instance().getAndRemove("oper")); + instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath); + assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("config")); + assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("oper")); } - private static void verifySnapshot(DatastoreSnapshot expected, DatastoreSnapshot actual) { + private static void assertDatastoreSnapshotEquals(DatastoreSnapshot expected, DatastoreSnapshot actual) { assertNotNull("DatastoreSnapshot is null", actual); assertEquals("getType", expected.getType(), actual.getType()); - assertTrue("ShardManager snapshots don't match", Objects.deepEquals(expected.getShardManagerSnapshot(), - actual.getShardManagerSnapshot())); + + if (expected.getShardManagerSnapshot() == null) { + assertNull("Expected null ShardManagerSnapshot", actual.getShardManagerSnapshot()); + } else { + assertEquals("ShardManagerSnapshot", expected.getShardManagerSnapshot().getShardList(), + actual.getShardManagerSnapshot().getShardList()); + } + assertEquals("ShardSnapshots size", expected.getShardSnapshots().size(), actual.getShardSnapshots().size()); - for(int i = 0; i < expected.getShardSnapshots().size(); i++) { + for (int i = 0; i < expected.getShardSnapshots().size(); i++) { assertEquals("ShardSnapshot " + (i + 1) + " name", expected.getShardSnapshots().get(i).getName(), actual.getShardSnapshots().get(i).getName()); - assertArrayEquals("ShardSnapshot " + (i + 1) + " snapshot", expected.getShardSnapshots().get(i).getSnapshot(), - actual.getShardSnapshots().get(i).getSnapshot()); + assertSnapshotEquals("ShardSnapshot " + (i + 1) + " snapshot", + expected.getShardSnapshots().get(i).getSnapshot(), actual.getShardSnapshots().get(i).getSnapshot()); } } + + private static void assertSnapshotEquals(String prefix, Snapshot expected, Snapshot actual) { + assertEquals(prefix + " lastIndex", expected.getLastIndex(), actual.getLastIndex()); + assertEquals(prefix + " lastTerm", expected.getLastTerm(), actual.getLastTerm()); + assertEquals(prefix + " lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex()); + assertEquals(prefix + " lastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm()); + assertEquals(prefix + " unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries()); + assertEquals(prefix + " electionTerm", expected.getElectionTerm(), actual.getElectionTerm()); + assertEquals(prefix + " electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor()); + assertEquals(prefix + " Root node", ((ShardSnapshotState)expected.getState()).getSnapshot().getRootNode(), + ((ShardSnapshotState)actual.getState()).getSnapshot().getRootNode()); + } + + private static ShardManagerSnapshot newShardManagerSnapshot(String... shards) { + return new ShardManagerSnapshot(Arrays.asList(shards)); + } + + private static Snapshot newSnapshot(YangInstanceIdentifier path, NormalizedNode node) + 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); + + return Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)), + Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null); + } }