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