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.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
42 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
43 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
46 * Unit tests for backwards compatibility with pre-Lithium versions.
48 * @author Thomas Pantelis
50 public class PreLithiumShardTest extends AbstractShardTest {
52 private static CompositeModificationPayload newLegacyPayload(final Modification... mods) {
53 MutableCompositeModification compMod = new MutableCompositeModification(DataStoreVersions.HELIUM_2_VERSION);
54 for(Modification mod: mods) {
55 compMod.addModification(mod);
58 return new CompositeModificationPayload(compMod.toSerializable());
61 private static CompositeModificationByteStringPayload newLegacyByteStringPayload(final Modification... mods) {
62 MutableCompositeModification compMod = new MutableCompositeModification(DataStoreVersions.HELIUM_2_VERSION);
63 for(Modification mod: mods) {
64 compMod.addModification(mod);
67 return new CompositeModificationByteStringPayload(compMod.toSerializable());
71 public void testApplyHelium2VersionSnapshot() throws Exception {
72 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
73 "testApplyHelium2VersionSnapshot");
75 NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec(SCHEMA_CONTEXT);
77 DataTree store = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
78 store.setSchemaContext(SCHEMA_CONTEXT);
80 writeToStore(store, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
82 YangInstanceIdentifier root = YangInstanceIdentifier.builder().build();
83 NormalizedNode<?,?> expected = readStore(store, root);
85 NormalizedNodeMessages.Container encode = codec.encode(expected);
87 Snapshot snapshot = Snapshot.create(encode.getNormalizedNode().toByteString().toByteArray(),
88 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4);
90 shard.underlyingActor().getRaftActorSnapshotCohort().applySnapshot(snapshot.getState());
92 NormalizedNode<?,?> actual = readStore(shard, root);
94 assertEquals("Root node", expected, actual);
96 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
100 public void testHelium2VersionApplyStateLegacy() throws Exception {
101 new ShardTestKit(getSystem()) {{
102 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
103 "testHelium2VersionApplyStateLegacy");
105 waitUntilLeader(shard);
107 NormalizedNode<?, ?> node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
109 ApplyState applyState = new ApplyState(null, "test", new ReplicatedLogImplEntry(1, 2,
110 newLegacyByteStringPayload(new WriteModification(TestModel.TEST_PATH, node))));
112 shard.underlyingActor().onReceiveCommand(applyState);
114 NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
115 assertEquals("Applied state", node, actual);
117 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
122 public void testHelium2VersionRecovery() throws Exception {
124 DataTree testStore = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
125 testStore.setSchemaContext(SCHEMA_CONTEXT);
127 writeToStore(testStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
129 NormalizedNode<?, ?> root = readStore(testStore, YangInstanceIdentifier.builder().build());
131 InMemorySnapshotStore.addSnapshot(shardID.toString(), Snapshot.create(
132 new NormalizedNodeToNodeCodec(SCHEMA_CONTEXT).encode(root).
133 getNormalizedNode().toByteString().toByteArray(),
134 Collections.<ReplicatedLogEntry>emptyList(), 0, 1, -1, -1));
136 InMemoryJournal.addEntry(shardID.toString(), 0, new String("Dummy data as snapshot sequence number is " +
137 "set to 0 in InMemorySnapshotStore and journal recovery seq number will start from 1"));
139 // Set up the InMemoryJournal.
141 InMemoryJournal.addEntry(shardID.toString(), 1, new ReplicatedLogImplEntry(0, 1, newLegacyPayload(
142 new WriteModification(TestModel.OUTER_LIST_PATH,
143 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build()))));
145 int nListEntries = 16;
146 Set<Integer> listEntryKeys = new HashSet<>();
149 // Add some CompositeModificationPayload entries
151 listEntryKeys.add(Integer.valueOf(i));
152 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
153 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
154 Modification mod = new MergeModification(path,
155 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
156 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
157 newLegacyPayload(mod)));
160 // Add some CompositeModificationByteStringPayload entries
161 for(; i <= nListEntries; i++) {
162 listEntryKeys.add(Integer.valueOf(i));
163 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
164 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
165 Modification mod = new MergeModification(path,
166 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
167 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
168 newLegacyByteStringPayload(mod)));
171 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2, new ApplyLogEntries(nListEntries));
173 testRecovery(listEntryKeys);