c9797b5414589018b83baeffeb307b5fec81585f
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / compat / PreLithiumShardTest.java
1 /*
2  * Copyright (c) 2015 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.compat;
9
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;
16 import java.util.Set;
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;
44
45 /**
46  * Unit tests for backwards compatibility with pre-Lithium versions.
47  *
48  * @author Thomas Pantelis
49  */
50 public class PreLithiumShardTest extends AbstractShardTest {
51
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);
56         }
57
58         return new CompositeModificationPayload(compMod.toSerializable());
59     }
60
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);
65         }
66
67         return new CompositeModificationByteStringPayload(compMod.toSerializable());
68     }
69
70     @Test
71     public void testApplyHelium2VersionSnapshot() throws Exception {
72         TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
73                 "testApplyHelium2VersionSnapshot");
74
75         NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec(SCHEMA_CONTEXT);
76
77         DataTree store = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
78         store.setSchemaContext(SCHEMA_CONTEXT);
79
80         writeToStore(store, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
81
82         YangInstanceIdentifier root = YangInstanceIdentifier.builder().build();
83         NormalizedNode<?,?> expected = readStore(store, root);
84
85         NormalizedNodeMessages.Container encode = codec.encode(expected);
86
87         Snapshot snapshot = Snapshot.create(encode.getNormalizedNode().toByteString().toByteArray(),
88                 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4);
89
90         shard.underlyingActor().getRaftActorSnapshotCohort().applySnapshot(snapshot.getState());
91
92         NormalizedNode<?,?> actual = readStore(shard, root);
93
94         assertEquals("Root node", expected, actual);
95
96         shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
97     }
98
99     @Test
100     public void testHelium2VersionApplyStateLegacy() throws Exception {
101         new ShardTestKit(getSystem()) {{
102             TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
103                     "testHelium2VersionApplyStateLegacy");
104
105             waitUntilLeader(shard);
106
107             NormalizedNode<?, ?> node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
108
109             ApplyState applyState = new ApplyState(null, "test", new ReplicatedLogImplEntry(1, 2,
110                     newLegacyByteStringPayload(new WriteModification(TestModel.TEST_PATH, node))));
111
112             shard.underlyingActor().onReceiveCommand(applyState);
113
114             NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
115             assertEquals("Applied state", node, actual);
116
117             shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
118         }};
119     }
120
121     @Test
122     public void testHelium2VersionRecovery() throws Exception {
123
124         DataTree testStore = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
125         testStore.setSchemaContext(SCHEMA_CONTEXT);
126
127         writeToStore(testStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
128
129         NormalizedNode<?, ?> root = readStore(testStore, YangInstanceIdentifier.builder().build());
130
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));
135
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"));
138
139         // Set up the InMemoryJournal.
140
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()))));
144
145         int nListEntries = 16;
146         Set<Integer> listEntryKeys = new HashSet<>();
147         int i = 1;
148
149         // Add some CompositeModificationPayload entries
150         for(; i <= 8; i++) {
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)));
158         }
159
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)));
169         }
170
171         InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2, new ApplyLogEntries(nListEntries));
172
173         testRecovery(listEntryKeys);
174     }
175 }