Reduce use of getChildByName()
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / context / DataTreeCandidateValidatorTest3.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.Set;
14 import org.apache.log4j.BasicConfigurator;
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.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
32 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
33 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidation;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
39 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class DataTreeCandidateValidatorTest3 {
48
49     private static SchemaContext context;
50     private static Module mainModule;
51     private static QNameModule rootModuleQname;
52     private static LeafRefContext rootLeafRefContext;
53     public static DataTree inMemoryDataTree;
54
55     private static QName chips;
56     private static QName chip;
57     private static QName devType;
58     private static QName chipDesc;
59
60     private static QName devices;
61     private static QName device;
62     private static QName typeText1;
63     private static QName typeText2;
64     private static QName typeText3;
65     private static QName devDesc;
66     private static QName sn;
67     private static QName defaultIp;
68
69     private static QName deviceTypeStr;
70     private static QName deviceType;
71     private static QName type1;
72     private static QName type2;
73     private static QName type3;
74     private static QName desc;
75
76     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
77     private static final String NEW_LINE = System.getProperty("line.separator");
78
79     static {
80         BasicConfigurator.configure();
81     }
82
83     @BeforeClass
84     public static void init() {
85         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
86
87         final Set<Module> modules = context.getModules();
88         for (final Module module : modules) {
89             if (module.getName().equals("leafref-validation3")) {
90                 mainModule = module;
91             }
92         }
93
94         rootModuleQname = mainModule.getQNameModule();
95         rootLeafRefContext = LeafRefContext.create(context);
96
97
98         chips = QName.create(rootModuleQname, "chips");
99         chip = QName.create(rootModuleQname, "chip");
100         devType = QName.create(rootModuleQname, "dev_type");
101         chipDesc = QName.create(rootModuleQname, "chip_desc");
102
103         devices = QName.create(rootModuleQname, "devices");
104         device = QName.create(rootModuleQname, "device");
105         typeText1 = QName.create(rootModuleQname, "type_text1");
106         typeText2 = QName.create(rootModuleQname, "type_text2");
107         typeText3 = QName.create(rootModuleQname, "type_text3");
108         devDesc = QName.create(rootModuleQname, "dev_desc");
109         sn = QName.create(rootModuleQname, "sn");
110         defaultIp = QName.create(rootModuleQname, "default_ip");
111
112         deviceTypeStr = QName.create(rootModuleQname, "device_types");
113         deviceType = QName.create(rootModuleQname, "device_type");
114         type1 = QName.create(rootModuleQname, "type1");
115         type2 = QName.create(rootModuleQname, "type2");
116         type3 = QName.create(rootModuleQname, "type3");
117         desc = QName.create(rootModuleQname, "desc");
118
119         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
120
121         final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
122
123         final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule.findDataChildByName(chips)
124                 .get();
125         final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
126         final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
127         initialDataTreeModification.write(path1, chipsContainer);
128
129         final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
130                 .findDataChildByName(deviceTypeStr).get();
131         final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
132         final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
133         initialDataTreeModification.write(path2, deviceTypesContainer);
134
135         initialDataTreeModification.ready();
136         final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
137
138         inMemoryDataTree.commit(writeChipsCandidate);
139
140         LOG.debug("{}", inMemoryDataTree);
141     }
142
143     @AfterClass
144     public static void cleanup() {
145         inMemoryDataTree = null;
146         rootLeafRefContext = null;
147         mainModule = null;
148         context = null;
149     }
150
151     @Test
152     public void dataTreeCanditateValidationTest2() {
153
154         writeDevices();
155
156         mergeDevices();
157     }
158
159     private static void writeDevices() {
160
161         final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.findDataChildByName(devices)
162                 .get();
163
164         final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
165
166         final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
167         final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
168         writeModification.write(devicesPath, devicesContainer);
169
170         writeModification.ready();
171         final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
172
173         LOG.debug("*************************");
174         LOG.debug("Before writeDevices: ");
175         LOG.debug("*************************");
176         LOG.debug("{}", inMemoryDataTree);
177
178         boolean exception = false;
179         try {
180             LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext);
181         } catch (final LeafRefDataValidationFailedException e) {
182             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
183             assertEquals(6, e.getValidationsErrorsCount());
184             exception = true;
185         }
186
187         assertTrue(exception);
188
189         inMemoryDataTree.commit(writeDevicesCandidate);
190
191         LOG.debug("*************************");
192         LOG.debug("After writeDevices: ");
193         LOG.debug("*************************");
194         LOG.debug("{}", inMemoryDataTree);
195     }
196
197     private static void mergeDevices() {
198
199         final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.findDataChildByName(devices)
200                 .get();
201
202         final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
203
204         final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
205         final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
206         mergeModification.write(devicesPath, devicesContainer);
207         mergeModification.merge(devicesPath, devicesContainer);
208
209         mergeModification.ready();
210         final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
211
212         LOG.debug("*************************");
213         LOG.debug("Before mergeDevices: ");
214         LOG.debug("*************************");
215         LOG.debug("{}", inMemoryDataTree);
216
217         boolean exception = false;
218         try {
219             LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext);
220         } catch (final LeafRefDataValidationFailedException e) {
221             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
222             // :TODO verify errors count gz
223             assertEquals(6, e.getValidationsErrorsCount());
224             exception = true;
225         }
226
227         inMemoryDataTree.commit(mergeDevicesCandidate);
228
229         LOG.debug("*************************");
230         LOG.debug("After mergeDevices: ");
231         LOG.debug("*************************");
232         LOG.debug("{}", inMemoryDataTree);
233
234         assertTrue(exception);
235     }
236
237     private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
238
239         final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.findDataChildByName(deviceType).get();
240
241         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
242                 .containerBuilder(container);
243
244         final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
245         devTypeContainerBldr.addChild(devTypeMap);
246
247         return devTypeContainerBldr.build();
248     }
249
250     private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
251
252         final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
253
254         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1",
255                 devTypeListSchemaNode));
256         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2",
257                 devTypeListSchemaNode));
258         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3",
259                 devTypeListSchemaNode));
260
261         return devTypeMapBldr.build();
262     }
263
264     private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
265             final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
266
267         final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
268         final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
269         final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
270         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
271
272         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
273                 .mapEntryBuilder(devTypeListSchemaNode);
274
275         devTypeMapEntryBldr.addChild(type1Leaf);
276         devTypeMapEntryBldr.addChild(type2Leaf);
277         devTypeMapEntryBldr.addChild(type3Leaf);
278         devTypeMapEntryBldr.addChild(descLeaf);
279
280         return devTypeMapEntryBldr.build();
281     }
282
283     private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
284
285         final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.findDataChildByName(chip).get();
286
287         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
288                 .containerBuilder(container);
289
290         final MapNode chipsMap = createChipsList(chipsListSchemaNode);
291         chipsContainerBldr.addChild(chipsMap);
292
293         return chipsContainerBldr.build();
294     }
295
296     private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
297
298         final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
299
300         chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
301         chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
302
303         return chipsMapBldr.build();
304     }
305
306     private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
307             final ListSchemaNode chipsListSchemaNode) {
308
309         final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
310         final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
311
312         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
313                 .mapEntryBuilder(chipsListSchemaNode);
314
315         chipsMapEntryBldr.addChild(devTypeLeaf);
316         chipsMapEntryBldr.addChild(chipDescLeaf);
317
318         return chipsMapEntryBldr.build();
319     }
320
321     private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
322
323         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.findDataChildByName(device).get();
324
325         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
326                 .containerBuilder(container);
327
328         final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
329         devicesContainerBldr.addChild(devicesMap);
330
331         return devicesContainerBldr.build();
332     }
333
334     private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
335
336         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
337
338         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
339                 "192.168.0.1", deviceListSchemaNode));
340         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
341                 "192.168.0.1", deviceListSchemaNode));
342         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
343                 "192.168.0.1", deviceListSchemaNode));
344         devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1",
345                 deviceListSchemaNode));
346
347         return devicesMapBldr.build();
348     }
349
350     private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
351
352         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.findDataChildByName(device).get();
353
354         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
355                 .containerBuilder(container);
356
357         final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
358         devicesContainerBldr.addChild(devicesMap);
359
360         return devicesContainerBldr.build();
361     }
362
363     private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
364
365         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
366
367         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
368                 "192.168.0.1", deviceListSchemaNode));
369         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
370                 "192.168.0.1", deviceListSchemaNode));
371         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
372                 "192.168.0.1", deviceListSchemaNode));
373         devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1",
374                 deviceListSchemaNode));
375
376         return devicesMapBldr.build();
377     }
378
379     private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
380             final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal,
381             final ListSchemaNode devicesListSchemaNode) {
382
383         final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1, type1TextVal);
384         final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2, type2TextVal);
385         final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3, type3TextVal);
386         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
387         final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
388         final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp, defaultIpVal);
389
390         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
391                 .mapEntryBuilder(devicesListSchemaNode);
392
393         devicesMapEntryBldr.addChild(typeText1Leaf);
394         devicesMapEntryBldr.addChild(typeText2Leaf);
395         devicesMapEntryBldr.addChild(typeText3Leaf);
396         devicesMapEntryBldr.addChild(descLeaf);
397         devicesMapEntryBldr.addChild(snValLeaf);
398         devicesMapEntryBldr.addChild(defaultIpLeaf);
399
400         return devicesMapEntryBldr.build();
401     }
402 }