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.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import com.google.common.base.Optional;
16 import java.io.IOException;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mockito;
20 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload;
21 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
22 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
23 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
24 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
25 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
33 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
34 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class ShardRecoveryCoordinatorTest extends AbstractTest {
40 private static final Logger FOO_LOGGER = LoggerFactory.getLogger("foo");
42 private ShardDataTree peopleDataTree;
43 private SchemaContext peopleSchemaContext;
44 private SchemaContext carsSchemaContext;
45 private ShardRecoveryCoordinator coordinator;
49 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
50 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
52 final Shard mockShard = Mockito.mock(Shard.class);
54 peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
55 coordinator = ShardRecoveryCoordinator.create(peopleDataTree, "foobar", FOO_LOGGER);
56 coordinator.startLogRecoveryBatch(10);
60 public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
62 coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
63 } catch (final SchemaValidationFailedException e) {
64 fail("SchemaValidationFailedException should not happen if pruning is done");
67 coordinator.applyCurrentLogRecoveryBatch();
71 public void testApplyRecoverySnapshot() {
72 coordinator.applyRecoverySnapshot(createSnapshot());
74 assertFalse(readCars(peopleDataTree).isPresent());
75 assertTrue(readPeople(peopleDataTree).isPresent());
80 public void testApplyCurrentLogRecoveryBatch() {
82 coordinator.applyCurrentLogRecoveryBatch();
83 } catch (final IllegalArgumentException e) {
84 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
88 private DataTreeCandidateTip createCar() {
89 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
90 dataTree.setSchemaContext(carsSchemaContext);
92 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
94 final DataTreeModification modification = snapshot.newModification();
96 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
98 return dataTree.prepare(modification);
101 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree) {
102 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
103 // FIXME: this should not be called here
104 dataTree.setSchemaContext(peopleSchemaContext);
106 return shardDataTree.readNode(CarsModel.BASE_PATH);
109 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree) {
110 final TipProducingDataTree dataTree = shardDataTree.getDataTree();
111 // FIXME: this should not be called here
112 dataTree.setSchemaContext(peopleSchemaContext);
114 return shardDataTree.readNode(PeopleModel.BASE_PATH);
117 private static ShardSnapshotState createSnapshot() {
118 final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
119 dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
120 SchemaContextHelper.PEOPLE_YANG));
122 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
124 DataTreeModification modification = snapshot.newModification();
126 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
127 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
128 modification.ready();
129 dataTree.commit(dataTree.prepare(modification));
131 return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(
132 YangInstanceIdentifier.EMPTY).get()));