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.Shard;
20 import org.opendaylight.controller.cluster.datastore.ShardTestKit;
21 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
22 import org.opendaylight.controller.cluster.datastore.modification.Modification;
23 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
24 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
25 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
26 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
27 import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
28 import org.opendaylight.controller.cluster.raft.Snapshot;
29 import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries;
30 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
31 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
32 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
33 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
34 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
35 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
36 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
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.TreeType;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
45 * Unit tests for backwards compatibility with pre-Lithium versions.
47 * @author Thomas Pantelis
49 public class PreLithiumShardTest extends AbstractShardTest {
51 private static CompositeModificationPayload newLegacyPayload(final Modification... mods) {
52 MutableCompositeModification compMod = new MutableCompositeModification();
53 for(Modification mod: mods) {
54 compMod.addModification(mod);
57 return new CompositeModificationPayload(compMod.toSerializable());
60 private static CompositeModificationByteStringPayload newLegacyByteStringPayload(final Modification... mods) {
61 MutableCompositeModification compMod = new MutableCompositeModification();
62 for(Modification mod: mods) {
63 compMod.addModification(mod);
66 return new CompositeModificationByteStringPayload(compMod.toSerializable());
70 public void testApplyHelium2VersionSnapshot() throws Exception {
71 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
72 "testApplyHelium2VersionSnapshot");
74 NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec(SCHEMA_CONTEXT);
76 DataTree store = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
77 store.setSchemaContext(SCHEMA_CONTEXT);
79 writeToStore(store, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
81 YangInstanceIdentifier root = YangInstanceIdentifier.builder().build();
82 NormalizedNode<?,?> expected = readStore(store, root);
84 NormalizedNodeMessages.Container encode = codec.encode(expected);
86 Snapshot snapshot = Snapshot.create(encode.getNormalizedNode().toByteString().toByteArray(),
87 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4);
89 shard.underlyingActor().getRaftActorSnapshotCohort().applySnapshot(snapshot.getState());
91 NormalizedNode<?,?> actual = readStore(shard, root);
93 assertEquals("Root node", expected, actual);
95 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
99 public void testHelium2VersionApplyStateLegacy() throws Exception {
100 new ShardTestKit(getSystem()) {{
101 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
102 "testHelium2VersionApplyStateLegacy");
104 waitUntilLeader(shard);
106 NormalizedNode<?, ?> node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
108 ApplyState applyState = new ApplyState(null, "test", new ReplicatedLogImplEntry(1, 2,
109 newLegacyByteStringPayload(new WriteModification(TestModel.TEST_PATH, node))));
111 shard.underlyingActor().onReceiveCommand(applyState);
113 NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
114 assertEquals("Applied state", node, actual);
116 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
121 public void testHelium2VersionRecovery() throws Exception {
123 DataTree testStore = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
124 testStore.setSchemaContext(SCHEMA_CONTEXT);
126 writeToStore(testStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
128 NormalizedNode<?, ?> root = readStore(testStore, YangInstanceIdentifier.builder().build());
130 InMemorySnapshotStore.addSnapshot(shardID.toString(), Snapshot.create(
131 new NormalizedNodeToNodeCodec(SCHEMA_CONTEXT).encode(root).
132 getNormalizedNode().toByteString().toByteArray(),
133 Collections.<ReplicatedLogEntry>emptyList(), 0, 1, -1, -1));
135 InMemoryJournal.addEntry(shardID.toString(), 0, new String("Dummy data as snapshot sequence number is " +
136 "set to 0 in InMemorySnapshotStore and journal recovery seq number will start from 1"));
138 // Set up the InMemoryJournal.
140 InMemoryJournal.addEntry(shardID.toString(), 1, new ReplicatedLogImplEntry(0, 1, newLegacyPayload(
141 new WriteModification(TestModel.OUTER_LIST_PATH,
142 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build()))));
144 int nListEntries = 16;
145 Set<Integer> listEntryKeys = new HashSet<>();
148 // Add some CompositeModificationPayload entries
150 listEntryKeys.add(Integer.valueOf(i));
151 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
152 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
153 Modification mod = new MergeModification(path,
154 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
155 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
156 newLegacyPayload(mod)));
159 // Add some CompositeModificationByteStringPayload entries
160 for(; i <= nListEntries; i++) {
161 listEntryKeys.add(Integer.valueOf(i));
162 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
163 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
164 Modification mod = new MergeModification(path,
165 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
166 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
167 newLegacyByteStringPayload(mod)));
170 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2, new ApplyLogEntries(nListEntries));
172 testRecovery(listEntryKeys);