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;
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;
38 public class ShardRecoveryCoordinatorTest {
40 private ShardDataTree peopleDataTree;
41 private SchemaContext peopleSchemaContext;
42 private SchemaContext carsSchemaContext;
46 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
47 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
49 peopleDataTree = new ShardDataTree(peopleSchemaContext);
53 public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
54 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
55 coordinator.startLogRecoveryBatch(10);
57 coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
58 } catch(final SchemaValidationFailedException e){
59 fail("SchemaValidationFailedException should not happen if pruning is done");
62 coordinator.applyCurrentLogRecoveryBatch();
66 public void testAppendRecoveredLogEntryModificationPayload() throws IOException {
67 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
68 coordinator.startLogRecoveryBatch(10);
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");
79 public void testAppendRecoveredLogEntryCompositeModificationPayload() throws IOException {
80 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
81 coordinator.startLogRecoveryBatch(10);
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");
92 public void testAppendRecoveredLogEntryCompositeModificationByteStringPayload() throws IOException {
93 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
94 coordinator.startLogRecoveryBatch(10);
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");
103 assertEquals(false, readCars(peopleDataTree).isPresent());
107 public void testApplyRecoverySnapshot(){
108 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree , peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
109 coordinator.startLogRecoveryBatch(10);
111 coordinator.applyRecoverySnapshot(createSnapshot());
113 assertEquals(false, readCars(peopleDataTree).isPresent());
114 assertEquals(true, readPeople(peopleDataTree).isPresent());
119 public void testApplyCurrentLogRecoveryBatch(){
120 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree, peopleSchemaContext, "foobar", LoggerFactory.getLogger("foo"));
121 coordinator.startLogRecoveryBatch(10);
124 coordinator.applyCurrentLogRecoveryBatch();
125 } catch(final IllegalArgumentException e){
126 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
130 private DataTreeCandidateTip createCar(){
131 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
132 dataTree.setSchemaContext(carsSchemaContext);
134 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
136 final DataTreeModification modification = snapshot.newModification();
138 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
139 modification.ready();
140 return dataTree.prepare(modification);
143 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree){
144 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
145 dataTree.setSchemaContext(peopleSchemaContext);
147 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
149 final DataTreeModification modification = snapshot.newModification();
151 return modification.readNode(CarsModel.BASE_PATH);
154 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree){
155 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
156 dataTree.setSchemaContext(peopleSchemaContext);
158 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
160 final DataTreeModification modification = snapshot.newModification();
162 return modification.readNode(PeopleModel.BASE_PATH);
167 private byte[] createSnapshot(){
168 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
169 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
171 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
173 DataTreeModification modification = snapshot.newModification();
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);
180 dataTree.commit(prepare);
182 snapshot = dataTree.takeSnapshot();
184 modification = snapshot.newModification();
186 final Optional<NormalizedNode<?, ?>> optional = modification.readNode(YangInstanceIdentifier.EMPTY);
188 final byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get());