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.MutableCompositeModification;
18 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
19 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
22 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
23 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
24 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
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, TreeType.OPERATIONAL);
52 public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
53 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
54 peopleSchemaContext, null, "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 testAppendRecoveredLogEntryCompositeModificationPayload() throws IOException {
67 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
68 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
69 coordinator.startLogRecoveryBatch(10);
71 final MutableCompositeModification modification = new MutableCompositeModification((short) 1);
72 modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create()));
73 coordinator.appendRecoveredLogEntry(new CompositeModificationPayload(modification.toSerializable()));
74 } catch(final SchemaValidationFailedException e){
75 fail("SchemaValidationFailedException should not happen if pruning is done");
80 public void testAppendRecoveredLogEntryCompositeModificationByteStringPayload() throws IOException {
81 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
82 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
83 coordinator.startLogRecoveryBatch(10);
85 final MutableCompositeModification modification = new MutableCompositeModification((short) 1);
86 modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create()));
87 coordinator.appendRecoveredLogEntry(new CompositeModificationByteStringPayload(modification.toSerializable()));
88 } catch(final SchemaValidationFailedException e){
89 fail("SchemaValidationFailedException should not happen if pruning is done");
92 assertEquals(false, readCars(peopleDataTree).isPresent());
96 public void testApplyRecoverySnapshot(){
97 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
98 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
99 coordinator.startLogRecoveryBatch(10);
101 coordinator.applyRecoverySnapshot(createSnapshot());
103 assertEquals(false, readCars(peopleDataTree).isPresent());
104 assertEquals(true, readPeople(peopleDataTree).isPresent());
109 public void testApplyCurrentLogRecoveryBatch(){
110 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
111 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
112 coordinator.startLogRecoveryBatch(10);
115 coordinator.applyCurrentLogRecoveryBatch();
116 } catch(final IllegalArgumentException e){
117 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
121 private DataTreeCandidateTip createCar(){
122 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
123 dataTree.setSchemaContext(carsSchemaContext);
125 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
127 final DataTreeModification modification = snapshot.newModification();
129 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
130 modification.ready();
131 return dataTree.prepare(modification);
134 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree){
135 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
136 // FIXME: this should not be called here
137 dataTree.setSchemaContext(peopleSchemaContext);
139 return shardDataTree.readNode(CarsModel.BASE_PATH);
142 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree){
143 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
144 // FIXME: this should not be called here
145 dataTree.setSchemaContext(peopleSchemaContext);
147 return shardDataTree.readNode(PeopleModel.BASE_PATH);
150 private static byte[] createSnapshot(){
151 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
152 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
154 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
156 DataTreeModification modification = snapshot.newModification();
158 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
159 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
160 modification.ready();
161 final DataTreeCandidateTip prepare = dataTree.prepare(modification);
163 dataTree.commit(prepare);
165 snapshot = dataTree.takeSnapshot();
167 modification = snapshot.newModification();
169 final Optional<NormalizedNode<?, ?>> optional = modification.readNode(YangInstanceIdentifier.EMPTY);
171 final byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get());