2 * Copyright (c) 2016 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;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
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.impl.leafref.LeafRefContext;
32 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
33 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidation;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
39 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class DataTreeCandidateValidatorTest3 {
49 private static SchemaContext context;
50 private static Module mainModule;
51 private static QNameModule rootModuleQname;
52 private static LeafRefContext rootLeafRefContext;
53 public static DataTree inMemoryDataTree;
55 private static QName chips;
56 private static QName chip;
57 private static QName devType;
58 private static QName chipDesc;
60 private static QName devices;
61 private static QName device;
62 private static QName typeText1;
63 private static QName typeText2;
64 private static QName typeText3;
65 private static QName devDesc;
66 private static QName sn;
67 private static QName defaultIp;
69 private static QName deviceTypeStr;
70 private static QName deviceType;
71 private static QName type1;
72 private static QName type2;
73 private static QName type3;
74 private static QName desc;
76 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
77 private static final String NEW_LINE = System.getProperty("line.separator");
80 BasicConfigurator.configure();
84 public static void init() {
85 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
87 final Set<Module> modules = context.getModules();
88 for (final Module module : modules) {
89 if (module.getName().equals("leafref-validation3")) {
94 rootModuleQname = mainModule.getQNameModule();
95 rootLeafRefContext = LeafRefContext.create(context);
98 chips = QName.create(rootModuleQname, "chips");
99 chip = QName.create(rootModuleQname, "chip");
100 devType = QName.create(rootModuleQname, "dev_type");
101 chipDesc = QName.create(rootModuleQname, "chip_desc");
103 devices = QName.create(rootModuleQname, "devices");
104 device = QName.create(rootModuleQname, "device");
105 typeText1 = QName.create(rootModuleQname, "type_text1");
106 typeText2 = QName.create(rootModuleQname, "type_text2");
107 typeText3 = QName.create(rootModuleQname, "type_text3");
108 devDesc = QName.create(rootModuleQname, "dev_desc");
109 sn = QName.create(rootModuleQname, "sn");
110 defaultIp = QName.create(rootModuleQname, "default_ip");
112 deviceTypeStr = QName.create(rootModuleQname, "device_types");
113 deviceType = QName.create(rootModuleQname, "device_type");
114 type1 = QName.create(rootModuleQname, "type1");
115 type2 = QName.create(rootModuleQname, "type2");
116 type3 = QName.create(rootModuleQname, "type3");
117 desc = QName.create(rootModuleQname, "desc");
119 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
121 final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
123 final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule.findDataChildByName(chips)
125 final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
126 final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
127 initialDataTreeModification.write(path1, chipsContainer);
129 final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
130 .findDataChildByName(deviceTypeStr).get();
131 final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
132 final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
133 initialDataTreeModification.write(path2, deviceTypesContainer);
135 initialDataTreeModification.ready();
136 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
138 inMemoryDataTree.commit(writeChipsCandidate);
140 LOG.debug("{}", inMemoryDataTree);
144 public static void cleanup() {
145 inMemoryDataTree = null;
146 rootLeafRefContext = null;
152 public void dataTreeCanditateValidationTest2() {
159 private static void writeDevices() {
161 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.findDataChildByName(devices)
164 final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
166 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
167 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
168 writeModification.write(devicesPath, devicesContainer);
170 writeModification.ready();
171 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
173 LOG.debug("*************************");
174 LOG.debug("Before writeDevices: ");
175 LOG.debug("*************************");
176 LOG.debug("{}", inMemoryDataTree);
178 boolean exception = false;
180 LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext);
181 } catch (final LeafRefDataValidationFailedException e) {
182 LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
183 assertEquals(6, e.getValidationsErrorsCount());
187 assertTrue(exception);
189 inMemoryDataTree.commit(writeDevicesCandidate);
191 LOG.debug("*************************");
192 LOG.debug("After writeDevices: ");
193 LOG.debug("*************************");
194 LOG.debug("{}", inMemoryDataTree);
197 private static void mergeDevices() {
199 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.findDataChildByName(devices)
202 final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
204 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
205 final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
206 mergeModification.write(devicesPath, devicesContainer);
207 mergeModification.merge(devicesPath, devicesContainer);
209 mergeModification.ready();
210 final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
212 LOG.debug("*************************");
213 LOG.debug("Before mergeDevices: ");
214 LOG.debug("*************************");
215 LOG.debug("{}", inMemoryDataTree);
217 boolean exception = false;
219 LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext);
220 } catch (final LeafRefDataValidationFailedException e) {
221 LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
222 // :TODO verify errors count gz
223 assertEquals(6, e.getValidationsErrorsCount());
227 inMemoryDataTree.commit(mergeDevicesCandidate);
229 LOG.debug("*************************");
230 LOG.debug("After mergeDevices: ");
231 LOG.debug("*************************");
232 LOG.debug("{}", inMemoryDataTree);
234 assertTrue(exception);
237 private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
239 final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.findDataChildByName(deviceType).get();
241 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
242 .containerBuilder(container);
244 final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
245 devTypeContainerBldr.addChild(devTypeMap);
247 return devTypeContainerBldr.build();
250 private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
252 final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
254 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1",
255 devTypeListSchemaNode));
256 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2",
257 devTypeListSchemaNode));
258 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3",
259 devTypeListSchemaNode));
261 return devTypeMapBldr.build();
264 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
265 final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
267 final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
268 final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
269 final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
270 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
272 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
273 .mapEntryBuilder(devTypeListSchemaNode);
275 devTypeMapEntryBldr.addChild(type1Leaf);
276 devTypeMapEntryBldr.addChild(type2Leaf);
277 devTypeMapEntryBldr.addChild(type3Leaf);
278 devTypeMapEntryBldr.addChild(descLeaf);
280 return devTypeMapEntryBldr.build();
283 private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
285 final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.findDataChildByName(chip).get();
287 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
288 .containerBuilder(container);
290 final MapNode chipsMap = createChipsList(chipsListSchemaNode);
291 chipsContainerBldr.addChild(chipsMap);
293 return chipsContainerBldr.build();
296 private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
298 final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
300 chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
301 chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
303 return chipsMapBldr.build();
306 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
307 final ListSchemaNode chipsListSchemaNode) {
309 final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
310 final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
312 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
313 .mapEntryBuilder(chipsListSchemaNode);
315 chipsMapEntryBldr.addChild(devTypeLeaf);
316 chipsMapEntryBldr.addChild(chipDescLeaf);
318 return chipsMapEntryBldr.build();
321 private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
323 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.findDataChildByName(device).get();
325 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
326 .containerBuilder(container);
328 final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
329 devicesContainerBldr.addChild(devicesMap);
331 return devicesContainerBldr.build();
334 private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
336 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
338 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
339 "192.168.0.1", deviceListSchemaNode));
340 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
341 "192.168.0.1", deviceListSchemaNode));
342 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
343 "192.168.0.1", deviceListSchemaNode));
344 devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1",
345 deviceListSchemaNode));
347 return devicesMapBldr.build();
350 private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
352 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.findDataChildByName(device).get();
354 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
355 .containerBuilder(container);
357 final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
358 devicesContainerBldr.addChild(devicesMap);
360 return devicesContainerBldr.build();
363 private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
365 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
367 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
368 "192.168.0.1", deviceListSchemaNode));
369 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
370 "192.168.0.1", deviceListSchemaNode));
371 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
372 "192.168.0.1", deviceListSchemaNode));
373 devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1",
374 deviceListSchemaNode));
376 return devicesMapBldr.build();
379 private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
380 final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal,
381 final ListSchemaNode devicesListSchemaNode) {
383 final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1, type1TextVal);
384 final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2, type2TextVal);
385 final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3, type3TextVal);
386 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
387 final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
388 final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp, defaultIpVal);
390 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
391 .mapEntryBuilder(devicesListSchemaNode);
393 devicesMapEntryBldr.addChild(typeText1Leaf);
394 devicesMapEntryBldr.addChild(typeText2Leaf);
395 devicesMapEntryBldr.addChild(typeText3Leaf);
396 devicesMapEntryBldr.addChild(descLeaf);
397 devicesMapEntryBldr.addChild(snValLeaf);
398 devicesMapEntryBldr.addChild(defaultIpLeaf);
400 return devicesMapEntryBldr.build();