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.mockito.Mockito;
18 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload;
19 import org.opendaylight.controller.cluster.datastore.persisted.PreBoronShardDataTreeSnapshot;
20 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
21 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
22 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
30 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
31 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.LoggerFactory;
35 public class ShardRecoveryCoordinatorTest extends AbstractTest {
37 private ShardDataTree peopleDataTree;
38 private SchemaContext peopleSchemaContext;
39 private SchemaContext carsSchemaContext;
43 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
44 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
46 final Shard mockShard = Mockito.mock(Shard.class);
48 peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
53 public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
54 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
55 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 testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
68 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
69 null, "foobar", LoggerFactory.getLogger("foo"));
70 coordinator.startLogRecoveryBatch(10);
72 coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
73 } catch(final SchemaValidationFailedException e){
74 fail("SchemaValidationFailedException should not happen if pruning is done");
77 coordinator.applyCurrentLogRecoveryBatch();
81 public void testApplyRecoverySnapshot(){
82 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
83 null, "foobar", LoggerFactory.getLogger("foo"));
84 coordinator.startLogRecoveryBatch(10);
86 coordinator.applyRecoverySnapshot(createSnapshot());
88 assertEquals(false, readCars(peopleDataTree).isPresent());
89 assertEquals(true, readPeople(peopleDataTree).isPresent());
94 public void testApplyCurrentLogRecoveryBatch(){
95 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
96 null, "foobar", LoggerFactory.getLogger("foo"));
97 coordinator.startLogRecoveryBatch(10);
100 coordinator.applyCurrentLogRecoveryBatch();
101 } catch(final IllegalArgumentException e){
102 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
106 private DataTreeCandidateTip createCar(){
107 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
108 dataTree.setSchemaContext(carsSchemaContext);
110 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
112 final DataTreeModification modification = snapshot.newModification();
114 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
115 modification.ready();
116 return dataTree.prepare(modification);
119 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree){
120 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
121 // FIXME: this should not be called here
122 dataTree.setSchemaContext(peopleSchemaContext);
124 return shardDataTree.readNode(CarsModel.BASE_PATH);
127 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree){
128 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
129 // FIXME: this should not be called here
130 dataTree.setSchemaContext(peopleSchemaContext);
132 return shardDataTree.readNode(PeopleModel.BASE_PATH);
135 private static byte[] createSnapshot(){
136 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
137 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
139 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
141 DataTreeModification modification = snapshot.newModification();
143 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
144 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
145 modification.ready();
146 dataTree.commit(dataTree.prepare(modification));
148 return new PreBoronShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY).get())