Cleanup DataTree interfaces and InMemmoryDataTreeFactory
[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.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.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 DataTree 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);
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);
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);
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);
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 = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
214
215         final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
216
217         final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(chips);
218         final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
219         final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
220         initialDataTreeModification.write(path1, chipsContainer);
221
222         final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
223                 .getDataChildByName(deviceTypeStr);
224         final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
225         final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
226         initialDataTreeModification.write(path2, deviceTypesContainer);
227
228         initialDataTreeModification.ready();
229         final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
230
231         inMemoryDataTree.commit(writeChipsCandidate);
232
233         LOG.debug("{}", inMemoryDataTree);
234     }
235
236     private static void initLeafRefContext() {
237         rootLeafRefContext = LeafRefContext.create(context);
238     }
239
240     private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
241
242         final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.getDataChildByName(deviceType);
243
244         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
245                 .containerBuilder(container);
246
247         final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
248         devTypeContainerBldr.addChild(devTypeMap);
249
250         return devTypeContainerBldr.build();
251     }
252
253     private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
254
255         final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
256
257         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1",
258                 devTypeListSchemaNode));
259         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2",
260                 devTypeListSchemaNode));
261         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3",
262                 devTypeListSchemaNode));
263
264         return devTypeMapBldr.build();
265     }
266
267     private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
268             final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
269
270         final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
271         final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
272         final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
273         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
274
275         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
276                 .mapEntryBuilder(devTypeListSchemaNode);
277
278         devTypeMapEntryBldr.addChild(type1Leaf);
279         devTypeMapEntryBldr.addChild(type2Leaf);
280         devTypeMapEntryBldr.addChild(type3Leaf);
281         devTypeMapEntryBldr.addChild(descLeaf);
282
283         return devTypeMapEntryBldr.build();
284     }
285
286     private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
287
288         final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.getDataChildByName(chip);
289
290         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
291                 .containerBuilder(container);
292
293         final MapNode chipsMap = createChipsList(chipsListSchemaNode);
294         chipsContainerBldr.addChild(chipsMap);
295
296         return chipsContainerBldr.build();
297     }
298
299     private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
300
301         final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
302
303         chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
304         chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
305
306         return chipsMapBldr.build();
307     }
308
309     private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
310             final ListSchemaNode chipsListSchemaNode) {
311
312         final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
313         final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
314
315         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
316                 .mapEntryBuilder(chipsListSchemaNode);
317
318         chipsMapEntryBldr.addChild(devTypeLeaf);
319         chipsMapEntryBldr.addChild(chipDescLeaf);
320
321         return chipsMapEntryBldr.build();
322     }
323
324     private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
325
326         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
327
328         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
329                 .containerBuilder(container);
330
331         final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
332         devicesContainerBldr.addChild(devicesMap);
333
334         return devicesContainerBldr.build();
335     }
336
337     private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
338
339         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
340
341         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
342                 "192.168.0.1", deviceListSchemaNode));
343         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
344                 "192.168.0.1", deviceListSchemaNode));
345         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
346                 "192.168.0.1", deviceListSchemaNode));
347         devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1",
348                 deviceListSchemaNode));
349
350         return devicesMapBldr.build();
351     }
352
353     private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
354
355         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
356
357         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
358                 .containerBuilder(container);
359
360         final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
361         devicesContainerBldr.addChild(devicesMap);
362
363         return devicesContainerBldr.build();
364     }
365
366     private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
367
368         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
369
370         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
371                 "192.168.0.1", deviceListSchemaNode));
372         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
373                 "192.168.0.1", deviceListSchemaNode));
374         devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
375                 "192.168.0.1", deviceListSchemaNode));
376         devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1",
377                 deviceListSchemaNode));
378
379         return devicesMapBldr.build();
380     }
381
382     private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
383             final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal,
384             final ListSchemaNode devicesListSchemaNode) {
385
386         final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1, type1TextVal);
387         final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2, type2TextVal);
388         final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3, type3TextVal);
389         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
390         final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
391         final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp, defaultIpVal);
392
393         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
394                 .mapEntryBuilder(devicesListSchemaNode);
395
396         devicesMapEntryBldr.addChild(typeText1Leaf);
397         devicesMapEntryBldr.addChild(typeText2Leaf);
398         devicesMapEntryBldr.addChild(typeText3Leaf);
399         devicesMapEntryBldr.addChild(descLeaf);
400         devicesMapEntryBldr.addChild(snValLeaf);
401         devicesMapEntryBldr.addChild(defaultIpLeaf);
402
403         return devicesMapEntryBldr.build();
404     }
405 }