Cleanup resources allocated in @BeforeClass
[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.impl.leafref.LeafRefContext;
38 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
39 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidatation;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
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.NormalizedNodeAttrBuilder;
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()  {
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
157                 .getDataChildByName(odl);
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() {
180         write();
181
182         write2();
183
184         delete();
185
186         writeContributors();
187
188         writeMapEntry();
189
190         writeIntoMapEntry();
191     }
192
193     private static void writeContributors() {
194
195         final ContainerSchemaNode contributorContSchemaNode = (ContainerSchemaNode) valModule
196                 .getDataChildByName(odlContributor);
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             LeafRefValidatation.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() {
237
238         final Map<QName, Object> keys = new HashMap<>();
239         keys.put(name, "New Project");
240         final NodeIdentifierWithPredicates mapEntryPath = new NodeIdentifierWithPredicates(
241                 project, keys);
242
243         final YangInstanceIdentifier leaderPath = YangInstanceIdentifier
244                 .of(odl).node(project).node(mapEntryPath).node(lead);
245
246         final LeafNode<String> leader = ImmutableNodes.leafNode(lead,
247                 "Updated leader");
248
249         final DataTreeModification writeModification = inMemoryDataTree
250                 .takeSnapshot().newModification();
251         writeModification.write(leaderPath, leader);
252         writeModification.ready();
253
254         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
255                 .prepare(writeModification);
256
257         LOG.debug("*************************");
258         LOG.debug("Before write into map entry (update of leader name): ");
259         LOG.debug("*************************");
260         LOG.debug(inMemoryDataTree.toString());
261
262         boolean exception = false;
263         try {
264             LeafRefValidatation.validate(writeContributorsCandidate,
265                     rootLeafRefContext);
266         } catch (final LeafRefDataValidationFailedException e) {
267             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
268             assertEquals(1, e.getValidationsErrorsCount());
269             exception = true;
270         }
271
272         inMemoryDataTree.commit(writeContributorsCandidate);
273
274         LOG.debug("*************************");
275         LOG.debug("After write into map entry (update of leader name): ");
276         LOG.debug("*************************");
277         LOG.debug(inMemoryDataTree.toString());
278
279         assertTrue(exception);
280
281     }
282
283     private static void writeMapEntry() {
284
285         final Map<QName, Object> keys = new HashMap<>();
286         keys.put(name, "New Project");
287         final NodeIdentifierWithPredicates mapEntryPath = new NodeIdentifierWithPredicates(
288                 project, keys);
289
290         final YangInstanceIdentifier newOdlProjectMapEntryPath = YangInstanceIdentifier
291                 .of(odl).node(project).node(mapEntryPath);
292
293         final ContainerSchemaNode odlProjContSchemaNode = (ContainerSchemaNode) valModule
294                 .getDataChildByName(odl);
295         final ListSchemaNode projListSchemaNode = (ListSchemaNode) odlProjContSchemaNode
296                 .getDataChildByName(project);
297         final MapEntryNode newProjectMapEntry = createProjectListEntry(
298                 "New Project", "New Project description ...",
299                 "Leader of New Project", "Owner of New Project",
300                 projListSchemaNode);
301
302         final DataTreeModification writeModification = inMemoryDataTree
303                 .takeSnapshot().newModification();
304         writeModification.write(newOdlProjectMapEntryPath, newProjectMapEntry);
305         writeModification.ready();
306
307         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
308                 .prepare(writeModification);
309
310         LOG.debug("*************************");
311         LOG.debug("Before map entry write: ");
312         LOG.debug("*************************");
313         LOG.debug(inMemoryDataTree.toString());
314
315         boolean exception = false;
316         try {
317             LeafRefValidatation.validate(writeContributorsCandidate,
318                     rootLeafRefContext);
319         } catch (final LeafRefDataValidationFailedException e) {
320             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
321             assertEquals(2, e.getValidationsErrorsCount());
322             exception = true;
323         }
324
325         inMemoryDataTree.commit(writeContributorsCandidate);
326
327         LOG.debug("*************************");
328         LOG.debug("After map entry write: ");
329         LOG.debug("*************************");
330         LOG.debug(inMemoryDataTree.toString());
331
332         assertTrue(exception);
333
334     }
335
336     private static void write() {
337
338         final ContainerSchemaNode contributorContSchemaNode = (ContainerSchemaNode) valModule
339                 .getDataChildByName(odlContributor);
340
341         final ContainerNode contributorContainer = createContributorContainer(contributorContSchemaNode);
342
343         final YangInstanceIdentifier contributorPath = YangInstanceIdentifier
344                 .of(odlContributor);
345         final DataTreeModification writeModification = inMemoryDataTree
346                 .takeSnapshot().newModification();
347         writeModification.write(contributorPath, contributorContainer);
348
349         writeModification.write(YangInstanceIdentifier.of(l1),
350                 ImmutableNodes.leafNode(l1, "Leafref l1 under the root"));
351         writeModification
352                 .write(YangInstanceIdentifier.of(l2), ImmutableNodes.leafNode(
353                         l2, "Leafref target l2 under the root"));
354
355         writeModification.ready();
356         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
357                 .prepare(writeModification);
358
359         LOG.debug("*************************");
360         LOG.debug("Before write: ");
361         LOG.debug("*************************");
362         LOG.debug(inMemoryDataTree.toString());
363
364         boolean exception = false;
365         try {
366             LeafRefValidatation.validate(writeContributorsCandidate,
367                     rootLeafRefContext);
368         } catch (final LeafRefDataValidationFailedException e) {
369             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
370             assertEquals(12, e.getValidationsErrorsCount());
371             exception = true;
372         }
373
374         inMemoryDataTree.commit(writeContributorsCandidate);
375
376         LOG.debug("*************************");
377         LOG.debug("After write: ");
378         LOG.debug("*************************");
379         LOG.debug(inMemoryDataTree.toString());
380
381         assertTrue(exception);
382     }
383
384     private static void write2() {
385
386         final ContainerSchemaNode odlCon = (ContainerSchemaNode) valModule
387                 .getDataChildByName(odl);
388         final ContainerSchemaNode con1Con = (ContainerSchemaNode) odlCon
389                 .getDataChildByName(con1);
390         final LeafNode<String> l1Leaf = ImmutableNodes.leafNode(l1, "l1 value");
391         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders
392                 .containerBuilder(con1Con);
393         containerBuilder.addChild(l1Leaf);
394         final ContainerNode con1Node = containerBuilder.build();
395
396         final YangInstanceIdentifier con1Path = YangInstanceIdentifier.of(odl)
397                 .node(con1);
398         final DataTreeModification writeModification = inMemoryDataTree
399                 .takeSnapshot().newModification();
400         writeModification.write(con1Path, con1Node);
401
402         final ChoiceNode choiceNode = createChoiceNode();
403         final YangInstanceIdentifier choicePath = YangInstanceIdentifier
404                 .of(odl).node(ch1);
405         writeModification.write(choicePath, choiceNode);
406
407         final ContainerNode con3Node = createCon3Node();
408         final YangInstanceIdentifier con3Path = YangInstanceIdentifier.of(odl)
409                 .node(con3);
410         writeModification.write(con3Path, con3Node);
411
412         final LeafSetNode<?> leafListNode = createLeafRefLeafListNode();
413         final YangInstanceIdentifier leafListPath = YangInstanceIdentifier.of(
414                 odl).node(leafrefLeafList);
415         writeModification.write(leafListPath, leafListNode);
416         writeModification.ready();
417
418         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
419                 .prepare(writeModification);
420
421         LOG.debug("*************************");
422         LOG.debug("Before write2: ");
423         LOG.debug("*************************");
424         LOG.debug(inMemoryDataTree.toString());
425
426         boolean exception = false;
427         try {
428             LeafRefValidatation.validate(writeContributorsCandidate,
429                     rootLeafRefContext);
430         } catch (final LeafRefDataValidationFailedException e) {
431             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
432             assertEquals(6, e.getValidationsErrorsCount());
433             exception = true;
434         }
435
436         assertTrue(exception);
437
438         inMemoryDataTree.commit(writeContributorsCandidate);
439
440         LOG.debug("*************************");
441         LOG.debug("After write2: ");
442         LOG.debug("*************************");
443         LOG.debug(inMemoryDataTree.toString());
444
445     }
446
447     private static LeafSetNode<?> createLeafRefLeafListNode() {
448
449         final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders
450                 .leafSetBuilder();
451         leafSetBuilder.withNodeIdentifier(new NodeIdentifier(leafrefLeafList));
452
453         leafSetBuilder.addChild(createLeafSetEntry(leafrefLeafList, "k1"));
454         leafSetBuilder.addChild(createLeafSetEntry(leafrefLeafList, "k2"));
455         leafSetBuilder.addChild(createLeafSetEntry(leafrefLeafList, "k3"));
456
457         return leafSetBuilder.build();
458     }
459
460     private static ContainerNode createCon3Node() {
461
462         final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders
463                 .mapBuilder();
464         mapBuilder.withNodeIdentifier(new NodeIdentifier(list3InChoice));
465
466         mapBuilder.addChild(createList3Entry("k1", "val1", "valA", "valX"));
467         mapBuilder.addChild(createList3Entry("k2", "val2", "valB", "valY"));
468
469         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choiceBuilder = Builders
470                 .choiceBuilder();
471         choiceBuilder.withNodeIdentifier(new NodeIdentifier(choiceInCon3));
472
473         choiceBuilder.addChild(mapBuilder.build());
474
475         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders
476                 .containerBuilder();
477         containerBuilder.withNodeIdentifier(new NodeIdentifier(con3));
478
479         containerBuilder.addChild(choiceBuilder.build());
480
481         return containerBuilder.build();
482     }
483
484     private static MapEntryNode createList3Entry(final String keyVal,
485             final String l3Val1, final String l3Val2, final String l3Val3) {
486         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = Builders
487                 .mapEntryBuilder();
488         mapEntryBuilder.withNodeIdentifier(new NodeIdentifierWithPredicates(
489                 list3InChoice, k, keyVal));
490
491         final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders
492                 .leafSetBuilder();
493         leafSetBuilder.withNodeIdentifier(new NodeIdentifier(l3));
494
495         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val1));
496         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val2));
497         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val3));
498
499         mapEntryBuilder.addChild(ImmutableNodes.leafNode(k, keyVal));
500         mapEntryBuilder.addChild(leafSetBuilder.build());
501
502         return mapEntryBuilder.build();
503     }
504
505     private static LeafSetEntryNode<Object> createLeafSetEntry(
506             final QName qname, final String val) {
507         final NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder = Builders
508                 .leafSetEntryBuilder();
509         leafSetEntryBuilder.withNodeIdentifier(new NodeWithValue<>(qname, val));
510         leafSetEntryBuilder.withValue(val);
511         return leafSetEntryBuilder.build();
512     }
513
514     private static ChoiceNode createChoiceNode() {
515
516         final CollectionNodeBuilder<MapEntryNode, MapNode> listInChoiceBuilder = Builders
517                 .mapBuilder();
518         listInChoiceBuilder
519                 .withNodeIdentifier(new NodeIdentifier(listInChoice));
520
521         listInChoiceBuilder.addChild(createListInChoiceEntry("key1",
522                 "leafref-in-choice value", "val1"));
523         listInChoiceBuilder.addChild(createListInChoiceEntry("key2",
524                 "l1 value", "val2"));
525         listInChoiceBuilder.addChild(createListInChoiceEntry("key3",
526                 "l1 value", "val3"));
527
528         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice2Builder = Builders
529                 .choiceBuilder();
530         choice2Builder.withNodeIdentifier(new NodeIdentifier(ch2));
531
532         choice2Builder.addChild(listInChoiceBuilder.build());
533
534         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choiceBuilder = Builders
535                 .choiceBuilder();
536         choiceBuilder.withNodeIdentifier(new NodeIdentifier(ch1));
537         choiceBuilder.addChild(choice2Builder.build());
538
539         return choiceBuilder.build();
540     }
541
542     private static MapEntryNode createListInChoiceEntry(final String keyVal,
543             final String leafrefInChoiceVal,
544             final String leafrefInChoiceToChoiceVal) {
545
546         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = Builders
547                 .mapEntryBuilder();
548
549         mapEntryBuilder.withNodeIdentifier(new NodeIdentifierWithPredicates(
550                 listInChoice, listInChoiceKey, keyVal));
551
552         mapEntryBuilder.addChild(ImmutableNodes.leafNode(listInChoiceKey,
553                 keyVal));
554         mapEntryBuilder.addChild(ImmutableNodes.leafNode(leafrefInChoice,
555                 leafrefInChoiceVal));
556         mapEntryBuilder.addChild(ImmutableNodes.leafNode(
557                 leafrefInChoiceToChoice, leafrefInChoiceToChoiceVal));
558
559         return mapEntryBuilder.build();
560     }
561
562     private static void delete() {
563
564         final YangInstanceIdentifier contributorPath = YangInstanceIdentifier
565                 .of(odlContributor);
566         final DataTreeModification delete = inMemoryDataTree.takeSnapshot()
567                 .newModification();
568         delete.delete(contributorPath);
569         delete.ready();
570
571         final DataTreeCandidate deleteContributorsCanditate = inMemoryDataTree
572                 .prepare(delete);
573
574         LOG.debug("*************************");
575         LOG.debug("Before delete: ");
576         LOG.debug("*************************");
577         LOG.debug(inMemoryDataTree.toString());
578
579         boolean exception = false;
580         try {
581             LeafRefValidatation.validate(deleteContributorsCanditate,
582                     rootLeafRefContext);
583         } catch (final LeafRefDataValidationFailedException e) {
584             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
585             assertEquals(6, e.getValidationsErrorsCount());
586             exception = true;
587         }
588
589         assertTrue(exception);
590
591         inMemoryDataTree.commit(deleteContributorsCanditate);
592
593         LOG.debug("*************************");
594         LOG.debug("After delete: ");
595         LOG.debug("*************************");
596         LOG.debug(inMemoryDataTree.toString());
597
598     }
599
600     private static ContainerNode createContributorContainer(
601             final ContainerSchemaNode contributorContSchemaNode) {
602
603         final ListSchemaNode contributorListSchemaNode = (ListSchemaNode) contributorContSchemaNode
604                 .getDataChildByName(contributor);
605
606         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> contributorContainerBldr = Builders
607                 .containerBuilder(contributorContSchemaNode);
608
609         final MapNode contributorMap = createContributorList(contributorListSchemaNode);
610         contributorContainerBldr.addChild(contributorMap);
611
612         final ContainerNode contributorContainer = contributorContainerBldr
613                 .build();
614
615         return contributorContainer;
616
617     }
618
619     private static MapNode createContributorList(
620             final ListSchemaNode contributorListSchemaNode) {
621
622         final CollectionNodeBuilder<MapEntryNode, MapNode> contributorMapBldr = Builders
623                 .mapBuilder(contributorListSchemaNode);
624
625         final MapEntryNode contributorMapEntry1 = createContributorListEntry(
626                 "Leader of Yangtools", "Yangtools Leader name", "Yangtools",
627                 "Yangtools description ...", contributorListSchemaNode);
628         final MapEntryNode contributorMapEntry2 = createContributorListEntry(
629                 "Leader of MD-SAL", "MD-SAL Leader name", "MD-SAL",
630                 "MD-SAL description ...", contributorListSchemaNode);
631         final MapEntryNode contributorMapEntry3 = createContributorListEntry(
632                 "Leader of Controller", "Controller Leader name", "Controller",
633                 "Controller description ...", contributorListSchemaNode);
634
635         final MapEntryNode contributorMapEntry4 = createContributorListEntry(
636                 "jdoe", "John Doe", "MD-SAL", "Yangtools description ...",
637                 contributorListSchemaNode);
638
639         final MapEntryNode contributorMapEntry5 = createContributorListEntry(
640                 "foo", "foo name", "Controller", "MD-SAL description ...",
641                 contributorListSchemaNode);
642
643         final MapEntryNode contributorMapEntry6 = createContributorListEntry(
644                 "bar", "bar name", "Yangtools", "Controller description ...",
645                 contributorListSchemaNode);
646
647         final MapEntryNode contributorMapEntry7 = createContributorListEntry(
648                 "baz", "baz name", "Unknown Project",
649                 "Unknown Project description ...", contributorListSchemaNode);
650
651         final MapEntryNode contributorMapEntry8 = createContributorListEntry(
652                 "pk", "pk name", "Unknown Project 2",
653                 "Controller description ...", contributorListSchemaNode);
654
655         contributorMapBldr.addChild(contributorMapEntry1);
656         contributorMapBldr.addChild(contributorMapEntry2);
657         contributorMapBldr.addChild(contributorMapEntry3);
658         contributorMapBldr.addChild(contributorMapEntry4);
659         contributorMapBldr.addChild(contributorMapEntry5);
660         contributorMapBldr.addChild(contributorMapEntry6);
661         contributorMapBldr.addChild(contributorMapEntry7);
662         contributorMapBldr.addChild(contributorMapEntry8);
663
664         final MapNode contributorMap = contributorMapBldr.build();
665
666         return contributorMap;
667
668     }
669
670     private static MapEntryNode createContributorListEntry(
671             final String loginVal, final String contributorNameVal,
672             final String odlProjectNameVal, final String odlProjectDescVal,
673             final ListSchemaNode contributorListSchemaNode) {
674
675         final LeafNode<String> loginLeaf = ImmutableNodes.leafNode(login,
676                 loginVal);
677         final LeafNode<String> contributorNameLeaf = ImmutableNodes.leafNode(
678                 contributorName, contributorNameVal);
679         final LeafNode<String> odlProjectNameLeafRef = ImmutableNodes.leafNode(
680                 odlProjectName, odlProjectNameVal);
681         final LeafNode<String> odlProjectDescLeafRef = ImmutableNodes.leafNode(
682                 odlProjectDesc, odlProjectDescVal);
683
684         return Builders.mapEntryBuilder(contributorListSchemaNode)
685                 .addChild(loginLeaf)
686                 .addChild(contributorNameLeaf)
687                 .addChild(odlProjectNameLeafRef)
688                 .addChild(odlProjectDescLeafRef)
689                 .build();
690     }
691
692     private static ContainerNode createOdlContainer(
693             final ContainerSchemaNode container) {
694
695         final ListSchemaNode projListSchemaNode = (ListSchemaNode) container
696                 .getDataChildByName(project);
697
698         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> odlProjectContainerBldr = Builders
699                 .containerBuilder(container);
700
701         final MapNode projectMap = createProjectList(projListSchemaNode);
702         odlProjectContainerBldr.addChild(projectMap);
703
704         final ContainerNode odlProjectContainer = odlProjectContainerBldr
705                 .build();
706
707         return odlProjectContainer;
708     }
709
710     private static MapNode createProjectList(
711             final ListSchemaNode projListSchemaNode) {
712
713         final CollectionNodeBuilder<MapEntryNode, MapNode> projectMapBldr = Builders
714                 .mapBuilder(projListSchemaNode);
715
716         final MapEntryNode projMapEntry1 = createProjectListEntry("Yangtools",
717                 "Yangtools description ...", "Leader of Yangtools",
718                 "Owner of Yangtools", projListSchemaNode);
719         final MapEntryNode projMapEntry2 = createProjectListEntry("MD-SAL",
720                 "MD-SAL description ...", "Leader of MD-SAL",
721                 "Owner of MD-SAL", projListSchemaNode);
722         final MapEntryNode projMapEntry3 = createProjectListEntry("Controller",
723                 "Controller description ...", "Leader of Controller",
724                 "Owner of Controller", projListSchemaNode);
725
726         projectMapBldr.addChild(projMapEntry1);
727         projectMapBldr.addChild(projMapEntry2);
728         projectMapBldr.addChild(projMapEntry3);
729
730         final MapNode projectMap = projectMapBldr.build();
731
732         return projectMap;
733     }
734
735     private static MapEntryNode createProjectListEntry(final String nameVal,
736             final String descVal, final String leadVal, final String ownerVal,
737             final ListSchemaNode projListSchemaNode) {
738
739         final LeafNode<String> nameLeaf = ImmutableNodes
740                 .leafNode(name, nameVal);
741         final LeafNode<String> descLeaf = ImmutableNodes
742                 .leafNode(desc, descVal);
743         final LeafNode<String> leadLeafRef = ImmutableNodes.leafNode(lead,
744                 leadVal);
745         final LeafNode<String> ownerLeafRef = ImmutableNodes.leafNode(owner,
746                 ownerVal);
747
748         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> projMapEntryBldr = Builders
749                 .mapEntryBuilder(projListSchemaNode);
750
751         projMapEntryBldr.addChild(nameLeaf);
752         projMapEntryBldr.addChild(descLeaf);
753         projMapEntryBldr.addChild(leadLeafRef);
754         projMapEntryBldr.addChild(ownerLeafRef);
755         final MapEntryNode projMapEntry = projMapEntryBldr.build();
756
757         return projMapEntry;
758     }
759
760     private static ContainerNode createBasicContributorContainer(
761             final ContainerSchemaNode contributorContSchemaNode) {
762
763         final ListSchemaNode contributorListSchemaNode = (ListSchemaNode) contributorContSchemaNode
764                 .getDataChildByName(contributor);
765
766         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> contributorContainerBldr = Builders
767                 .containerBuilder(contributorContSchemaNode);
768
769         final MapNode contributorMap = createBasicContributorList(contributorListSchemaNode);
770         contributorContainerBldr.addChild(contributorMap);
771
772         final ContainerNode contributorContainer = contributorContainerBldr
773                 .build();
774
775         return contributorContainer;
776
777     }
778
779     private static MapNode createBasicContributorList(
780             final ListSchemaNode contributorListSchemaNode) {
781
782         final CollectionNodeBuilder<MapEntryNode, MapNode> contributorMapBldr = Builders
783                 .mapBuilder(contributorListSchemaNode);
784
785         final MapEntryNode contributorMapEntry1 = createContributorListEntry(
786                 "Leader of Yangtools", "Yangtools Leader name", "Yangtools",
787                 "Yangtools description ...", contributorListSchemaNode);
788         final MapEntryNode contributorMapEntry2 = createContributorListEntry(
789                 "Leader of MD-SAL", "MD-SAL Leader name", "MD-SAL",
790                 "MD-SAL description ...", contributorListSchemaNode);
791         final MapEntryNode contributorMapEntry3 = createContributorListEntry(
792                 "Leader of Controller", "Controller Leader name", "Controller",
793                 "Controller description ...", contributorListSchemaNode);
794
795         contributorMapBldr.addChild(contributorMapEntry1);
796         contributorMapBldr.addChild(contributorMapEntry2);
797         contributorMapBldr.addChild(contributorMapEntry3);
798
799         final MapNode contributorMap = contributorMapBldr.build();
800
801         return contributorMap;
802     }
803 }