bbbd95fd08ad777f3462bed408f66d994d4ec773
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DatastoreSnapshotRestoreTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
15
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.math.BigInteger;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.Objects;
22 import org.apache.commons.lang3.SerializationUtils;
23 import org.junit.After;
24 import org.junit.Test;
25 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
26 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList;
27 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
28 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
29 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerSnapshot;
30 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
31 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
32 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
33 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
34 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
35 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
42
43
44 /**
45  * Unit tests for DatastoreSnapshotRestore.
46  *
47  * @author Thomas Pantelis
48  */
49 public class DatastoreSnapshotRestoreTest {
50     String restoreDirectoryPath = "target/DatastoreSnapshotRestoreTest-" + System.nanoTime();
51     File restoreDirectoryFile = new File(restoreDirectoryPath);
52     File backupFile = new File(restoreDirectoryFile, "backup");
53
54     @After
55     public void tearDown() {
56         backupFile.delete();
57         restoreDirectoryFile.delete();
58     }
59
60     @Test
61     public void test() throws Exception {
62         assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs());
63
64         final DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config",
65                 SerializationUtils.serialize(newShardManagerSnapshot("config-one", "config-two")),
66                 Arrays.asList(new DatastoreSnapshot.ShardSnapshot("config-one", newSnapshot(CarsModel.BASE_PATH,
67                         CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima",
68                             BigInteger.valueOf(20000L)),CarsModel.newCarEntry("sportage",
69                                 BigInteger.valueOf(30000L)))))),
70                         new DatastoreSnapshot.ShardSnapshot("config-two", newSnapshot(PeopleModel.BASE_PATH,
71                             PeopleModel.emptyContainer()))));
72
73         DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper",
74                 null, Arrays.asList(new DatastoreSnapshot.ShardSnapshot("oper-one", newSnapshot(TestModel.TEST_PATH,
75                         ImmutableNodes.containerNode(TestModel.TEST_QNAME)))));
76
77         DatastoreSnapshotList snapshotList = new DatastoreSnapshotList(Arrays.asList(configSnapshot, operSnapshot));
78
79         try (FileOutputStream fos = new FileOutputStream(backupFile)) {
80             SerializationUtils.serialize(snapshotList, fos);
81         }
82
83         DatastoreSnapshotRestore instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
84
85         assertDatastoreSnapshotEquals(configSnapshot, instance.getAndRemove("config"));
86         assertDatastoreSnapshotEquals(operSnapshot, instance.getAndRemove("oper"));
87
88         assertNull("DatastoreSnapshot was not removed", instance.getAndRemove("config"));
89
90         assertFalse(backupFile + " was not deleted", backupFile.exists());
91
92         instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
93         assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("config"));
94         assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("oper"));
95     }
96
97     private static void assertDatastoreSnapshotEquals(DatastoreSnapshot expected, DatastoreSnapshot actual) {
98         assertNotNull("DatastoreSnapshot is null", actual);
99         assertEquals("getType", expected.getType(), actual.getType());
100         assertTrue("ShardManager snapshots don't match", Objects.deepEquals(expected.getShardManagerSnapshot(),
101                 actual.getShardManagerSnapshot()));
102         assertEquals("ShardSnapshots size", expected.getShardSnapshots().size(), actual.getShardSnapshots().size());
103         for (int i = 0; i < expected.getShardSnapshots().size(); i++) {
104             assertEquals("ShardSnapshot " + (i + 1) + " name", expected.getShardSnapshots().get(i).getName(),
105                     actual.getShardSnapshots().get(i).getName());
106             assertSnapshotEquals("ShardSnapshot " + (i + 1) + " snapshot",
107                     expected.getShardSnapshots().get(i).getSnapshot(), actual.getShardSnapshots().get(i).getSnapshot());
108         }
109     }
110
111     private static void assertSnapshotEquals(String prefix, Snapshot expected, Snapshot actual) {
112         assertEquals(prefix + " lastIndex", expected.getLastIndex(), actual.getLastIndex());
113         assertEquals(prefix + " lastTerm", expected.getLastTerm(), actual.getLastTerm());
114         assertEquals(prefix + " lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
115         assertEquals(prefix + " lastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
116         assertEquals(prefix + " unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries());
117         assertEquals(prefix + " electionTerm", expected.getElectionTerm(), actual.getElectionTerm());
118         assertEquals(prefix + " electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor());
119         assertEquals(prefix + " Root node", ((ShardSnapshotState)expected.getState()).getSnapshot().getRootNode(),
120                 ((ShardSnapshotState)actual.getState()).getSnapshot().getRootNode());
121     }
122
123     private static ShardManagerSnapshot newShardManagerSnapshot(String... shards) {
124         return ShardManagerSnapshot.forShardList(Arrays.asList(shards));
125     }
126
127     private static Snapshot newSnapshot(YangInstanceIdentifier path, NormalizedNode<?, ?> node)
128             throws Exception {
129         DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
130         dataTree.setSchemaContext(SchemaContextHelper.full());
131         AbstractShardTest.writeToStore(dataTree, path, node);
132         NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
133
134         return Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
135                 Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null);
136     }
137 }