2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
37 public class ShardRecoveryCoordinatorTest {
39 private ShardDataTree peopleDataTree;
40 private SchemaContext peopleSchemaContext;
41 private SchemaContext carsSchemaContext;
45 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
46 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
48 peopleDataTree = new ShardDataTree(peopleSchemaContext);
52 public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
53 ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
54 coordinator.startLogRecoveryBatch(10);
56 coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
57 } catch(SchemaValidationFailedException e){
58 fail("SchemaValidationFailedException should not happen if pruning is done");
61 coordinator.applyCurrentLogRecoveryBatch();
65 public void testAppendRecoveredLogEntryModificationPayload() throws IOException {
66 ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
67 coordinator.startLogRecoveryBatch(10);
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");
78 public void testAppendRecoveredLogEntryCompositeModificationPayload() throws IOException {
79 ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
80 coordinator.startLogRecoveryBatch(10);
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");
91 public void testAppendRecoveredLogEntryCompositeModificationByteStringPayload() throws IOException {
92 ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
93 coordinator.startLogRecoveryBatch(10);
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");
102 assertEquals(false, readCars(peopleDataTree).isPresent());
106 public void testApplyRecoverySnapshot(){
107 ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree , peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
108 coordinator.startLogRecoveryBatch(10);
110 coordinator.applyRecoverySnapshot(createSnapshot());
112 assertEquals(false, readCars(peopleDataTree).isPresent());
113 assertEquals(true, readPeople(peopleDataTree).isPresent());
118 public void testApplyCurrentLogRecoveryBatch(){
119 ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
120 coordinator.startLogRecoveryBatch(10);
123 coordinator.applyCurrentLogRecoveryBatch();
124 } catch(IllegalArgumentException e){
125 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
129 private DataTreeCandidateTip createCar(){
130 TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
131 dataTree.setSchemaContext(carsSchemaContext);
133 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
135 DataTreeModification modification = snapshot.newModification();
137 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
139 return dataTree.prepare(modification);
142 private Optional<NormalizedNode<?,?>> readCars(ShardDataTree shardDataTree){
143 TipProducingDataTree dataTree = shardDataTree.getDataTree();
144 dataTree.setSchemaContext(peopleSchemaContext);
146 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
148 DataTreeModification modification = snapshot.newModification();
150 return modification.readNode(CarsModel.BASE_PATH);
153 private Optional<NormalizedNode<?,?>> readPeople(ShardDataTree shardDataTree){
154 TipProducingDataTree dataTree = shardDataTree.getDataTree();
155 dataTree.setSchemaContext(peopleSchemaContext);
157 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
159 DataTreeModification modification = snapshot.newModification();
161 return modification.readNode(PeopleModel.BASE_PATH);
166 private byte[] createSnapshot(){
167 TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
168 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
170 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
172 DataTreeModification modification = snapshot.newModification();
174 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
175 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
177 DataTreeCandidateTip prepare = dataTree.prepare(modification);
179 dataTree.commit(prepare);
181 snapshot = dataTree.takeSnapshot();
183 modification = snapshot.newModification();
185 Optional<NormalizedNode<?, ?>> optional = modification.readNode(YangInstanceIdentifier.EMPTY);
187 byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get());