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