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.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;
46 public class DataTreeCandidateValidatorTest3 {
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;
54 private static QName chips;
55 private static QName chip;
56 private static QName devType;
57 private static QName chipDesc;
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;
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;
75 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
76 private static final String NEW_LINE = System.getProperty("line.separator");
79 BasicConfigurator.configure();
83 public static void init() {
91 public void dataTreeCanditateValidationTest2() {
98 private static void writeDevices() {
100 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
102 final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
104 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
105 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
106 writeModification.write(devicesPath, devicesContainer);
108 writeModification.ready();
109 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
111 LOG.debug("*************************");
112 LOG.debug("Before writeDevices: ");
113 LOG.debug("*************************");
114 LOG.debug("{}", inMemoryDataTree);
116 boolean exception = false;
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());
125 assertTrue(exception);
127 inMemoryDataTree.commit(writeDevicesCandidate);
129 LOG.debug("*************************");
130 LOG.debug("After writeDevices: ");
131 LOG.debug("*************************");
132 LOG.debug("{}", inMemoryDataTree);
135 private static void mergeDevices() {
137 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
139 final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
141 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
142 final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
143 mergeModification.write(devicesPath, devicesContainer);
144 mergeModification.merge(devicesPath, devicesContainer);
146 mergeModification.ready();
147 final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
149 LOG.debug("*************************");
150 LOG.debug("Before mergeDevices: ");
151 LOG.debug("*************************");
152 LOG.debug("{}", inMemoryDataTree);
154 boolean exception = false;
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());
164 inMemoryDataTree.commit(mergeDevicesCandidate);
166 LOG.debug("*************************");
167 LOG.debug("After mergeDevices: ");
168 LOG.debug("*************************");
169 LOG.debug("{}", inMemoryDataTree);
171 assertTrue(exception);
174 private static void initQnames() {
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");
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");
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");
198 private static void initSchemaContext() {
199 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
201 final Set<Module> modules = context.getModules();
202 for (final Module module : modules) {
203 if (module.getName().equals("leafref-validation3")) {
208 rootModuleQname = mainModule.getQNameModule();
211 private static void initDataTree() {
213 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
215 final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
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);
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);
228 initialDataTreeModification.ready();
229 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
231 inMemoryDataTree.commit(writeChipsCandidate);
233 LOG.debug("{}", inMemoryDataTree);
236 private static void initLeafRefContext() {
237 rootLeafRefContext = LeafRefContext.create(context);
240 private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
242 final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.getDataChildByName(deviceType);
244 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
245 .containerBuilder(container);
247 final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
248 devTypeContainerBldr.addChild(devTypeMap);
250 return devTypeContainerBldr.build();
253 private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
255 final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
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));
264 return devTypeMapBldr.build();
267 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
268 final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
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);
275 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
276 .mapEntryBuilder(devTypeListSchemaNode);
278 devTypeMapEntryBldr.addChild(type1Leaf);
279 devTypeMapEntryBldr.addChild(type2Leaf);
280 devTypeMapEntryBldr.addChild(type3Leaf);
281 devTypeMapEntryBldr.addChild(descLeaf);
283 return devTypeMapEntryBldr.build();
286 private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
288 final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.getDataChildByName(chip);
290 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
291 .containerBuilder(container);
293 final MapNode chipsMap = createChipsList(chipsListSchemaNode);
294 chipsContainerBldr.addChild(chipsMap);
296 return chipsContainerBldr.build();
299 private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
301 final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
303 chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
304 chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
306 return chipsMapBldr.build();
309 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
310 final ListSchemaNode chipsListSchemaNode) {
312 final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
313 final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
315 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
316 .mapEntryBuilder(chipsListSchemaNode);
318 chipsMapEntryBldr.addChild(devTypeLeaf);
319 chipsMapEntryBldr.addChild(chipDescLeaf);
321 return chipsMapEntryBldr.build();
324 private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
326 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
328 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
329 .containerBuilder(container);
331 final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
332 devicesContainerBldr.addChild(devicesMap);
334 return devicesContainerBldr.build();
337 private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
339 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
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));
350 return devicesMapBldr.build();
353 private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
355 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
357 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
358 .containerBuilder(container);
360 final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
361 devicesContainerBldr.addChild(devicesMap);
363 return devicesContainerBldr.build();
366 private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
368 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
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));
379 return devicesMapBldr.build();
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) {
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);
393 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
394 .mapEntryBuilder(devicesListSchemaNode);
396 devicesMapEntryBldr.addChild(typeText1Leaf);
397 devicesMapEntryBldr.addChild(typeText2Leaf);
398 devicesMapEntryBldr.addChild(typeText3Leaf);
399 devicesMapEntryBldr.addChild(descLeaf);
400 devicesMapEntryBldr.addChild(snValLeaf);
401 devicesMapEntryBldr.addChild(defaultIpLeaf);
403 return devicesMapEntryBldr.build();