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