BUG 2970 : Recovery fails with SchemaValidationException when removing modules
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardRecoveryCoordinatorTest.java
1 /*
2  * Copyright (c) 2015 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13 import com.google.common.base.Optional;
14 import java.io.IOException;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.modification.ModificationPayload;
18 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
19 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
20 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
23 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
24 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
25 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
32 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
33 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.slf4j.LoggerFactory;
36
37 public class ShardRecoveryCoordinatorTest {
38
39     private ShardDataTree peopleDataTree;
40     private SchemaContext peopleSchemaContext;
41     private SchemaContext carsSchemaContext;
42
43     @Before
44     public void setUp(){
45         peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
46         carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
47
48         peopleDataTree = new ShardDataTree(peopleSchemaContext);
49     }
50
51     @Test
52     public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
53         ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
54         coordinator.startLogRecoveryBatch(10);
55         try {
56             coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
57         } catch(SchemaValidationFailedException e){
58             fail("SchemaValidationFailedException should not happen if pruning is done");
59         }
60
61         coordinator.applyCurrentLogRecoveryBatch();
62     }
63
64     @Test
65     public void testAppendRecoveredLogEntryModificationPayload() throws IOException {
66         ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
67         coordinator.startLogRecoveryBatch(10);
68         try {
69             MutableCompositeModification modification  = new MutableCompositeModification((short) 1);
70             modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create()));
71             coordinator.appendRecoveredLogEntry(new ModificationPayload(modification));
72         } catch(SchemaValidationFailedException e){
73             fail("SchemaValidationFailedException should not happen if pruning is done");
74         }
75     }
76
77     @Test
78     public void testAppendRecoveredLogEntryCompositeModificationPayload() throws IOException {
79         ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
80         coordinator.startLogRecoveryBatch(10);
81         try {
82             MutableCompositeModification modification  = new MutableCompositeModification((short) 1);
83             modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create()));
84             coordinator.appendRecoveredLogEntry(new CompositeModificationPayload(modification.toSerializable()));
85         } catch(SchemaValidationFailedException e){
86             fail("SchemaValidationFailedException should not happen if pruning is done");
87         }
88     }
89
90     @Test
91     public void testAppendRecoveredLogEntryCompositeModificationByteStringPayload() throws IOException {
92         ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
93         coordinator.startLogRecoveryBatch(10);
94         try {
95             MutableCompositeModification modification  = new MutableCompositeModification((short) 1);
96             modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create()));
97             coordinator.appendRecoveredLogEntry(new CompositeModificationByteStringPayload(modification.toSerializable()));
98         } catch(SchemaValidationFailedException e){
99             fail("SchemaValidationFailedException should not happen if pruning is done");
100         }
101
102         assertEquals(false, readCars(peopleDataTree).isPresent());
103     }
104
105     @Test
106     public void testApplyRecoverySnapshot(){
107         ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree , peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
108         coordinator.startLogRecoveryBatch(10);
109
110         coordinator.applyRecoverySnapshot(createSnapshot());
111
112         assertEquals(false, readCars(peopleDataTree).isPresent());
113         assertEquals(true, readPeople(peopleDataTree).isPresent());
114     }
115
116
117     @Test
118     public void testApplyCurrentLogRecoveryBatch(){
119         ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
120         coordinator.startLogRecoveryBatch(10);
121
122         try {
123             coordinator.applyCurrentLogRecoveryBatch();
124         } catch(IllegalArgumentException e){
125             fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
126         }
127     }
128
129     private DataTreeCandidateTip createCar(){
130         TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
131         dataTree.setSchemaContext(carsSchemaContext);
132
133         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
134
135         DataTreeModification modification = snapshot.newModification();
136
137         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
138
139         return dataTree.prepare(modification);
140     }
141
142     private Optional<NormalizedNode<?,?>> readCars(ShardDataTree shardDataTree){
143         TipProducingDataTree dataTree = shardDataTree.getDataTree();
144         dataTree.setSchemaContext(peopleSchemaContext);
145
146         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
147
148         DataTreeModification modification = snapshot.newModification();
149
150         return modification.readNode(CarsModel.BASE_PATH);
151     }
152
153     private Optional<NormalizedNode<?,?>> readPeople(ShardDataTree shardDataTree){
154         TipProducingDataTree dataTree = shardDataTree.getDataTree();
155         dataTree.setSchemaContext(peopleSchemaContext);
156
157         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
158
159         DataTreeModification modification = snapshot.newModification();
160
161         return modification.readNode(PeopleModel.BASE_PATH);
162     }
163
164
165
166     private byte[] createSnapshot(){
167         TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
168         dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
169
170         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
171
172         DataTreeModification modification = snapshot.newModification();
173
174         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
175         modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
176
177         DataTreeCandidateTip prepare = dataTree.prepare(modification);
178
179         dataTree.commit(prepare);
180
181         snapshot = dataTree.takeSnapshot();
182
183         modification = snapshot.newModification();
184
185         Optional<NormalizedNode<?, ?>> optional = modification.readNode(YangInstanceIdentifier.EMPTY);
186
187         byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get());
188
189         return bytes;
190
191
192     }
193 }