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