Bug 7521: Convert DatastoreSnapshot.ShardSnapshot to store Snapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / messages / DatastoreSnapshotListTest.java
1 /*
2  * Copyright (c) 2017 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.messages;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.ByteArrayOutputStream;
15 import java.io.DataOutputStream;
16 import java.io.IOException;
17 import java.io.ObjectOutputStream;
18 import java.math.BigInteger;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.Optional;
22 import org.apache.commons.lang.SerializationUtils;
23 import org.junit.Test;
24 import org.opendaylight.controller.cluster.datastore.AbstractShardTest;
25 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
26 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
27 import org.opendaylight.controller.cluster.datastore.persisted.PayloadVersion;
28 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
29 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
30 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerSnapshot;
31 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
32 import org.opendaylight.controller.cluster.raft.Snapshot;
33 import org.opendaylight.controller.cluster.raft.persisted.EmptyState;
34 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
35 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
36 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
42 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
43
44 /**
45  * Unit tests for DatastoreSnapshotList.
46  *
47  * @author Thomas Pantelis
48  */
49 @Deprecated
50 public class DatastoreSnapshotListTest {
51     @Test
52     public void testSerialization() throws Exception {
53         NormalizedNode<?, ?> legacyConfigRoot1 = toRootNode(CarsModel.BASE_PATH,
54                 CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima",
55                         BigInteger.valueOf(20000L)),CarsModel.newCarEntry("sportage",
56                             BigInteger.valueOf(30000L)))));
57
58         NormalizedNode<?, ?> legacyConfigRoot2 = toRootNode(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
59
60         DatastoreSnapshot legacyConfigSnapshot = new DatastoreSnapshot("config",
61                 SerializationUtils.serialize(newLegacyShardManagerSnapshot("config-one", "config-two")),
62                 Arrays.asList(newLegacyShardSnapshot("config-one", newLegacySnapshot(legacyConfigRoot1)),
63                     newLegacyShardSnapshot("config-two", newLegacySnapshot(legacyConfigRoot2))));
64
65         DatastoreSnapshot legacyOperSnapshot = new DatastoreSnapshot("oper",
66                 null, Arrays.asList(newLegacyShardSnapshot("oper-one", newLegacySnapshot(null))));
67
68         DatastoreSnapshotList legacy = new DatastoreSnapshotList(Arrays.asList(legacyConfigSnapshot,
69                 legacyOperSnapshot));
70
71         org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList cloned =
72             (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList)
73                 SerializationUtils.clone(legacy);
74
75         assertEquals("DatastoreSnapshotList size", 2, cloned.size());
76         assertDatastoreSnapshotEquals(legacyConfigSnapshot, cloned.get(0), Optional.of(legacyConfigRoot1),
77                 Optional.of(legacyConfigRoot2));
78         assertDatastoreSnapshotEquals(legacyOperSnapshot, cloned.get(1), Optional.empty());
79     }
80
81     private void assertDatastoreSnapshotEquals(DatastoreSnapshot legacy,
82             org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot actual,
83             Optional<NormalizedNode<?, ?>>... shardRoots) throws IOException {
84         assertEquals("Type", legacy.getType(), actual.getType());
85
86         if (legacy.getShardManagerSnapshot() == null) {
87             assertNull("Expected null ShardManagerSnapshot", actual.getShardManagerSnapshot());
88         } else {
89             ShardManagerSnapshot legacyShardManagerSnapshot =
90                     (ShardManagerSnapshot) SerializationUtils.deserialize(legacy.getShardManagerSnapshot());
91             ShardManagerSnapshot actualShardManagerSnapshot =
92                     (ShardManagerSnapshot) SerializationUtils.deserialize(actual.getShardManagerSnapshot());
93             assertEquals("ShardManagerSnapshot", legacyShardManagerSnapshot.getShardList(),
94                     actualShardManagerSnapshot.getShardList());
95         }
96
97         assertEquals("ShardSnapshots size", legacy.getShardSnapshots().size(), actual.getShardSnapshots().size());
98
99         for (int i = 0; i < actual.getShardSnapshots().size(); i++) {
100             ShardSnapshot legacyShardSnapshot = legacy.getShardSnapshots().get(i);
101             org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot
102                 actualShardSnapshot = actual.getShardSnapshots().get(i);
103             assertEquals("Shard name", legacyShardSnapshot.getName(), actualShardSnapshot.getName());
104             assertSnapshotEquals((Snapshot) SerializationUtils.deserialize(legacyShardSnapshot.getSnapshot()),
105                     shardRoots[i], actualShardSnapshot.getSnapshot());
106         }
107     }
108
109     private static void assertSnapshotEquals(Snapshot expected, Optional<NormalizedNode<?, ?>> expRoot,
110             org.opendaylight.controller.cluster.raft.persisted.Snapshot actual) throws IOException {
111         assertEquals("lastIndex", expected.getLastIndex(), actual.getLastIndex());
112         assertEquals("lastTerm", expected.getLastTerm(), actual.getLastTerm());
113         assertEquals("lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
114         assertEquals("lastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
115         assertEquals("unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries());
116         assertEquals("electionTerm", expected.getElectionTerm(), actual.getElectionTerm());
117         assertEquals("electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor());
118
119         if (expRoot.isPresent()) {
120             ShardDataTreeSnapshot actualSnapshot = ((ShardSnapshotState)actual.getState()).getSnapshot();
121             assertEquals("ShardDataTreeSnapshot type", MetadataShardDataTreeSnapshot.class, actualSnapshot.getClass());
122             assertTrue("Expected root node present", actualSnapshot.getRootNode().isPresent());
123             assertEquals("Root node", expRoot.get(), actualSnapshot.getRootNode().get());
124         } else {
125             assertEquals("State type", EmptyState.class, actual.getState().getClass());
126         }
127     }
128
129     private static ShardManagerSnapshot newLegacyShardManagerSnapshot(String... shards) {
130         return ShardManagerSnapshot.forShardList(Arrays.asList(shards));
131     }
132
133     private static DatastoreSnapshot.ShardSnapshot newLegacyShardSnapshot(String name,
134             org.opendaylight.controller.cluster.raft.Snapshot snapshot) {
135         return new DatastoreSnapshot.ShardSnapshot(name, SerializationUtils.serialize(snapshot));
136     }
137
138     private static Snapshot newLegacySnapshot(NormalizedNode<?, ?> root)
139             throws Exception {
140         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
141         if (root != null) {
142             MetadataShardDataTreeSnapshot snapshot = new MetadataShardDataTreeSnapshot(root);
143             try (final DataOutputStream dos = new DataOutputStream(bos)) {
144                 PayloadVersion.BORON.writeTo(dos);
145                 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
146                     oos.writeObject(snapshot);
147                 }
148             }
149         }
150
151         return Snapshot.create(bos.toByteArray(), Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1,
152                 "member-1", null);
153     }
154
155     private static NormalizedNode<?, ?> toRootNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node)
156             throws DataValidationFailedException {
157         DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
158         dataTree.setSchemaContext(SchemaContextHelper.full());
159         AbstractShardTest.writeToStore(dataTree, path, node);
160         return AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
161     }
162 }

©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.