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