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