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