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;
14 import com.google.common.base.Optional;
15 import java.io.IOException;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload;
20 import org.opendaylight.controller.cluster.datastore.persisted.PreBoronShardDataTreeSnapshot;
21 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
22 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
23 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
31 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
32 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.slf4j.LoggerFactory;
36 public class ShardRecoveryCoordinatorTest extends AbstractTest {
38 private ShardDataTree peopleDataTree;
39 private SchemaContext peopleSchemaContext;
40 private SchemaContext carsSchemaContext;
44 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
45 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
47 final Shard mockShard = Mockito.mock(Shard.class);
49 peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
54 public void testAppendRecoveredLogEntryDataTreeCandidatePayload() {
55 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
56 null, "foobar", LoggerFactory.getLogger("foo"));
57 coordinator.startLogRecoveryBatch(10);
59 coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
60 } catch (final SchemaValidationFailedException e) {
61 fail("SchemaValidationFailedException should not happen if pruning is done");
64 coordinator.applyCurrentLogRecoveryBatch();
68 public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
69 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
70 null, "foobar", LoggerFactory.getLogger("foo"));
71 coordinator.startLogRecoveryBatch(10);
73 coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
74 } catch (final SchemaValidationFailedException e) {
75 fail("SchemaValidationFailedException should not happen if pruning is done");
78 coordinator.applyCurrentLogRecoveryBatch();
82 public void testApplyRecoverySnapshot() {
83 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
84 null, "foobar", LoggerFactory.getLogger("foo"));
85 coordinator.startLogRecoveryBatch(10);
87 coordinator.applyRecoverySnapshot(createSnapshot());
89 assertEquals(false, readCars(peopleDataTree).isPresent());
90 assertEquals(true, readPeople(peopleDataTree).isPresent());
95 public void testApplyCurrentLogRecoveryBatch() {
96 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
97 null, "foobar", LoggerFactory.getLogger("foo"));
98 coordinator.startLogRecoveryBatch(10);
101 coordinator.applyCurrentLogRecoveryBatch();
102 } catch (final IllegalArgumentException e) {
103 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
107 private DataTreeCandidateTip createCar() {
108 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
109 dataTree.setSchemaContext(carsSchemaContext);
111 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
113 final DataTreeModification modification = snapshot.newModification();
115 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
116 modification.ready();
117 return dataTree.prepare(modification);
120 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree) {
121 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
122 // FIXME: this should not be called here
123 dataTree.setSchemaContext(peopleSchemaContext);
125 return shardDataTree.readNode(CarsModel.BASE_PATH);
128 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree) {
129 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
130 // FIXME: this should not be called here
131 dataTree.setSchemaContext(peopleSchemaContext);
133 return shardDataTree.readNode(PeopleModel.BASE_PATH);
136 private static byte[] createSnapshot() {
137 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
138 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
139 SchemaContextHelper.PEOPLE_YANG));
141 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
143 DataTreeModification modification = snapshot.newModification();
145 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
146 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
147 modification.ready();
148 dataTree.commit(dataTree.prepare(modification));
150 return new PreBoronShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY).get())