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.DataTree;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
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.SchemaContext;
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 SchemaContext peopleSchemaContext;
45 private SchemaContext 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 {
63 coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
64 } catch (final SchemaValidationFailedException e) {
65 fail("SchemaValidationFailedException should not happen if pruning is done");
68 coordinator.applyCurrentLogRecoveryBatch();
72 public void testApplyRecoverySnapshot() {
73 coordinator.applyRecoverySnapshot(createSnapshot());
75 assertFalse(readCars(peopleDataTree).isPresent());
76 assertTrue(readPeople(peopleDataTree).isPresent());
81 public void testApplyCurrentLogRecoveryBatch() {
83 coordinator.applyCurrentLogRecoveryBatch();
84 } catch (final IllegalArgumentException e) {
85 fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
89 private DataTreeCandidate createCar() {
90 final DataTree dataTree = new InMemoryDataTreeFactory().create(
91 DataTreeConfiguration.DEFAULT_OPERATIONAL, carsSchemaContext);
93 final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
95 final DataTreeModification modification = snapshot.newModification();
97 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
99 return dataTree.prepare(modification);
102 private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree) {
103 final DataTree dataTree = shardDataTree.getDataTree();
104 // FIXME: this should not be called here
105 dataTree.setSchemaContext(peopleSchemaContext);
107 return shardDataTree.readNode(CarsModel.BASE_PATH);
110 private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree) {
111 final DataTree dataTree = shardDataTree.getDataTree();
112 // FIXME: this should not be called here
113 dataTree.setSchemaContext(peopleSchemaContext);
115 return shardDataTree.readNode(PeopleModel.BASE_PATH);
118 private static ShardSnapshotState createSnapshot() {
119 final DataTree dataTree = new InMemoryDataTreeFactory().create(
120 DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
121 SchemaContextHelper.PEOPLE_YANG));
123 DataTreeSnapshot snapshot = dataTree.takeSnapshot();
125 DataTreeModification modification = snapshot.newModification();
127 modification.merge(CarsModel.BASE_PATH, CarsModel.create());
128 modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
129 modification.ready();
130 dataTree.commit(dataTree.prepare(modification));
132 return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(
133 YangInstanceIdentifier.EMPTY).get()));