BUG-865: deprecate DataTreeFactor.create()
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / context / test / DataTreeCandidateValidatorTest3.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.leafref.context.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.util.Set;
16 import org.apache.log4j.BasicConfigurator;
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.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
32 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
33 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
34 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidatation;
35 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefYangSyntaxErrorException;
36 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
41 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
46 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class DataTreeCandidateValidatorTest3 {
51
52     private static SchemaContext context;
53     private static Module mainModule;
54     private static QNameModule rootModuleQname;
55     private static LeafRefContext rootLeafRefContext;
56     public static TipProducingDataTree inMemoryDataTree;
57
58     private static QName chips;
59     private static QName chip;
60     private static QName devType;
61     private static QName chipDesc;
62
63     private static QName devices;
64     private static QName device;
65     private static QName typeText1;
66     private static QName typeText2;
67     private static QName typeText3;
68     private static QName devDesc;
69     private static QName sn;
70     private static QName defaultIp;
71
72     private static QName deviceTypeStr;
73     private static QName deviceType;
74     private static QName type1;
75     private static QName type2;
76     private static QName type3;
77     private static QName desc;
78
79     private static final Logger LOG = LoggerFactory.getLogger("");
80     private static final String NEW_LINE = System.getProperty("line.separator");
81
82     static {
83         BasicConfigurator.configure();
84     }
85
86     @BeforeClass
87     public static void init() throws URISyntaxException, IOException,
88             YangSyntaxErrorException, LeafRefYangSyntaxErrorException {
89
90         initSchemaContext();
91         initLeafRefContext();
92         initQnames();
93         initDataTree();
94     }
95
96     @Test
97     public void dataTreeCanditateValidationTest2() {
98
99         writeDevices();
100
101         mergeDevices();
102     }
103
104     private static void writeDevices() {
105
106         final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule
107                 .getDataChildByName(devices);
108
109         final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
110
111         final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
112         final DataTreeModification writeModification = inMemoryDataTree
113                 .takeSnapshot().newModification();
114         writeModification.write(devicesPath, devicesContainer);
115
116         writeModification.ready();
117         final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree
118                 .prepare(writeModification);
119
120         LOG.debug("*************************");
121         LOG.debug("Before writeDevices: ");
122         LOG.debug("*************************");
123         LOG.debug(inMemoryDataTree.toString());
124
125         boolean exception = false;
126         try {
127             LeafRefValidatation.validate(writeDevicesCandidate, rootLeafRefContext);
128         } catch (final LeafRefDataValidationFailedException e) {
129             LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
130             assertEquals(6, e.getValidationsErrorsCount());
131             exception = true;
132         }
133
134         assertTrue(exception);
135
136         inMemoryDataTree.commit(writeDevicesCandidate);
137
138         LOG.debug("*************************");
139         LOG.debug("After writeDevices: ");
140         LOG.debug("*************************");
141         LOG.debug(inMemoryDataTree.toString());
142     }
143
144     private static void mergeDevices() {
145
146         final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule
147                 .getDataChildByName(devices);
148
149         final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
150
151         final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
152         final DataTreeModification mergeModification = inMemoryDataTree
153                 .takeSnapshot().newModification();
154         mergeModification.write(devicesPath, devicesContainer);
155         mergeModification.merge(devicesPath, devicesContainer);
156
157         mergeModification.ready();
158         final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree
159                 .prepare(mergeModification);
160
161         LOG.debug("*************************");
162         LOG.debug("Before mergeDevices: ");
163         LOG.debug("*************************");
164         LOG.debug(inMemoryDataTree.toString());
165
166         boolean exception = false;
167         try {
168             LeafRefValidatation.validate(mergeDevicesCandidate, rootLeafRefContext);
169         } catch (final LeafRefDataValidationFailedException e) {
170             LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
171             // :TODO verify errors count gz
172             assertEquals(6, e.getValidationsErrorsCount());
173             exception = true;
174         }
175
176         inMemoryDataTree.commit(mergeDevicesCandidate);
177
178         LOG.debug("*************************");
179         LOG.debug("After mergeDevices: ");
180         LOG.debug("*************************");
181         LOG.debug(inMemoryDataTree.toString());
182
183         assertTrue(exception);
184     }
185
186     private static void initQnames() {
187
188         chips = QName.create(rootModuleQname, "chips");
189         chip = QName.create(rootModuleQname, "chip");
190         devType = QName.create(rootModuleQname, "dev_type");
191         chipDesc = QName.create(rootModuleQname, "chip_desc");
192
193         devices = QName.create(rootModuleQname, "devices");
194         device = QName.create(rootModuleQname, "device");
195         typeText1 = QName.create(rootModuleQname, "type_text1");
196         typeText2 = QName.create(rootModuleQname, "type_text2");
197         typeText3 = QName.create(rootModuleQname, "type_text3");
198         devDesc = QName.create(rootModuleQname, "dev_desc");
199         sn = QName.create(rootModuleQname, "sn");
200         defaultIp = QName.create(rootModuleQname, "default_ip");
201
202         deviceTypeStr = QName.create(rootModuleQname, "device_types");
203         deviceType = QName.create(rootModuleQname, "device_type");
204         type1 = QName.create(rootModuleQname, "type1");
205         type2 = QName.create(rootModuleQname, "type2");
206         type3 = QName.create(rootModuleQname, "type3");
207         desc = QName.create(rootModuleQname, "desc");
208     }
209
210     private static void initSchemaContext() throws URISyntaxException,
211             IOException, YangSyntaxErrorException {
212
213         final File resourceFile = new File(DataTreeCandidateValidatorTest.class
214                 .getResource("/leafref-validation/leafref-validation3.yang")
215                 .toURI());
216         final File resourceDir = resourceFile.getParentFile();
217
218         final YangParserImpl parser = YangParserImpl.getInstance();
219         context = parser.parseFile(resourceFile, resourceDir);
220
221         final Set<Module> modules = context.getModules();
222         for (final Module module : modules) {
223             if (module.getName().equals("leafref-validation3")) {
224                 mainModule = module;
225             }
226         }
227
228         rootModuleQname = mainModule.getQNameModule();
229     }
230
231     private static void initDataTree() {
232
233         inMemoryDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
234         inMemoryDataTree.setSchemaContext(context);
235
236         final DataTreeModification initialDataTreeModification = inMemoryDataTree
237                 .takeSnapshot().newModification();
238
239         final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule
240                 .getDataChildByName(chips);
241         final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
242         final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
243         initialDataTreeModification.write(path1, chipsContainer);
244
245         final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
246                 .getDataChildByName(deviceTypeStr);
247         final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
248         final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
249         initialDataTreeModification.write(path2, deviceTypesContainer);
250
251         initialDataTreeModification.ready();
252         final DataTreeCandidate writeChipsCandidate = inMemoryDataTree
253                 .prepare(initialDataTreeModification);
254
255         inMemoryDataTree.commit(writeChipsCandidate);
256
257         System.out.println(inMemoryDataTree.toString());
258     }
259
260     private static void initLeafRefContext() throws IOException,
261             LeafRefYangSyntaxErrorException {
262         rootLeafRefContext = LeafRefContext.create(context);
263     }
264
265     private static ContainerNode createDevTypeStrContainer(
266             final ContainerSchemaNode container) {
267
268         final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container
269                 .getDataChildByName(deviceType);
270
271         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
272                 .containerBuilder(container);
273
274         final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
275         devTypeContainerBldr.addChild(devTypeMap);
276
277         return devTypeContainerBldr.build();
278     }
279
280     private static MapNode createDevTypeList(
281             final ListSchemaNode devTypeListSchemaNode) {
282
283         final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders
284                 .mapBuilder(devTypeListSchemaNode);
285
286         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1",
287                 "dev_type2_1", "dev_type3_1", "typedesc1",
288                 devTypeListSchemaNode));
289         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2",
290                 "dev_type2_2", "dev_type3_2", "typedesc2",
291                 devTypeListSchemaNode));
292         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3",
293                 "dev_type2_3", "dev_type3_3", "typedesc3",
294                 devTypeListSchemaNode));
295
296         return devTypeMapBldr.build();
297     }
298
299     private static MapEntryNode createDevTypeListEntry(final String type1Val,
300             final String type2Val, final String type3Val, final String descVal,
301             final ListSchemaNode devTypeListSchemaNode) {
302
303         final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
304         final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
305         final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
306         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
307
308         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
309                 .mapEntryBuilder(devTypeListSchemaNode);
310
311         devTypeMapEntryBldr.addChild(type1Leaf);
312         devTypeMapEntryBldr.addChild(type2Leaf);
313         devTypeMapEntryBldr.addChild(type3Leaf);
314         devTypeMapEntryBldr.addChild(descLeaf);
315
316         return devTypeMapEntryBldr.build();
317     }
318
319     private static ContainerNode createChipsContainer(
320             final ContainerSchemaNode container) {
321
322         final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container
323                 .getDataChildByName(chip);
324
325         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
326                 .containerBuilder(container);
327
328         final MapNode chipsMap = createChipsList(chipsListSchemaNode);
329         chipsContainerBldr.addChild(chipsMap);
330
331         return chipsContainerBldr.build();
332     }
333
334     private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
335
336         final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders
337                 .mapBuilder(chipsListSchemaNode);
338
339         chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1",
340                 chipsListSchemaNode));
341         chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2",
342                 chipsListSchemaNode));
343
344         return chipsMapBldr.build();
345     }
346
347     private static MapEntryNode createChipsListEntry(final String devTypeVal,
348             final String chipDescVal, final ListSchemaNode chipsListSchemaNode) {
349
350         final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType,
351                 devTypeVal);
352         final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc,
353                 chipDescVal);
354
355         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
356                 .mapEntryBuilder(chipsListSchemaNode);
357
358         chipsMapEntryBldr.addChild(devTypeLeaf);
359         chipsMapEntryBldr.addChild(chipDescLeaf);
360
361         return chipsMapEntryBldr.build();
362     }
363
364     private static ContainerNode createDevicesContainer(
365             final ContainerSchemaNode container) {
366
367         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container
368                 .getDataChildByName(device);
369
370         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
371                 .containerBuilder(container);
372
373         final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
374         devicesContainerBldr.addChild(devicesMap);
375
376         return devicesContainerBldr.build();
377     }
378
379     private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
380
381         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders
382                 .mapBuilder(deviceListSchemaNode);
383
384         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1",
385                 "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
386                 "192.168.0.1", deviceListSchemaNode));
387         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2",
388                 "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
389                 "192.168.0.1", deviceListSchemaNode));
390         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1",
391                 "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
392                 "192.168.0.1", deviceListSchemaNode));
393         devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22",
394                 "unk33", "unk_desc2", 123457, "192.168.0.1",
395                 deviceListSchemaNode));
396
397         return devicesMapBldr.build();
398     }
399
400     private static ContainerNode createDevices2Container(
401             final ContainerSchemaNode container) {
402
403         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container
404                 .getDataChildByName(device);
405
406         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
407                 .containerBuilder(container);
408
409         final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
410         devicesContainerBldr.addChild(devicesMap);
411
412         return devicesContainerBldr.build();
413     }
414
415     private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
416
417         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders
418                 .mapBuilder(deviceListSchemaNode);
419
420         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3",
421                 "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
422                 "192.168.0.1", deviceListSchemaNode));
423         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3",
424                 "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
425                 "192.168.0.1", deviceListSchemaNode));
426         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3",
427                 "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
428                 "192.168.0.1", deviceListSchemaNode));
429         devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3",
430                 "unk_desc", 123462, "192.168.0.1", deviceListSchemaNode));
431
432         return devicesMapBldr.build();
433     }
434
435     private static MapEntryNode createDeviceListEntry(final String type1TextVal,
436             final String type2TextVal, final String type3TextVal, final String descVal,
437             final int snVal, final String defaultIpVal, final ListSchemaNode devicesListSchemaNode) {
438
439         final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1,
440                 type1TextVal);
441         final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2,
442                 type2TextVal);
443         final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3,
444                 type3TextVal);
445         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
446         final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
447         final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp,
448                 defaultIpVal);
449
450         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
451                 .mapEntryBuilder(devicesListSchemaNode);
452
453         devicesMapEntryBldr.addChild(typeText1Leaf);
454         devicesMapEntryBldr.addChild(typeText2Leaf);
455         devicesMapEntryBldr.addChild(typeText3Leaf);
456         devicesMapEntryBldr.addChild(descLeaf);
457         devicesMapEntryBldr.addChild(snValLeaf);
458         devicesMapEntryBldr.addChild(defaultIpLeaf);
459
460         return devicesMapEntryBldr.build();
461     }
462 }