Convert DatastoreSnapshotRestore to OSGi DS
[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 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13
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;
39
40 public class ShardRecoveryCoordinatorTest extends AbstractTest {
41     private static final Logger FOO_LOGGER = LoggerFactory.getLogger("foo");
42
43     private ShardDataTree peopleDataTree;
44     private EffectiveModelContext peopleSchemaContext;
45     private EffectiveModelContext 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             DataValidationFailedException {
63         try {
64             coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
65         } catch (final SchemaValidationFailedException e) {
66             fail("SchemaValidationFailedException should not happen if pruning is done");
67         }
68
69         coordinator.applyCurrentLogRecoveryBatch();
70     }
71
72     @Test
73     public void testApplyRecoverySnapshot() throws DataValidationFailedException {
74         coordinator.applyRecoverySnapshot(createSnapshot());
75
76         assertFalse(readCars(peopleDataTree).isPresent());
77         assertTrue(readPeople(peopleDataTree).isPresent());
78     }
79
80
81     @Test
82     public void testApplyCurrentLogRecoveryBatch() {
83         try {
84             coordinator.applyCurrentLogRecoveryBatch();
85         } catch (final IllegalArgumentException e) {
86             fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
87         }
88     }
89
90     private DataTreeCandidate createCar() throws DataValidationFailedException {
91         final DataTree dataTree = new InMemoryDataTreeFactory().create(
92             DataTreeConfiguration.DEFAULT_OPERATIONAL, carsSchemaContext);
93
94         final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
95
96         final DataTreeModification modification = snapshot.newModification();
97
98         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
99         modification.ready();
100         return dataTree.prepare(modification);
101     }
102
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);
107
108         return shardDataTree.readNode(CarsModel.BASE_PATH);
109     }
110
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);
115
116         return shardDataTree.readNode(PeopleModel.BASE_PATH);
117     }
118
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));
123
124         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
125
126         DataTreeModification modification = snapshot.newModification();
127
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));
132
133         return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(
134                 YangInstanceIdentifier.empty()).get()));
135     }
136 }