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 org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
14 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
16 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Deviation;
20 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
21 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
22 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
23 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
29 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
30 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.Status;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
37 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
38 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
39 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
40 import org.opendaylight.yangtools.yang.model.util.Decimal64;
41 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
42 import org.opendaylight.yangtools.yang.model.util.Int16;
43 import org.opendaylight.yangtools.yang.model.util.Int32;
44 import org.opendaylight.yangtools.yang.model.util.StringType;
45 import org.opendaylight.yangtools.yang.model.util.Uint32;
46 import org.opendaylight.yangtools.yang.model.util.UnionType;
49 import java.io.FileInputStream;
50 import java.io.FileNotFoundException;
51 import java.io.InputStream;
53 import java.net.URISyntaxException;
54 import java.text.DateFormat;
55 import java.text.ParseException;
56 import java.text.SimpleDateFormat;
57 import java.util.ArrayList;
58 import java.util.Collection;
59 import java.util.Collections;
60 import java.util.Date;
61 import java.util.Iterator;
62 import java.util.LinkedHashSet;
63 import java.util.List;
67 import static org.junit.Assert.assertArrayEquals;
68 import static org.junit.Assert.assertEquals;
69 import static org.junit.Assert.assertFalse;
70 import static org.junit.Assert.assertNotNull;
71 import static org.junit.Assert.assertNull;
72 import static org.junit.Assert.assertTrue;
74 public class YangParserTest {
75 public static final String FS = File.separator;
77 private final URI fooNS = URI.create("urn:opendaylight.foo");
78 private final URI barNS = URI.create("urn:opendaylight.bar");
79 private final URI bazNS = URI.create("urn:opendaylight.baz");
84 private Set<Module> modules;
87 public void init() throws Exception {
88 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
89 fooRev = simpleDateFormat.parse("2013-02-27");
90 barRev = simpleDateFormat.parse("2013-07-03");
91 bazRev = simpleDateFormat.parse("2013-02-27");
93 modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
94 assertEquals(3, modules.size());
98 public void testHeaders() throws ParseException {
99 Module foo = TestUtils.findModule(modules, "foo");
101 assertEquals("foo", foo.getName());
102 assertEquals("1", foo.getYangVersion());
103 assertEquals(fooNS, foo.getNamespace());
104 assertEquals("foo", foo.getPrefix());
106 Set<ModuleImport> imports = foo.getImports();
107 assertEquals(2, imports.size());
109 ModuleImport import2 = TestUtils.findImport(imports, "br");
110 assertEquals("bar", import2.getModuleName());
111 assertEquals(barRev, import2.getRevision());
113 ModuleImport import3 = TestUtils.findImport(imports, "bz");
114 assertEquals("baz", import3.getModuleName());
115 assertEquals(bazRev, import3.getRevision());
117 assertEquals("opendaylight", foo.getOrganization());
118 assertEquals("http://www.opendaylight.org/", foo.getContact());
119 Date expectedRevision = TestUtils.createDate("2013-02-27");
120 assertEquals(expectedRevision, foo.getRevision());
121 assertEquals(" WILL BE DEFINED LATER", foo.getReference());
125 public void testOrderingTypedef() {
126 Module bar = TestUtils.findModule(modules, "bar");
127 Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
128 String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "my-decimal-type", "my-union",
129 "my-union-ext", "nested-union2", "string-ext1", "string-ext2", "string-ext3", "string-ext4" };
130 String[] actualOrder = new String[typedefs.size()];
133 for (TypeDefinition<?> type : typedefs) {
134 actualOrder[i] = type.getQName().getLocalName();
137 assertArrayEquals(expectedOrder, actualOrder);
141 public void testOrderingChildNodes() {
142 Module foo = TestUtils.findModule(modules, "foo");
143 AugmentationSchema augment1 = null;
144 for (AugmentationSchema as : foo.getAugmentations()) {
145 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
150 assertNotNull(augment1);
152 String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "odl", "schemas" };
153 String[] actualOrder = new String[expectedOrder.length];
156 for (DataSchemaNode augmentChild : augment1.getChildNodes()) {
157 actualOrder[i] = augmentChild.getQName().getLocalName();
161 assertArrayEquals(expectedOrder, actualOrder);
165 public void testOrderingNestedChildNodes1() {
166 Module foo = TestUtils.findModule(modules, "foo");
168 Set<DataSchemaNode> childNodes = foo.getChildNodes();
169 String[] expectedOrder = new String[] { "address", "addresses", "custom-union-leaf", "data", "datas",
170 "decimal-leaf", "decimal-leaf2", "ext", "how", "int32-leaf", "length-leaf", "mycont", "peer", "port",
171 "string-leaf", "transfer", "union-leaf" };
172 String[] actualOrder = new String[childNodes.size()];
175 for (DataSchemaNode child : childNodes) {
176 actualOrder[i] = child.getQName().getLocalName();
179 assertArrayEquals(expectedOrder, actualOrder);
183 public void testOrderingNestedChildNodes2() {
184 Module baz = TestUtils.findModule(modules, "baz");
185 Set<GroupingDefinition> groupings = baz.getGroupings();
186 assertEquals(1, groupings.size());
187 GroupingDefinition target = groupings.iterator().next();
189 Set<DataSchemaNode> childNodes = target.getChildNodes();
190 String[] expectedOrder = new String[] { "address", "addresses", "data", "how", "port" };
191 String[] actualOrder = new String[childNodes.size()];
194 for (DataSchemaNode child : childNodes) {
195 actualOrder[i] = child.getQName().getLocalName();
198 assertArrayEquals(expectedOrder, actualOrder);
202 public void testParseList() {
203 Module bar = TestUtils.findModule(modules, "bar");
204 URI expectedNamespace = URI.create("urn:opendaylight.bar");
205 String expectedPrefix = "bar";
207 ContainerSchemaNode interfaces = (ContainerSchemaNode) bar.getDataChildByName("interfaces");
209 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
210 // test SchemaNode args
211 QName expectedQName = new QName(expectedNamespace, barRev, expectedPrefix, "ifEntry");
212 assertEquals(expectedQName, ifEntry.getQName());
213 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, barRev, expectedPrefix, "interfaces",
215 assertEquals(expectedPath, ifEntry.getPath());
216 assertNull(ifEntry.getDescription());
217 assertNull(ifEntry.getReference());
218 assertEquals(Status.CURRENT, ifEntry.getStatus());
219 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
220 // test DataSchemaNode args
221 assertFalse(ifEntry.isAugmenting());
222 assertTrue(ifEntry.isConfiguration());
223 ConstraintDefinition constraints = ifEntry.getConstraints();
224 assertNull(constraints.getWhenCondition());
225 assertEquals(0, constraints.getMustConstraints().size());
226 assertFalse(constraints.isMandatory());
227 assertEquals(1, (int) constraints.getMinElements());
228 assertEquals(11, (int) constraints.getMaxElements());
229 // test AugmentationTarget args
230 Set<AugmentationSchema> availableAugmentations = ifEntry.getAvailableAugmentations();
231 assertEquals(2, availableAugmentations.size());
232 // test ListSchemaNode args
233 List<QName> expectedKey = new ArrayList<>();
234 expectedKey.add(new QName(expectedNamespace, barRev, expectedPrefix, "ifIndex"));
235 assertEquals(expectedKey, ifEntry.getKeyDefinition());
236 assertFalse(ifEntry.isUserOrdered());
237 // test DataNodeContainer args
238 assertEquals(0, ifEntry.getTypeDefinitions().size());
239 assertEquals(4, ifEntry.getChildNodes().size());
240 assertEquals(0, ifEntry.getGroupings().size());
241 assertEquals(0, ifEntry.getUses().size());
243 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
244 assertEquals(ifEntry.getKeyDefinition().get(0), ifIndex.getQName());
245 assertTrue(ifIndex.getType() instanceof Uint32);
246 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
247 assertTrue(ifMtu.getType() instanceof Int32);
251 public void testTypedefRangesResolving() throws ParseException {
252 Module foo = TestUtils.findModule(modules, "foo");
253 LeafSchemaNode int32Leaf = (LeafSchemaNode) foo.getDataChildByName("int32-leaf");
255 ExtendedType leafType = (ExtendedType) int32Leaf.getType();
256 QName leafTypeQName = leafType.getQName();
257 assertEquals("int32-ext2", leafTypeQName.getLocalName());
258 assertEquals("foo", leafTypeQName.getPrefix());
259 assertEquals(fooNS, leafTypeQName.getNamespace());
260 assertEquals(fooRev, leafTypeQName.getRevision());
261 assertNull(leafType.getUnits());
262 assertNull(leafType.getDefaultValue());
263 assertTrue(leafType.getLengthConstraints().isEmpty());
264 assertTrue(leafType.getPatternConstraints().isEmpty());
265 List<RangeConstraint> ranges = leafType.getRangeConstraints();
266 assertEquals(1, ranges.size());
267 RangeConstraint range = ranges.get(0);
268 assertEquals(12L, range.getMin());
269 assertEquals(20L, range.getMax());
271 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
272 QName baseTypeQName = baseType.getQName();
273 assertEquals("int32-ext2", baseTypeQName.getLocalName());
274 assertEquals("bar", baseTypeQName.getPrefix());
275 assertEquals(barNS, baseTypeQName.getNamespace());
276 assertEquals(barRev, baseTypeQName.getRevision());
277 assertEquals("mile", baseType.getUnits());
278 assertEquals("11", baseType.getDefaultValue());
279 assertTrue(leafType.getLengthConstraints().isEmpty());
280 assertTrue(leafType.getPatternConstraints().isEmpty());
281 List<RangeConstraint> baseTypeRanges = baseType.getRangeConstraints();
282 assertEquals(2, baseTypeRanges.size());
283 RangeConstraint baseTypeRange1 = baseTypeRanges.get(0);
284 assertEquals(3L, baseTypeRange1.getMin());
285 assertEquals(9L, baseTypeRange1.getMax());
286 RangeConstraint baseTypeRange2 = baseTypeRanges.get(1);
287 assertEquals(11L, baseTypeRange2.getMin());
288 assertEquals(20L, baseTypeRange2.getMax());
290 ExtendedType base = (ExtendedType) baseType.getBaseType();
291 QName baseQName = base.getQName();
292 assertEquals("int32-ext1", baseQName.getLocalName());
293 assertEquals("bar", baseQName.getPrefix());
294 assertEquals(barNS, baseQName.getNamespace());
295 assertEquals(barRev, baseQName.getRevision());
296 assertNull(base.getUnits());
297 assertNull(base.getDefaultValue());
298 assertTrue(leafType.getLengthConstraints().isEmpty());
299 assertTrue(leafType.getPatternConstraints().isEmpty());
300 List<RangeConstraint> baseRanges = base.getRangeConstraints();
301 assertEquals(1, baseRanges.size());
302 RangeConstraint baseRange = baseRanges.get(0);
303 assertEquals(2L, baseRange.getMin());
304 assertEquals(20L, baseRange.getMax());
306 assertTrue(base.getBaseType() instanceof Int32);
310 public void testTypedefPatternsResolving() {
311 Module foo = TestUtils.findModule(modules, "foo");
312 LeafSchemaNode stringleaf = (LeafSchemaNode) foo.getDataChildByName("string-leaf");
314 ExtendedType type = (ExtendedType) stringleaf.getType();
315 QName typeQName = type.getQName();
316 assertEquals("string-ext4", typeQName.getLocalName());
317 assertEquals("bar", typeQName.getPrefix());
318 assertEquals(barNS, typeQName.getNamespace());
319 assertEquals(barRev, typeQName.getRevision());
320 assertNull(type.getUnits());
321 assertNull(type.getDefaultValue());
322 List<PatternConstraint> patterns = type.getPatternConstraints();
323 assertEquals(1, patterns.size());
324 PatternConstraint pattern = patterns.iterator().next();
325 assertEquals("[e-z]*", pattern.getRegularExpression());
326 assertTrue(type.getLengthConstraints().isEmpty());
327 assertTrue(type.getRangeConstraints().isEmpty());
329 ExtendedType baseType1 = (ExtendedType) type.getBaseType();
330 QName baseType1QName = baseType1.getQName();
331 assertEquals("string-ext3", baseType1QName.getLocalName());
332 assertEquals("bar", baseType1QName.getPrefix());
333 assertEquals(barNS, baseType1QName.getNamespace());
334 assertEquals(barRev, baseType1QName.getRevision());
335 assertNull(baseType1.getUnits());
336 assertNull(baseType1.getDefaultValue());
337 patterns = baseType1.getPatternConstraints();
338 assertEquals(1, patterns.size());
339 pattern = patterns.iterator().next();
340 assertEquals("[b-u]*", pattern.getRegularExpression());
341 assertTrue(baseType1.getLengthConstraints().isEmpty());
342 assertTrue(baseType1.getRangeConstraints().isEmpty());
344 ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
345 QName baseType2QName = baseType2.getQName();
346 assertEquals("string-ext2", baseType2QName.getLocalName());
347 assertEquals("bar", baseType2QName.getPrefix());
348 assertEquals(barNS, baseType2QName.getNamespace());
349 assertEquals(barRev, baseType2QName.getRevision());
350 assertNull(baseType2.getUnits());
351 assertNull(baseType2.getDefaultValue());
352 assertTrue(baseType2.getPatternConstraints().isEmpty());
353 List<LengthConstraint> baseType2Lengths = baseType2.getLengthConstraints();
354 assertEquals(1, baseType2Lengths.size());
355 LengthConstraint length = baseType2Lengths.get(0);
356 assertEquals(6L, length.getMin());
357 assertEquals(10L, length.getMax());
358 assertTrue(baseType2.getRangeConstraints().isEmpty());
360 ExtendedType baseType3 = (ExtendedType) baseType2.getBaseType();
361 QName baseType3QName = baseType3.getQName();
362 assertEquals("string-ext1", baseType3QName.getLocalName());
363 assertEquals("bar", baseType3QName.getPrefix());
364 assertEquals(barNS, baseType3QName.getNamespace());
365 assertEquals(barRev, baseType3QName.getRevision());
366 assertNull(baseType3.getUnits());
367 assertNull(baseType3.getDefaultValue());
368 patterns = baseType3.getPatternConstraints();
369 assertEquals(1, patterns.size());
370 pattern = patterns.iterator().next();
371 assertEquals("[a-k]*", pattern.getRegularExpression());
372 List<LengthConstraint> baseType3Lengths = baseType3.getLengthConstraints();
373 assertEquals(1, baseType3Lengths.size());
374 length = baseType3Lengths.get(0);
375 assertEquals(5L, length.getMin());
376 assertEquals(11L, length.getMax());
377 assertTrue(baseType3.getRangeConstraints().isEmpty());
379 assertTrue(baseType3.getBaseType() instanceof StringType);
383 public void testTypedefLengthsResolving() {
384 Module foo = TestUtils.findModule(modules, "foo");
386 LeafSchemaNode lengthLeaf = (LeafSchemaNode) foo.getDataChildByName("length-leaf");
387 ExtendedType type = (ExtendedType) lengthLeaf.getType();
389 QName typeQName = type.getQName();
390 assertEquals("string-ext2", typeQName.getLocalName());
391 assertEquals("foo", typeQName.getPrefix());
392 assertEquals(fooNS, typeQName.getNamespace());
393 assertEquals(fooRev, typeQName.getRevision());
394 assertNull(type.getUnits());
395 assertNull(type.getDefaultValue());
396 assertTrue(type.getPatternConstraints().isEmpty());
397 List<LengthConstraint> typeLengths = type.getLengthConstraints();
398 assertEquals(1, typeLengths.size());
399 LengthConstraint length = typeLengths.get(0);
400 assertEquals(7L, length.getMin());
401 assertEquals(10L, length.getMax());
402 assertTrue(type.getRangeConstraints().isEmpty());
404 ExtendedType baseType1 = (ExtendedType) type.getBaseType();
405 QName baseType1QName = baseType1.getQName();
406 assertEquals("string-ext2", baseType1QName.getLocalName());
407 assertEquals("bar", baseType1QName.getPrefix());
408 assertEquals(barNS, baseType1QName.getNamespace());
409 assertEquals(barRev, baseType1QName.getRevision());
410 assertNull(baseType1.getUnits());
411 assertNull(baseType1.getDefaultValue());
412 assertTrue(baseType1.getPatternConstraints().isEmpty());
413 List<LengthConstraint> baseType2Lengths = baseType1.getLengthConstraints();
414 assertEquals(1, baseType2Lengths.size());
415 length = baseType2Lengths.get(0);
416 assertEquals(6L, length.getMin());
417 assertEquals(10L, length.getMax());
418 assertTrue(baseType1.getRangeConstraints().isEmpty());
420 ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
421 QName baseType2QName = baseType2.getQName();
422 assertEquals("string-ext1", baseType2QName.getLocalName());
423 assertEquals("bar", baseType2QName.getPrefix());
424 assertEquals(barNS, baseType2QName.getNamespace());
425 assertEquals(barRev, baseType2QName.getRevision());
426 assertNull(baseType2.getUnits());
427 assertNull(baseType2.getDefaultValue());
428 List<PatternConstraint> patterns = baseType2.getPatternConstraints();
429 assertEquals(1, patterns.size());
430 PatternConstraint pattern = patterns.iterator().next();
431 assertEquals("[a-k]*", pattern.getRegularExpression());
432 List<LengthConstraint> baseType3Lengths = baseType2.getLengthConstraints();
433 assertEquals(1, baseType3Lengths.size());
434 length = baseType3Lengths.get(0);
435 assertEquals(5L, length.getMin());
436 assertEquals(11L, length.getMax());
437 assertTrue(baseType2.getRangeConstraints().isEmpty());
439 assertTrue(baseType2.getBaseType() instanceof StringType);
443 public void testTypedefDecimal1() {
444 Module foo = TestUtils.findModule(modules, "foo");
445 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf");
447 ExtendedType type = (ExtendedType) testleaf.getType();
448 QName typeQName = type.getQName();
449 assertEquals("my-decimal-type", typeQName.getLocalName());
450 assertEquals("foo", typeQName.getPrefix());
451 assertEquals(fooNS, typeQName.getNamespace());
452 assertEquals(fooRev, typeQName.getRevision());
453 assertNull(type.getUnits());
454 assertNull(type.getDefaultValue());
455 assertEquals(4, (int) type.getFractionDigits());
456 assertTrue(type.getLengthConstraints().isEmpty());
457 assertTrue(type.getPatternConstraints().isEmpty());
458 assertTrue(type.getRangeConstraints().isEmpty());
460 ExtendedType typeBase = (ExtendedType) type.getBaseType();
461 QName typeBaseQName = typeBase.getQName();
462 assertEquals("my-decimal-type", typeBaseQName.getLocalName());
463 assertEquals("bar", typeBaseQName.getPrefix());
464 assertEquals(barNS, typeBaseQName.getNamespace());
465 assertEquals(barRev, typeBaseQName.getRevision());
466 assertNull(typeBase.getUnits());
467 assertNull(typeBase.getDefaultValue());
468 assertNull(typeBase.getFractionDigits());
469 assertTrue(typeBase.getLengthConstraints().isEmpty());
470 assertTrue(typeBase.getPatternConstraints().isEmpty());
471 assertTrue(typeBase.getRangeConstraints().isEmpty());
473 Decimal64 decimal = (Decimal64) typeBase.getBaseType();
474 assertEquals(6, (int) decimal.getFractionDigits());
478 public void testTypedefDecimal2() {
479 Module foo = TestUtils.findModule(modules, "foo");
480 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf2");
482 ExtendedType type = (ExtendedType) testleaf.getType();
483 QName typeQName = type.getQName();
484 assertEquals("my-decimal-type", typeQName.getLocalName());
485 assertEquals("bar", typeQName.getPrefix());
486 assertEquals(barNS, typeQName.getNamespace());
487 assertEquals(barRev, typeQName.getRevision());
488 assertNull(type.getUnits());
489 assertNull(type.getDefaultValue());
490 assertNull(type.getFractionDigits());
491 assertTrue(type.getLengthConstraints().isEmpty());
492 assertTrue(type.getPatternConstraints().isEmpty());
493 assertTrue(type.getRangeConstraints().isEmpty());
495 Decimal64 baseTypeDecimal = (Decimal64) type.getBaseType();
496 assertEquals(6, (int) baseTypeDecimal.getFractionDigits());
500 public void testTypedefUnion() {
501 Module foo = TestUtils.findModule(modules, "foo");
502 LeafSchemaNode unionleaf = (LeafSchemaNode) foo.getDataChildByName("union-leaf");
504 ExtendedType type = (ExtendedType) unionleaf.getType();
505 QName typeQName = type.getQName();
506 assertEquals("my-union-ext", typeQName.getLocalName());
507 assertEquals("bar", typeQName.getPrefix());
508 assertEquals(barNS, typeQName.getNamespace());
509 assertEquals(barRev, typeQName.getRevision());
510 assertNull(type.getUnits());
511 assertNull(type.getDefaultValue());
512 assertNull(type.getFractionDigits());
513 assertTrue(type.getLengthConstraints().isEmpty());
514 assertTrue(type.getPatternConstraints().isEmpty());
515 assertTrue(type.getRangeConstraints().isEmpty());
517 ExtendedType baseType = (ExtendedType) type.getBaseType();
518 QName baseTypeQName = baseType.getQName();
519 assertEquals("my-union", baseTypeQName.getLocalName());
520 assertEquals("bar", baseTypeQName.getPrefix());
521 assertEquals(barNS, baseTypeQName.getNamespace());
522 assertEquals(barRev, baseTypeQName.getRevision());
523 assertNull(baseType.getUnits());
524 assertNull(baseType.getDefaultValue());
525 assertNull(baseType.getFractionDigits());
526 assertTrue(baseType.getLengthConstraints().isEmpty());
527 assertTrue(baseType.getPatternConstraints().isEmpty());
528 assertTrue(baseType.getRangeConstraints().isEmpty());
530 UnionType unionType = (UnionType) baseType.getBaseType();
531 List<TypeDefinition<?>> unionTypes = unionType.getTypes();
532 assertEquals(2, unionTypes.size());
534 ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
535 QName unionType1QName = baseType.getQName();
536 assertEquals("my-union", unionType1QName.getLocalName());
537 assertEquals("bar", unionType1QName.getPrefix());
538 assertEquals(barNS, unionType1QName.getNamespace());
539 assertEquals(barRev, unionType1QName.getRevision());
540 assertNull(unionType1.getUnits());
541 assertNull(unionType1.getDefaultValue());
542 assertNull(unionType1.getFractionDigits());
543 assertTrue(unionType1.getLengthConstraints().isEmpty());
544 assertTrue(unionType1.getPatternConstraints().isEmpty());
545 List<RangeConstraint> ranges = unionType1.getRangeConstraints();
546 assertEquals(1, ranges.size());
547 RangeConstraint range = ranges.get(0);
548 assertEquals(1L, range.getMin());
549 assertEquals(100L, range.getMax());
550 assertTrue(unionType1.getBaseType() instanceof Int16);
552 assertTrue(unionTypes.get(1) instanceof Int32);
556 public void testNestedUnionResolving() {
557 Module foo = TestUtils.findModule(modules, "foo");
558 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("custom-union-leaf");
560 ExtendedType type = (ExtendedType) testleaf.getType();
561 QName testleafTypeQName = type.getQName();
562 assertEquals(bazNS, testleafTypeQName.getNamespace());
563 assertEquals(bazRev, testleafTypeQName.getRevision());
564 assertEquals("baz", testleafTypeQName.getPrefix());
565 assertEquals("union1", testleafTypeQName.getLocalName());
566 assertNull(type.getUnits());
567 assertNull(type.getDefaultValue());
568 assertNull(type.getFractionDigits());
569 assertTrue(type.getLengthConstraints().isEmpty());
570 assertTrue(type.getPatternConstraints().isEmpty());
571 assertTrue(type.getRangeConstraints().isEmpty());
573 ExtendedType typeBase = (ExtendedType) type.getBaseType();
574 QName typeBaseQName = typeBase.getQName();
575 assertEquals(bazNS, typeBaseQName.getNamespace());
576 assertEquals(bazRev, typeBaseQName.getRevision());
577 assertEquals("baz", typeBaseQName.getPrefix());
578 assertEquals("union2", typeBaseQName.getLocalName());
579 assertNull(typeBase.getUnits());
580 assertNull(typeBase.getDefaultValue());
581 assertNull(typeBase.getFractionDigits());
582 assertTrue(typeBase.getLengthConstraints().isEmpty());
583 assertTrue(typeBase.getPatternConstraints().isEmpty());
584 assertTrue(typeBase.getRangeConstraints().isEmpty());
586 UnionType union = (UnionType) typeBase.getBaseType();
587 List<TypeDefinition<?>> unionTypes = union.getTypes();
588 assertEquals(2, unionTypes.size());
589 assertTrue(unionTypes.get(0) instanceof Int32);
590 assertTrue(unionTypes.get(1) instanceof ExtendedType);
592 ExtendedType unionType1 = (ExtendedType) unionTypes.get(1);
593 QName uniontType1QName = unionType1.getQName();
594 assertEquals(barNS, uniontType1QName.getNamespace());
595 assertEquals(barRev, uniontType1QName.getRevision());
596 assertEquals("bar", uniontType1QName.getPrefix());
597 assertEquals("nested-union2", uniontType1QName.getLocalName());
598 assertNull(unionType1.getUnits());
599 assertNull(unionType1.getDefaultValue());
600 assertNull(unionType1.getFractionDigits());
601 assertTrue(unionType1.getLengthConstraints().isEmpty());
602 assertTrue(unionType1.getPatternConstraints().isEmpty());
603 assertTrue(unionType1.getRangeConstraints().isEmpty());
605 UnionType nestedUnion = (UnionType) unionType1.getBaseType();
606 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion.getTypes();
607 assertEquals(2, nestedUnion2Types.size());
608 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
609 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
611 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
612 QName myUnionExtQName = myUnionExt.getQName();
613 assertEquals(barNS, myUnionExtQName.getNamespace());
614 assertEquals(barRev, myUnionExtQName.getRevision());
615 assertEquals("bar", myUnionExtQName.getPrefix());
616 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
617 assertNull(myUnionExt.getUnits());
618 assertNull(myUnionExt.getDefaultValue());
619 assertNull(myUnionExt.getFractionDigits());
620 assertTrue(myUnionExt.getLengthConstraints().isEmpty());
621 assertTrue(myUnionExt.getPatternConstraints().isEmpty());
622 assertTrue(myUnionExt.getRangeConstraints().isEmpty());
624 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
625 QName myUnionQName = myUnion.getQName();
626 assertEquals(barNS, myUnionQName.getNamespace());
627 assertEquals(barRev, myUnionQName.getRevision());
628 assertEquals("bar", myUnionQName.getPrefix());
629 assertEquals("my-union", myUnionQName.getLocalName());
630 assertNull(myUnion.getUnits());
631 assertNull(myUnion.getDefaultValue());
632 assertNull(myUnion.getFractionDigits());
633 assertTrue(myUnion.getLengthConstraints().isEmpty());
634 assertTrue(myUnion.getPatternConstraints().isEmpty());
635 assertTrue(myUnion.getRangeConstraints().isEmpty());
637 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
638 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
639 assertEquals(2, myUnionBaseTypes.size());
640 assertTrue(myUnionBaseTypes.get(0) instanceof ExtendedType);
641 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
643 ExtendedType int16Ext = (ExtendedType) myUnionBaseTypes.get(0);
644 QName int16ExtQName = int16Ext.getQName();
645 assertEquals(barNS, int16ExtQName.getNamespace());
646 assertEquals(barRev, int16ExtQName.getRevision());
647 assertEquals("bar", int16ExtQName.getPrefix());
648 assertEquals("int16", int16ExtQName.getLocalName());
649 assertNull(int16Ext.getUnits());
650 assertNull(int16Ext.getDefaultValue());
651 assertNull(int16Ext.getFractionDigits());
652 assertTrue(int16Ext.getLengthConstraints().isEmpty());
653 assertTrue(int16Ext.getPatternConstraints().isEmpty());
654 List<RangeConstraint> ranges = int16Ext.getRangeConstraints();
655 assertEquals(1, ranges.size());
656 RangeConstraint range = ranges.get(0);
657 assertEquals(1L, range.getMin());
658 assertEquals(100L, range.getMax());
660 assertTrue(int16Ext.getBaseType() instanceof Int16);
664 public void testChoice() {
665 Module foo = TestUtils.findModule(modules, "foo");
666 ContainerSchemaNode transfer = (ContainerSchemaNode) foo.getDataChildByName("transfer");
667 ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how");
668 Set<ChoiceCaseNode> cases = how.getCases();
669 assertEquals(5, cases.size());
670 ChoiceCaseNode input = null;
671 ChoiceCaseNode output = null;
672 for (ChoiceCaseNode caseNode : cases) {
673 if ("input".equals(caseNode.getQName().getLocalName())) {
675 } else if ("output".equals(caseNode.getQName().getLocalName())) {
679 assertNotNull(input);
680 assertNotNull(input.getPath());
681 assertNotNull(output);
682 assertNotNull(output.getPath());
686 public void testDeviation() {
687 Module foo = TestUtils.findModule(modules, "foo");
688 Set<Deviation> deviations = foo.getDeviations();
689 assertEquals(1, deviations.size());
690 Deviation dev = deviations.iterator().next();
691 assertEquals("system/user ref", dev.getReference());
693 List<QName> path = new ArrayList<>();
694 path.add(new QName(barNS, barRev, "br", "interfaces"));
695 path.add(new QName(barNS, barRev, "br", "ifEntry"));
696 SchemaPath expectedPath = new SchemaPath(path, true);
698 assertEquals(expectedPath, dev.getTargetPath());
699 assertEquals(Deviate.ADD, dev.getDeviate());
703 public void testUnknownNode() {
704 Module baz = TestUtils.findModule(modules, "baz");
705 ContainerSchemaNode network = (ContainerSchemaNode) baz.getDataChildByName("network");
706 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
707 assertEquals(1, unknownNodes.size());
708 UnknownSchemaNode unknownNode = unknownNodes.get(0);
709 assertNotNull(unknownNode.getNodeType());
710 assertEquals("point", unknownNode.getNodeParameter());
714 public void testFeature() {
715 Module baz = TestUtils.findModule(modules, "baz");
716 Set<FeatureDefinition> features = baz.getFeatures();
717 assertEquals(1, features.size());
721 public void testExtension() {
722 Module baz = TestUtils.findModule(modules, "baz");
723 List<ExtensionDefinition> extensions = baz.getExtensionSchemaNodes();
724 assertEquals(1, extensions.size());
725 ExtensionDefinition extension = extensions.get(0);
726 assertEquals("name", extension.getArgument());
727 assertTrue(extension.isYinElement());
731 public void testNotification() {
732 Module baz = TestUtils.findModule(modules, "baz");
733 String expectedPrefix = "c";
735 Set<NotificationDefinition> notifications = baz.getNotifications();
736 assertEquals(1, notifications.size());
738 NotificationDefinition notification = notifications.iterator().next();
739 // test SchemaNode args
740 QName expectedQName = new QName(bazNS, bazRev, expectedPrefix, "event");
741 assertEquals(expectedQName, notification.getQName());
742 SchemaPath expectedPath = TestUtils.createPath(true, bazNS, bazRev, expectedPrefix, "event");
743 assertEquals(expectedPath, notification.getPath());
744 assertNull(notification.getDescription());
745 assertNull(notification.getReference());
746 assertEquals(Status.CURRENT, notification.getStatus());
747 assertEquals(0, notification.getUnknownSchemaNodes().size());
748 // test DataNodeContainer args
749 assertEquals(0, notification.getTypeDefinitions().size());
750 assertEquals(3, notification.getChildNodes().size());
751 assertEquals(0, notification.getGroupings().size());
752 assertEquals(0, notification.getUses().size());
754 LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
755 assertTrue(eventClass.getType() instanceof StringType);
756 LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
757 assertTrue(severity.getType() instanceof StringType);
761 public void testRpc() {
762 Module baz = TestUtils.findModule(modules, "baz");
764 Set<RpcDefinition> rpcs = baz.getRpcs();
765 assertEquals(1, rpcs.size());
767 RpcDefinition rpc = rpcs.iterator().next();
768 assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
769 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
773 public void testTypePath() throws ParseException {
774 Module bar = TestUtils.findModule(modules, "bar");
775 Set<TypeDefinition<?>> types = bar.getTypeDefinitions();
778 ExtendedType int32ext1 = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1");
779 QName int32TypedefQName = int32ext1.getQName();
781 assertEquals(barNS, int32TypedefQName.getNamespace());
782 assertEquals(barRev, int32TypedefQName.getRevision());
783 assertEquals("bar", int32TypedefQName.getPrefix());
784 assertEquals("int32-ext1", int32TypedefQName.getLocalName());
786 SchemaPath typeSchemaPath = int32ext1.getPath();
787 List<QName> typePath = typeSchemaPath.getPath();
788 assertEquals(1, typePath.size());
789 assertEquals(int32TypedefQName, typePath.get(0));
792 Int32 int32 = (Int32) int32ext1.getBaseType();
793 assertEquals(Int32.getInstance(), int32);
797 public void testTypePath2() throws ParseException {
798 Module bar = TestUtils.findModule(modules, "bar");
799 Set<TypeDefinition<?>> types = bar.getTypeDefinitions();
802 ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
803 QName myDecTypeQName = myDecType.getQName();
805 assertEquals(barNS, myDecTypeQName.getNamespace());
806 assertEquals(barRev, myDecTypeQName.getRevision());
807 assertEquals("bar", myDecTypeQName.getPrefix());
808 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
810 SchemaPath typeSchemaPath = myDecType.getPath();
811 List<QName> typePath = typeSchemaPath.getPath();
812 assertEquals(1, typePath.size());
813 assertEquals(myDecTypeQName, typePath.get(0));
815 // my-base-int32-type/int32
816 Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
817 QName dec64QName = dec64.getQName();
819 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
820 assertNull(dec64QName.getRevision());
821 assertEquals("", dec64QName.getPrefix());
822 assertEquals("decimal64", dec64QName.getLocalName());
824 SchemaPath dec64SchemaPath = dec64.getPath();
825 List<QName> dec64Path = dec64SchemaPath.getPath();
826 assertEquals(2, dec64Path.size());
827 assertEquals(myDecTypeQName, dec64Path.get(0));
828 assertEquals(dec64QName, dec64Path.get(1));
832 public void testParseMethod1() throws Exception {
833 File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI());
834 File dependenciesDir = new File(getClass().getResource("/parse-methods").toURI());
835 YangModelParser parser = new YangParserImpl();
836 modules = parser.parseYangModels(yangFile, dependenciesDir);
837 assertEquals(6, modules.size());
841 public void testParseMethod2() throws Exception {
842 File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI());
843 File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").toURI());
844 YangModelParser parser = new YangParserImpl();
845 modules = parser.parseYangModels(yangFile, dependenciesDir);
846 assertEquals(6, modules.size());
850 public void testSorting() throws Exception {
851 // Correct order: m2, m4, m6, m8, m7, m6, m3, m1
852 File yangFile = new File(getClass().getResource("/sorting-test/m1.yang").toURI());
853 File dependenciesDir = new File(getClass().getResource("/sorting-test").toURI());
854 YangModelParser parser = new YangParserImpl();
855 modules = parser.parseYangModels(yangFile, dependenciesDir);
856 SchemaContext ctx = new SchemaContextImpl(modules, Collections.<ModuleIdentifier, String>emptyMap());
858 assertSetEquals(modules, ctx.getModules());
861 parser = new YangParserImpl();
862 final File testDir = dependenciesDir;
863 final String[] fileList = testDir.list();
864 final List<File> testFiles = new ArrayList<>();
865 if (fileList == null) {
866 throw new FileNotFoundException(dependenciesDir.getAbsolutePath());
868 for (String fileName : fileList) {
869 testFiles.add(new File(testDir, fileName));
871 Set<Module> newModules = parser.parseYangModels(testFiles);
872 assertSetEquals(newModules, modules);
873 ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
874 assertSetEquals(newModules, ctx.getModules());
876 newModules = parser.parseYangModels(testFiles, null);
877 assertSetEquals(newModules, modules);
878 ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
879 assertSetEquals(newModules, ctx.getModules());
881 List<InputStream> streams = new ArrayList<>();
882 for (File f : testFiles) {
883 streams.add(new FileInputStream(f));
885 newModules = parser.parseYangModelsFromStreams(streams);
886 assertSetEquals(newModules, modules);
887 ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
888 assertSetEquals(newModules, ctx.getModules());
891 for (File f : testFiles) {
892 streams.add(new FileInputStream(f));
894 newModules = parser.parseYangModelsFromStreams(streams, null);
895 assertSetEquals(newModules, modules);
896 ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
897 assertSetEquals(newModules, ctx.getModules());
899 Map<File, Module> mapped = parser.parseYangModelsMapped(testFiles);
900 newModules = new LinkedHashSet<>(mapped.values());
901 assertSetEquals(newModules, modules);
902 ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
903 assertSetEquals(newModules, ctx.getModules());
906 for (File f : testFiles) {
907 streams.add(new FileInputStream(f));
909 Map<InputStream, Module> mappedStreams = parser.parseYangModelsFromStreamsMapped(streams);
910 newModules = new LinkedHashSet<>(mappedStreams.values());
911 assertSetEquals(newModules, modules);
912 ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
913 assertSetEquals(newModules, ctx.getModules());
916 private void checkOrder(Collection<Module> modules) {
917 Iterator<Module> it = modules.iterator();
918 Module m = it.next();
919 assertEquals("m2", m.getName());
921 assertEquals("m4", m.getName());
923 assertEquals("m6", m.getName());
925 assertEquals("m8", m.getName());
927 assertEquals("m7", m.getName());
929 assertEquals("m5", m.getName());
931 assertEquals("m3", m.getName());
933 assertEquals("m1", m.getName());
936 private void assertSetEquals(Set<Module> s1, Set<Module> s2) {
937 assertEquals(s1, s2);
938 Iterator<Module> it = s1.iterator();
939 for (Module m : s2) {
940 assertEquals(m, it.next());
945 public void testSubmodules() throws URISyntaxException {
946 URI yangFilePath = getClass().getResource("/submodule-test/subfoo.yang").toURI();
947 URI directoryPath = getClass().getResource("/model").toURI();
949 File directory = new File(directoryPath);
950 File yangFile = new File(yangFilePath);
952 Set<Module> modules = new YangParserImpl().parseYangModels(yangFile, directory);
953 assertEquals(3, modules.size());
955 Module foo = TestUtils.findModule(modules, "foo");
957 DataSchemaNode id = foo.getDataChildByName("id");
959 DataSchemaNode subExt = foo.getDataChildByName("sub-ext");
960 assertNotNull(subExt);
961 DataSchemaNode subTransfer = foo.getDataChildByName("sub-transfer");
962 assertNotNull(subTransfer);
964 assertEquals(2, foo.getExtensionSchemaNodes().size());
965 assertEquals(2, foo.getAugmentations().size());