2 * Copyright (c) 2013 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.parser.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import com.google.common.collect.Lists;
16 import java.io.FileInputStream;
17 import java.io.InputStream;
18 import java.math.BigInteger;
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
29 import org.junit.Test;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Deviation;
36 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
37 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
38 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.UsesNode;
48 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
49 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
50 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
52 public class YangParserWithContextTest {
53 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
54 private final YangParserImpl parser = new YangParserImpl();
57 public void testTypeFromContext() throws Exception {
58 String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
59 InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()));
60 SchemaContext context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
63 resource = "/context-test/test1.yang";
64 InputStream stream2 = new FileInputStream(new File(getClass().getResource(resource).toURI()));
65 Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
67 assertNotNull(module);
69 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
71 ExtendedType leafType = (ExtendedType) leaf.getType();
72 QName qname = leafType.getQName();
73 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
74 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
75 assertEquals("port-number", qname.getLocalName());
77 ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
78 qname = leafBaseType.getQName();
79 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
80 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
81 assertEquals("port-number", qname.getLocalName());
83 ExtendedType dscpExt = (ExtendedType) TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
84 List<RangeConstraint> ranges = dscpExt.getRangeConstraints();
85 assertEquals(1, ranges.size());
86 RangeConstraint range = ranges.get(0);
87 assertEquals(BigInteger.ZERO, range.getMin());
88 assertEquals(BigInteger.valueOf(63), range.getMax());
92 public void testUsesFromContext() throws Exception {
93 SchemaContext context;
94 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
95 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
96 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()));
97 InputStream stream4 = new FileInputStream(
98 new File(getClass().getResource("/model/subfoo.yang").toURI()))) {
99 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3,
103 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
105 testModule = TestUtils.loadModuleWithContext("test2", stream, context);
107 assertNotNull(testModule);
109 // suffix _u = added by uses
110 // suffix _g = defined in grouping from context
113 Module contextModule = context.findModuleByNamespace(URI.create("urn:opendaylight.baz")).iterator().next();
114 assertNotNull(contextModule);
115 Set<GroupingDefinition> groupings = contextModule.getGroupings();
116 assertEquals(1, groupings.size());
117 GroupingDefinition grouping = groupings.iterator().next();
119 // get node containing uses
120 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
121 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
124 Set<UsesNode> uses = destination.getUses();
125 assertEquals(1, uses.size());
127 // check uses process
128 AnyXmlSchemaNode data_u = (AnyXmlSchemaNode) destination.getDataChildByName("data");
129 assertNotNull(data_u);
130 assertTrue(data_u.isAddedByUses());
132 AnyXmlSchemaNode data_g = (AnyXmlSchemaNode) grouping.getDataChildByName("data");
133 assertNotNull(data_g);
134 assertFalse(data_g.isAddedByUses());
135 assertFalse(data_u.equals(data_g));
137 ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName("how");
138 assertNotNull(how_u);
139 assertTrue(how_u.isAddedByUses());
141 ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName("how");
142 assertNotNull(how_g);
143 assertFalse(how_g.isAddedByUses());
144 assertFalse(how_u.equals(how_g));
146 LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address");
147 assertNotNull(address_u);
148 assertTrue(address_u.isAddedByUses());
150 LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName("address");
151 assertNotNull(address_g);
152 assertFalse(address_g.isAddedByUses());
153 assertFalse(address_u.equals(address_g));
155 ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port");
156 assertNotNull(port_u);
157 assertTrue(port_u.isAddedByUses());
159 ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName("port");
160 assertNotNull(port_g);
161 assertFalse(port_g.isAddedByUses());
162 assertFalse(port_u.equals(port_g));
164 ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses");
165 assertNotNull(addresses_u);
166 assertTrue(addresses_u.isAddedByUses());
168 ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName("addresses");
169 assertNotNull(addresses_g);
170 assertFalse(addresses_g.isAddedByUses());
171 assertFalse(addresses_u.equals(addresses_g));
173 // grouping defined by 'uses'
174 Set<GroupingDefinition> groupings_u = destination.getGroupings();
175 assertEquals(1, groupings_u.size());
176 GroupingDefinition grouping_u = groupings_u.iterator().next();
177 assertTrue(grouping_u.isAddedByUses());
179 // grouping defined in 'grouping' node
180 Set<GroupingDefinition> groupings_g = grouping.getGroupings();
181 assertEquals(1, groupings_g.size());
182 GroupingDefinition grouping_g = groupings_g.iterator().next();
183 assertFalse(grouping_g.isAddedByUses());
184 assertFalse(grouping_u.equals(grouping_g));
186 List<UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
187 assertEquals(1, nodes_u.size());
188 UnknownSchemaNode node_u = nodes_u.get(0);
189 assertTrue(node_u.isAddedByUses());
191 List<UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
192 assertEquals(1, nodes_g.size());
193 UnknownSchemaNode node_g = nodes_g.get(0);
194 assertFalse(node_g.isAddedByUses());
195 assertFalse(node_u.equals(node_g));
199 public void testUsesRefineFromContext() throws Exception {
200 SchemaContext context;
201 try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
202 InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
203 InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()));
204 InputStream stream4 = new FileInputStream(
205 new File(getClass().getResource("/model/subfoo.yang").toURI()))) {
206 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3,
210 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
212 module = TestUtils.loadModuleWithContext("test2", stream, context);
214 assertNotNull(module);
216 ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
217 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
218 Set<UsesNode> usesNodes = destination.getUses();
219 assertEquals(1, usesNodes.size());
220 UsesNode usesNode = usesNodes.iterator().next();
222 // test grouping path
223 List<QName> path = new ArrayList<>();
224 QName qname = QName.create(URI.create("urn:opendaylight.baz"), simpleDateFormat.parse("2013-02-27"), "target");
226 SchemaPath expectedPath = SchemaPath.create(path, true);
227 assertEquals(expectedPath, usesNode.getGroupingPath());
230 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
231 assertEquals(3, refines.size());
233 LeafSchemaNode refineLeaf = null;
234 ContainerSchemaNode refineContainer = null;
235 ListSchemaNode refineList = null;
236 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
237 SchemaNode value = entry.getValue();
238 if (value instanceof LeafSchemaNode) {
239 refineLeaf = (LeafSchemaNode) value;
240 } else if (value instanceof ContainerSchemaNode) {
241 refineContainer = (ContainerSchemaNode) value;
242 } else if (value instanceof ListSchemaNode) {
243 refineList = (ListSchemaNode) value;
248 assertNotNull(refineLeaf);
249 assertEquals("address", refineLeaf.getQName().getLocalName());
250 assertEquals("description of address defined by refine", refineLeaf.getDescription());
251 assertEquals("address reference added by refine", refineLeaf.getReference());
252 assertFalse(refineLeaf.isConfiguration());
253 assertTrue(refineLeaf.getConstraints().isMandatory());
254 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
255 assertEquals(1, leafMustConstraints.size());
256 MustDefinition leafMust = leafMustConstraints.iterator().next();
257 assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
260 assertNotNull(refineContainer);
261 Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
262 assertTrue(mustConstraints.isEmpty());
263 assertEquals("description of port defined by refine", refineContainer.getDescription());
264 assertEquals("port reference added by refine", refineContainer.getReference());
265 assertFalse(refineContainer.isConfiguration());
266 assertTrue(refineContainer.isPresenceContainer());
269 assertNotNull(refineList);
270 assertEquals("description of addresses defined by refine", refineList.getDescription());
271 assertEquals("addresses reference added by refine", refineList.getReference());
272 assertFalse(refineList.isConfiguration());
273 assertEquals(2, (int) refineList.getConstraints().getMinElements());
274 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
278 public void testIdentity() throws Exception {
279 SchemaContext context;
280 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
281 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
282 YangContextParser parser = new YangParserImpl();
283 context = parser.parseFile(yangFile, dependenciesDir);
286 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
288 module = TestUtils.loadModuleWithContext("test3", stream, context);
290 assertNotNull(module);
292 Set<IdentitySchemaNode> identities = module.getIdentities();
293 assertEquals(1, identities.size());
295 IdentitySchemaNode identity = identities.iterator().next();
296 QName idQName = identity.getQName();
297 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
298 assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
299 assertEquals("pt", idQName.getLocalName());
301 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
302 QName idBaseQName = baseIdentity.getQName();
303 assertEquals(URI.create("urn:custom.types.demo"), idBaseQName.getNamespace());
304 assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
305 assertEquals("service-type", idBaseQName.getLocalName());
309 public void testUnknownNodes() throws Exception {
310 SchemaContext context;
311 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
312 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
313 YangContextParser parser = new YangParserImpl();
314 context = parser.parseFile(yangFile, dependenciesDir);
317 try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test3.yang")
319 module = TestUtils.loadModuleWithContext("test3", stream, context);
322 ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
323 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
324 assertEquals(1, unknownNodes.size());
326 UnknownSchemaNode un = unknownNodes.get(0);
327 QName unType = un.getNodeType();
328 assertEquals(URI.create("urn:custom.types.demo"), unType.getNamespace());
329 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
330 assertEquals("mountpoint", unType.getLocalName());
331 assertEquals("point", un.getNodeParameter());
332 assertNotNull(un.getExtensionDefinition());
336 public void testAugment() throws Exception {
338 String resource = "/context-augment-test/test4.yang";
339 SchemaContext context = parser.parseFiles(Collections.singleton(new File(getClass().getResource(resource)
342 // load another modules and parse them against already existing context
343 File test1 = new File(getClass().getResource("/context-augment-test/test1.yang").toURI());
344 File test2 = new File(getClass().getResource("/context-augment-test/test2.yang").toURI());
345 File test3 = new File(getClass().getResource("/context-augment-test/test3.yang").toURI());
346 Set<Module> modules = parser.parseFiles(Arrays.asList(test1, test2, test3), context).getModules();
347 assertNotNull(modules);
349 Module t4 = TestUtils.findModule(modules, "test4");
350 ContainerSchemaNode interfaces = (ContainerSchemaNode) t4.getDataChildByName("interfaces");
351 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
353 // test augmentation process
354 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
355 assertNotNull(augmentHolder);
356 DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
358 DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
359 assertNotNull(interfaceId);
360 DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
361 assertNotNull(higherLayerIf);
362 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
363 assertNotNull(schemas);
364 assertNotNull(schemas.getDataChildByName("id"));
366 // test augment target after augmentation: check if it is same instance
367 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
368 assertTrue(ifEntry == ifEntryAfterAugment);
372 public void testDeviation() throws Exception {
374 SchemaContext context;
375 String resource = "/model/bar.yang";
377 try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
378 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
381 // load another modules and parse them against already existing context
383 try (InputStream stream = new FileInputStream(new File(getClass().getResource(
384 "/context-test/deviation-test.yang").toURI()))) {
385 List<InputStream> input = Lists.newArrayList(stream);
386 modules = TestUtils.loadModulesWithContext(input, context);
388 assertNotNull(modules);
391 Module testModule = TestUtils.findModule(modules, "deviation-test");
392 Set<Deviation> deviations = testModule.getDeviations();
393 assertEquals(1, deviations.size());
394 Deviation dev = deviations.iterator().next();
396 assertEquals("system/user ref", dev.getReference());
398 URI expectedNS = URI.create("urn:opendaylight.bar");
399 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
400 Date expectedRev = simpleDateFormat.parse("2013-07-03");
401 List<QName> path = new ArrayList<>();
402 path.add(QName.create(expectedNS, expectedRev, "interfaces"));
403 path.add(QName.create(expectedNS, expectedRev, "ifEntry"));
404 SchemaPath expectedPath = SchemaPath.create(path, true);
406 assertEquals(expectedPath, dev.getTargetPath());
407 assertEquals(Deviate.ADD, dev.getDeviate());