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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.