Rework NormalizedNode type hierarchy
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / context / DataTreeCandidateValidatorTest.java
1 /*
2  * Copyright (c) 2016 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.yangtools.yang.data.impl.leafref.context;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.junit.AfterClass;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
25 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
38 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
39 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
40 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidation;
41 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
42 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
48 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
50 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.Module;
52 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public class DataTreeCandidateValidatorTest {
57
58     private static EffectiveModelContext context;
59     private static Module valModule;
60     private static QNameModule valModuleQname;
61     private static LeafRefContext rootLeafRefContext;
62     public static DataTree inMemoryDataTree;
63
64     private static QName odl;
65     private static QName project;
66     private static QName name;
67     private static QName desc;
68     private static QName lead;
69     private static QName owner;
70     private static QName odlContributor;
71     private static QName contributor;
72     private static QName odlProjectName;
73     private static QName odlProjectDesc;
74     private static QName login;
75     private static QName contributorName;
76     private static QName l1;
77     private static QName l2;
78     private static QName con1;
79     private static QName ch1;
80     private static QName ch2;
81     private static QName leafrefInChoice;
82     private static QName listInChoice;
83
84     private static QName leafrefInChoiceToChoice;
85     private static QName con3;
86     private static QName list3InChoice;
87     private static QName l3;
88     private static QName choiceInCon3;
89
90     private static QName listInChoiceKey;
91     private static QName k;
92
93     private static QName leafrefLeafList;
94
95     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest.class);
96     private static final String NEW_LINE = System.getProperty("line.separator");
97
98     @BeforeClass
99     public static void init() throws DataValidationFailedException {
100         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
101
102         for (final Module module : context.getModules()) {
103             if (module.getName().equals("leafref-validation")) {
104                 valModule = module;
105             }
106         }
107
108         valModuleQname = valModule.getQNameModule();
109         rootLeafRefContext = LeafRefContext.create(context);
110
111         odl = QName.create(valModuleQname, "odl-project");
112         project = QName.create(valModuleQname, "project");
113         name = QName.create(valModuleQname, "name");
114         desc = QName.create(valModuleQname, "desc");
115         lead = QName.create(valModuleQname, "project-lead");
116         owner = QName.create(valModuleQname, "project-owner");
117
118         odlContributor = QName.create(valModuleQname, "odl-contributor");
119         contributor = QName.create(valModuleQname, "contributor");
120         odlProjectName = QName.create(valModuleQname, "odl-project-name");
121         login = QName.create(valModuleQname, "login");
122         contributorName = QName.create(valModuleQname, "contributor-name");
123
124         con1 = QName.create(valModuleQname, "con1");
125         l1 = QName.create(valModuleQname, "l1");
126         l2 = QName.create(valModuleQname, "l2");
127         odlProjectDesc = QName.create(valModuleQname, "odl-project-desc");
128
129         ch1 = QName.create(valModuleQname, "ch1");
130         ch2 = QName.create(valModuleQname, "ch2");
131         leafrefInChoice = QName.create(valModuleQname, "leafref-in-choice");
132         listInChoice = QName.create(valModuleQname, "list-in-choice");
133
134         leafrefInChoiceToChoice = QName.create(valModuleQname,
135                 "leafref-in-choice-to-choice");
136         con3 = QName.create(valModuleQname, "con3");
137         list3InChoice = QName.create(valModuleQname, "list3-in-choice");
138         l3 = QName.create(valModuleQname, "l3");
139         choiceInCon3 = QName.create(valModuleQname, "choice-in-con3");
140
141         listInChoiceKey = QName.create(valModuleQname, "list-in-choice-key");
142         k = QName.create(valModuleQname, "k");
143
144         leafrefLeafList = QName.create(valModuleQname, "leafref-leaf-list");
145
146         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
147
148         final DataTreeModification initialDataTreeModification = inMemoryDataTree
149                 .takeSnapshot().newModification();
150
151         final ContainerSchemaNode odlProjContSchemaNode = (ContainerSchemaNode) valModule.findDataChildByName(odl)
152                 .get();
153
154         final ContainerNode odlProjectContainer = createOdlContainer(odlProjContSchemaNode);
155
156         final YangInstanceIdentifier path = YangInstanceIdentifier.of(odl);
157         initialDataTreeModification.write(path, odlProjectContainer);
158         initialDataTreeModification.ready();
159
160         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
161                 .prepare(initialDataTreeModification);
162         inMemoryDataTree.commit(writeContributorsCandidate);
163     }
164
165     @AfterClass
166     public static void cleanup() {
167         inMemoryDataTree = null;
168         rootLeafRefContext = null;
169         valModule = null;
170         context = null;
171     }
172
173     @Test
174     public void dataTreeCanditateValidationTest() throws DataValidationFailedException {
175         write();
176
177         write2();
178
179         delete();
180
181         writeContributors();
182
183         writeMapEntry();
184
185         writeIntoMapEntry();
186     }
187
188     private static void writeContributors() throws DataValidationFailedException {
189
190         final ContainerSchemaNode contributorContSchemaNode = (ContainerSchemaNode) valModule
191                 .findDataChildByName(odlContributor).get();
192
193         final ContainerNode contributorContainer = createBasicContributorContainer(contributorContSchemaNode);
194
195         final YangInstanceIdentifier contributorPath = YangInstanceIdentifier
196                 .of(odlContributor);
197         final DataTreeModification writeModification = inMemoryDataTree
198                 .takeSnapshot().newModification();
199         writeModification.write(contributorPath, contributorContainer);
200         writeModification.ready();
201
202         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
203                 .prepare(writeModification);
204
205         LOG.debug("*************************");
206         LOG.debug("Before write of contributors: ");
207         LOG.debug("*************************");
208         LOG.debug(inMemoryDataTree.toString());
209
210         boolean exception = false;
211         try {
212             LeafRefValidation.validate(writeContributorsCandidate,
213                     rootLeafRefContext);
214         } catch (final LeafRefDataValidationFailedException e) {
215             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
216             assertEquals(3, e.getValidationsErrorsCount());
217             exception = true;
218         }
219
220         inMemoryDataTree.commit(writeContributorsCandidate);
221
222         LOG.debug("*************************");
223         LOG.debug("After write of contributors: ");
224         LOG.debug("*************************");
225         LOG.debug(inMemoryDataTree.toString());
226
227         assertTrue(exception);
228
229     }
230
231     private static void writeIntoMapEntry() throws DataValidationFailedException {
232
233         final Map<QName, Object> keys = new HashMap<>();
234         keys.put(name, "New Project");
235         final NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(project, keys);
236
237         final YangInstanceIdentifier leaderPath = YangInstanceIdentifier
238                 .of(odl).node(project).node(mapEntryPath).node(lead);
239
240         final LeafNode<String> leader = ImmutableNodes.leafNode(lead,
241                 "Updated leader");
242
243         final DataTreeModification writeModification = inMemoryDataTree
244                 .takeSnapshot().newModification();
245         writeModification.write(leaderPath, leader);
246         writeModification.ready();
247
248         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
249                 .prepare(writeModification);
250
251         LOG.debug("*************************");
252         LOG.debug("Before write into map entry (update of leader name): ");
253         LOG.debug("*************************");
254         LOG.debug(inMemoryDataTree.toString());
255
256         boolean exception = false;
257         try {
258             LeafRefValidation.validate(writeContributorsCandidate,
259                     rootLeafRefContext);
260         } catch (final LeafRefDataValidationFailedException e) {
261             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
262             assertEquals(1, e.getValidationsErrorsCount());
263             exception = true;
264         }
265
266         inMemoryDataTree.commit(writeContributorsCandidate);
267
268         LOG.debug("*************************");
269         LOG.debug("After write into map entry (update of leader name): ");
270         LOG.debug("*************************");
271         LOG.debug(inMemoryDataTree.toString());
272
273         assertTrue(exception);
274
275     }
276
277     private static void writeMapEntry() throws DataValidationFailedException {
278
279         final Map<QName, Object> keys = new HashMap<>();
280         keys.put(name, "New Project");
281         final NodeIdentifierWithPredicates mapEntryPath = NodeIdentifierWithPredicates.of(project, keys);
282
283         final YangInstanceIdentifier newOdlProjectMapEntryPath = YangInstanceIdentifier
284                 .of(odl).node(project).node(mapEntryPath);
285
286         final ContainerSchemaNode odlProjContSchemaNode = (ContainerSchemaNode) valModule.findDataChildByName(odl)
287                 .get();
288         final ListSchemaNode projListSchemaNode = (ListSchemaNode) odlProjContSchemaNode.findDataChildByName(project)
289                 .get();
290         final MapEntryNode newProjectMapEntry = createProjectListEntry(
291                 "New Project", "New Project description ...",
292                 "Leader of New Project", "Owner of New Project",
293                 projListSchemaNode);
294
295         final DataTreeModification writeModification = inMemoryDataTree
296                 .takeSnapshot().newModification();
297         writeModification.write(newOdlProjectMapEntryPath, newProjectMapEntry);
298         writeModification.ready();
299
300         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
301                 .prepare(writeModification);
302
303         LOG.debug("*************************");
304         LOG.debug("Before map entry write: ");
305         LOG.debug("*************************");
306         LOG.debug(inMemoryDataTree.toString());
307
308         boolean exception = false;
309         try {
310             LeafRefValidation.validate(writeContributorsCandidate,
311                     rootLeafRefContext);
312         } catch (final LeafRefDataValidationFailedException e) {
313             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
314             assertEquals(2, e.getValidationsErrorsCount());
315             exception = true;
316         }
317
318         inMemoryDataTree.commit(writeContributorsCandidate);
319
320         LOG.debug("*************************");
321         LOG.debug("After map entry write: ");
322         LOG.debug("*************************");
323         LOG.debug(inMemoryDataTree.toString());
324
325         assertTrue(exception);
326
327     }
328
329     private static void write() throws DataValidationFailedException {
330
331         final ContainerSchemaNode contributorContSchemaNode = (ContainerSchemaNode) valModule
332                 .findDataChildByName(odlContributor).get();
333
334         final ContainerNode contributorContainer = createContributorContainer(contributorContSchemaNode);
335
336         final YangInstanceIdentifier contributorPath = YangInstanceIdentifier
337                 .of(odlContributor);
338         final DataTreeModification writeModification = inMemoryDataTree
339                 .takeSnapshot().newModification();
340         writeModification.write(contributorPath, contributorContainer);
341
342         writeModification.write(YangInstanceIdentifier.of(l1),
343                 ImmutableNodes.leafNode(l1, "Leafref l1 under the root"));
344         writeModification
345                 .write(YangInstanceIdentifier.of(l2), ImmutableNodes.leafNode(
346                         l2, "Leafref target l2 under the root"));
347
348         writeModification.ready();
349         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
350                 .prepare(writeModification);
351
352         LOG.debug("*************************");
353         LOG.debug("Before write: ");
354         LOG.debug("*************************");
355         LOG.debug(inMemoryDataTree.toString());
356
357         boolean exception = false;
358         try {
359             LeafRefValidation.validate(writeContributorsCandidate,
360                     rootLeafRefContext);
361         } catch (final LeafRefDataValidationFailedException e) {
362             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
363             assertEquals(12, e.getValidationsErrorsCount());
364             exception = true;
365         }
366
367         inMemoryDataTree.commit(writeContributorsCandidate);
368
369         LOG.debug("*************************");
370         LOG.debug("After write: ");
371         LOG.debug("*************************");
372         LOG.debug(inMemoryDataTree.toString());
373
374         assertTrue(exception);
375     }
376
377     private static void write2() throws DataValidationFailedException {
378
379         final ContainerSchemaNode odlCon = (ContainerSchemaNode) valModule.findDataChildByName(odl).get();
380         final ContainerSchemaNode con1Con = (ContainerSchemaNode) odlCon.findDataChildByName(con1).get();
381         final LeafNode<String> l1Leaf = ImmutableNodes.leafNode(l1, "l1 value");
382         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders
383                 .containerBuilder(con1Con);
384         containerBuilder.addChild(l1Leaf);
385         final ContainerNode con1Node = containerBuilder.build();
386
387         final YangInstanceIdentifier con1Path = YangInstanceIdentifier.of(odl)
388                 .node(con1);
389         final DataTreeModification writeModification = inMemoryDataTree
390                 .takeSnapshot().newModification();
391         writeModification.write(con1Path, con1Node);
392
393         final ChoiceNode choiceNode = createChoiceNode();
394         final YangInstanceIdentifier choicePath = YangInstanceIdentifier
395                 .of(odl).node(ch1);
396         writeModification.write(choicePath, choiceNode);
397
398         final ContainerNode con3Node = createCon3Node();
399         final YangInstanceIdentifier con3Path = YangInstanceIdentifier.of(odl)
400                 .node(con3);
401         writeModification.write(con3Path, con3Node);
402
403         final LeafSetNode<?> leafListNode = createLeafRefLeafListNode();
404         final YangInstanceIdentifier leafListPath = YangInstanceIdentifier.of(
405                 odl).node(leafrefLeafList);
406         writeModification.write(leafListPath, leafListNode);
407         writeModification.ready();
408
409         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
410                 .prepare(writeModification);
411
412         LOG.debug("*************************");
413         LOG.debug("Before write2: ");
414         LOG.debug("*************************");
415         LOG.debug(inMemoryDataTree.toString());
416
417         boolean exception = false;
418         try {
419             LeafRefValidation.validate(writeContributorsCandidate,
420                     rootLeafRefContext);
421         } catch (final LeafRefDataValidationFailedException e) {
422             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
423             assertEquals(6, e.getValidationsErrorsCount());
424             exception = true;
425         }
426
427         assertTrue(exception);
428
429         inMemoryDataTree.commit(writeContributorsCandidate);
430
431         LOG.debug("*************************");
432         LOG.debug("After write2: ");
433         LOG.debug("*************************");
434         LOG.debug(inMemoryDataTree.toString());
435
436     }
437
438     private static @NonNull LeafSetNode<Object> createLeafRefLeafListNode() {
439         return Builders.leafSetBuilder()
440             .withNodeIdentifier(new NodeIdentifier(leafrefLeafList))
441             .addChild(createLeafSetEntry(leafrefLeafList, "k1"))
442             .addChild(createLeafSetEntry(leafrefLeafList, "k2"))
443             .addChild(createLeafSetEntry(leafrefLeafList, "k3"))
444             .build();
445     }
446
447     private static ContainerNode createCon3Node() {
448
449         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapBuilder = Builders.mapBuilder();
450         mapBuilder.withNodeIdentifier(new NodeIdentifier(list3InChoice));
451
452         mapBuilder.addChild(createList3Entry("k1", "val1", "valA", "valX"));
453         mapBuilder.addChild(createList3Entry("k2", "val2", "valB", "valY"));
454
455         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choiceBuilder = Builders
456                 .choiceBuilder();
457         choiceBuilder.withNodeIdentifier(new NodeIdentifier(choiceInCon3));
458
459         choiceBuilder.addChild(mapBuilder.build());
460
461         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders
462                 .containerBuilder();
463         containerBuilder.withNodeIdentifier(new NodeIdentifier(con3));
464
465         containerBuilder.addChild(choiceBuilder.build());
466
467         return containerBuilder.build();
468     }
469
470     private static MapEntryNode createList3Entry(final String keyVal,
471             final String l3Val1, final String l3Val2, final String l3Val3) {
472         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = Builders
473                 .mapEntryBuilder();
474         mapEntryBuilder.withNodeIdentifier(NodeIdentifierWithPredicates.of(list3InChoice, k, keyVal));
475
476         final ListNodeBuilder<Object, SystemLeafSetNode<Object>> leafSetBuilder = Builders.leafSetBuilder();
477         leafSetBuilder.withNodeIdentifier(new NodeIdentifier(l3));
478
479         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val1));
480         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val2));
481         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val3));
482
483         mapEntryBuilder.addChild(ImmutableNodes.leafNode(k, keyVal));
484         mapEntryBuilder.addChild(leafSetBuilder.build());
485
486         return mapEntryBuilder.build();
487     }
488
489     private static LeafSetEntryNode<Object> createLeafSetEntry(
490             final QName qname, final String val) {
491         final NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder = Builders
492                 .leafSetEntryBuilder();
493         leafSetEntryBuilder.withNodeIdentifier(new NodeWithValue<>(qname, val));
494         leafSetEntryBuilder.withValue(val);
495         return leafSetEntryBuilder.build();
496     }
497
498     private static ChoiceNode createChoiceNode() {
499
500         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> listInChoiceBuilder = Builders.mapBuilder();
501         listInChoiceBuilder.withNodeIdentifier(new NodeIdentifier(listInChoice));
502
503         listInChoiceBuilder.addChild(createListInChoiceEntry("key1", "leafref-in-choice value", "val1"));
504         listInChoiceBuilder.addChild(createListInChoiceEntry("key2", "l1 value", "val2"));
505         listInChoiceBuilder.addChild(createListInChoiceEntry("key3", "l1 value", "val3"));
506
507         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice2Builder = Builders.choiceBuilder();
508         choice2Builder.withNodeIdentifier(new NodeIdentifier(ch2));
509
510         choice2Builder.addChild(listInChoiceBuilder.build());
511
512         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choiceBuilder = Builders.choiceBuilder();
513         choiceBuilder.withNodeIdentifier(new NodeIdentifier(ch1));
514         choiceBuilder.addChild(choice2Builder.build());
515
516         return choiceBuilder.build();
517     }
518
519     private static MapEntryNode createListInChoiceEntry(final String keyVal,
520             final String leafrefInChoiceVal,
521             final String leafrefInChoiceToChoiceVal) {
522
523         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = Builders
524                 .mapEntryBuilder();
525
526         mapEntryBuilder.withNodeIdentifier(NodeIdentifierWithPredicates.of(
527                 listInChoice, listInChoiceKey, keyVal));
528
529         mapEntryBuilder.addChild(ImmutableNodes.leafNode(listInChoiceKey,
530                 keyVal));
531         mapEntryBuilder.addChild(ImmutableNodes.leafNode(leafrefInChoice,
532                 leafrefInChoiceVal));
533         mapEntryBuilder.addChild(ImmutableNodes.leafNode(
534                 leafrefInChoiceToChoice, leafrefInChoiceToChoiceVal));
535
536         return mapEntryBuilder.build();
537     }
538
539     private static void delete() throws DataValidationFailedException {
540
541         final YangInstanceIdentifier contributorPath = YangInstanceIdentifier
542                 .of(odlContributor);
543         final DataTreeModification delete = inMemoryDataTree.takeSnapshot()
544                 .newModification();
545         delete.delete(contributorPath);
546         delete.ready();
547
548         final DataTreeCandidate deleteContributorsCanditate = inMemoryDataTree.prepare(delete);
549
550         LOG.debug("*************************");
551         LOG.debug("Before delete: ");
552         LOG.debug("*************************");
553         LOG.debug(inMemoryDataTree.toString());
554
555         boolean exception = false;
556         try {
557             LeafRefValidation.validate(deleteContributorsCanditate,
558                     rootLeafRefContext);
559         } catch (final LeafRefDataValidationFailedException e) {
560             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
561             assertEquals(6, e.getValidationsErrorsCount());
562             exception = true;
563         }
564
565         assertTrue(exception);
566
567         inMemoryDataTree.commit(deleteContributorsCanditate);
568
569         LOG.debug("*************************");
570         LOG.debug("After delete: ");
571         LOG.debug("*************************");
572         LOG.debug(inMemoryDataTree.toString());
573
574     }
575
576     private static ContainerNode createContributorContainer(
577             final ContainerSchemaNode contributorContSchemaNode) {
578
579         final ListSchemaNode contributorListSchemaNode =
580                 (ListSchemaNode) contributorContSchemaNode.findDataChildByName(contributor).get();
581
582         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contributorContainerBldr =
583                 Builders.containerBuilder(contributorContSchemaNode);
584
585         final SystemMapNode contributorMap = createContributorList(contributorListSchemaNode);
586         contributorContainerBldr.addChild(contributorMap);
587
588         final ContainerNode contributorContainer = contributorContainerBldr.build();
589
590         return contributorContainer;
591
592     }
593
594     private static SystemMapNode createContributorList(
595             final ListSchemaNode contributorListSchemaNode) {
596
597         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> contributorMapBldr =
598                 Builders.mapBuilder(contributorListSchemaNode);
599
600         final MapEntryNode contributorMapEntry1 = createContributorListEntry(
601                 "Leader of Yangtools", "Yangtools Leader name", "Yangtools",
602                 "Yangtools description ...", contributorListSchemaNode);
603         final MapEntryNode contributorMapEntry2 = createContributorListEntry(
604                 "Leader of MD-SAL", "MD-SAL Leader name", "MD-SAL",
605                 "MD-SAL description ...", contributorListSchemaNode);
606         final MapEntryNode contributorMapEntry3 = createContributorListEntry(
607                 "Leader of Controller", "Controller Leader name", "Controller",
608                 "Controller description ...", contributorListSchemaNode);
609
610         final MapEntryNode contributorMapEntry4 = createContributorListEntry(
611                 "jdoe", "John Doe", "MD-SAL", "Yangtools description ...",
612                 contributorListSchemaNode);
613
614         final MapEntryNode contributorMapEntry5 = createContributorListEntry(
615                 "foo", "foo name", "Controller", "MD-SAL description ...",
616                 contributorListSchemaNode);
617
618         final MapEntryNode contributorMapEntry6 = createContributorListEntry(
619                 "bar", "bar name", "Yangtools", "Controller description ...",
620                 contributorListSchemaNode);
621
622         final MapEntryNode contributorMapEntry7 = createContributorListEntry(
623                 "baz", "baz name", "Unknown Project",
624                 "Unknown Project description ...", contributorListSchemaNode);
625
626         final MapEntryNode contributorMapEntry8 = createContributorListEntry(
627                 "pk", "pk name", "Unknown Project 2",
628                 "Controller description ...", contributorListSchemaNode);
629
630         contributorMapBldr.addChild(contributorMapEntry1);
631         contributorMapBldr.addChild(contributorMapEntry2);
632         contributorMapBldr.addChild(contributorMapEntry3);
633         contributorMapBldr.addChild(contributorMapEntry4);
634         contributorMapBldr.addChild(contributorMapEntry5);
635         contributorMapBldr.addChild(contributorMapEntry6);
636         contributorMapBldr.addChild(contributorMapEntry7);
637         contributorMapBldr.addChild(contributorMapEntry8);
638
639         return contributorMapBldr.build();
640     }
641
642     private static MapEntryNode createContributorListEntry(
643             final String loginVal, final String contributorNameVal,
644             final String odlProjectNameVal, final String odlProjectDescVal,
645             final ListSchemaNode contributorListSchemaNode) {
646
647         final LeafNode<String> loginLeaf = ImmutableNodes.leafNode(login, loginVal);
648         final LeafNode<String> contributorNameLeaf = ImmutableNodes.leafNode(contributorName, contributorNameVal);
649         final LeafNode<String> odlProjectNameLeafRef = ImmutableNodes.leafNode(odlProjectName, odlProjectNameVal);
650         final LeafNode<String> odlProjectDescLeafRef = ImmutableNodes.leafNode(odlProjectDesc, odlProjectDescVal);
651
652         return Builders.mapEntryBuilder(contributorListSchemaNode)
653                 .addChild(loginLeaf)
654                 .addChild(contributorNameLeaf)
655                 .addChild(odlProjectNameLeafRef)
656                 .addChild(odlProjectDescLeafRef)
657                 .build();
658     }
659
660     private static ContainerNode createOdlContainer(
661             final ContainerSchemaNode container) {
662
663         final ListSchemaNode projListSchemaNode = (ListSchemaNode) container.findDataChildByName(project).get();
664
665         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> odlProjectContainerBldr = Builders
666                 .containerBuilder(container);
667
668         final SystemMapNode projectMap = createProjectList(projListSchemaNode);
669         odlProjectContainerBldr.addChild(projectMap);
670
671         final ContainerNode odlProjectContainer = odlProjectContainerBldr
672                 .build();
673
674         return odlProjectContainer;
675     }
676
677     private static SystemMapNode createProjectList(
678             final ListSchemaNode projListSchemaNode) {
679
680         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> projectMapBldr = Builders
681                 .mapBuilder(projListSchemaNode);
682
683         final MapEntryNode projMapEntry1 = createProjectListEntry("Yangtools",
684                 "Yangtools description ...", "Leader of Yangtools",
685                 "Owner of Yangtools", projListSchemaNode);
686         final MapEntryNode projMapEntry2 = createProjectListEntry("MD-SAL",
687                 "MD-SAL description ...", "Leader of MD-SAL",
688                 "Owner of MD-SAL", projListSchemaNode);
689         final MapEntryNode projMapEntry3 = createProjectListEntry("Controller",
690                 "Controller description ...", "Leader of Controller",
691                 "Owner of Controller", projListSchemaNode);
692
693         projectMapBldr.addChild(projMapEntry1);
694         projectMapBldr.addChild(projMapEntry2);
695         projectMapBldr.addChild(projMapEntry3);
696
697         return projectMapBldr.build();
698     }
699
700     private static MapEntryNode createProjectListEntry(final String nameVal,
701             final String descVal, final String leadVal, final String ownerVal,
702             final ListSchemaNode projListSchemaNode) {
703
704         final LeafNode<String> nameLeaf = ImmutableNodes
705                 .leafNode(name, nameVal);
706         final LeafNode<String> descLeaf = ImmutableNodes
707                 .leafNode(desc, descVal);
708         final LeafNode<String> leadLeafRef = ImmutableNodes.leafNode(lead,
709                 leadVal);
710         final LeafNode<String> ownerLeafRef = ImmutableNodes.leafNode(owner,
711                 ownerVal);
712
713         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> projMapEntryBldr = Builders
714                 .mapEntryBuilder(projListSchemaNode);
715
716         projMapEntryBldr.addChild(nameLeaf);
717         projMapEntryBldr.addChild(descLeaf);
718         projMapEntryBldr.addChild(leadLeafRef);
719         projMapEntryBldr.addChild(ownerLeafRef);
720         final MapEntryNode projMapEntry = projMapEntryBldr.build();
721
722         return projMapEntry;
723     }
724
725     private static ContainerNode createBasicContributorContainer(
726             final ContainerSchemaNode contributorContSchemaNode) {
727
728         final ListSchemaNode contributorListSchemaNode = (ListSchemaNode) contributorContSchemaNode
729                 .findDataChildByName(contributor).get();
730
731         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contributorContainerBldr = Builders
732                 .containerBuilder(contributorContSchemaNode);
733
734         final SystemMapNode contributorMap = createBasicContributorList(contributorListSchemaNode);
735         contributorContainerBldr.addChild(contributorMap);
736
737         final ContainerNode contributorContainer = contributorContainerBldr
738                 .build();
739
740         return contributorContainer;
741
742     }
743
744     private static SystemMapNode createBasicContributorList(
745             final ListSchemaNode contributorListSchemaNode) {
746
747         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> contributorMapBldr =
748             Builders.mapBuilder(contributorListSchemaNode);
749
750         final MapEntryNode contributorMapEntry1 = createContributorListEntry(
751                 "Leader of Yangtools", "Yangtools Leader name", "Yangtools",
752                 "Yangtools description ...", contributorListSchemaNode);
753         final MapEntryNode contributorMapEntry2 = createContributorListEntry(
754                 "Leader of MD-SAL", "MD-SAL Leader name", "MD-SAL",
755                 "MD-SAL description ...", contributorListSchemaNode);
756         final MapEntryNode contributorMapEntry3 = createContributorListEntry(
757                 "Leader of Controller", "Controller Leader name", "Controller",
758                 "Controller description ...", contributorListSchemaNode);
759
760         contributorMapBldr.addChild(contributorMapEntry1);
761         contributorMapBldr.addChild(contributorMapEntry2);
762         contributorMapBldr.addChild(contributorMapEntry3);
763
764         return contributorMapBldr.build();
765     }
766 }