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