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.DataTreeCandidate;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
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 TipProducingDataTree 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.toString());
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.toString());
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.toString());
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.toString());
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 = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
214 inMemoryDataTree.setSchemaContext(context);
216 final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
218 final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(chips);
219 final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
220 final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
221 initialDataTreeModification.write(path1, chipsContainer);
223 final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
224 .getDataChildByName(deviceTypeStr);
225 final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
226 final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
227 initialDataTreeModification.write(path2, deviceTypesContainer);
229 initialDataTreeModification.ready();
230 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
232 inMemoryDataTree.commit(writeChipsCandidate);
234 System.out.println(inMemoryDataTree.toString());
237 private static void initLeafRefContext() {
238 rootLeafRefContext = LeafRefContext.create(context);
241 private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
243 final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.getDataChildByName(deviceType);
245 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
246 .containerBuilder(container);
248 final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
249 devTypeContainerBldr.addChild(devTypeMap);
251 return devTypeContainerBldr.build();
254 private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
256 final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
258 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1",
259 devTypeListSchemaNode));
260 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2",
261 devTypeListSchemaNode));
262 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3",
263 devTypeListSchemaNode));
265 return devTypeMapBldr.build();
268 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
269 final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
271 final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
272 final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
273 final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
274 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
276 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
277 .mapEntryBuilder(devTypeListSchemaNode);
279 devTypeMapEntryBldr.addChild(type1Leaf);
280 devTypeMapEntryBldr.addChild(type2Leaf);
281 devTypeMapEntryBldr.addChild(type3Leaf);
282 devTypeMapEntryBldr.addChild(descLeaf);
284 return devTypeMapEntryBldr.build();
287 private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
289 final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.getDataChildByName(chip);
291 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
292 .containerBuilder(container);
294 final MapNode chipsMap = createChipsList(chipsListSchemaNode);
295 chipsContainerBldr.addChild(chipsMap);
297 return chipsContainerBldr.build();
300 private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
302 final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
304 chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
305 chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
307 return chipsMapBldr.build();
310 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
311 final ListSchemaNode chipsListSchemaNode) {
313 final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
314 final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
316 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
317 .mapEntryBuilder(chipsListSchemaNode);
319 chipsMapEntryBldr.addChild(devTypeLeaf);
320 chipsMapEntryBldr.addChild(chipDescLeaf);
322 return chipsMapEntryBldr.build();
325 private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
327 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
329 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
330 .containerBuilder(container);
332 final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
333 devicesContainerBldr.addChild(devicesMap);
335 return devicesContainerBldr.build();
338 private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
340 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
342 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
343 "192.168.0.1", deviceListSchemaNode));
344 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
345 "192.168.0.1", deviceListSchemaNode));
346 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
347 "192.168.0.1", deviceListSchemaNode));
348 devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1",
349 deviceListSchemaNode));
351 return devicesMapBldr.build();
354 private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
356 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
358 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
359 .containerBuilder(container);
361 final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
362 devicesContainerBldr.addChild(devicesMap);
364 return devicesContainerBldr.build();
367 private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
369 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
371 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
372 "192.168.0.1", deviceListSchemaNode));
373 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
374 "192.168.0.1", deviceListSchemaNode));
375 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
376 "192.168.0.1", deviceListSchemaNode));
377 devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1",
378 deviceListSchemaNode));
380 return devicesMapBldr.build();
383 private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
384 final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal,
385 final ListSchemaNode devicesListSchemaNode) {
387 final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1, type1TextVal);
388 final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2, type2TextVal);
389 final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3, type3TextVal);
390 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
391 final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
392 final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp, defaultIpVal);
394 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
395 .mapEntryBuilder(devicesListSchemaNode);
397 devicesMapEntryBldr.addChild(typeText1Leaf);
398 devicesMapEntryBldr.addChild(typeText2Leaf);
399 devicesMapEntryBldr.addChild(typeText3Leaf);
400 devicesMapEntryBldr.addChild(descLeaf);
401 devicesMapEntryBldr.addChild(snValLeaf);
402 devicesMapEntryBldr.addChild(defaultIpLeaf);
404 return devicesMapEntryBldr.build();