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