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 DataTreeCandidateValidatorTest2 {
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 typeText;
51 private static QName devDesc;
52 private static QName sn;
53 private static QName defaultIp;
55 private static QName deviceTypeStr;
56 private static QName deviceType;
57 private static QName type;
58 private static QName desc;
60 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest2.class);
63 static void init() throws DataValidationFailedException {
64 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
66 for (final var module : context.getModules()) {
67 if (module.getName().equals("leafref-validation2")) {
72 rootModuleQname = mainModule.getQNameModule();
73 rootLeafRefContext = LeafRefContext.create(context);
75 chips = QName.create(rootModuleQname, "chips");
76 chip = QName.create(rootModuleQname, "chip");
77 devType = QName.create(rootModuleQname, "dev_type");
78 chipDesc = QName.create(rootModuleQname, "chip_desc");
80 devices = QName.create(rootModuleQname, "devices");
81 device = QName.create(rootModuleQname, "device");
82 typeText = QName.create(rootModuleQname, "type_text");
83 devDesc = QName.create(rootModuleQname, "dev_desc");
84 sn = QName.create(rootModuleQname, "sn");
85 defaultIp = QName.create(rootModuleQname, "default_ip");
87 deviceTypeStr = QName.create(rootModuleQname, "device_types");
88 deviceType = QName.create(rootModuleQname, "device_type");
89 type = QName.create(rootModuleQname, "type");
90 desc = QName.create(rootModuleQname, "desc");
92 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
93 final var initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
94 initialDataTreeModification.write(YangInstanceIdentifier.of(chips), Builders.containerBuilder()
95 .withNodeIdentifier(new NodeIdentifier(chips))
96 .addChild(Builders.mapBuilder()
97 .withNodeIdentifier(new NodeIdentifier(chip))
98 .addChild(createChipsListEntry("dev_type_1", "desc1"))
99 .addChild(createChipsListEntry("dev_type_2", "desc2"))
103 initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), Builders.containerBuilder()
104 .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
105 .addChild(Builders.mapBuilder()
106 .withNodeIdentifier(new NodeIdentifier(deviceType))
107 .addChild(createDevTypeListEntry("dev_type_1", "typedesc1"))
108 .addChild(createDevTypeListEntry("dev_type_2", "typedesc2"))
109 .addChild(createDevTypeListEntry("dev_type_3", "typedesc3"))
113 initialDataTreeModification.ready();
114 final var writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
116 inMemoryDataTree.commit(writeChipsCandidate);
117 LOG.debug("{}", inMemoryDataTree);
121 static void cleanup() {
122 inMemoryDataTree = null;
123 rootLeafRefContext = null;
129 void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
133 private static void writeDevices() throws DataValidationFailedException {
134 final var writeModification = inMemoryDataTree.takeSnapshot().newModification();
135 writeModification.write(YangInstanceIdentifier.of(devices), Builders.containerBuilder()
136 .withNodeIdentifier(new NodeIdentifier(devices))
137 .addChild(Builders.mapBuilder()
138 .withNodeIdentifier(new NodeIdentifier(device))
139 .addChild(createDeviceListEntry("dev_type_1", "typedesc1", 123456, "192.168.0.1"))
140 .addChild(createDeviceListEntry("dev_type_2", "typedesc2", 123457, "192.168.0.1"))
141 .addChild(createDeviceListEntry("dev_type_2", "typedesc3", 123457, "192.168.0.1"))
142 .addChild(createDeviceListEntry("dev_type_1", "typedesc2", 123458, "192.168.0.1"))
143 .addChild(createDeviceListEntry("unknown", "unknown", 123457, "192.168.0.1"))
147 writeModification.ready();
148 final var writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
150 LOG.debug("*************************");
151 LOG.debug("Before writeDevices: ");
152 LOG.debug("*************************");
153 LOG.debug("{}", inMemoryDataTree);
155 final var ex = assertThrows(LeafRefDataValidationFailedException.class,
156 () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
157 assertEquals(4, ex.getValidationsErrorsCount());
159 inMemoryDataTree.commit(writeDevicesCandidate);
161 LOG.debug("*************************");
162 LOG.debug("After write: ");
163 LOG.debug("*************************");
164 LOG.debug("{}", inMemoryDataTree);
167 private static MapEntryNode createDevTypeListEntry(final String typeVal, final String descVal) {
168 return Builders.mapEntryBuilder()
169 .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType, type, typeVal))
170 .addChild(ImmutableNodes.leafNode(type, typeVal))
171 .addChild(ImmutableNodes.leafNode(desc, descVal))
175 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
176 return Builders.mapEntryBuilder()
177 .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
178 .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
179 .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
183 private static MapEntryNode createDeviceListEntry(final String typeTextVal, final String descVal, final int snVal,
184 final String defaultIpVal) {
185 return Builders.mapEntryBuilder()
186 .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText, typeTextVal, sn, snVal)))
187 .addChild(ImmutableNodes.leafNode(typeText, typeTextVal))
188 .addChild(ImmutableNodes.leafNode(devDesc, descVal))
189 .addChild(ImmutableNodes.leafNode(sn, snVal))
190 .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))