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