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