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