8c8e329e14df9c4e8e27eb9f217260f331cb08b2
[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         ShardManagerSnapshot legacyShardManagerSnapshot = newLegacyShardManagerSnapshot("config-one", "config-two");
61         DatastoreSnapshot legacyConfigSnapshot = new DatastoreSnapshot("config",
62                 SerializationUtils.serialize(legacyShardManagerSnapshot),
63                 Arrays.asList(newLegacyShardSnapshot("config-one", newLegacySnapshot(legacyConfigRoot1)),
64                     newLegacyShardSnapshot("config-two", newLegacySnapshot(legacyConfigRoot2))));
65
66         DatastoreSnapshot legacyOperSnapshot = new DatastoreSnapshot("oper",
67                 null, Arrays.asList(newLegacyShardSnapshot("oper-one", newLegacySnapshot(null))));
68
69         DatastoreSnapshotList legacy = new DatastoreSnapshotList(Arrays.asList(legacyConfigSnapshot,
70                 legacyOperSnapshot));
71
72         org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList cloned =
73             (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList)
74                 SerializationUtils.clone(legacy);
75
76         assertEquals("DatastoreSnapshotList size", 2, cloned.size());
77         assertDatastoreSnapshotEquals(legacyConfigSnapshot, cloned.get(0),
78                 new org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot(
79                         legacyShardManagerSnapshot.getShardList()),
80                 Optional.of(legacyConfigRoot1), Optional.of(legacyConfigRoot2));
81         assertDatastoreSnapshotEquals(legacyOperSnapshot, cloned.get(1),
82                 (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)null,
83                 Optional.empty());
84     }
85
86     @SuppressWarnings("unchecked")
87     private void assertDatastoreSnapshotEquals(DatastoreSnapshot legacy,
88             org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot actual,
89             org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot expShardMgrSnapshot,
90             Optional<NormalizedNode<?, ?>>... shardRoots) throws IOException {
91         assertEquals("Type", legacy.getType(), actual.getType());
92
93         if (legacy.getShardManagerSnapshot() == null) {
94             assertNull("Expected null ShardManagerSnapshot", actual.getShardManagerSnapshot());
95         } else {
96             org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot actualShardManagerSnapshot =
97                 (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)
98                     SerializationUtils.deserialize(legacy.getShardManagerSnapshot());
99             assertEquals("ShardManagerSnapshot", expShardMgrSnapshot.getShardList(),
100                     actualShardManagerSnapshot.getShardList());
101         }
102
103         assertEquals("ShardSnapshots size", legacy.getShardSnapshots().size(), actual.getShardSnapshots().size());
104
105         for (int i = 0; i < actual.getShardSnapshots().size(); i++) {
106             ShardSnapshot legacyShardSnapshot = legacy.getShardSnapshots().get(i);
107             org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot
108                 actualShardSnapshot = actual.getShardSnapshots().get(i);
109             assertEquals("Shard name", legacyShardSnapshot.getName(), actualShardSnapshot.getName());
110             assertSnapshotEquals((Snapshot) SerializationUtils.deserialize(legacyShardSnapshot.getSnapshot()),
111                     shardRoots[i], actualShardSnapshot.getSnapshot());
112         }
113     }
114
115     private static void assertSnapshotEquals(Snapshot expected, Optional<NormalizedNode<?, ?>> expRoot,
116             org.opendaylight.controller.cluster.raft.persisted.Snapshot actual) throws IOException {
117         assertEquals("lastIndex", expected.getLastIndex(), actual.getLastIndex());
118         assertEquals("lastTerm", expected.getLastTerm(), actual.getLastTerm());
119         assertEquals("lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
120         assertEquals("lastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
121         assertEquals("unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries());
122         assertEquals("electionTerm", expected.getElectionTerm(), actual.getElectionTerm());
123         assertEquals("electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor());
124
125         if (expRoot.isPresent()) {
126             ShardDataTreeSnapshot actualSnapshot = ((ShardSnapshotState)actual.getState()).getSnapshot();
127             assertEquals("ShardDataTreeSnapshot type", MetadataShardDataTreeSnapshot.class, actualSnapshot.getClass());
128             assertTrue("Expected root node present", actualSnapshot.getRootNode().isPresent());
129             assertEquals("Root node", expRoot.get(), actualSnapshot.getRootNode().get());
130         } else {
131             assertEquals("State type", EmptyState.class, actual.getState().getClass());
132         }
133     }
134
135     private static ShardManagerSnapshot newLegacyShardManagerSnapshot(String... shards) {
136         return ShardManagerSnapshot.forShardList(Arrays.asList(shards));
137     }
138
139     private static DatastoreSnapshot.ShardSnapshot newLegacyShardSnapshot(String name,
140             org.opendaylight.controller.cluster.raft.Snapshot snapshot) {
141         return new DatastoreSnapshot.ShardSnapshot(name, SerializationUtils.serialize(snapshot));
142     }
143
144     private static Snapshot newLegacySnapshot(NormalizedNode<?, ?> root)
145             throws Exception {
146         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
147         if (root != null) {
148             MetadataShardDataTreeSnapshot snapshot = new MetadataShardDataTreeSnapshot(root);
149             try (final DataOutputStream dos = new DataOutputStream(bos)) {
150                 PayloadVersion.BORON.writeTo(dos);
151                 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
152                     oos.writeObject(snapshot);
153                 }
154             }
155         }
156
157         return Snapshot.create(bos.toByteArray(), Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1,
158                 "member-1", null);
159     }
160
161     private static NormalizedNode<?, ?> toRootNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node)
162             throws DataValidationFailedException {
163         DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
164         dataTree.setSchemaContext(SchemaContextHelper.full());
165         AbstractShardTest.writeToStore(dataTree, path, node);
166         return AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
167     }
168 }

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