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.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.DataTreeCandidate;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
28 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
30 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class DataTreeCandidateValidatorTest2 {
39 private static EffectiveModelContext context;
40 private static Module mainModule;
41 private static QNameModule rootModuleQname;
42 private static LeafRefContext rootLeafRefContext;
43 public static DataTree inMemoryDataTree;
45 private static QName chips;
46 private static QName chip;
47 private static QName devType;
48 private static QName chipDesc;
50 private static QName devices;
51 private static QName device;
52 private static QName typeText;
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 type;
60 private static QName desc;
62 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest2.class);
65 public static void init() throws DataValidationFailedException {
66 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
68 for (final Module module : context.getModules()) {
69 if (module.getName().equals("leafref-validation2")) {
74 rootModuleQname = mainModule.getQNameModule();
75 rootLeafRefContext = LeafRefContext.create(context);
77 chips = QName.create(rootModuleQname, "chips");
78 chip = QName.create(rootModuleQname, "chip");
79 devType = QName.create(rootModuleQname, "dev_type");
80 chipDesc = QName.create(rootModuleQname, "chip_desc");
82 devices = QName.create(rootModuleQname, "devices");
83 device = QName.create(rootModuleQname, "device");
84 typeText = QName.create(rootModuleQname, "type_text");
85 devDesc = QName.create(rootModuleQname, "dev_desc");
86 sn = QName.create(rootModuleQname, "sn");
87 defaultIp = QName.create(rootModuleQname, "default_ip");
89 deviceTypeStr = QName.create(rootModuleQname, "device_types");
90 deviceType = QName.create(rootModuleQname, "device_type");
91 type = QName.create(rootModuleQname, "type");
92 desc = QName.create(rootModuleQname, "desc");
94 inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
95 final DataTreeModification initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
96 initialDataTreeModification.write(YangInstanceIdentifier.of(chips), Builders.containerBuilder()
97 .withNodeIdentifier(new NodeIdentifier(chips))
98 .addChild(Builders.mapBuilder()
99 .withNodeIdentifier(new NodeIdentifier(chip))
100 .addChild(createChipsListEntry("dev_type_1", "desc1"))
101 .addChild(createChipsListEntry("dev_type_2", "desc2"))
105 initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), Builders.containerBuilder()
106 .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
107 .addChild(Builders.mapBuilder()
108 .withNodeIdentifier(new NodeIdentifier(deviceType))
109 .addChild(createDevTypeListEntry("dev_type_1", "typedesc1"))
110 .addChild(createDevTypeListEntry("dev_type_2", "typedesc2"))
111 .addChild(createDevTypeListEntry("dev_type_3", "typedesc3"))
115 initialDataTreeModification.ready();
116 final DataTreeCandidate writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
118 inMemoryDataTree.commit(writeChipsCandidate);
119 LOG.debug("{}", inMemoryDataTree);
123 public static void cleanup() {
124 inMemoryDataTree = null;
125 rootLeafRefContext = null;
131 public void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
135 private static void writeDevices() throws DataValidationFailedException {
136 final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
137 writeModification.write(YangInstanceIdentifier.of(devices), Builders.containerBuilder()
138 .withNodeIdentifier(new NodeIdentifier(devices))
139 .addChild(Builders.mapBuilder()
140 .withNodeIdentifier(new NodeIdentifier(device))
141 .addChild(createDeviceListEntry("dev_type_1", "typedesc1", 123456, "192.168.0.1"))
142 .addChild(createDeviceListEntry("dev_type_2", "typedesc2", 123457, "192.168.0.1"))
143 .addChild(createDeviceListEntry("dev_type_2", "typedesc3", 123457, "192.168.0.1"))
144 .addChild(createDeviceListEntry("dev_type_1", "typedesc2", 123458, "192.168.0.1"))
145 .addChild(createDeviceListEntry("unknown", "unknown", 123457, "192.168.0.1"))
149 writeModification.ready();
150 final DataTreeCandidate writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
152 LOG.debug("*************************");
153 LOG.debug("Before writeDevices: ");
154 LOG.debug("*************************");
155 LOG.debug("{}", inMemoryDataTree);
157 final LeafRefDataValidationFailedException ex = assertThrows(LeafRefDataValidationFailedException.class,
158 () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
159 assertEquals(4, ex.getValidationsErrorsCount());
161 inMemoryDataTree.commit(writeDevicesCandidate);
163 LOG.debug("*************************");
164 LOG.debug("After write: ");
165 LOG.debug("*************************");
166 LOG.debug("{}", inMemoryDataTree);
169 private static MapEntryNode createDevTypeListEntry(final String typeVal, final String descVal) {
170 return Builders.mapEntryBuilder()
171 .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType, type, typeVal))
172 .addChild(ImmutableNodes.leafNode(type, typeVal))
173 .addChild(ImmutableNodes.leafNode(desc, descVal))
177 private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
178 return Builders.mapEntryBuilder()
179 .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
180 .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
181 .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
185 private static MapEntryNode createDeviceListEntry(final String typeTextVal, final String descVal, final int snVal,
186 final String defaultIpVal) {
187 return Builders.mapEntryBuilder()
188 .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText, typeTextVal, sn, snVal)))
189 .addChild(ImmutableNodes.leafNode(typeText, typeTextVal))
190 .addChild(ImmutableNodes.leafNode(devDesc, descVal))
191 .addChild(ImmutableNodes.leafNode(sn, snVal))
192 .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))