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.tree.leafref;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import org.junit.jupiter.api.AfterAll;
15 import org.junit.jupiter.api.BeforeAll;
16 import org.junit.jupiter.api.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.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class DataTreeCandidateValidatorTest3 {
37 private static EffectiveModelContext context;
38 private static Module mainModule;
39 private static QNameModule rootModuleQname;
40 private static LeafRefContext rootLeafRefContext;
41 public static DataTree inMemoryDataTree;
43 private static QName chips;
44 private static QName chip;
45 private static QName devType;
46 private static QName chipDesc;
48 private static QName devices;
49 private static QName device;
50 private static QName typeText1;
51 private static QName typeText2;
52 private static QName typeText3;
53 private static QName devDesc;
54 private static QName sn;
55 private static QName defaultIp;
57 private static QName deviceTypeStr;
58 private static QName deviceType;
59 private static QName type1;
60 private static QName type2;
61 private static QName type3;
62 private static QName desc;
64 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
65 private static final String NEW_LINE = System.getProperty("line.separator");
68 static void init() throws DataValidationFailedException {
69 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
71 for (final var module : context.getModules()) {
72 if (module.getName().equals("leafref-validation3")) {
77 rootModuleQname = mainModule.getQNameModule();
78 rootLeafRefContext = LeafRefContext.create(context);
81 chips = QName.create(rootModuleQname, "chips");
82 chip = QName.create(rootModuleQname, "chip");
83 devType = QName.create(rootModuleQname, "dev_type");
84 chipDesc = QName.create(rootModuleQname, "chip_desc");
86 devices = QName.create(rootModuleQname, "devices");
87 device = QName.create(rootModuleQname, "device");
88 typeText1 = QName.create(rootModuleQname, "type_text1");
89 typeText2 = QName.create(rootModuleQname, "type_text2");
90 typeText3 = QName.create(rootModuleQname, "type_text3");
91 devDesc = QName.create(rootModuleQname, "dev_desc");
92 sn = QName.create(rootModuleQname, "sn");
93 defaultIp = QName.create(rootModuleQname, "default_ip");
95 deviceTypeStr = QName.create(rootModuleQname, "device_types");
96 deviceType = QName.create(rootModuleQname, "device_type");
97 type1 = QName.create(rootModuleQname, "type1");
98 type2 = QName.create(rootModuleQname, "type2");
99 type3 = QName.create(rootModuleQname, "type3");
100 desc = QName.create(rootModuleQname, "desc");
102 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
104 final var initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
106 initialDataTreeModification.write(YangInstanceIdentifier.of(chips), Builders.containerBuilder()
107 .withNodeIdentifier(new NodeIdentifier(chips))
108 .addChild(Builders.mapBuilder()
109 .withNodeIdentifier(new NodeIdentifier(chip))
110 .addChild(createChipsListEntry("dev_type_1", "desc1"))
111 .addChild(createChipsListEntry("dev_type_2", "desc2"))
115 initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), Builders.containerBuilder()
116 .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
117 .addChild(Builders.mapBuilder()
118 .withNodeIdentifier(new NodeIdentifier(deviceType))
119 .addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1"))
120 .addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2"))
121 .addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3"))
125 initialDataTreeModification.ready();
126 final var writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
128 inMemoryDataTree.commit(writeChipsCandidate);
130 LOG.debug("{}", inMemoryDataTree);
134 static void cleanup() {
135 inMemoryDataTree = null;
136 rootLeafRefContext = null;
142 void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
147 private static void writeDevices() throws DataValidationFailedException {
148 final var writeModification = inMemoryDataTree.takeSnapshot().newModification();
149 writeModification.write(YangInstanceIdentifier.of(devices), Builders.containerBuilder()
150 .withNodeIdentifier(new NodeIdentifier(devices))
151 .addChild(Builders.mapBuilder()
152 .withNodeIdentifier(new NodeIdentifier(device))
153 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
155 .addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
157 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
159 .addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1"))
163 writeModification.ready();
164 final var writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
166 LOG.debug("*************************");
167 LOG.debug("Before writeDevices: ");
168 LOG.debug("*************************");
169 LOG.debug("{}", inMemoryDataTree);
171 final var ex = assertThrows(LeafRefDataValidationFailedException.class,
172 () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
173 assertEquals(6, ex.getValidationsErrorsCount());
175 inMemoryDataTree.commit(writeDevicesCandidate);
177 LOG.debug("*************************");
178 LOG.debug("After writeDevices: ");
179 LOG.debug("*************************");
180 LOG.debug("{}", inMemoryDataTree);
183 private static void mergeDevices() throws DataValidationFailedException {
184 final var devicesContainer = Builders.containerBuilder()
185 .withNodeIdentifier(new NodeIdentifier(devices))
186 .addChild(Builders.mapBuilder()
187 .withNodeIdentifier(new NodeIdentifier(device))
188 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
190 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
192 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
194 .addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1"))
198 final var devicesPath = YangInstanceIdentifier.of(devices);
199 final var mergeModification = inMemoryDataTree.takeSnapshot().newModification();
200 mergeModification.write(devicesPath, devicesContainer);
201 mergeModification.merge(devicesPath, devicesContainer);
203 mergeModification.ready();
204 final var mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
206 LOG.debug("*************************");
207 LOG.debug("Before mergeDevices: ");
208 LOG.debug("*************************");
209 LOG.debug("{}", inMemoryDataTree);
211 final var ex = assertThrows(LeafRefDataValidationFailedException.class,
212 () -> LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext));
213 // :TODO verify errors count gz
214 assertEquals(6, ex.getValidationsErrorsCount());
216 inMemoryDataTree.commit(mergeDevicesCandidate);
218 LOG.debug("*************************");
219 LOG.debug("After mergeDevices: ");
220 LOG.debug("*************************");
221 LOG.debug("{}", inMemoryDataTree);
224 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
225 final String type3Val, final String descVal) {
226 return Builders.mapEntryBuilder()
227 .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType,
228 Map.of(type1, type1Val, type2, type2Val, type3, type3Val)))
229 .addChild(ImmutableNodes.leafNode(type1, type1Val))
230 .addChild(ImmutableNodes.leafNode(type2, type2Val))
231 .addChild(ImmutableNodes.leafNode(type3, type3Val))
232 .addChild(ImmutableNodes.leafNode(desc, descVal))
236 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
237 return Builders.mapEntryBuilder()
238 .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
239 .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
240 .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
244 private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
245 final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal) {
246 return Builders.mapEntryBuilder()
247 .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText1, type1TextVal, sn, snVal)))
248 .addChild(ImmutableNodes.leafNode(typeText1, type1TextVal))
249 .addChild(ImmutableNodes.leafNode(typeText2, type2TextVal))
250 .addChild(ImmutableNodes.leafNode(typeText3, type3TextVal))
251 .addChild(ImmutableNodes.leafNode(devDesc, descVal))
252 .addChild(ImmutableNodes.leafNode(sn, snVal))
253 .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))