BUG-5280: expand ShardDataTree to cover transaction mechanics
[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.assertEquals;
12 import static org.junit.Assert.fail;
13 import com.google.common.base.Optional;
14 import java.io.IOException;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mockito;
18 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload;
19 import org.opendaylight.controller.cluster.datastore.persisted.PreBoronShardDataTreeSnapshot;
20 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
21 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
22 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
30 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
31 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.LoggerFactory;
34
35 public class ShardRecoveryCoordinatorTest extends AbstractTest {
36
37     private ShardDataTree peopleDataTree;
38     private SchemaContext peopleSchemaContext;
39     private SchemaContext carsSchemaContext;
40
41     @Before
42     public void setUp(){
43         peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
44         carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
45
46         final Shard mockShard = Mockito.mock(Shard.class);
47
48         peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
49     }
50
51     @Deprecated
52     @Test
53     public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
54         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
55                 null, "foobar", LoggerFactory.getLogger("foo"));
56         coordinator.startLogRecoveryBatch(10);
57         try {
58             coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
59         } catch(final SchemaValidationFailedException e){
60             fail("SchemaValidationFailedException should not happen if pruning is done");
61         }
62
63         coordinator.applyCurrentLogRecoveryBatch();
64     }
65
66     @Test
67     public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
68         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
69                 null, "foobar", LoggerFactory.getLogger("foo"));
70         coordinator.startLogRecoveryBatch(10);
71         try {
72             coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
73         } catch(final SchemaValidationFailedException e){
74             fail("SchemaValidationFailedException should not happen if pruning is done");
75         }
76
77         coordinator.applyCurrentLogRecoveryBatch();
78     }
79
80     @Test
81     public void testApplyRecoverySnapshot(){
82         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
83                 null, "foobar", LoggerFactory.getLogger("foo"));
84         coordinator.startLogRecoveryBatch(10);
85
86         coordinator.applyRecoverySnapshot(createSnapshot());
87
88         assertEquals(false, readCars(peopleDataTree).isPresent());
89         assertEquals(true, readPeople(peopleDataTree).isPresent());
90     }
91
92
93     @Test
94     public void testApplyCurrentLogRecoveryBatch(){
95         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
96                 null, "foobar", LoggerFactory.getLogger("foo"));
97         coordinator.startLogRecoveryBatch(10);
98
99         try {
100             coordinator.applyCurrentLogRecoveryBatch();
101         } catch(final IllegalArgumentException e){
102             fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
103         }
104     }
105
106     private DataTreeCandidateTip createCar(){
107         final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
108         dataTree.setSchemaContext(carsSchemaContext);
109
110         final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
111
112         final DataTreeModification modification = snapshot.newModification();
113
114         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
115         modification.ready();
116         return dataTree.prepare(modification);
117     }
118
119     private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree){
120         final TipProducingDataTree dataTree = shardDataTree.getDataTree();
121         // FIXME: this should not be called here
122         dataTree.setSchemaContext(peopleSchemaContext);
123
124         return shardDataTree.readNode(CarsModel.BASE_PATH);
125     }
126
127     private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree){
128         final TipProducingDataTree dataTree = shardDataTree.getDataTree();
129         // FIXME: this should not be called here
130         dataTree.setSchemaContext(peopleSchemaContext);
131
132         return shardDataTree.readNode(PeopleModel.BASE_PATH);
133     }
134
135     private static byte[] createSnapshot(){
136         final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
137         dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
138
139         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
140
141         DataTreeModification modification = snapshot.newModification();
142
143         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
144         modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
145         modification.ready();
146         dataTree.commit(dataTree.prepare(modification));
147
148         return new PreBoronShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY).get())
149                 .serialize();
150     }
151 }