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