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.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;
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, TreeType.OPERATIONAL);
53 public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
54 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
55 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
56 coordinator.startLogRecoveryBatch(10);
58 coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
59 } catch(final SchemaValidationFailedException e){
60 fail("SchemaValidationFailedException should not happen if pruning is done");
63 coordinator.applyCurrentLogRecoveryBatch();
67 public void testAppendRecoveredLogEntryModificationPayload() throws IOException {
68 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
69 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
70 coordinator.startLogRecoveryBatch(10);
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");
81 public void testAppendRecoveredLogEntryCompositeModificationPayload() throws IOException {
82 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
83 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
84 coordinator.startLogRecoveryBatch(10);
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");
95 public void testAppendRecoveredLogEntryCompositeModificationByteStringPayload() throws IOException {
96 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
97 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
98 coordinator.startLogRecoveryBatch(10);
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");
107 assertEquals(false, readCars(peopleDataTree).isPresent());
111 public void testApplyRecoverySnapshot(){
112 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
113 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
114 coordinator.startLogRecoveryBatch(10);
116 coordinator.applyRecoverySnapshot(createSnapshot());
118 assertEquals(false, readCars(peopleDataTree).isPresent());
119 assertEquals(true, readPeople(peopleDataTree).isPresent());
124 public void testApplyCurrentLogRecoveryBatch(){
125 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
126 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
127 coordinator.startLogRecoveryBatch(10);
130 coordinator.applyCurrentLogRecoveryBatch();
131 } catch(final IllegalArgumentException e){
132 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
136 private DataTreeCandidateTip createCar(){
137 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
138 dataTree.setSchemaContext(carsSchemaContext);
140 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
142 final DataTreeModification modification = snapshot.newModification();
144 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
145 modification.ready();
146 return dataTree.prepare(modification);
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);
154 return shardDataTree.readNode(CarsModel.BASE_PATH);
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);
162 return shardDataTree.readNode(PeopleModel.BASE_PATH);
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));
169 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
171 DataTreeModification modification = snapshot.newModification();
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);
178 dataTree.commit(prepare);
180 snapshot = dataTree.takeSnapshot();
182 modification = snapshot.newModification();
184 final Optional<NormalizedNode<?, ?>> optional = modification.readNode(YangInstanceIdentifier.EMPTY);
186 final byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get());