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
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
14 import java.io.IOException;
15 import java.util.Optional;
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.DataTree;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
34 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
35 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class ShardRecoveryCoordinatorTest extends AbstractTest {
41 private static final Logger FOO_LOGGER = LoggerFactory.getLogger("foo");
43 private ShardDataTree peopleDataTree;
44 private EffectiveModelContext peopleSchemaContext;
45 private EffectiveModelContext carsSchemaContext;
46 private ShardRecoveryCoordinator coordinator;
50 peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
51 carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
53 final Shard mockShard = Mockito.mock(Shard.class);
55 peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
56 coordinator = ShardRecoveryCoordinator.create(peopleDataTree, "foobar", FOO_LOGGER);
57 coordinator.startLogRecoveryBatch(10);
61 public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException,
62 DataValidationFailedException {
64 coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
65 } catch (final SchemaValidationFailedException e) {
66 fail("SchemaValidationFailedException should not happen if pruning is done");
69 coordinator.applyCurrentLogRecoveryBatch();
73 public void testApplyRecoverySnapshot() throws DataValidationFailedException {
74 coordinator.applyRecoverySnapshot(createSnapshot());
76 assertFalse(readCars(peopleDataTree).isPresent());
77 assertTrue(readPeople(peopleDataTree).isPresent());
82 public void testApplyCurrentLogRecoveryBatch() {
84 coordinator.applyCurrentLogRecoveryBatch();
85 } catch (final IllegalArgumentException e) {
86 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
90 private DataTreeCandidate createCar() throws DataValidationFailedException {
91 final DataTree dataTree = new InMemoryDataTreeFactory().create(
92 DataTreeConfiguration.DEFAULT_OPERATIONAL, carsSchemaContext);
94 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
96 final DataTreeModification modification = snapshot.newModification();
98 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
100 return dataTree.prepare(modification);
103 private Optional<NormalizedNode> readCars(final ShardDataTree shardDataTree) {
104 final DataTree dataTree = shardDataTree.getDataTree();
105 // FIXME: this should not be called here
106 dataTree.setEffectiveModelContext(peopleSchemaContext);
108 return shardDataTree.readNode(CarsModel.BASE_PATH);
111 private Optional<NormalizedNode> readPeople(final ShardDataTree shardDataTree) {
112 final DataTree dataTree = shardDataTree.getDataTree();
113 // FIXME: this should not be called here
114 dataTree.setEffectiveModelContext(peopleSchemaContext);
116 return shardDataTree.readNode(PeopleModel.BASE_PATH);
119 private static ShardSnapshotState createSnapshot() throws DataValidationFailedException {
120 final DataTree dataTree = new InMemoryDataTreeFactory().create(
121 DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
122 SchemaContextHelper.PEOPLE_YANG));
124 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
126 DataTreeModification modification = snapshot.newModification();
128 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
129 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
130 modification.ready();
131 dataTree.commit(dataTree.prepare(modification));
133 return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(
134 YangInstanceIdentifier.empty()).get()));