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