summaryrefslogtreecommitdiffstats
path: root/v3po/data-impl/src/test/java/io/fd/honeycomb/v3po/data/impl/ModificationDiffTest.java
blob: f5c270618ba5c9e9506816754132c852cf3e6f19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package io.fd.honeycomb.v3po.data.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Map;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;

public class ModificationDiffTest {

    private static final QName TOP_CONTAINER_QNAME = QName.create("urn:opendaylight:params:xml:ns:yang:test:diff", "2015-01-05", "top-container");
    private static final QName STRING_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "string");
    private static final QName NAME_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "name");
    private static final QName TEXT_LEAF_QNAME = QName.create(TOP_CONTAINER_QNAME, "text");
    private static final QName NESTED_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "nested-list");
    private static final QName DEEP_LIST_QNAME = QName.create(TOP_CONTAINER_QNAME, "deep-list");

    private YangInstanceIdentifier topContainerId = YangInstanceIdentifier.of(TOP_CONTAINER_QNAME);;

    @Test
    public void testInitialWrite() throws Exception {
        final TipProducingDataTree dataTree = getDataTree();
        final DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        final DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();
        final NormalizedNode<?, ?> topContainer = getTopContainer("string1");
        final YangInstanceIdentifier topContainerId = YangInstanceIdentifier.of(TOP_CONTAINER_QNAME);
        dataTreeModification.write(topContainerId, topContainer);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        final DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);

        final ModifiableDataTreeDelegator.ModificationDiff modificationDiff =
                ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertTrue(modificationDiff.getModificationsBefore().isEmpty());
        assertAfter(topContainer, topContainerId, modificationDiff);
    }

    @Test
    public void testUpdateWrite() throws Exception {
        final TipProducingDataTree dataTree = getDataTree();
        final NormalizedNode<?, ?> topContainerBefore = addTopContainer(dataTree);

        final DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        final DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();
        final NormalizedNode<?, ?> topContainerAfter = getTopContainer("string2");
        dataTreeModification.write(topContainerId, topContainerAfter);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        final DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);

        final ModifiableDataTreeDelegator.ModificationDiff modificationDiff =
                ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertBefore(topContainerBefore, topContainerId, modificationDiff);
        assertAfter(topContainerAfter, topContainerId, modificationDiff);
    }

    @Test
    public void testUpdateMerge() throws Exception {
        final TipProducingDataTree dataTree = getDataTree();
        final NormalizedNode<?, ?> topContainerBefore = addTopContainer(dataTree);

        final DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        final DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();
        final NormalizedNode<?, ?> topContainerAfter = getTopContainer("string2");
        dataTreeModification.merge(topContainerId, topContainerAfter);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        final DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);

        final ModifiableDataTreeDelegator.ModificationDiff modificationDiff =
                ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertBefore(topContainerBefore, topContainerId, modificationDiff);
        assertAfter(topContainerAfter, topContainerId, modificationDiff);
    }

    @Test
    public void testUpdateDelete() throws Exception {
        final TipProducingDataTree dataTree = getDataTree();
        final NormalizedNode<?, ?> topContainerBefore = addTopContainer(dataTree);

        final DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        final DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();
        dataTreeModification.delete(topContainerId);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        final DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);

        final ModifiableDataTreeDelegator.ModificationDiff modificationDiff =
                ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertBefore(topContainerBefore, topContainerId, modificationDiff);
        assertTrue(modificationDiff.getModificationsAfter().isEmpty());
    }

    @Test
    public void testWriteAndUpdateInnerList() throws Exception {
        final TipProducingDataTree dataTree = getDataTree();
        addTopContainer(dataTree);

        DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();
        final YangInstanceIdentifier listId =
                YangInstanceIdentifier.create(
                        new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME),
                        new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME));

        final MapNode mapNode = getNestedList("name1", "text");
        dataTreeModification.write(listId, mapNode);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);

        ModifiableDataTreeDelegator.ModificationDiff modificationDiff =
                ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertTrue(modificationDiff.getModificationsBefore().isEmpty());
        assertAfter(mapNode, listId, modificationDiff);

        // Commit so that update can be tested next
        dataTree.commit(prepare);

        YangInstanceIdentifier listItemId = listId.node(
                new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_LIST_QNAME, NAME_LEAF_QNAME, "name1"));
        MapEntryNode mapEntryNode =
                getNestedList("name1", "text-update").getValue().iterator().next();

        dataTreeSnapshot = dataTree.takeSnapshot();
        dataTreeModification = dataTreeSnapshot.newModification();
        dataTreeModification.write(listItemId, mapEntryNode);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        prepare = dataTree.prepare(dataTreeModification);

        modificationDiff =
                ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertBefore(mapNode.getValue().iterator().next(), listItemId, modificationDiff);
        assertAfter(mapEntryNode, listItemId, modificationDiff);
    }

    @Test
    public void testWriteTopContainerAndInnerList() throws Exception {
        final TipProducingDataTree dataTree = getDataTree();

        DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();

        final ContainerNode topContainer = getTopContainer("string1");
        dataTreeModification.write(topContainerId, topContainer);

        final YangInstanceIdentifier listId =
                YangInstanceIdentifier.create(
                        new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME),
                        new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME));

        final MapNode mapNode = getNestedList("name1", "text");
        dataTreeModification.write(listId, mapNode);

        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        final DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);

        final ModifiableDataTreeDelegator.ModificationDiff modificationDiff =
                ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertTrue(modificationDiff.getModificationsBefore().isEmpty());

        // TODO HONEYCOMB-94 2 after modifications should appear, for top-container and nested-list entry
        assertAfter(Builders.containerBuilder(topContainer)
                        .withChild(mapNode)
                        .build(),
                topContainerId, modificationDiff);
    }

    @Test
    public void testWriteDeepList() throws Exception {
        final TipProducingDataTree dataTree = getDataTree();
        addTopContainer(dataTree);

        DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();

        YangInstanceIdentifier listId =
                YangInstanceIdentifier.create(
                        new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME),
                        new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME));

        MapNode mapNode = getNestedList("name1", "text");
        dataTreeModification.write(listId, mapNode);

        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);
        dataTree.commit(prepare);

        dataTreeSnapshot = dataTree.takeSnapshot();
        dataTreeModification = dataTreeSnapshot.newModification();

        final YangInstanceIdentifier.NodeIdentifierWithPredicates nestedListNodeId =
                new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_LIST_QNAME, NAME_LEAF_QNAME, "name1");
        listId = YangInstanceIdentifier.create(
                        new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME),
                        new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME),
                        nestedListNodeId);
        final YangInstanceIdentifier deepListId = listId.node(new YangInstanceIdentifier.NodeIdentifier(DEEP_LIST_QNAME));
        final YangInstanceIdentifier deepListEntryId = deepListId.node(
                new YangInstanceIdentifier.NodeIdentifierWithPredicates(DEEP_LIST_QNAME, NAME_LEAF_QNAME,"name1"));

        final MapEntryNode deepListEntry = getDeepList("name1").getValue().iterator().next();
        // Merge parent list, just to see no modifications on it
        dataTreeModification.merge(
                listId,
                Builders.mapEntryBuilder().withNodeIdentifier(nestedListNodeId).withChild(ImmutableNodes.leafNode(NAME_LEAF_QNAME, "name1")).build());
        dataTreeModification.merge(
                deepListId,
                Builders.mapBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DEEP_LIST_QNAME)).build());
        dataTreeModification.merge(
                deepListEntryId,
                deepListEntry);

        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        prepare = dataTree.prepare(dataTreeModification);
        dataTree.commit(prepare);

        final ModifiableDataTreeDelegator.ModificationDiff modificationDiff = ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertTrue(modificationDiff.getModificationsBefore().isEmpty());
        assertAfter(getDeepList("name1"), deepListId, modificationDiff);
    }

    @Test
    public void testDeleteInnerListItem() throws Exception {
        final TipProducingDataTree dataTree = getDataTree();
        addTopContainer(dataTree);

        DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();
        final YangInstanceIdentifier listId =
                YangInstanceIdentifier.create(
                        new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME),
                        new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME));

        final MapNode mapNode = getNestedList("name1", "text");
        dataTreeModification.write(listId, mapNode);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);

        // Commit so that update can be tested next
        dataTree.commit(prepare);

        YangInstanceIdentifier listItemId = listId.node(
                new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_LIST_QNAME, NAME_LEAF_QNAME, "name1"));

        dataTreeSnapshot = dataTree.takeSnapshot();
        dataTreeModification = dataTreeSnapshot.newModification();
        dataTreeModification.delete(listItemId);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        prepare = dataTree.prepare(dataTreeModification);

        final ModifiableDataTreeDelegator.ModificationDiff modificationDiff =
                ModifiableDataTreeDelegator.ModificationDiff
                        .recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, prepare.getRootNode());

        assertBefore(mapNode.getValue().iterator().next(), listItemId, modificationDiff);
        assertTrue(modificationDiff.getModificationsAfter().isEmpty());
    }

    private NormalizedNode<?, ?> addTopContainer(final TipProducingDataTree dataTree) throws DataValidationFailedException {
        DataTreeSnapshot dataTreeSnapshot = dataTree.takeSnapshot();
        DataTreeModification dataTreeModification = dataTreeSnapshot.newModification();
        final NormalizedNode<?, ?> topContainerBefore = getTopContainer("string1");
        dataTreeModification.write(topContainerId, topContainerBefore);
        dataTreeModification.ready();
        dataTree.validate(dataTreeModification);
        DataTreeCandidateTip prepare = dataTree.prepare(dataTreeModification);
        dataTree.commit(prepare);
        return topContainerBefore;
    }

    private void assertAfter(final NormalizedNode<?, ?> topContainer, final YangInstanceIdentifier topContainerId,
                             final ModifiableDataTreeDelegator.ModificationDiff modificationDiff) {
        assertModification(topContainer, topContainerId, modificationDiff.getModificationsAfter());
    }

    private void assertModification(final NormalizedNode<?, ?> topContainer,
                                    final YangInstanceIdentifier topContainerId,
                                    final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> modificationMap) {
        assertEquals(1, modificationMap.keySet().size());
        assertEquals(topContainerId, modificationMap.keySet().iterator().next());
        assertEquals(topContainer, modificationMap.values().iterator().next());
    }

    private void assertBefore(final NormalizedNode<?, ?> topContainerBefore,
                              final YangInstanceIdentifier topContainerId,
                              final ModifiableDataTreeDelegator.ModificationDiff modificationDiff) {
        assertModification(topContainerBefore, topContainerId, modificationDiff.getModificationsBefore());
    }

    private TipProducingDataTree getDataTree() throws ReactorException {
        final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
        dataTree.setSchemaContext(getSchemaCtx());
        return dataTree;
    }

    private ContainerNode getTopContainer(final String stringValue) {
        return Builders.containerBuilder()
                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TOP_CONTAINER_QNAME))
                .withChild(ImmutableNodes.leafNode(STRING_LEAF_QNAME, stringValue))
                .build();
    }

    private MapNode getNestedList(final String listItemName, final String text) {
        return Builders.mapBuilder()
                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NESTED_LIST_QNAME))
                .withChild(
                    Builders.mapEntryBuilder()
                        .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifierWithPredicates(NESTED_LIST_QNAME, NAME_LEAF_QNAME, listItemName))
                        .withChild(ImmutableNodes.leafNode(NAME_LEAF_QNAME, listItemName))
                        .withChild(ImmutableNodes.leafNode(TEXT_LEAF_QNAME, text))
                        .build()
                )
                .build();
    }

    private MapNode getDeepList(final String listItemName) {
        return Builders.mapBuilder()
                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DEEP_LIST_QNAME))
                .withChild(
                    Builders.mapEntryBuilder()
                        .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifierWithPredicates(DEEP_LIST_QNAME, NAME_LEAF_QNAME, listItemName))
                        .withChild(ImmutableNodes.leafNode(NAME_LEAF_QNAME, listItemName))
                        .build()
                )
                .build();
    }

    private SchemaContext getSchemaCtx() throws ReactorException {
        final CrossSourceStatementReactor.BuildAction buildAction = YangInferencePipeline.RFC6020_REACTOR.newBuild();
        buildAction.addSource(new YangStatementSourceImpl(getClass().getResourceAsStream("/test-diff.yang")));
        return buildAction.buildEffective();
    }
}