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