Bug 4564: Implement datastore restore from backup file
[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.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Objects;
21 import org.apache.commons.lang3.SerializationUtils;
22 import org.junit.After;
23 import org.junit.Test;
24 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
25 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
26 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshotList;
27
28 /**
29  * Unit tests for DatastoreSnapshotRestore.
30  *
31  * @author Thomas Pantelis
32  */
33 public class DatastoreSnapshotRestoreTest {
34     String restoreDirectoryPath = "target/DatastoreSnapshotRestoreTest-" + System.nanoTime();
35     File restoreDirectoryFile = new File(restoreDirectoryPath);
36     File backupFile = new File(restoreDirectoryFile, "backup");
37
38     @After
39     public void tearDown() {
40         backupFile.delete();
41         restoreDirectoryFile.delete();
42     }
43
44     @Test
45     public void test() throws Exception {
46         assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs());
47
48         List<ShardSnapshot> shardSnapshots = new ArrayList<>();
49         shardSnapshots.add(new ShardSnapshot("cars", new byte[]{1,2}));
50         shardSnapshots.add(new ShardSnapshot("people", new byte[]{3,4}));
51         DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config", null, shardSnapshots );
52
53         shardSnapshots = new ArrayList<>();
54         shardSnapshots.add(new ShardSnapshot("cars", new byte[]{5,6}));
55         shardSnapshots.add(new ShardSnapshot("people", new byte[]{7,8}));
56         shardSnapshots.add(new ShardSnapshot("bikes", new byte[]{9,0}));
57         DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper", null, shardSnapshots );
58
59         DatastoreSnapshotList snapshotList = new DatastoreSnapshotList();
60         snapshotList.add(configSnapshot);
61         snapshotList.add(operSnapshot);
62
63         File backupFile = new File(restoreDirectoryFile, "backup");
64         try(FileOutputStream fos = new FileOutputStream(backupFile)) {
65             SerializationUtils.serialize(snapshotList, fos);
66         }
67
68         DatastoreSnapshotRestore.createInstance(restoreDirectoryPath);
69
70         verifySnapshot(configSnapshot, DatastoreSnapshotRestore.instance().getAndRemove("config"));
71         verifySnapshot(operSnapshot, DatastoreSnapshotRestore.instance().getAndRemove("oper"));
72
73         assertNull("DatastoreSnapshot was not removed", DatastoreSnapshotRestore.instance().getAndRemove("config"));
74
75         assertFalse(backupFile + " was not deleted", backupFile.exists());
76
77         DatastoreSnapshotRestore.removeInstance();
78         DatastoreSnapshotRestore.createInstance("target/does-not-exist");
79         assertNull("Expected null DatastoreSnapshot", DatastoreSnapshotRestore.instance().getAndRemove("config"));
80         assertNull("Expected null DatastoreSnapshot", DatastoreSnapshotRestore.instance().getAndRemove("oper"));
81     }
82
83     private void verifySnapshot(DatastoreSnapshot expected, DatastoreSnapshot actual) {
84         assertNotNull("DatastoreSnapshot is null", actual);
85         assertEquals("getType", expected.getType(), actual.getType());
86         assertTrue("ShardManager snapshots don't match", Objects.deepEquals(expected.getShardManagerSnapshot(),
87                 actual.getShardManagerSnapshot()));
88         assertEquals("ShardSnapshots size", expected.getShardSnapshots().size(), actual.getShardSnapshots().size());
89         for(int i = 0; i < expected.getShardSnapshots().size(); i++) {
90             assertEquals("ShardSnapshot " + (i + 1) + " name", expected.getShardSnapshots().get(i).getName(),
91                     actual.getShardSnapshots().get(i).getName());
92             assertArrayEquals("ShardSnapshot " + (i + 1) + " snapshot", expected.getShardSnapshots().get(i).getSnapshot(),
93                     actual.getShardSnapshots().get(i).getSnapshot());
94         }
95     }
96 }