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 org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
17 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
18 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
19 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
27 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
28 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.slf4j.LoggerFactory;
32 public class ShardRecoveryCoordinatorTest {
34 private ShardDataTree peopleDataTree;
35 private SchemaContext peopleSchemaContext;
36 private SchemaContext carsSchemaContext;
40 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
41 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
43 peopleDataTree = new ShardDataTree(peopleSchemaContext, TreeType.OPERATIONAL);
47 public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
48 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
49 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
50 coordinator.startLogRecoveryBatch(10);
52 coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
53 } catch(final SchemaValidationFailedException e){
54 fail("SchemaValidationFailedException should not happen if pruning is done");
57 coordinator.applyCurrentLogRecoveryBatch();
61 public void testApplyRecoverySnapshot(){
62 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
63 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
64 coordinator.startLogRecoveryBatch(10);
66 coordinator.applyRecoverySnapshot(createSnapshot());
68 assertEquals(false, readCars(peopleDataTree).isPresent());
69 assertEquals(true, readPeople(peopleDataTree).isPresent());
74 public void testApplyCurrentLogRecoveryBatch(){
75 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
76 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
77 coordinator.startLogRecoveryBatch(10);
80 coordinator.applyCurrentLogRecoveryBatch();
81 } catch(final IllegalArgumentException e){
82 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
86 private DataTreeCandidateTip createCar(){
87 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
88 dataTree.setSchemaContext(carsSchemaContext);
90 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
92 final DataTreeModification modification = snapshot.newModification();
94 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
96 return dataTree.prepare(modification);
99 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree){
100 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
101 // FIXME: this should not be called here
102 dataTree.setSchemaContext(peopleSchemaContext);
104 return shardDataTree.readNode(CarsModel.BASE_PATH);
107 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree){
108 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
109 // FIXME: this should not be called here
110 dataTree.setSchemaContext(peopleSchemaContext);
112 return shardDataTree.readNode(PeopleModel.BASE_PATH);
115 private static byte[] createSnapshot(){
116 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
117 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
119 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
121 DataTreeModification modification = snapshot.newModification();
123 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
124 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
125 modification.ready();
126 final DataTreeCandidateTip prepare = dataTree.prepare(modification);
128 dataTree.commit(prepare);
130 snapshot = dataTree.takeSnapshot();
132 modification = snapshot.newModification();
134 final Optional<NormalizedNode<?, ?>> optional = modification.readNode(YangInstanceIdentifier.EMPTY);
136 final byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get());