2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.compat;
10 import static org.junit.Assert.assertEquals;
11 import akka.actor.ActorRef;
12 import akka.actor.PoisonPill;
13 import akka.testkit.TestActorRef;
14 import java.util.Collections;
15 import java.util.HashSet;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.datastore.AbstractShardTest;
19 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
20 import org.opendaylight.controller.cluster.datastore.Shard;
21 import org.opendaylight.controller.cluster.datastore.ShardTestKit;
22 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
23 import org.opendaylight.controller.cluster.datastore.modification.Modification;
24 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
25 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
26 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
27 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
28 import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
29 import org.opendaylight.controller.cluster.raft.Snapshot;
30 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
31 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
32 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
33 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
34 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
35 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
36 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
37 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
38 import org.opendaylight.yangtools.util.StringIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
47 * Unit tests for backwards compatibility with pre-Lithium versions.
49 * @author Thomas Pantelis
51 public class PreLithiumShardTest extends AbstractShardTest {
53 private static CompositeModificationPayload newLegacyPayload(final Modification... mods) {
54 MutableCompositeModification compMod = new MutableCompositeModification(DataStoreVersions.HELIUM_2_VERSION);
55 for(Modification mod: mods) {
56 compMod.addModification(mod);
59 return new CompositeModificationPayload(compMod.toSerializable());
62 private static CompositeModificationByteStringPayload newLegacyByteStringPayload(final Modification... mods) {
63 MutableCompositeModification compMod = new MutableCompositeModification(DataStoreVersions.HELIUM_2_VERSION);
64 for(Modification mod: mods) {
65 compMod.addModification(mod);
68 return new CompositeModificationByteStringPayload(compMod.toSerializable());
72 public void testApplyHelium2VersionSnapshot() throws Exception {
73 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
74 "testApplyHelium2VersionSnapshot");
76 NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec(SCHEMA_CONTEXT);
78 DataTree store = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
79 store.setSchemaContext(SCHEMA_CONTEXT);
81 writeToStore(store, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
83 YangInstanceIdentifier root = YangInstanceIdentifier.EMPTY;
84 NormalizedNode<?,?> expected = readStore(store, root);
86 NormalizedNodeMessages.Container encode = codec.encode(expected);
88 Snapshot snapshot = Snapshot.create(encode.getNormalizedNode().toByteString().toByteArray(),
89 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4);
91 shard.underlyingActor().getRaftActorSnapshotCohort().applySnapshot(snapshot.getState());
93 NormalizedNode<?,?> actual = readStore(shard, root);
95 assertEquals("Root node", expected, actual);
97 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
101 public void testHelium2VersionApplyStateLegacy() throws Exception {
102 new ShardTestKit(getSystem()) {{
103 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
104 "testHelium2VersionApplyStateLegacy");
106 waitUntilLeader(shard);
108 NormalizedNode<?, ?> node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
110 ApplyState applyState = new ApplyState(null, new StringIdentifier("test"),
111 new ReplicatedLogImplEntry(1, 2,
112 newLegacyByteStringPayload(new WriteModification(TestModel.TEST_PATH, node))));
114 shard.underlyingActor().onReceiveCommand(applyState);
116 NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
117 assertEquals("Applied state", node, actual);
119 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
124 public void testHelium2VersionRecovery() throws Exception {
126 DataTree testStore = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
127 testStore.setSchemaContext(SCHEMA_CONTEXT);
129 writeToStore(testStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
131 NormalizedNode<?, ?> root = readStore(testStore, YangInstanceIdentifier.builder().build());
133 InMemorySnapshotStore.addSnapshot(shardID.toString(), Snapshot.create(
134 new NormalizedNodeToNodeCodec(SCHEMA_CONTEXT).encode(root).
135 getNormalizedNode().toByteString().toByteArray(),
136 Collections.<ReplicatedLogEntry>emptyList(), 0, 1, -1, -1));
138 InMemoryJournal.addEntry(shardID.toString(), 0, new String("Dummy data as snapshot sequence number is " +
139 "set to 0 in InMemorySnapshotStore and journal recovery seq number will start from 1"));
141 // Set up the InMemoryJournal.
143 InMemoryJournal.addEntry(shardID.toString(), 1, new ReplicatedLogImplEntry(0, 1, newLegacyPayload(
144 new WriteModification(TestModel.OUTER_LIST_PATH,
145 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build()))));
147 int nListEntries = 16;
148 Set<Integer> listEntryKeys = new HashSet<>();
151 // Add some CompositeModificationPayload entries
153 listEntryKeys.add(Integer.valueOf(i));
154 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
155 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
156 Modification mod = new MergeModification(path,
157 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
158 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
159 newLegacyPayload(mod)));
162 // Add some CompositeModificationByteStringPayload entries
163 for(; i <= nListEntries; i++) {
164 listEntryKeys.add(Integer.valueOf(i));
165 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
166 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
167 Modification mod = new MergeModification(path,
168 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
169 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
170 newLegacyByteStringPayload(mod)));
173 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2, new ApplyLogEntries(nListEntries));
175 testRecovery(listEntryKeys);