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