2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
17 import java.io.FileOutputStream;
18 import java.util.Arrays;
19 import java.util.Collections;
20 import org.apache.commons.lang3.SerializationUtils;
21 import org.junit.After;
22 import org.junit.Test;
23 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
24 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList;
25 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
26 import org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot;
27 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
28 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
29 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
30 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
31 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
32 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
33 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
34 import org.opendaylight.yangtools.yang.common.Uint64;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
43 * Unit tests for DatastoreSnapshotRestore.
45 * @author Thomas Pantelis
47 public class DatastoreSnapshotRestoreTest {
48 String restoreDirectoryPath = "target/DatastoreSnapshotRestoreTest-" + System.nanoTime();
49 File restoreDirectoryFile = new File(restoreDirectoryPath);
50 File backupFile = new File(restoreDirectoryFile, "backup");
53 public void tearDown() {
55 restoreDirectoryFile.delete();
59 public void test() throws Exception {
60 assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs());
62 final DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config",
63 newShardManagerSnapshot("config-one", "config-two"),
64 Arrays.asList(new DatastoreSnapshot.ShardSnapshot("config-one", newSnapshot(CarsModel.BASE_PATH,
65 CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima",
66 Uint64.valueOf(20000)),CarsModel.newCarEntry("sportage", Uint64.valueOf(30000)))))),
67 new DatastoreSnapshot.ShardSnapshot("config-two", newSnapshot(PeopleModel.BASE_PATH,
68 PeopleModel.emptyContainer()))));
70 DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper",
71 null, Arrays.asList(new DatastoreSnapshot.ShardSnapshot("oper-one", newSnapshot(TestModel.TEST_PATH,
72 ImmutableNodes.containerNode(TestModel.TEST_QNAME)))));
74 DatastoreSnapshotList snapshotList = new DatastoreSnapshotList(Arrays.asList(configSnapshot, operSnapshot));
76 try (FileOutputStream fos = new FileOutputStream(backupFile)) {
77 SerializationUtils.serialize(snapshotList, fos);
80 DatastoreSnapshotRestore instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
82 assertDatastoreSnapshotEquals(configSnapshot, instance.getAndRemove("config"));
83 assertDatastoreSnapshotEquals(operSnapshot, instance.getAndRemove("oper"));
85 assertNull("DatastoreSnapshot was not removed", instance.getAndRemove("config"));
87 assertFalse(backupFile + " was not deleted", backupFile.exists());
89 instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
90 assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("config"));
91 assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("oper"));
94 private static void assertDatastoreSnapshotEquals(final DatastoreSnapshot expected,
95 final DatastoreSnapshot actual) {
96 assertNotNull("DatastoreSnapshot is null", actual);
97 assertEquals("getType", expected.getType(), actual.getType());
99 if (expected.getShardManagerSnapshot() == null) {
100 assertNull("Expected null ShardManagerSnapshot", actual.getShardManagerSnapshot());
102 assertEquals("ShardManagerSnapshot", expected.getShardManagerSnapshot().getShardList(),
103 actual.getShardManagerSnapshot().getShardList());
106 assertEquals("ShardSnapshots size", expected.getShardSnapshots().size(), actual.getShardSnapshots().size());
107 for (int i = 0; i < expected.getShardSnapshots().size(); i++) {
108 assertEquals("ShardSnapshot " + (i + 1) + " name", expected.getShardSnapshots().get(i).getName(),
109 actual.getShardSnapshots().get(i).getName());
110 assertSnapshotEquals("ShardSnapshot " + (i + 1) + " snapshot",
111 expected.getShardSnapshots().get(i).getSnapshot(), actual.getShardSnapshots().get(i).getSnapshot());
115 private static void assertSnapshotEquals(final String prefix, final Snapshot expected, final Snapshot actual) {
116 assertEquals(prefix + " lastIndex", expected.getLastIndex(), actual.getLastIndex());
117 assertEquals(prefix + " lastTerm", expected.getLastTerm(), actual.getLastTerm());
118 assertEquals(prefix + " lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
119 assertEquals(prefix + " lastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
120 assertEquals(prefix + " unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries());
121 assertEquals(prefix + " electionTerm", expected.getElectionTerm(), actual.getElectionTerm());
122 assertEquals(prefix + " electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor());
123 assertEquals(prefix + " Root node", ((ShardSnapshotState)expected.getState()).getSnapshot().getRootNode(),
124 ((ShardSnapshotState)actual.getState()).getSnapshot().getRootNode());
127 private static ShardManagerSnapshot newShardManagerSnapshot(final String... shards) {
128 return new ShardManagerSnapshot(Arrays.asList(shards), Collections.emptyMap());
131 private static Snapshot newSnapshot(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node)
133 DataTree dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
134 SchemaContextHelper.full());
135 AbstractShardTest.writeToStore(dataTree, path, node);
136 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.empty());
138 return Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
139 Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null);