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.getDataChildByName(chips);
124 final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
125 final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
126 initialDataTreeModification.write(path1, chipsContainer);
128 final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
129 .getDataChildByName(deviceTypeStr);
130 final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
131 final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
132 initialDataTreeModification.write(path2, deviceTypesContainer);
134 initialDataTreeModification.ready();
135 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
137 inMemoryDataTree.commit(writeChipsCandidate);
139 LOG.debug("{}", inMemoryDataTree);
143 public static void cleanup() {
144 inMemoryDataTree = null;
145 rootLeafRefContext = null;
151 public void dataTreeCanditateValidationTest2() {
158 private static void writeDevices() {
160 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
162 final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
164 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
165 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
166 writeModification.write(devicesPath, devicesContainer);
168 writeModification.ready();
169 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
171 LOG.debug("*************************");
172 LOG.debug("Before writeDevices: ");
173 LOG.debug("*************************");
174 LOG.debug("{}", inMemoryDataTree);
176 boolean exception = false;
178 LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext);
179 } catch (final LeafRefDataValidationFailedException e) {
180 LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
181 assertEquals(6, e.getValidationsErrorsCount());
185 assertTrue(exception);
187 inMemoryDataTree.commit(writeDevicesCandidate);
189 LOG.debug("*************************");
190 LOG.debug("After writeDevices: ");
191 LOG.debug("*************************");
192 LOG.debug("{}", inMemoryDataTree);
195 private static void mergeDevices() {
197 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
199 final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
201 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
202 final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
203 mergeModification.write(devicesPath, devicesContainer);
204 mergeModification.merge(devicesPath, devicesContainer);
206 mergeModification.ready();
207 final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
209 LOG.debug("*************************");
210 LOG.debug("Before mergeDevices: ");
211 LOG.debug("*************************");
212 LOG.debug("{}", inMemoryDataTree);
214 boolean exception = false;
216 LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext);
217 } catch (final LeafRefDataValidationFailedException e) {
218 LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
219 // :TODO verify errors count gz
220 assertEquals(6, e.getValidationsErrorsCount());
224 inMemoryDataTree.commit(mergeDevicesCandidate);
226 LOG.debug("*************************");
227 LOG.debug("After mergeDevices: ");
228 LOG.debug("*************************");
229 LOG.debug("{}", inMemoryDataTree);
231 assertTrue(exception);
234 private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
236 final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.getDataChildByName(deviceType);
238 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
239 .containerBuilder(container);
241 final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
242 devTypeContainerBldr.addChild(devTypeMap);
244 return devTypeContainerBldr.build();
247 private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
249 final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
251 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1",
252 devTypeListSchemaNode));
253 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2",
254 devTypeListSchemaNode));
255 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3",
256 devTypeListSchemaNode));
258 return devTypeMapBldr.build();
261 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
262 final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
264 final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
265 final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
266 final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
267 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
269 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
270 .mapEntryBuilder(devTypeListSchemaNode);
272 devTypeMapEntryBldr.addChild(type1Leaf);
273 devTypeMapEntryBldr.addChild(type2Leaf);
274 devTypeMapEntryBldr.addChild(type3Leaf);
275 devTypeMapEntryBldr.addChild(descLeaf);
277 return devTypeMapEntryBldr.build();
280 private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
282 final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.getDataChildByName(chip);
284 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
285 .containerBuilder(container);
287 final MapNode chipsMap = createChipsList(chipsListSchemaNode);
288 chipsContainerBldr.addChild(chipsMap);
290 return chipsContainerBldr.build();
293 private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
295 final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
297 chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
298 chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
300 return chipsMapBldr.build();
303 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
304 final ListSchemaNode chipsListSchemaNode) {
306 final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
307 final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
309 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
310 .mapEntryBuilder(chipsListSchemaNode);
312 chipsMapEntryBldr.addChild(devTypeLeaf);
313 chipsMapEntryBldr.addChild(chipDescLeaf);
315 return chipsMapEntryBldr.build();
318 private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
320 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
322 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
323 .containerBuilder(container);
325 final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
326 devicesContainerBldr.addChild(devicesMap);
328 return devicesContainerBldr.build();
331 private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
333 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
335 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
336 "192.168.0.1", deviceListSchemaNode));
337 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
338 "192.168.0.1", deviceListSchemaNode));
339 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
340 "192.168.0.1", deviceListSchemaNode));
341 devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1",
342 deviceListSchemaNode));
344 return devicesMapBldr.build();
347 private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
349 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
351 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
352 .containerBuilder(container);
354 final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
355 devicesContainerBldr.addChild(devicesMap);
357 return devicesContainerBldr.build();
360 private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
362 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
364 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
365 "192.168.0.1", deviceListSchemaNode));
366 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
367 "192.168.0.1", deviceListSchemaNode));
368 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
369 "192.168.0.1", deviceListSchemaNode));
370 devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1",
371 deviceListSchemaNode));
373 return devicesMapBldr.build();
376 private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
377 final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal,
378 final ListSchemaNode devicesListSchemaNode) {
380 final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1, type1TextVal);
381 final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2, type2TextVal);
382 final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3, type3TextVal);
383 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
384 final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
385 final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp, defaultIpVal);
387 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
388 .mapEntryBuilder(devicesListSchemaNode);
390 devicesMapEntryBldr.addChild(typeText1Leaf);
391 devicesMapEntryBldr.addChild(typeText2Leaf);
392 devicesMapEntryBldr.addChild(typeText3Leaf);
393 devicesMapEntryBldr.addChild(descLeaf);
394 devicesMapEntryBldr.addChild(snValLeaf);
395 devicesMapEntryBldr.addChild(defaultIpLeaf);
397 return devicesMapEntryBldr.build();