8a613fccd43a1467d3bb443c727daa9684f4a6ce
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / context / test / DataTreeCandidateValidatorTest2.java
1 /**
2  * Copyright (c) 2015 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.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.Set;
17 import org.apache.log4j.BasicConfigurator;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
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.LeafRefValidatation;
35 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefYangSyntaxErrorException;
36 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
41 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
46 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class DataTreeCandidateValidatorTest2 {
51
52     private static SchemaContext context;
53     private static Module mainModule;
54     private static QNameModule rootModuleQname;
55     private static LeafRefContext rootLeafRefContext;
56     public static TipProducingDataTree inMemoryDataTree;
57
58     private static QName chips;
59     private static QName chip;
60     private static QName devType;
61     private static QName chipDesc;
62
63     private static QName devices;
64     private static QName device;
65     private static QName typeChoice;
66     private static QName typeText;
67     private static QName devDesc;
68     private static QName sn;
69     private static QName defaultIp;
70
71     private static QName deviceTypeStr;
72     private static QName deviceType;
73     private static QName type;
74     private static QName desc;
75
76     private static final Logger LOG = LoggerFactory.getLogger("");
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 URISyntaxException, IOException,
85             YangSyntaxErrorException, LeafRefYangSyntaxErrorException {
86
87         initSchemaContext();
88         initLeafRefContext();
89         initQnames();
90         initDataTree();
91     }
92
93     @Test
94     public void dataTreeCanditateValidationTest2() {
95
96         writeDevices();
97     }
98
99     private static void writeDevices() {
100
101         final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule
102                 .getDataChildByName(devices);
103
104         final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
105
106         final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
107         final DataTreeModification writeModification = inMemoryDataTree
108                 .takeSnapshot().newModification();
109         writeModification.write(devicesPath, devicesContainer);
110
111         writeModification.ready();
112         final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree
113                 .prepare(writeModification);
114
115         LOG.debug("*************************");
116         LOG.debug("Before writeDevices: ");
117         LOG.debug("*************************");
118         LOG.debug(inMemoryDataTree.toString());
119
120         boolean exception = false;
121         try {
122             LeafRefValidatation.validate(writeDevicesCandidate, rootLeafRefContext);
123         } catch (final LeafRefDataValidationFailedException e) {
124             LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
125
126             assertEquals(4, e.getValidationsErrorsCount());
127             exception = true;
128         }
129
130         assertTrue(exception);
131
132         inMemoryDataTree.commit(writeDevicesCandidate);
133
134         LOG.debug("*************************");
135         LOG.debug("After write: ");
136         LOG.debug("*************************");
137         LOG.debug(inMemoryDataTree.toString());
138     }
139
140     private static void initQnames() {
141
142         chips = QName.create(rootModuleQname, "chips");
143         chip = QName.create(rootModuleQname, "chip");
144         devType = QName.create(rootModuleQname, "dev_type");
145         chipDesc = QName.create(rootModuleQname, "chip_desc");
146
147         devices = QName.create(rootModuleQname, "devices");
148         device = QName.create(rootModuleQname, "device");
149         typeText = QName.create(rootModuleQname, "type_text");
150         devDesc = QName.create(rootModuleQname, "dev_desc");
151         sn = QName.create(rootModuleQname, "sn");
152         defaultIp = QName.create(rootModuleQname, "default_ip");
153
154         deviceTypeStr = QName.create(rootModuleQname, "device_types");
155         deviceType = QName.create(rootModuleQname, "device_type");
156         type = QName.create(rootModuleQname, "type");
157         desc = QName.create(rootModuleQname, "desc");
158     }
159
160     private static void initSchemaContext() throws URISyntaxException,
161             IOException, YangSyntaxErrorException {
162
163         final File resourceFile = new File(DataTreeCandidateValidatorTest.class
164                 .getResource("/leafref-validation/leafref-validation2.yang")
165                 .toURI());
166         final File resourceDir = resourceFile.getParentFile();
167
168         final YangParserImpl parser = YangParserImpl.getInstance();
169         context = parser.parseFile(resourceFile, resourceDir);
170
171         final Set<Module> modules = context.getModules();
172         for (final Module module : modules) {
173             if (module.getName().equals("leafref-validation2")) {
174                 mainModule = module;
175             }
176         }
177
178         rootModuleQname = mainModule.getQNameModule();
179     }
180
181     private static void initDataTree() {
182
183         inMemoryDataTree = InMemoryDataTreeFactory.getInstance().create();
184         inMemoryDataTree.setSchemaContext(context);
185
186         final DataTreeModification initialDataTreeModification = inMemoryDataTree
187                 .takeSnapshot().newModification();
188
189         final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule
190                 .getDataChildByName(chips);
191         final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
192         final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
193         initialDataTreeModification.write(path1, chipsContainer);
194
195         final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
196                 .getDataChildByName(deviceTypeStr);
197         final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
198         final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
199         initialDataTreeModification.write(path2, deviceTypesContainer);
200
201         initialDataTreeModification.ready();
202         final DataTreeCandidate writeChipsCandidate = inMemoryDataTree
203                 .prepare(initialDataTreeModification);
204
205         inMemoryDataTree.commit(writeChipsCandidate);
206
207         System.out.println(inMemoryDataTree.toString());
208     }
209
210     private static void initLeafRefContext() throws IOException,
211             LeafRefYangSyntaxErrorException {
212         rootLeafRefContext = LeafRefContext.create(context);
213     }
214
215     private static ContainerNode createDevTypeStrContainer(
216             final ContainerSchemaNode container) {
217
218         final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container
219                 .getDataChildByName(deviceType);
220
221         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
222                 .containerBuilder(container);
223
224         final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
225         devTypeContainerBldr.addChild(devTypeMap);
226
227         return devTypeContainerBldr.build();
228     }
229
230     private static MapNode createDevTypeList(
231             final ListSchemaNode devTypeListSchemaNode) {
232
233         final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders
234                 .mapBuilder(devTypeListSchemaNode);
235
236         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type_1",
237                 "typedesc1", devTypeListSchemaNode));
238         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type_2",
239                 "typedesc2", devTypeListSchemaNode));
240         devTypeMapBldr.addChild(createDevTypeListEntry("dev_type_3",
241                 "typedesc3", devTypeListSchemaNode));
242
243         return devTypeMapBldr.build();
244     }
245
246     private static MapEntryNode createDevTypeListEntry(final String typeVal,
247             final String descVal, final ListSchemaNode devTypeListSchemaNode) {
248
249         final LeafNode<String> typeLeaf = ImmutableNodes.leafNode(type, typeVal);
250         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
251
252         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
253                 .mapEntryBuilder(devTypeListSchemaNode);
254
255         devTypeMapEntryBldr.addChild(typeLeaf);
256         devTypeMapEntryBldr.addChild(descLeaf);
257
258         return devTypeMapEntryBldr.build();
259     }
260
261     private static ContainerNode createChipsContainer(
262             final ContainerSchemaNode container) {
263
264         final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container
265                 .getDataChildByName(chip);
266
267         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
268                 .containerBuilder(container);
269
270         final MapNode chipsMap = createChipsList(chipsListSchemaNode);
271         chipsContainerBldr.addChild(chipsMap);
272
273         return chipsContainerBldr.build();
274     }
275
276     private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
277
278         final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders
279                 .mapBuilder(chipsListSchemaNode);
280
281         chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1",
282                 chipsListSchemaNode));
283         chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2",
284                 chipsListSchemaNode));
285
286         return chipsMapBldr.build();
287     }
288
289     private static MapEntryNode createChipsListEntry(final String devTypeVal,
290             final String chipDescVal, final ListSchemaNode chipsListSchemaNode) {
291
292         final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType,
293                 devTypeVal);
294         final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc,
295                 chipDescVal);
296
297         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
298                 .mapEntryBuilder(chipsListSchemaNode);
299
300         chipsMapEntryBldr.addChild(devTypeLeaf);
301         chipsMapEntryBldr.addChild(chipDescLeaf);
302
303         return chipsMapEntryBldr.build();
304     }
305
306     private static ContainerNode createDevicesContainer(
307             final ContainerSchemaNode container) {
308
309         final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container
310                 .getDataChildByName(device);
311
312         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
313                 .containerBuilder(container);
314
315         final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
316         devicesContainerBldr.addChild(devicesMap);
317
318         return devicesContainerBldr.build();
319     }
320
321     private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
322
323         final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders
324                 .mapBuilder(deviceListSchemaNode);
325
326         devicesMapBldr.addChild(createDeviceListEntry("dev_type_1",
327                 "typedesc1", 123456, "192.168.0.1", deviceListSchemaNode));
328         devicesMapBldr.addChild(createDeviceListEntry("dev_type_2",
329                 "typedesc2", 123457, "192.168.0.1", deviceListSchemaNode));
330         devicesMapBldr.addChild(createDeviceListEntry("dev_type_2",
331                 "typedesc3", 123457, "192.168.0.1", deviceListSchemaNode));
332         devicesMapBldr.addChild(createDeviceListEntry("dev_type_1",
333                 "typedesc2", 123458, "192.168.0.1", deviceListSchemaNode));
334         devicesMapBldr.addChild(createDeviceListEntry("unknown", "unknown",
335                 123457, "192.168.0.1", deviceListSchemaNode));
336
337         return devicesMapBldr.build();
338     }
339
340     private static MapEntryNode createDeviceListEntry(final String typeTextVal,
341             final String descVal, final int snVal, final String defaultIpVal,
342             final ListSchemaNode devicesListSchemaNode) {
343
344         final LeafNode<String> typeTextLeaf = ImmutableNodes.leafNode(typeText,
345                 typeTextVal);
346         final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
347         final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
348         final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp,
349                 defaultIpVal);
350
351         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
352                 .mapEntryBuilder(devicesListSchemaNode);
353
354         devicesMapEntryBldr.addChild(typeTextLeaf);
355         devicesMapEntryBldr.addChild(descLeaf);
356         devicesMapEntryBldr.addChild(snValLeaf);
357         devicesMapEntryBldr.addChild(defaultIpLeaf);
358
359         return devicesMapEntryBldr.build();
360     }
361 }