e03e7bbf9a7493e4b0e71cc29671e0913c3469df
[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.getDataChildByName(chips);
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                 .getDataChildByName(deviceTypeStr);
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() {
152
153         writeDevices();
154
155         mergeDevices();
156     }
157
158     private static void writeDevices() {
159
160         final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
161
162         final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
163
164         final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
165         final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
166         writeModification.write(devicesPath, devicesContainer);
167
168         writeModification.ready();
169         final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
170
171         LOG.debug("*************************");
172         LOG.debug("Before writeDevices: ");
173         LOG.debug("*************************");
174         LOG.debug("{}", inMemoryDataTree);
175
176         boolean exception = false;
177         try {
178             LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext);
179         } catch (final LeafRefDataValidationFailedException e) {
180             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
181             assertEquals(6, e.getValidationsErrorsCount());
182             exception = true;
183         }
184
185         assertTrue(exception);
186
187         inMemoryDataTree.commit(writeDevicesCandidate);
188
189         LOG.debug("*************************");
190         LOG.debug("After writeDevices: ");
191         LOG.debug("*************************");
192         LOG.debug("{}", inMemoryDataTree);
193     }
194
195     private static void mergeDevices() {
196
197         final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
198
199         final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
200
201         final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
202         final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
203         mergeModification.write(devicesPath, devicesContainer);
204         mergeModification.merge(devicesPath, devicesContainer);
205
206         mergeModification.ready();
207         final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
208
209         LOG.debug("*************************");
210         LOG.debug("Before mergeDevices: ");
211         LOG.debug("*************************");
212         LOG.debug("{}", inMemoryDataTree);
213
214         boolean exception = false;
215         try {
216             LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext);
217         } catch (final LeafRefDataValidationFailedException e) {
218             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
219             // :TODO verify errors count gz
220             assertEquals(6, e.getValidationsErrorsCount());
221             exception = true;
222         }
223
224         inMemoryDataTree.commit(mergeDevicesCandidate);
225
226         LOG.debug("*************************");
227         LOG.debug("After mergeDevices: ");
228         LOG.debug("*************************");
229         LOG.debug("{}", inMemoryDataTree);
230
231         assertTrue(exception);
232     }
233
234     private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
235
236         final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.getDataChildByName(deviceType);
237
238         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
239                 .containerBuilder(container);
240
241         final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
242         devTypeContainerBldr.addChild(devTypeMap);
243
244         return devTypeContainerBldr.build();
245     }
246
247     private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
248
249         final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
250
251         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1",
252                 devTypeListSchemaNode));
253         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2",
254                 devTypeListSchemaNode));
255         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3",
256                 devTypeListSchemaNode));
257
258         return devTypeMapBldr.build();
259     }
260
261     private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
262             final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
263
264         final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
265         final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
266         final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
267         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
268
269         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
270                 .mapEntryBuilder(devTypeListSchemaNode);
271
272         devTypeMapEntryBldr.addChild(type1Leaf);
273         devTypeMapEntryBldr.addChild(type2Leaf);
274         devTypeMapEntryBldr.addChild(type3Leaf);
275         devTypeMapEntryBldr.addChild(descLeaf);
276
277         return devTypeMapEntryBldr.build();
278     }
279
280     private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
281
282         final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.getDataChildByName(chip);
283
284         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
285                 .containerBuilder(container);
286
287         final MapNode chipsMap = createChipsList(chipsListSchemaNode);
288         chipsContainerBldr.addChild(chipsMap);
289
290         return chipsContainerBldr.build();
291     }
292
293     private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
294
295         final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
296
297         chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
298         chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
299
300         return chipsMapBldr.build();
301     }
302
303     private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
304             final ListSchemaNode chipsListSchemaNode) {
305
306         final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
307         final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
308
309         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
310                 .mapEntryBuilder(chipsListSchemaNode);
311
312         chipsMapEntryBldr.addChild(devTypeLeaf);
313         chipsMapEntryBldr.addChild(chipDescLeaf);
314
315         return chipsMapEntryBldr.build();
316     }
317
318     private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
319
320         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
321
322         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
323                 .containerBuilder(container);
324
325         final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
326         devicesContainerBldr.addChild(devicesMap);
327
328         return devicesContainerBldr.build();
329     }
330
331     private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
332
333         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
334
335         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
336                 "192.168.0.1", deviceListSchemaNode));
337         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
338                 "192.168.0.1", deviceListSchemaNode));
339         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
340                 "192.168.0.1", deviceListSchemaNode));
341         devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1",
342                 deviceListSchemaNode));
343
344         return devicesMapBldr.build();
345     }
346
347     private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
348
349         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
350
351         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
352                 .containerBuilder(container);
353
354         final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
355         devicesContainerBldr.addChild(devicesMap);
356
357         return devicesContainerBldr.build();
358     }
359
360     private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
361
362         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
363
364         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
365                 "192.168.0.1", deviceListSchemaNode));
366         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
367                 "192.168.0.1", deviceListSchemaNode));
368         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
369                 "192.168.0.1", deviceListSchemaNode));
370         devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1",
371                 deviceListSchemaNode));
372
373         return devicesMapBldr.build();
374     }
375
376     private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
377             final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal,
378             final ListSchemaNode devicesListSchemaNode) {
379
380         final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1, type1TextVal);
381         final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2, type2TextVal);
382         final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3, type3TextVal);
383         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
384         final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
385         final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp, defaultIpVal);
386
387         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
388                 .mapEntryBuilder(devicesListSchemaNode);
389
390         devicesMapEntryBldr.addChild(typeText1Leaf);
391         devicesMapEntryBldr.addChild(typeText2Leaf);
392         devicesMapEntryBldr.addChild(typeText3Leaf);
393         devicesMapEntryBldr.addChild(descLeaf);
394         devicesMapEntryBldr.addChild(snValLeaf);
395         devicesMapEntryBldr.addChild(defaultIpLeaf);
396
397         return devicesMapEntryBldr.build();
398     }
399 }