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.MetadataShardDataTreeSnapshot;
21 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
22 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
23 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
24 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
32 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
33 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.slf4j.LoggerFactory;
37 public class ShardRecoveryCoordinatorTest extends AbstractTest {
39 private ShardDataTree peopleDataTree;
40 private SchemaContext peopleSchemaContext;
41 private SchemaContext carsSchemaContext;
45 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
46 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
48 final Shard mockShard = Mockito.mock(Shard.class);
50 peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
55 public void testAppendRecoveredLogEntryDataTreeCandidatePayload() {
56 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
57 null, "foobar", LoggerFactory.getLogger("foo"));
58 coordinator.startLogRecoveryBatch(10);
60 coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
61 } catch (final SchemaValidationFailedException e) {
62 fail("SchemaValidationFailedException should not happen if pruning is done");
65 coordinator.applyCurrentLogRecoveryBatch();
69 public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
70 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
71 null, "foobar", LoggerFactory.getLogger("foo"));
72 coordinator.startLogRecoveryBatch(10);
74 coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
75 } catch (final SchemaValidationFailedException e) {
76 fail("SchemaValidationFailedException should not happen if pruning is done");
79 coordinator.applyCurrentLogRecoveryBatch();
83 public void testApplyRecoverySnapshot() {
84 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
85 null, "foobar", LoggerFactory.getLogger("foo"));
86 coordinator.startLogRecoveryBatch(10);
88 coordinator.applyRecoverySnapshot(createSnapshot());
90 assertEquals(false, readCars(peopleDataTree).isPresent());
91 assertEquals(true, readPeople(peopleDataTree).isPresent());
96 public void testApplyCurrentLogRecoveryBatch() {
97 final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
98 null, "foobar", LoggerFactory.getLogger("foo"));
99 coordinator.startLogRecoveryBatch(10);
102 coordinator.applyCurrentLogRecoveryBatch();
103 } catch (final IllegalArgumentException e) {
104 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
108 private DataTreeCandidateTip createCar() {
109 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
110 dataTree.setSchemaContext(carsSchemaContext);
112 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
114 final DataTreeModification modification = snapshot.newModification();
116 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
117 modification.ready();
118 return dataTree.prepare(modification);
121 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree) {
122 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
123 // FIXME: this should not be called here
124 dataTree.setSchemaContext(peopleSchemaContext);
126 return shardDataTree.readNode(CarsModel.BASE_PATH);
129 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree) {
130 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
131 // FIXME: this should not be called here
132 dataTree.setSchemaContext(peopleSchemaContext);
134 return shardDataTree.readNode(PeopleModel.BASE_PATH);
137 private static ShardSnapshotState createSnapshot() {
138 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
139 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
140 SchemaContextHelper.PEOPLE_YANG));
142 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
144 DataTreeModification modification = snapshot.newModification();
146 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
147 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
148 modification.ready();
149 dataTree.commit(dataTree.prepare(modification));
151 return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(
152 YangInstanceIdentifier.EMPTY).get()));