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.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.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
28 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
31 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class DataTreeCandidateValidatorTest3 {
40 private static EffectiveModelContext context;
41 private static Module mainModule;
42 private static QNameModule rootModuleQname;
43 private static LeafRefContext rootLeafRefContext;
44 public static DataTree inMemoryDataTree;
46 private static QName chips;
47 private static QName chip;
48 private static QName devType;
49 private static QName chipDesc;
51 private static QName devices;
52 private static QName device;
53 private static QName typeText1;
54 private static QName typeText2;
55 private static QName typeText3;
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 type1;
63 private static QName type2;
64 private static QName type3;
65 private static QName desc;
67 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
68 private static final String NEW_LINE = System.getProperty("line.separator");
71 public static void init() throws DataValidationFailedException {
72 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
74 for (final Module module : context.getModules()) {
75 if (module.getName().equals("leafref-validation3")) {
80 rootModuleQname = mainModule.getQNameModule();
81 rootLeafRefContext = LeafRefContext.create(context);
84 chips = QName.create(rootModuleQname, "chips");
85 chip = QName.create(rootModuleQname, "chip");
86 devType = QName.create(rootModuleQname, "dev_type");
87 chipDesc = QName.create(rootModuleQname, "chip_desc");
89 devices = QName.create(rootModuleQname, "devices");
90 device = QName.create(rootModuleQname, "device");
91 typeText1 = QName.create(rootModuleQname, "type_text1");
92 typeText2 = QName.create(rootModuleQname, "type_text2");
93 typeText3 = QName.create(rootModuleQname, "type_text3");
94 devDesc = QName.create(rootModuleQname, "dev_desc");
95 sn = QName.create(rootModuleQname, "sn");
96 defaultIp = QName.create(rootModuleQname, "default_ip");
98 deviceTypeStr = QName.create(rootModuleQname, "device_types");
99 deviceType = QName.create(rootModuleQname, "device_type");
100 type1 = QName.create(rootModuleQname, "type1");
101 type2 = QName.create(rootModuleQname, "type2");
102 type3 = QName.create(rootModuleQname, "type3");
103 desc = QName.create(rootModuleQname, "desc");
105 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
107 final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
109 initialDataTreeModification.write(YangInstanceIdentifier.of(chips), Builders.containerBuilder()
110 .withNodeIdentifier(new NodeIdentifier(chips))
111 .addChild(Builders.mapBuilder()
112 .withNodeIdentifier(new NodeIdentifier(chip))
113 .addChild(createChipsListEntry("dev_type_1", "desc1"))
114 .addChild(createChipsListEntry("dev_type_2", "desc2"))
118 initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), Builders.containerBuilder()
119 .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
120 .addChild(Builders.mapBuilder()
121 .withNodeIdentifier(new NodeIdentifier(deviceType))
122 .addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1"))
123 .addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2"))
124 .addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3"))
128 initialDataTreeModification.ready();
129 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
131 inMemoryDataTree.commit(writeChipsCandidate);
133 LOG.debug("{}", inMemoryDataTree);
137 public static void cleanup() {
138 inMemoryDataTree = null;
139 rootLeafRefContext = null;
145 public void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
150 private static void writeDevices() throws DataValidationFailedException {
151 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
152 writeModification.write(YangInstanceIdentifier.of(devices), Builders.containerBuilder()
153 .withNodeIdentifier(new NodeIdentifier(devices))
154 .addChild(Builders.mapBuilder()
155 .withNodeIdentifier(new NodeIdentifier(device))
156 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
158 .addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
160 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
162 .addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1"))
166 writeModification.ready();
167 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
169 LOG.debug("*************************");
170 LOG.debug("Before writeDevices: ");
171 LOG.debug("*************************");
172 LOG.debug("{}", inMemoryDataTree);
174 final LeafRefDataValidationFailedException ex = assertThrows(LeafRefDataValidationFailedException.class,
175 () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
176 assertEquals(6, ex.getValidationsErrorsCount());
178 inMemoryDataTree.commit(writeDevicesCandidate);
180 LOG.debug("*************************");
181 LOG.debug("After writeDevices: ");
182 LOG.debug("*************************");
183 LOG.debug("{}", inMemoryDataTree);
186 private static void mergeDevices() throws DataValidationFailedException {
187 final ContainerNode devicesContainer = Builders.containerBuilder()
188 .withNodeIdentifier(new NodeIdentifier(devices))
189 .addChild(Builders.mapBuilder()
190 .withNodeIdentifier(new NodeIdentifier(device))
191 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
193 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
195 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
197 .addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1"))
201 final YangInstanceIdentifier devicesPath = YangInstanceIdentifier.of(devices);
202 final DataTreeModification mergeModification = inMemoryDataTree.takeSnapshot().newModification();
203 mergeModification.write(devicesPath, devicesContainer);
204 mergeModification.merge(devicesPath, devicesContainer);
206 mergeModification.ready();
207 final DataTreeCandidate mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
209 LOG.debug("*************************");
210 LOG.debug("Before mergeDevices: ");
211 LOG.debug("*************************");
212 LOG.debug("{}", inMemoryDataTree);
214 final LeafRefDataValidationFailedException ex = assertThrows(LeafRefDataValidationFailedException.class,
215 () -> LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext));
216 // :TODO verify errors count gz
217 assertEquals(6, ex.getValidationsErrorsCount());
219 inMemoryDataTree.commit(mergeDevicesCandidate);
221 LOG.debug("*************************");
222 LOG.debug("After mergeDevices: ");
223 LOG.debug("*************************");
224 LOG.debug("{}", inMemoryDataTree);
227 private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
228 final String type3Val, final String descVal) {
229 return Builders.mapEntryBuilder()
230 .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType,
231 Map.of(type1, type1Val, type2, type2Val, type3, type3Val)))
232 .addChild(ImmutableNodes.leafNode(type1, type1Val))
233 .addChild(ImmutableNodes.leafNode(type2, type2Val))
234 .addChild(ImmutableNodes.leafNode(type3, type3Val))
235 .addChild(ImmutableNodes.leafNode(desc, descVal))
239 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
240 return Builders.mapEntryBuilder()
241 .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
242 .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
243 .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
247 private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
248 final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal) {
249 return Builders.mapEntryBuilder()
250 .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText1, type1TextVal, sn, snVal)))
251 .addChild(ImmutableNodes.leafNode(typeText1, type1TextVal))
252 .addChild(ImmutableNodes.leafNode(typeText2, type2TextVal))
253 .addChild(ImmutableNodes.leafNode(typeText3, type3TextVal))
254 .addChild(ImmutableNodes.leafNode(devDesc, descVal))
255 .addChild(ImmutableNodes.leafNode(sn, snVal))
256 .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))