39285f9d67f90bfe6c51ec981debf5ff1b305833
[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.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;
39
40 public class ShardRecoveryCoordinatorTest extends AbstractTest {
41     private static final Logger FOO_LOGGER = LoggerFactory.getLogger("foo");
42
43     private ShardDataTree peopleDataTree;
44     private SchemaContext peopleSchemaContext;
45     private SchemaContext carsSchemaContext;
46     private ShardRecoveryCoordinator coordinator;
47
48     @Before
49     public void setUp() {
50         peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
51         carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
52
53         final Shard mockShard = Mockito.mock(Shard.class);
54
55         peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
56         coordinator = ShardRecoveryCoordinator.create(peopleDataTree, "foobar", FOO_LOGGER);
57         coordinator.startLogRecoveryBatch(10);
58     }
59
60     @Test
61     public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
62         try {
63             coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
64         } catch (final SchemaValidationFailedException e) {
65             fail("SchemaValidationFailedException should not happen if pruning is done");
66         }
67
68         coordinator.applyCurrentLogRecoveryBatch();
69     }
70
71     @Test
72     public void testApplyRecoverySnapshot() {
73         coordinator.applyRecoverySnapshot(createSnapshot());
74
75         assertFalse(readCars(peopleDataTree).isPresent());
76         assertTrue(readPeople(peopleDataTree).isPresent());
77     }
78
79
80     @Test
81     public void testApplyCurrentLogRecoveryBatch() {
82         try {
83             coordinator.applyCurrentLogRecoveryBatch();
84         } catch (final IllegalArgumentException e) {
85             fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
86         }
87     }
88
89     private DataTreeCandidate createCar() {
90         final DataTree dataTree = new InMemoryDataTreeFactory().create(
91             DataTreeConfiguration.DEFAULT_OPERATIONAL, carsSchemaContext);
92
93         final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
94
95         final DataTreeModification modification = snapshot.newModification();
96
97         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
98         modification.ready();
99         return dataTree.prepare(modification);
100     }
101
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);
106
107         return shardDataTree.readNode(CarsModel.BASE_PATH);
108     }
109
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);
114
115         return shardDataTree.readNode(PeopleModel.BASE_PATH);
116     }
117
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));
122
123         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
124
125         DataTreeModification modification = snapshot.newModification();
126
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));
131
132         return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(
133                 YangInstanceIdentifier.EMPTY).get()));
134     }
135 }