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.assertThrows;
14 import org.junit.AfterClass;
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.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
29 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
30 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidation;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class DataTreeCandidateValidatorTest2 {
42 private static EffectiveModelContext context;
43 private static Module mainModule;
44 private static QNameModule rootModuleQname;
45 private static LeafRefContext rootLeafRefContext;
46 public static DataTree inMemoryDataTree;
48 private static QName chips;
49 private static QName chip;
50 private static QName devType;
51 private static QName chipDesc;
53 private static QName devices;
54 private static QName device;
55 private static QName typeText;
56 private static QName devDesc;
57 private static QName sn;
58 private static QName defaultIp;
60 private static QName deviceTypeStr;
61 private static QName deviceType;
62 private static QName type;
63 private static QName desc;
65 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest2.class);
68 public static void init() throws DataValidationFailedException {
69 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
71 for (final Module module : context.getModules()) {
72 if (module.getName().equals("leafref-validation2")) {
77 rootModuleQname = mainModule.getQNameModule();
78 rootLeafRefContext = LeafRefContext.create(context);
80 chips = QName.create(rootModuleQname, "chips");
81 chip = QName.create(rootModuleQname, "chip");
82 devType = QName.create(rootModuleQname, "dev_type");
83 chipDesc = QName.create(rootModuleQname, "chip_desc");
85 devices = QName.create(rootModuleQname, "devices");
86 device = QName.create(rootModuleQname, "device");
87 typeText = QName.create(rootModuleQname, "type_text");
88 devDesc = QName.create(rootModuleQname, "dev_desc");
89 sn = QName.create(rootModuleQname, "sn");
90 defaultIp = QName.create(rootModuleQname, "default_ip");
92 deviceTypeStr = QName.create(rootModuleQname, "device_types");
93 deviceType = QName.create(rootModuleQname, "device_type");
94 type = QName.create(rootModuleQname, "type");
95 desc = QName.create(rootModuleQname, "desc");
97 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
98 final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
99 initialDataTreeModification.write(YangInstanceIdentifier.of(chips), Builders.containerBuilder()
100 .withNodeIdentifier(new NodeIdentifier(chips))
101 .addChild(Builders.mapBuilder()
102 .withNodeIdentifier(new NodeIdentifier(chip))
103 .addChild(createChipsListEntry("dev_type_1", "desc1"))
104 .addChild(createChipsListEntry("dev_type_2", "desc2"))
108 initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), Builders.containerBuilder()
109 .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
110 .addChild(Builders.mapBuilder()
111 .withNodeIdentifier(new NodeIdentifier(deviceType))
112 .addChild(createDevTypeListEntry("dev_type_1", "typedesc1"))
113 .addChild(createDevTypeListEntry("dev_type_2", "typedesc2"))
114 .addChild(createDevTypeListEntry("dev_type_3", "typedesc3"))
118 initialDataTreeModification.ready();
119 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
121 inMemoryDataTree.commit(writeChipsCandidate);
122 LOG.debug("{}", inMemoryDataTree);
126 public static void cleanup() {
127 inMemoryDataTree = null;
128 rootLeafRefContext = null;
134 public void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
138 private static void writeDevices() throws DataValidationFailedException {
139 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
140 writeModification.write(YangInstanceIdentifier.of(devices), Builders.containerBuilder()
141 .withNodeIdentifier(new NodeIdentifier(devices))
142 .addChild(Builders.mapBuilder()
143 .withNodeIdentifier(new NodeIdentifier(device))
144 .addChild(createDeviceListEntry("dev_type_1", "typedesc1", 123456, "192.168.0.1"))
145 .addChild(createDeviceListEntry("dev_type_2", "typedesc2", 123457, "192.168.0.1"))
146 .addChild(createDeviceListEntry("dev_type_2", "typedesc3", 123457, "192.168.0.1"))
147 .addChild(createDeviceListEntry("dev_type_1", "typedesc2", 123458, "192.168.0.1"))
148 .addChild(createDeviceListEntry("unknown", "unknown", 123457, "192.168.0.1"))
152 writeModification.ready();
153 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
155 LOG.debug("*************************");
156 LOG.debug("Before writeDevices: ");
157 LOG.debug("*************************");
158 LOG.debug("{}", inMemoryDataTree);
160 final LeafRefDataValidationFailedException ex = assertThrows(LeafRefDataValidationFailedException.class,
161 () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
162 assertEquals(4, ex.getValidationsErrorsCount());
164 inMemoryDataTree.commit(writeDevicesCandidate);
166 LOG.debug("*************************");
167 LOG.debug("After write: ");
168 LOG.debug("*************************");
169 LOG.debug("{}", inMemoryDataTree);
172 private static MapEntryNode createDevTypeListEntry(final String typeVal, final String descVal) {
173 return Builders.mapEntryBuilder()
174 .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType, type, typeVal))
175 .addChild(ImmutableNodes.leafNode(type, typeVal))
176 .addChild(ImmutableNodes.leafNode(desc, descVal))
180 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
181 return Builders.mapEntryBuilder()
182 .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
183 .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
184 .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
188 private static MapEntryNode createDeviceListEntry(final String typeTextVal, final String descVal, final int snVal,
189 final String defaultIpVal) {
190 return Builders.mapEntryBuilder()
191 .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText, typeTextVal, sn, snVal)))
192 .addChild(ImmutableNodes.leafNode(typeText, typeTextVal))
193 .addChild(ImmutableNodes.leafNode(devDesc, descVal))
194 .addChild(ImmutableNodes.leafNode(sn, snVal))
195 .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))