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.persisted.CommitTransactionPayload;
18 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
19 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
20 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
21 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
29 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
30 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.slf4j.LoggerFactory;
34 public class ShardRecoveryCoordinatorTest extends AbstractTest {
36 private ShardDataTree peopleDataTree;
37 private SchemaContext peopleSchemaContext;
38 private SchemaContext carsSchemaContext;
42 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
43 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
45 peopleDataTree = new ShardDataTree(peopleSchemaContext, TreeType.OPERATIONAL);
50 public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
51 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
52 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
53 coordinator.startLogRecoveryBatch(10);
55 coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
56 } catch(final SchemaValidationFailedException e){
57 fail("SchemaValidationFailedException should not happen if pruning is done");
60 coordinator.applyCurrentLogRecoveryBatch();
64 public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
65 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
66 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
67 coordinator.startLogRecoveryBatch(10);
69 coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
70 } catch(final SchemaValidationFailedException e){
71 fail("SchemaValidationFailedException should not happen if pruning is done");
74 coordinator.applyCurrentLogRecoveryBatch();
78 public void testApplyRecoverySnapshot(){
79 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
80 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
81 coordinator.startLogRecoveryBatch(10);
83 coordinator.applyRecoverySnapshot(createSnapshot());
85 assertEquals(false, readCars(peopleDataTree).isPresent());
86 assertEquals(true, readPeople(peopleDataTree).isPresent());
91 public void testApplyCurrentLogRecoveryBatch(){
92 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
93 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
94 coordinator.startLogRecoveryBatch(10);
97 coordinator.applyCurrentLogRecoveryBatch();
98 } catch(final IllegalArgumentException e){
99 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
103 private DataTreeCandidateTip createCar(){
104 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
105 dataTree.setSchemaContext(carsSchemaContext);
107 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
109 final DataTreeModification modification = snapshot.newModification();
111 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
112 modification.ready();
113 return dataTree.prepare(modification);
116 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree){
117 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
118 // FIXME: this should not be called here
119 dataTree.setSchemaContext(peopleSchemaContext);
121 return shardDataTree.readNode(CarsModel.BASE_PATH);
124 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree){
125 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
126 // FIXME: this should not be called here
127 dataTree.setSchemaContext(peopleSchemaContext);
129 return shardDataTree.readNode(PeopleModel.BASE_PATH);
132 private static byte[] createSnapshot(){
133 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
134 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
136 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
138 DataTreeModification modification = snapshot.newModification();
140 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
141 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
142 modification.ready();
143 final DataTreeCandidateTip prepare = dataTree.prepare(modification);
145 dataTree.commit(prepare);
147 snapshot = dataTree.takeSnapshot();
149 modification = snapshot.newModification();
151 final Optional<NormalizedNode<?, ?>> optional = modification.readNode(YangInstanceIdentifier.EMPTY);
153 final byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get());