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