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