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.spi.node.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class DataTreeCandidateValidatorTest3 {
36 private static EffectiveModelContext context;
37 private static Module mainModule;
38 private static QNameModule rootModuleQname;
39 private static LeafRefContext rootLeafRefContext;
40 public static DataTree inMemoryDataTree;
42 private static QName chips;
43 private static QName chip;
44 private static QName devType;
45 private static QName chipDesc;
47 private static QName devices;
48 private static QName device;
49 private static QName typeText1;
50 private static QName typeText2;
51 private static QName typeText3;
52 private static QName devDesc;
53 private static QName sn;
54 private static QName defaultIp;
56 private static QName deviceTypeStr;
57 private static QName deviceType;
58 private static QName type1;
59 private static QName type2;
60 private static QName type3;
61 private static QName desc;
63 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
64 private static final String NEW_LINE = System.getProperty("line.separator");
67 static void init() throws DataValidationFailedException {
68 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
70 for (final var module : context.getModules()) {
71 if (module.getName().equals("leafref-validation3")) {
76 rootModuleQname = mainModule.getQNameModule();
77 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 typeText1 = QName.create(rootModuleQname, "type_text1");
88 typeText2 = QName.create(rootModuleQname, "type_text2");
89 typeText3 = QName.create(rootModuleQname, "type_text3");
90 devDesc = QName.create(rootModuleQname, "dev_desc");
91 sn = QName.create(rootModuleQname, "sn");
92 defaultIp = QName.create(rootModuleQname, "default_ip");
94 deviceTypeStr = QName.create(rootModuleQname, "device_types");
95 deviceType = QName.create(rootModuleQname, "device_type");
96 type1 = QName.create(rootModuleQname, "type1");
97 type2 = QName.create(rootModuleQname, "type2");
98 type3 = QName.create(rootModuleQname, "type3");
99 desc = QName.create(rootModuleQname, "desc");
101 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
103 final var initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
105 initialDataTreeModification.write(YangInstanceIdentifier.of(chips), ImmutableNodes.newContainerBuilder()
106 .withNodeIdentifier(new NodeIdentifier(chips))
107 .addChild(ImmutableNodes.newSystemMapBuilder()
108 .withNodeIdentifier(new NodeIdentifier(chip))
109 .addChild(createChipsListEntry("dev_type_1", "desc1"))
110 .addChild(createChipsListEntry("dev_type_2", "desc2"))
114 initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), ImmutableNodes.newContainerBuilder()
115 .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
116 .addChild(ImmutableNodes.newSystemMapBuilder()
117 .withNodeIdentifier(new NodeIdentifier(deviceType))
118 .addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1"))
119 .addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2"))
120 .addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3"))
124 initialDataTreeModification.ready();
125 final var writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
127 inMemoryDataTree.commit(writeChipsCandidate);
129 LOG.debug("{}", inMemoryDataTree);
133 static void cleanup() {
134 inMemoryDataTree = null;
135 rootLeafRefContext = null;
141 void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
146 private static void writeDevices() throws DataValidationFailedException {
147 final var writeModification = inMemoryDataTree.takeSnapshot().newModification();
148 writeModification.write(YangInstanceIdentifier.of(devices), ImmutableNodes.newContainerBuilder()
149 .withNodeIdentifier(new NodeIdentifier(devices))
150 .addChild(ImmutableNodes.newSystemMapBuilder()
151 .withNodeIdentifier(new NodeIdentifier(device))
152 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
154 .addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
156 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
158 .addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1"))
162 writeModification.ready();
163 final var writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
165 LOG.debug("*************************");
166 LOG.debug("Before writeDevices: ");
167 LOG.debug("*************************");
168 LOG.debug("{}", inMemoryDataTree);
170 final var ex = assertThrows(LeafRefDataValidationFailedException.class,
171 () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
172 assertEquals(6, ex.getValidationsErrorsCount());
174 inMemoryDataTree.commit(writeDevicesCandidate);
176 LOG.debug("*************************");
177 LOG.debug("After writeDevices: ");
178 LOG.debug("*************************");
179 LOG.debug("{}", inMemoryDataTree);
182 private static void mergeDevices() throws DataValidationFailedException {
183 final var devicesContainer = ImmutableNodes.newContainerBuilder()
184 .withNodeIdentifier(new NodeIdentifier(devices))
185 .addChild(ImmutableNodes.newSystemMapBuilder()
186 .withNodeIdentifier(new NodeIdentifier(device))
187 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
189 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
191 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
193 .addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1"))
197 final var devicesPath = YangInstanceIdentifier.of(devices);
198 final var mergeModification = inMemoryDataTree.takeSnapshot().newModification();
199 mergeModification.write(devicesPath, devicesContainer);
200 mergeModification.merge(devicesPath, devicesContainer);
202 mergeModification.ready();
203 final var mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
205 LOG.debug("*************************");
206 LOG.debug("Before mergeDevices: ");
207 LOG.debug("*************************");
208 LOG.debug("{}", inMemoryDataTree);
210 final var ex = assertThrows(LeafRefDataValidationFailedException.class,
211 () -> LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext));
212 // :TODO verify errors count gz
213 assertEquals(6, ex.getValidationsErrorsCount());
215 inMemoryDataTree.commit(mergeDevicesCandidate);
217 LOG.debug("*************************");
218 LOG.debug("After mergeDevices: ");
219 LOG.debug("*************************");
220 LOG.debug("{}", inMemoryDataTree);
223 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
224 final String type3Val, final String descVal) {
225 return ImmutableNodes.newMapEntryBuilder()
226 .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType,
227 Map.of(type1, type1Val, type2, type2Val, type3, type3Val)))
228 .addChild(ImmutableNodes.leafNode(type1, type1Val))
229 .addChild(ImmutableNodes.leafNode(type2, type2Val))
230 .addChild(ImmutableNodes.leafNode(type3, type3Val))
231 .addChild(ImmutableNodes.leafNode(desc, descVal))
235 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
236 return ImmutableNodes.newMapEntryBuilder()
237 .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
238 .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
239 .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
243 private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
244 final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal) {
245 return ImmutableNodes.newMapEntryBuilder()
246 .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText1, type1TextVal, sn, snVal)))
247 .addChild(ImmutableNodes.leafNode(typeText1, type1TextVal))
248 .addChild(ImmutableNodes.leafNode(typeText2, type2TextVal))
249 .addChild(ImmutableNodes.leafNode(typeText3, type3TextVal))
250 .addChild(ImmutableNodes.leafNode(devDesc, descVal))
251 .addChild(ImmutableNodes.leafNode(sn, snVal))
252 .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))