Separate ShardRecoveryCoordinator implementations
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardRecoveryCoordinatorTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
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;
38
39 public class ShardRecoveryCoordinatorTest extends AbstractTest {
40     private static final Logger FOO_LOGGER = LoggerFactory.getLogger("foo");
41
42     private ShardDataTree peopleDataTree;
43     private SchemaContext peopleSchemaContext;
44     private SchemaContext carsSchemaContext;
45     private ShardRecoveryCoordinator coordinator;
46
47     @Before
48     public void setUp() {
49         peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
50         carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
51
52         final Shard mockShard = Mockito.mock(Shard.class);
53
54         peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
55         coordinator = ShardRecoveryCoordinator.create(peopleDataTree, "foobar", FOO_LOGGER);
56         coordinator.startLogRecoveryBatch(10);
57     }
58
59     @Test
60     public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
61         try {
62             coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
63         } catch (final SchemaValidationFailedException e) {
64             fail("SchemaValidationFailedException should not happen if pruning is done");
65         }
66
67         coordinator.applyCurrentLogRecoveryBatch();
68     }
69
70     @Test
71     public void testApplyRecoverySnapshot() {
72         coordinator.applyRecoverySnapshot(createSnapshot());
73
74         assertFalse(readCars(peopleDataTree).isPresent());
75         assertTrue(readPeople(peopleDataTree).isPresent());
76     }
77
78
79     @Test
80     public void testApplyCurrentLogRecoveryBatch() {
81         try {
82             coordinator.applyCurrentLogRecoveryBatch();
83         } catch (final IllegalArgumentException e) {
84             fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
85         }
86     }
87
88     private DataTreeCandidateTip createCar() {
89         final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
90         dataTree.setSchemaContext(carsSchemaContext);
91
92         final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
93
94         final DataTreeModification modification = snapshot.newModification();
95
96         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
97         modification.ready();
98         return dataTree.prepare(modification);
99     }
100
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);
105
106         return shardDataTree.readNode(CarsModel.BASE_PATH);
107     }
108
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);
113
114         return shardDataTree.readNode(PeopleModel.BASE_PATH);
115     }
116
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));
121
122         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
123
124         DataTreeModification modification = snapshot.newModification();
125
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));
130
131         return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(
132                 YangInstanceIdentifier.EMPTY).get()));
133     }
134 }