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;
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.api.schema.tree.TreeType;
33 import org.opendaylight.yangtools.yang.data.impl.TestUtils;
34 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
35 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
36 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidatation;
37 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
42 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class DataTreeCandidateValidatorTest3 {
52 private static SchemaContext context;
53 private static Module mainModule;
54 private static QNameModule rootModuleQname;
55 private static LeafRefContext rootLeafRefContext;
56 public static TipProducingDataTree inMemoryDataTree;
58 private static QName chips;
59 private static QName chip;
60 private static QName devType;
61 private static QName chipDesc;
63 private static QName devices;
64 private static QName device;
65 private static QName typeText1;
66 private static QName typeText2;
67 private static QName typeText3;
68 private static QName devDesc;
69 private static QName sn;
70 private static QName defaultIp;
72 private static QName deviceTypeStr;
73 private static QName deviceType;
74 private static QName type1;
75 private static QName type2;
76 private static QName type3;
77 private static QName desc;
79 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
80 private static final String NEW_LINE = System.getProperty("line.separator");
83 BasicConfigurator.configure();
87 public static void init() throws FileNotFoundException, ReactorException, URISyntaxException {
96 public void dataTreeCanditateValidationTest2() {
103 private static void writeDevices() {
105 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
107 final ContainerNode devicesContainer = createDevicesContainer(devicesContSchemaNode);
109 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
110 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
111 writeModification.write(devicesPath, devicesContainer);
113 writeModification.ready();
114 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
116 LOG.debug("*************************");
117 LOG.debug("Before writeDevices: ");
118 LOG.debug("*************************");
119 LOG.debug(inMemoryDataTree.toString());
121 boolean exception = false;
123 LeafRefValidatation.validate(writeDevicesCandidate, rootLeafRefContext);
124 } catch (final LeafRefDataValidationFailedException e) {
125 LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
126 assertEquals(6, e.getValidationsErrorsCount());
130 assertTrue(exception);
132 inMemoryDataTree.commit(writeDevicesCandidate);
134 LOG.debug("*************************");
135 LOG.debug("After writeDevices: ");
136 LOG.debug("*************************");
137 LOG.debug(inMemoryDataTree.toString());
140 private static void mergeDevices() {
142 final ContainerSchemaNode devicesContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(devices);
144 final ContainerNode devicesContainer = createDevices2Container(devicesContSchemaNode);
146 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
147 final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
148 mergeModification.write(devicesPath, devicesContainer);
149 mergeModification.merge(devicesPath, devicesContainer);
151 mergeModification.ready();
152 final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
154 LOG.debug("*************************");
155 LOG.debug("Before mergeDevices: ");
156 LOG.debug("*************************");
157 LOG.debug(inMemoryDataTree.toString());
159 boolean exception = false;
161 LeafRefValidatation.validate(mergeDevicesCandidate, rootLeafRefContext);
162 } catch (final LeafRefDataValidationFailedException e) {
163 LOG.debug("All validation errors:" + NEW_LINE + e.getMessage());
164 // :TODO verify errors count gz
165 assertEquals(6, e.getValidationsErrorsCount());
169 inMemoryDataTree.commit(mergeDevicesCandidate);
171 LOG.debug("*************************");
172 LOG.debug("After mergeDevices: ");
173 LOG.debug("*************************");
174 LOG.debug(inMemoryDataTree.toString());
176 assertTrue(exception);
179 private static void initQnames() {
181 chips = QName.create(rootModuleQname, "chips");
182 chip = QName.create(rootModuleQname, "chip");
183 devType = QName.create(rootModuleQname, "dev_type");
184 chipDesc = QName.create(rootModuleQname, "chip_desc");
186 devices = QName.create(rootModuleQname, "devices");
187 device = QName.create(rootModuleQname, "device");
188 typeText1 = QName.create(rootModuleQname, "type_text1");
189 typeText2 = QName.create(rootModuleQname, "type_text2");
190 typeText3 = QName.create(rootModuleQname, "type_text3");
191 devDesc = QName.create(rootModuleQname, "dev_desc");
192 sn = QName.create(rootModuleQname, "sn");
193 defaultIp = QName.create(rootModuleQname, "default_ip");
195 deviceTypeStr = QName.create(rootModuleQname, "device_types");
196 deviceType = QName.create(rootModuleQname, "device_type");
197 type1 = QName.create(rootModuleQname, "type1");
198 type2 = QName.create(rootModuleQname, "type2");
199 type3 = QName.create(rootModuleQname, "type3");
200 desc = QName.create(rootModuleQname, "desc");
203 private static void initSchemaContext() throws URISyntaxException, FileNotFoundException, ReactorException {
205 final File resourceFile = new File(DataTreeCandidateValidatorTest.class.getResource(
206 "/leafref-validation/leafref-validation3.yang").toURI());
207 final File resourceDir = resourceFile.getParentFile();
209 context = TestUtils.parseYangSources(Arrays.asList(resourceDir.listFiles()));
211 final Set<Module> modules = context.getModules();
212 for (final Module module : modules) {
213 if (module.getName().equals("leafref-validation3")) {
218 rootModuleQname = mainModule.getQNameModule();
221 private static void initDataTree() {
223 inMemoryDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
224 inMemoryDataTree.setSchemaContext(context);
226 final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
228 final ContainerSchemaNode chipsListContSchemaNode = (ContainerSchemaNode) mainModule.getDataChildByName(chips);
229 final ContainerNode chipsContainer = createChipsContainer(chipsListContSchemaNode);
230 final YangInstanceIdentifier path1 = YangInstanceIdentifier.of(chips);
231 initialDataTreeModification.write(path1, chipsContainer);
233 final ContainerSchemaNode devTypesListContSchemaNode = (ContainerSchemaNode) mainModule
234 .getDataChildByName(deviceTypeStr);
235 final ContainerNode deviceTypesContainer = createDevTypeStrContainer(devTypesListContSchemaNode);
236 final YangInstanceIdentifier path2 = YangInstanceIdentifier.of(deviceTypeStr);
237 initialDataTreeModification.write(path2, deviceTypesContainer);
239 initialDataTreeModification.ready();
240 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
242 inMemoryDataTree.commit(writeChipsCandidate);
244 System.out.println(inMemoryDataTree.toString());
247 private static void initLeafRefContext() {
248 rootLeafRefContext = LeafRefContext.create(context);
251 private static ContainerNode createDevTypeStrContainer(final ContainerSchemaNode container) {
253 final ListSchemaNode devTypeListSchemaNode = (ListSchemaNode) container.getDataChildByName(deviceType);
255 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devTypeContainerBldr = Builders
256 .containerBuilder(container);
258 final MapNode devTypeMap = createDevTypeList(devTypeListSchemaNode);
259 devTypeContainerBldr.addChild(devTypeMap);
261 return devTypeContainerBldr.build();
264 private static MapNode createDevTypeList(final ListSchemaNode devTypeListSchemaNode) {
266 final CollectionNodeBuilder<MapEntryNode, MapNode> devTypeMapBldr = Builders.mapBuilder(devTypeListSchemaNode);
268 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1",
269 devTypeListSchemaNode));
270 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2",
271 devTypeListSchemaNode));
272 devTypeMapBldr.addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3",
273 devTypeListSchemaNode));
275 return devTypeMapBldr.build();
278 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
279 final String type3Val, final String descVal, final ListSchemaNode devTypeListSchemaNode) {
281 final LeafNode<String> type1Leaf = ImmutableNodes.leafNode(type1, type1Val);
282 final LeafNode<String> type2Leaf = ImmutableNodes.leafNode(type2, type2Val);
283 final LeafNode<String> type3Leaf = ImmutableNodes.leafNode(type3, type3Val);
284 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(desc, descVal);
286 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devTypeMapEntryBldr = Builders
287 .mapEntryBuilder(devTypeListSchemaNode);
289 devTypeMapEntryBldr.addChild(type1Leaf);
290 devTypeMapEntryBldr.addChild(type2Leaf);
291 devTypeMapEntryBldr.addChild(type3Leaf);
292 devTypeMapEntryBldr.addChild(descLeaf);
294 return devTypeMapEntryBldr.build();
297 private static ContainerNode createChipsContainer(final ContainerSchemaNode container) {
299 final ListSchemaNode chipsListSchemaNode = (ListSchemaNode) container.getDataChildByName(chip);
301 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> chipsContainerBldr = Builders
302 .containerBuilder(container);
304 final MapNode chipsMap = createChipsList(chipsListSchemaNode);
305 chipsContainerBldr.addChild(chipsMap);
307 return chipsContainerBldr.build();
310 private static MapNode createChipsList(final ListSchemaNode chipsListSchemaNode) {
312 final CollectionNodeBuilder<MapEntryNode, MapNode> chipsMapBldr = Builders.mapBuilder(chipsListSchemaNode);
314 chipsMapBldr.addChild(createChipsListEntry("dev_type_1", "desc1", chipsListSchemaNode));
315 chipsMapBldr.addChild(createChipsListEntry("dev_type_2", "desc2", chipsListSchemaNode));
317 return chipsMapBldr.build();
320 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal,
321 final ListSchemaNode chipsListSchemaNode) {
323 final LeafNode<String> devTypeLeaf = ImmutableNodes.leafNode(devType, devTypeVal);
324 final LeafNode<String> chipDescLeaf = ImmutableNodes.leafNode(chipDesc, chipDescVal);
326 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> chipsMapEntryBldr = Builders
327 .mapEntryBuilder(chipsListSchemaNode);
329 chipsMapEntryBldr.addChild(devTypeLeaf);
330 chipsMapEntryBldr.addChild(chipDescLeaf);
332 return chipsMapEntryBldr.build();
335 private static ContainerNode createDevicesContainer(final ContainerSchemaNode container) {
337 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
339 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
340 .containerBuilder(container);
342 final MapNode devicesMap = createDeviceList(devicesListSchemaNode);
343 devicesContainerBldr.addChild(devicesMap);
345 return devicesContainerBldr.build();
348 private static MapNode createDeviceList(final ListSchemaNode deviceListSchemaNode) {
350 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
352 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
353 "192.168.0.1", deviceListSchemaNode));
354 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
355 "192.168.0.1", deviceListSchemaNode));
356 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
357 "192.168.0.1", deviceListSchemaNode));
358 devicesMapBldr.addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1",
359 deviceListSchemaNode));
361 return devicesMapBldr.build();
364 private static ContainerNode createDevices2Container(final ContainerSchemaNode container) {
366 final ListSchemaNode devicesListSchemaNode = (ListSchemaNode) container.getDataChildByName(device);
368 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> devicesContainerBldr = Builders
369 .containerBuilder(container);
371 final MapNode devicesMap = createDevice2List(devicesListSchemaNode);
372 devicesContainerBldr.addChild(devicesMap);
374 return devicesContainerBldr.build();
377 private static MapNode createDevice2List(final ListSchemaNode deviceListSchemaNode) {
379 final CollectionNodeBuilder<MapEntryNode, MapNode> devicesMapBldr = Builders.mapBuilder(deviceListSchemaNode);
381 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
382 "192.168.0.1", deviceListSchemaNode));
383 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
384 "192.168.0.1", deviceListSchemaNode));
385 devicesMapBldr.addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
386 "192.168.0.1", deviceListSchemaNode));
387 devicesMapBldr.addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1",
388 deviceListSchemaNode));
390 return devicesMapBldr.build();
393 private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
394 final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal,
395 final ListSchemaNode devicesListSchemaNode) {
397 final LeafNode<String> typeText1Leaf = ImmutableNodes.leafNode(typeText1, type1TextVal);
398 final LeafNode<String> typeText2Leaf = ImmutableNodes.leafNode(typeText2, type2TextVal);
399 final LeafNode<String> typeText3Leaf = ImmutableNodes.leafNode(typeText3, type3TextVal);
400 final LeafNode<String> descLeaf = ImmutableNodes.leafNode(devDesc, descVal);
401 final LeafNode<Integer> snValLeaf = ImmutableNodes.leafNode(sn, snVal);
402 final LeafNode<String> defaultIpLeaf = ImmutableNodes.leafNode(defaultIp, defaultIpVal);
404 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> devicesMapEntryBldr = Builders
405 .mapEntryBuilder(devicesListSchemaNode);
407 devicesMapEntryBldr.addChild(typeText1Leaf);
408 devicesMapEntryBldr.addChild(typeText2Leaf);
409 devicesMapEntryBldr.addChild(typeText3Leaf);
410 devicesMapEntryBldr.addChild(descLeaf);
411 devicesMapEntryBldr.addChild(snValLeaf);
412 devicesMapEntryBldr.addChild(defaultIpLeaf);
414 return devicesMapEntryBldr.build();