2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.leafref.context.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
16 import org.apache.log4j.BasicConfigurator;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
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;
50 public class DataTreeCandidateValidatorTest3 {
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;
58 private static QName chips;
59 private static QName chip;
60 private static QName devType;
61 private static QName chipDesc;
63 private static QName devices;
64 private static QName device;
65 private static QName typeText1;
66 private static QName typeText2;
67 private static QName typeText3;
68 private static QName devDesc;
69 private static QName sn;
70 private static QName defaultIp;
72 private static QName deviceTypeStr;
73 private static QName deviceType;
74 private static QName type1;
75 private static QName type2;
76 private static QName type3;
77 private static QName desc;
79 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
80 private static final String NEW_LINE = System.getProperty("line.separator");
83 BasicConfigurator.configure();
87 public static void init() throws URISyntaxException, IOException,
88 YangSyntaxErrorException, LeafRefYangSyntaxErrorException {
97 public void dataTreeCanditateValidationTest2() {
104 private static void writeDevices() {
106 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule
107 .getDataChildByName(devices);
109 final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
111 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
112 final DataTreeModification writeModification = inMemoryDataTree
113 .takeSnapshot().newModification();
114 writeModification.write(devicesPath, devicesContainer);
116 writeModification.ready();
117 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree
118 .prepare(writeModification);
120 LOG.debug("*************************");
121 LOG.debug("Before writeDevices: ");
122 LOG.debug("*************************");
123 LOG.debug(inMemoryDataTree.toString());
125 boolean exception = false;
127 LeafRefValidatation.validate(writeDevicesCandidate, rootLeafRefContext);
128 } catch (final LeafRefDataValidationFailedException e) {
129 LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
130 assertEquals(6, e.getValidationsErrorsCount());
134 assertTrue(exception);
136 inMemoryDataTree.commit(writeDevicesCandidate);
138 LOG.debug("*************************");
139 LOG.debug("After writeDevices: ");
140 LOG.debug("*************************");
141 LOG.debug(inMemoryDataTree.toString());
144 private static void mergeDevices() {
146 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule
147 .getDataChildByName(devices);
149 final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
151 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
152 final DataTreeModification mergeModification = inMemoryDataTree
153 .takeSnapshot().newModification();
154 mergeModification.write(devicesPath, devicesContainer);
155 mergeModification.merge(devicesPath, devicesContainer);
157 mergeModification.ready();
158 final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree
159 .prepare(mergeModification);
161 LOG.debug("*************************");
162 LOG.debug("Before mergeDevices: ");
163 LOG.debug("*************************");
164 LOG.debug(inMemoryDataTree.toString());
166 boolean exception = false;
168 LeafRefValidatation.validate(mergeDevicesCandidate, rootLeafRefContext);
169 } catch (final LeafRefDataValidationFailedException e) {
170 LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
171 // :TODO verify errors count gz
172 assertEquals(6, e.getValidationsErrorsCount());
176 inMemoryDataTree.commit(mergeDevicesCandidate);
178 LOG.debug("*************************");
179 LOG.debug("After mergeDevices: ");
180 LOG.debug("*************************");
181 LOG.debug(inMemoryDataTree.toString());
183 assertTrue(exception);
186 private static void initQnames() {
188 chips = QName.create(rootModuleQname, "chips");
189 chip = QName.create(rootModuleQname, "chip");
190 devType = QName.create(rootModuleQname, "dev_type");
191 chipDesc = QName.create(rootModuleQname, "chip_desc");
193 devices = QName.create(rootModuleQname, "devices");
194 device = QName.create(rootModuleQname, "device");
195 typeText1 = QName.create(rootModuleQname, "type_text1");
196 typeText2 = QName.create(rootModuleQname, "type_text2");
197 typeText3 = QName.create(rootModuleQname, "type_text3");
198 devDesc = QName.create(rootModuleQname, "dev_desc");
199 sn = QName.create(rootModuleQname, "sn");
200 defaultIp = QName.create(rootModuleQname, "default_ip");
202 deviceTypeStr = QName.create(rootModuleQname, "device_types");
203 deviceType = QName.create(rootModuleQname, "device_type");
204 type1 = QName.create(rootModuleQname, "type1");
205 type2 = QName.create(rootModuleQname, "type2");
206 type3 = QName.create(rootModuleQname, "type3");
207 desc = QName.create(rootModuleQname, "desc");
210 private static void initSchemaContext() throws URISyntaxException,
211 IOException, YangSyntaxErrorException {
213 final File resourceFile = new File(DataTreeCandidateValidatorTest.class
214 .getResource("/leafref-validation/leafref-validation3.yang")
216 final File resourceDir = resourceFile.getParentFile();
218 final YangParserImpl parser = YangParserImpl.getInstance();
219 context = parser.parseFile(resourceFile, resourceDir);
221 final Set<Module> modules = context.getModules();
222 for (final Module module : modules) {
223 if (module.getName().equals("leafref-validation3")) {
228 rootModuleQname = mainModule.getQNameModule();
231 private static void initDataTree() {
233 inMemoryDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
234 inMemoryDataTree.setSchemaContext(context);
236 final DataTreeModification initialDataTreeModification = inMemoryDataTree
237 .takeSnapshot().newModification();
239 final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule
240 .getDataChildByName(chips);
241 final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
242 final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
243 initialDataTreeModification.write(path1, chipsContainer);
245 final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
246 .getDataChildByName(deviceTypeStr);
247 final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
248 final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
249 initialDataTreeModification.write(path2, deviceTypesContainer);
251 initialDataTreeModification.ready();
252 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree
253 .prepare(initialDataTreeModification);
255 inMemoryDataTree.commit(writeChipsCandidate);
257 System.out.println(inMemoryDataTree.toString());
260 private static void initLeafRefContext() throws IOException,
261 LeafRefYangSyntaxErrorException {
262 rootLeafRefContext = LeafRefContext.create(context);
265 private static ContainerNode createDevTypeStrContainer(
266 final ContainerSchemaNode container) {
268 final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container
269 .getDataChildByName(deviceType);
271 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
272 .containerBuilder(container);
274 final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
275 devTypeContainerBldr.addChild(devTypeMap);
277 return devTypeContainerBldr.build();
280 private static MapNode createDevTypeList(
281 final ListSchemaNode devTypeListSchemaNode) {
283 final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders
284 .mapBuilder(devTypeListSchemaNode);
286 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1",
287 "dev_type2_1", "dev_type3_1", "typedesc1",
288 devTypeListSchemaNode));
289 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2",
290 "dev_type2_2", "dev_type3_2", "typedesc2",
291 devTypeListSchemaNode));
292 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3",
293 "dev_type2_3", "dev_type3_3", "typedesc3",
294 devTypeListSchemaNode));
296 return devTypeMapBldr.build();
299 private static MapEntryNode createDevTypeListEntry(final String type1Val,
300 final String type2Val, final String type3Val, final String descVal,
301 final ListSchemaNode devTypeListSchemaNode) {
303 final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
304 final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
305 final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
306 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
308 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
309 .mapEntryBuilder(devTypeListSchemaNode);
311 devTypeMapEntryBldr.addChild(type1Leaf);
312 devTypeMapEntryBldr.addChild(type2Leaf);
313 devTypeMapEntryBldr.addChild(type3Leaf);
314 devTypeMapEntryBldr.addChild(descLeaf);
316 return devTypeMapEntryBldr.build();
319 private static ContainerNode createChipsContainer(
320 final ContainerSchemaNode container) {
322 final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container
323 .getDataChildByName(chip);
325 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
326 .containerBuilder(container);
328 final MapNode chipsMap = createChipsList(chipsListSchemaNode);
329 chipsContainerBldr.addChild(chipsMap);
331 return chipsContainerBldr.build();
334 private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
336 final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders
337 .mapBuilder(chipsListSchemaNode);
339 chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1",
340 chipsListSchemaNode));
341 chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2",
342 chipsListSchemaNode));
344 return chipsMapBldr.build();
347 private static MapEntryNode createChipsListEntry(final String devTypeVal,
348 final String chipDescVal, final ListSchemaNode chipsListSchemaNode) {
350 final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType,
352 final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc,
355 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
356 .mapEntryBuilder(chipsListSchemaNode);
358 chipsMapEntryBldr.addChild(devTypeLeaf);
359 chipsMapEntryBldr.addChild(chipDescLeaf);
361 return chipsMapEntryBldr.build();
364 private static ContainerNode createDevicesContainer(
365 final ContainerSchemaNode container) {
367 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container
368 .getDataChildByName(device);
370 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
371 .containerBuilder(container);
373 final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
374 devicesContainerBldr.addChild(devicesMap);
376 return devicesContainerBldr.build();
379 private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
381 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders
382 .mapBuilder(deviceListSchemaNode);
384 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1",
385 "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
386 "192.168.0.1", deviceListSchemaNode));
387 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2",
388 "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
389 "192.168.0.1", deviceListSchemaNode));
390 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1",
391 "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
392 "192.168.0.1", deviceListSchemaNode));
393 devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22",
394 "unk33", "unk_desc2", 123457, "192.168.0.1",
395 deviceListSchemaNode));
397 return devicesMapBldr.build();
400 private static ContainerNode createDevices2Container(
401 final ContainerSchemaNode container) {
403 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container
404 .getDataChildByName(device);
406 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
407 .containerBuilder(container);
409 final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
410 devicesContainerBldr.addChild(devicesMap);
412 return devicesContainerBldr.build();
415 private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
417 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders
418 .mapBuilder(deviceListSchemaNode);
420 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3",
421 "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
422 "192.168.0.1", deviceListSchemaNode));
423 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3",
424 "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
425 "192.168.0.1", deviceListSchemaNode));
426 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3",
427 "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
428 "192.168.0.1", deviceListSchemaNode));
429 devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3",
430 "unk_desc", 123462, "192.168.0.1", deviceListSchemaNode));
432 return devicesMapBldr.build();
435 private static MapEntryNode createDeviceListEntry(final String type1TextVal,
436 final String type2TextVal, final String type3TextVal, final String descVal,
437 final int snVal, final String defaultIpVal, final ListSchemaNode devicesListSchemaNode) {
439 final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1,
441 final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2,
443 final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3,
445 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
446 final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
447 final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp,
450 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
451 .mapEntryBuilder(devicesListSchemaNode);
453 devicesMapEntryBldr.addChild(typeText1Leaf);
454 devicesMapEntryBldr.addChild(typeText2Leaf);
455 devicesMapEntryBldr.addChild(typeText3Leaf);
456 devicesMapEntryBldr.addChild(descLeaf);
457 devicesMapEntryBldr.addChild(snValLeaf);
458 devicesMapEntryBldr.addChild(defaultIpLeaf);
460 return devicesMapEntryBldr.build();