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.retest;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.io.FileNotFoundException;
14 import java.net.URISyntaxException;
15 import java.util.Arrays;
17 import org.apache.log4j.BasicConfigurator;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
32 import org.opendaylight.yangtools.yang.data.impl.RetestUtils;
33 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
34 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
35 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidatation;
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.parser.spi.meta.ReactorException;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class DataTreeCandidateValidatorTest3 {
51 private static SchemaContext context;
52 private static Module mainModule;
53 private static QNameModule rootModuleQname;
54 private static LeafRefContext rootLeafRefContext;
55 public static TipProducingDataTree inMemoryDataTree;
57 private static QName chips;
58 private static QName chip;
59 private static QName devType;
60 private static QName chipDesc;
62 private static QName devices;
63 private static QName device;
64 private static QName typeText1;
65 private static QName typeText2;
66 private static QName typeText3;
67 private static QName devDesc;
68 private static QName sn;
69 private static QName defaultIp;
71 private static QName deviceTypeStr;
72 private static QName deviceType;
73 private static QName type1;
74 private static QName type2;
75 private static QName type3;
76 private static QName desc;
78 private static final Logger LOG = LoggerFactory.getLogger("");
79 private static final String NEW_LINE = System.getProperty("line.separator");
82 BasicConfigurator.configure();
86 public static void init() throws FileNotFoundException, ReactorException, URISyntaxException {
95 public void dataTreeCanditateValidationTest2() {
102 private static void writeDevices() {
104 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
106 final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
108 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
109 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
110 writeModification.write(devicesPath, devicesContainer);
112 writeModification.ready();
113 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
115 LOG.debug("*************************");
116 LOG.debug("Before writeDevices: ");
117 LOG.debug("*************************");
118 LOG.debug(inMemoryDataTree.toString());
120 boolean exception = false;
122 LeafRefValidatation.validate(writeDevicesCandidate, rootLeafRefContext);
123 } catch (final LeafRefDataValidationFailedException e) {
124 LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
125 assertEquals(6, e.getValidationsErrorsCount());
129 assertTrue(exception);
131 inMemoryDataTree.commit(writeDevicesCandidate);
133 LOG.debug("*************************");
134 LOG.debug("After writeDevices: ");
135 LOG.debug("*************************");
136 LOG.debug(inMemoryDataTree.toString());
139 private static void mergeDevices() {
141 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
143 final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
145 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
146 final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
147 mergeModification.write(devicesPath, devicesContainer);
148 mergeModification.merge(devicesPath, devicesContainer);
150 mergeModification.ready();
151 final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
153 LOG.debug("*************************");
154 LOG.debug("Before mergeDevices: ");
155 LOG.debug("*************************");
156 LOG.debug(inMemoryDataTree.toString());
158 boolean exception = false;
160 LeafRefValidatation.validate(mergeDevicesCandidate, rootLeafRefContext);
161 } catch (final LeafRefDataValidationFailedException e) {
162 LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
163 // :TODO verify errors count gz
164 assertEquals(6, e.getValidationsErrorsCount());
168 inMemoryDataTree.commit(mergeDevicesCandidate);
170 LOG.debug("*************************");
171 LOG.debug("After mergeDevices: ");
172 LOG.debug("*************************");
173 LOG.debug(inMemoryDataTree.toString());
175 assertTrue(exception);
178 private static void initQnames() {
180 chips = QName.create(rootModuleQname, "chips");
181 chip = QName.create(rootModuleQname, "chip");
182 devType = QName.create(rootModuleQname, "dev_type");
183 chipDesc = QName.create(rootModuleQname, "chip_desc");
185 devices = QName.create(rootModuleQname, "devices");
186 device = QName.create(rootModuleQname, "device");
187 typeText1 = QName.create(rootModuleQname, "type_text1");
188 typeText2 = QName.create(rootModuleQname, "type_text2");
189 typeText3 = QName.create(rootModuleQname, "type_text3");
190 devDesc = QName.create(rootModuleQname, "dev_desc");
191 sn = QName.create(rootModuleQname, "sn");
192 defaultIp = QName.create(rootModuleQname, "default_ip");
194 deviceTypeStr = QName.create(rootModuleQname, "device_types");
195 deviceType = QName.create(rootModuleQname, "device_type");
196 type1 = QName.create(rootModuleQname, "type1");
197 type2 = QName.create(rootModuleQname, "type2");
198 type3 = QName.create(rootModuleQname, "type3");
199 desc = QName.create(rootModuleQname, "desc");
202 private static void initSchemaContext() throws URISyntaxException, FileNotFoundException, ReactorException {
204 final File resourceFile = new File(DataTreeCandidateValidatorTest.class.getResource(
205 "/leafref-validation/leafref-validation3.yang").toURI());
206 final File resourceDir = resourceFile.getParentFile();
208 context = RetestUtils.parseYangSources(Arrays.asList(resourceDir.listFiles()));
210 final Set<Module> modules = context.getModules();
211 for (final Module module : modules) {
212 if (module.getName().equals("leafref-validation3")) {
217 rootModuleQname = mainModule.getQNameModule();
220 private static void initDataTree() {
222 inMemoryDataTree = InMemoryDataTreeFactory.getInstance().create();
223 inMemoryDataTree.setSchemaContext(context);
225 final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
227 final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(chips);
228 final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
229 final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
230 initialDataTreeModification.write(path1, chipsContainer);
232 final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
233 .getDataChildByName(deviceTypeStr);
234 final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
235 final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
236 initialDataTreeModification.write(path2, deviceTypesContainer);
238 initialDataTreeModification.ready();
239 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
241 inMemoryDataTree.commit(writeChipsCandidate);
243 System.out.println(inMemoryDataTree.toString());
246 private static void initLeafRefContext() {
247 rootLeafRefContext = LeafRefContext.create(context);
250 private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
252 final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.getDataChildByName(deviceType);
254 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
255 .containerBuilder(container);
257 final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
258 devTypeContainerBldr.addChild(devTypeMap);
260 return devTypeContainerBldr.build();
263 private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
265 final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
267 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1",
268 devTypeListSchemaNode));
269 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2",
270 devTypeListSchemaNode));
271 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3",
272 devTypeListSchemaNode));
274 return devTypeMapBldr.build();
277 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
278 final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
280 final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
281 final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
282 final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
283 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
285 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
286 .mapEntryBuilder(devTypeListSchemaNode);
288 devTypeMapEntryBldr.addChild(type1Leaf);
289 devTypeMapEntryBldr.addChild(type2Leaf);
290 devTypeMapEntryBldr.addChild(type3Leaf);
291 devTypeMapEntryBldr.addChild(descLeaf);
293 return devTypeMapEntryBldr.build();
296 private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
298 final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.getDataChildByName(chip);
300 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
301 .containerBuilder(container);
303 final MapNode chipsMap = createChipsList(chipsListSchemaNode);
304 chipsContainerBldr.addChild(chipsMap);
306 return chipsContainerBldr.build();
309 private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
311 final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
313 chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
314 chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
316 return chipsMapBldr.build();
319 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
320 final ListSchemaNode chipsListSchemaNode) {
322 final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
323 final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
325 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
326 .mapEntryBuilder(chipsListSchemaNode);
328 chipsMapEntryBldr.addChild(devTypeLeaf);
329 chipsMapEntryBldr.addChild(chipDescLeaf);
331 return chipsMapEntryBldr.build();
334 private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
336 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
338 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
339 .containerBuilder(container);
341 final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
342 devicesContainerBldr.addChild(devicesMap);
344 return devicesContainerBldr.build();
347 private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
349 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
351 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
352 "192.168.0.1", deviceListSchemaNode));
353 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
354 "192.168.0.1", deviceListSchemaNode));
355 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
356 "192.168.0.1", deviceListSchemaNode));
357 devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1",
358 deviceListSchemaNode));
360 return devicesMapBldr.build();
363 private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
365 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
367 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
368 .containerBuilder(container);
370 final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
371 devicesContainerBldr.addChild(devicesMap);
373 return devicesContainerBldr.build();
376 private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
378 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
380 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
381 "192.168.0.1", deviceListSchemaNode));
382 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
383 "192.168.0.1", deviceListSchemaNode));
384 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
385 "192.168.0.1", deviceListSchemaNode));
386 devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1",
387 deviceListSchemaNode));
389 return devicesMapBldr.build();
392 private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
393 final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal,
394 final ListSchemaNode devicesListSchemaNode) {
396 final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1, type1TextVal);
397 final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2, type2TextVal);
398 final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3, type3TextVal);
399 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
400 final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
401 final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp, defaultIpVal);
403 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
404 .mapEntryBuilder(devicesListSchemaNode);
406 devicesMapEntryBldr.addChild(typeText1Leaf);
407 devicesMapEntryBldr.addChild(typeText2Leaf);
408 devicesMapEntryBldr.addChild(typeText3Leaf);
409 devicesMapEntryBldr.addChild(descLeaf);
410 devicesMapEntryBldr.addChild(snValLeaf);
411 devicesMapEntryBldr.addChild(defaultIpLeaf);
413 return devicesMapEntryBldr.build();