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.*;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.InputStream;
17 import java.text.DateFormat;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.LinkedHashSet;
25 import java.util.List;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.*;
33 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
34 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
35 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
36 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
37 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
38 import org.opendaylight.yangtools.yang.model.util.Decimal64;
39 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
40 import org.opendaylight.yangtools.yang.model.util.Int16;
41 import org.opendaylight.yangtools.yang.model.util.Int32;
42 import org.opendaylight.yangtools.yang.model.util.StringType;
43 import org.opendaylight.yangtools.yang.model.util.Uint32;
44 import org.opendaylight.yangtools.yang.model.util.UnionType;
46 public class YangParserTest {
47 private final URI fooNS = URI.create("urn:opendaylight.foo");
48 private final URI barNS = URI.create("urn:opendaylight.bar");
49 private final URI bazNS = URI.create("urn:opendaylight.baz");
54 private Set<Module> modules;
57 public void init() throws FileNotFoundException, ParseException {
58 DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
59 fooRev = simpleDateFormat.parse("2013-02-27");
60 barRev = simpleDateFormat.parse("2013-07-03");
61 bazRev = simpleDateFormat.parse("2013-02-27");
63 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
64 assertEquals(3, modules.size());
68 public void testHeaders() throws ParseException {
69 Module foo = TestUtils.findModule(modules, "foo");
71 assertEquals("foo", foo.getName());
72 assertEquals("1", foo.getYangVersion());
73 assertEquals(fooNS, foo.getNamespace());
74 assertEquals("foo", foo.getPrefix());
76 Set<ModuleImport> imports = foo.getImports();
77 assertEquals(2, imports.size());
79 ModuleImport import2 = TestUtils.findImport(imports, "br");
80 assertEquals("bar", import2.getModuleName());
81 assertEquals(barRev, import2.getRevision());
83 ModuleImport import3 = TestUtils.findImport(imports, "bz");
84 assertEquals("baz", import3.getModuleName());
85 assertEquals(bazRev, import3.getRevision());
87 assertEquals("opendaylight", foo.getOrganization());
88 assertEquals("http://www.opendaylight.org/", foo.getContact());
89 Date expectedRevision = TestUtils.createDate("2013-02-27");
90 assertEquals(expectedRevision, foo.getRevision());
91 assertEquals(" WILL BE DEFINED LATER", foo.getReference());
95 public void testOrderingTypedef() {
96 Module bar = TestUtils.findModule(modules, "bar");
97 Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
98 String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "my-decimal-type", "my-union",
99 "my-union-ext", "nested-union2", "string-ext1", "string-ext2", "string-ext3", "string-ext4" };
100 String[] actualOrder = new String[typedefs.size()];
103 for (TypeDefinition<?> type : typedefs) {
104 actualOrder[i] = type.getQName().getLocalName();
107 assertArrayEquals(expectedOrder, actualOrder);
111 public void testOrderingChildNodes() {
112 Module foo = TestUtils.findModule(modules, "foo");
113 AugmentationSchema augment1 = null;
114 for (AugmentationSchema as : foo.getAugmentations()) {
115 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
120 assertNotNull(augment1);
122 String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "odl", "schemas" };
123 String[] actualOrder = new String[expectedOrder.length];
126 for (DataSchemaNode augmentChild : augment1.getChildNodes()) {
127 actualOrder[i] = augmentChild.getQName().getLocalName();
131 assertArrayEquals(expectedOrder, actualOrder);
135 public void testOrderingNestedChildNodes1() {
136 Module foo = TestUtils.findModule(modules, "foo");
138 Set<DataSchemaNode> childNodes = foo.getChildNodes();
139 String[] expectedOrder = new String[] { "address", "addresses", "custom-union-leaf", "data", "datas",
140 "decimal-leaf", "decimal-leaf2", "ext", "how", "int32-leaf", "length-leaf", "mycont", "peer", "port",
141 "string-leaf", "transfer", "union-leaf" };
142 String[] actualOrder = new String[childNodes.size()];
145 for (DataSchemaNode child : childNodes) {
146 actualOrder[i] = child.getQName().getLocalName();
149 assertArrayEquals(expectedOrder, actualOrder);
153 public void testOrderingNestedChildNodes2() {
154 Module baz = TestUtils.findModule(modules, "baz");
155 Set<GroupingDefinition> groupings = baz.getGroupings();
156 assertEquals(1, groupings.size());
157 GroupingDefinition target = groupings.iterator().next();
159 Set<DataSchemaNode> childNodes = target.getChildNodes();
160 String[] expectedOrder = new String[] { "address", "addresses", "data", "how", "port" };
161 String[] actualOrder = new String[childNodes.size()];
164 for (DataSchemaNode child : childNodes) {
165 actualOrder[i] = child.getQName().getLocalName();
168 assertArrayEquals(expectedOrder, actualOrder);
172 public void testParseList() {
173 Module bar = TestUtils.findModule(modules, "bar");
174 URI expectedNamespace = URI.create("urn:opendaylight.bar");
175 String expectedPrefix = "bar";
177 ContainerSchemaNode interfaces = (ContainerSchemaNode) bar.getDataChildByName("interfaces");
179 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
180 // test SchemaNode args
181 QName expectedQName = new QName(expectedNamespace, barRev, expectedPrefix, "ifEntry");
182 assertEquals(expectedQName, ifEntry.getQName());
183 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, barRev, expectedPrefix, "interfaces",
185 assertEquals(expectedPath, ifEntry.getPath());
186 assertNull(ifEntry.getDescription());
187 assertNull(ifEntry.getReference());
188 assertEquals(Status.CURRENT, ifEntry.getStatus());
189 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
190 // test DataSchemaNode args
191 assertFalse(ifEntry.isAugmenting());
192 assertTrue(ifEntry.isConfiguration());
193 ConstraintDefinition constraints = ifEntry.getConstraints();
194 assertNull(constraints.getWhenCondition());
195 assertEquals(0, constraints.getMustConstraints().size());
196 assertFalse(constraints.isMandatory());
197 assertEquals(1, (int) constraints.getMinElements());
198 assertEquals(11, (int) constraints.getMaxElements());
199 // test AugmentationTarget args
200 Set<AugmentationSchema> availableAugmentations = ifEntry.getAvailableAugmentations();
201 assertEquals(2, availableAugmentations.size());
202 // test ListSchemaNode args
203 List<QName> expectedKey = new ArrayList<>();
204 expectedKey.add(new QName(expectedNamespace, barRev, expectedPrefix, "ifIndex"));
205 assertEquals(expectedKey, ifEntry.getKeyDefinition());
206 assertFalse(ifEntry.isUserOrdered());
207 // test DataNodeContainer args
208 assertEquals(0, ifEntry.getTypeDefinitions().size());
209 assertEquals(4, ifEntry.getChildNodes().size());
210 assertEquals(0, ifEntry.getGroupings().size());
211 assertEquals(0, ifEntry.getUses().size());
213 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
214 assertTrue(ifIndex.getType() instanceof Uint32);
215 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
216 assertTrue(ifMtu.getType() instanceof Int32);
220 public void testTypedefRangesResolving() throws ParseException {
221 Module foo = TestUtils.findModule(modules, "foo");
222 LeafSchemaNode int32Leaf = (LeafSchemaNode) foo.getDataChildByName("int32-leaf");
224 ExtendedType leafType = (ExtendedType) int32Leaf.getType();
225 QName leafTypeQName = leafType.getQName();
226 assertEquals("int32-ext2", leafTypeQName.getLocalName());
227 assertEquals("foo", leafTypeQName.getPrefix());
228 assertEquals(fooNS, leafTypeQName.getNamespace());
229 assertEquals(fooRev, leafTypeQName.getRevision());
230 assertNull(leafType.getUnits());
231 assertNull(leafType.getDefaultValue());
232 assertTrue(leafType.getLengthConstraints().isEmpty());
233 assertTrue(leafType.getPatternConstraints().isEmpty());
234 List<RangeConstraint> ranges = leafType.getRangeConstraints();
235 assertEquals(1, ranges.size());
236 RangeConstraint range = ranges.get(0);
237 assertEquals(12L, range.getMin());
238 assertEquals(20L, range.getMax());
240 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
241 QName baseTypeQName = baseType.getQName();
242 assertEquals("int32-ext2", baseTypeQName.getLocalName());
243 assertEquals("bar", baseTypeQName.getPrefix());
244 assertEquals(barNS, baseTypeQName.getNamespace());
245 assertEquals(barRev, baseTypeQName.getRevision());
246 assertEquals("mile", baseType.getUnits());
247 assertEquals("11", baseType.getDefaultValue());
248 assertTrue(leafType.getLengthConstraints().isEmpty());
249 assertTrue(leafType.getPatternConstraints().isEmpty());
250 List<RangeConstraint> baseTypeRanges = baseType.getRangeConstraints();
251 assertEquals(2, baseTypeRanges.size());
252 RangeConstraint baseTypeRange1 = baseTypeRanges.get(0);
253 assertEquals(3L, baseTypeRange1.getMin());
254 assertEquals(9L, baseTypeRange1.getMax());
255 RangeConstraint baseTypeRange2 = baseTypeRanges.get(1);
256 assertEquals(11L, baseTypeRange2.getMin());
257 assertEquals(20L, baseTypeRange2.getMax());
259 ExtendedType base = (ExtendedType) baseType.getBaseType();
260 QName baseQName = base.getQName();
261 assertEquals("int32-ext1", baseQName.getLocalName());
262 assertEquals("bar", baseQName.getPrefix());
263 assertEquals(barNS, baseQName.getNamespace());
264 assertEquals(barRev, baseQName.getRevision());
265 assertNull(base.getUnits());
266 assertNull(base.getDefaultValue());
267 assertTrue(leafType.getLengthConstraints().isEmpty());
268 assertTrue(leafType.getPatternConstraints().isEmpty());
269 List<RangeConstraint> baseRanges = base.getRangeConstraints();
270 assertEquals(1, baseRanges.size());
271 RangeConstraint baseRange = baseRanges.get(0);
272 assertEquals(2L, baseRange.getMin());
273 assertEquals(20L, baseRange.getMax());
275 assertTrue(base.getBaseType() instanceof Int32);
279 public void testTypedefPatternsResolving() {
280 Module foo = TestUtils.findModule(modules, "foo");
281 LeafSchemaNode stringleaf = (LeafSchemaNode) foo.getDataChildByName("string-leaf");
283 ExtendedType type = (ExtendedType) stringleaf.getType();
284 QName typeQName = type.getQName();
285 assertEquals("string-ext4", typeQName.getLocalName());
286 assertEquals("bar", typeQName.getPrefix());
287 assertEquals(barNS, typeQName.getNamespace());
288 assertEquals(barRev, typeQName.getRevision());
289 assertNull(type.getUnits());
290 assertNull(type.getDefaultValue());
291 List<PatternConstraint> patterns = type.getPatternConstraints();
292 assertEquals(1, patterns.size());
293 PatternConstraint pattern = patterns.iterator().next();
294 assertEquals("[e-z]*", pattern.getRegularExpression());
295 assertTrue(type.getLengthConstraints().isEmpty());
296 assertTrue(type.getRangeConstraints().isEmpty());
298 ExtendedType baseType1 = (ExtendedType) type.getBaseType();
299 QName baseType1QName = baseType1.getQName();
300 assertEquals("string-ext3", baseType1QName.getLocalName());
301 assertEquals("bar", baseType1QName.getPrefix());
302 assertEquals(barNS, baseType1QName.getNamespace());
303 assertEquals(barRev, baseType1QName.getRevision());
304 assertNull(baseType1.getUnits());
305 assertNull(baseType1.getDefaultValue());
306 patterns = baseType1.getPatternConstraints();
307 assertEquals(1, patterns.size());
308 pattern = patterns.iterator().next();
309 assertEquals("[b-u]*", pattern.getRegularExpression());
310 assertTrue(baseType1.getLengthConstraints().isEmpty());
311 assertTrue(baseType1.getRangeConstraints().isEmpty());
313 ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
314 QName baseType2QName = baseType2.getQName();
315 assertEquals("string-ext2", baseType2QName.getLocalName());
316 assertEquals("bar", baseType2QName.getPrefix());
317 assertEquals(barNS, baseType2QName.getNamespace());
318 assertEquals(barRev, baseType2QName.getRevision());
319 assertNull(baseType2.getUnits());
320 assertNull(baseType2.getDefaultValue());
321 assertTrue(baseType2.getPatternConstraints().isEmpty());
322 List<LengthConstraint> baseType2Lengths = baseType2.getLengthConstraints();
323 assertEquals(1, baseType2Lengths.size());
324 LengthConstraint length = baseType2Lengths.get(0);
325 assertEquals(6L, length.getMin());
326 assertEquals(10L, length.getMax());
327 assertTrue(baseType2.getRangeConstraints().isEmpty());
329 ExtendedType baseType3 = (ExtendedType) baseType2.getBaseType();
330 QName baseType3QName = baseType3.getQName();
331 assertEquals("string-ext1", baseType3QName.getLocalName());
332 assertEquals("bar", baseType3QName.getPrefix());
333 assertEquals(barNS, baseType3QName.getNamespace());
334 assertEquals(barRev, baseType3QName.getRevision());
335 assertNull(baseType3.getUnits());
336 assertNull(baseType3.getDefaultValue());
337 patterns = baseType3.getPatternConstraints();
338 assertEquals(1, patterns.size());
339 pattern = patterns.iterator().next();
340 assertEquals("[a-k]*", pattern.getRegularExpression());
341 List<LengthConstraint> baseType3Lengths = baseType3.getLengthConstraints();
342 assertEquals(1, baseType3Lengths.size());
343 length = baseType3Lengths.get(0);
344 assertEquals(5L, length.getMin());
345 assertEquals(11L, length.getMax());
346 assertTrue(baseType3.getRangeConstraints().isEmpty());
348 assertTrue(baseType3.getBaseType() instanceof StringType);
352 public void testTypedefLengthsResolving() {
353 Module foo = TestUtils.findModule(modules, "foo");
355 LeafSchemaNode lengthLeaf = (LeafSchemaNode) foo.getDataChildByName("length-leaf");
356 ExtendedType type = (ExtendedType) lengthLeaf.getType();
358 QName typeQName = type.getQName();
359 assertEquals("string-ext2", typeQName.getLocalName());
360 assertEquals("foo", typeQName.getPrefix());
361 assertEquals(fooNS, typeQName.getNamespace());
362 assertEquals(fooRev, typeQName.getRevision());
363 assertNull(type.getUnits());
364 assertNull(type.getDefaultValue());
365 assertTrue(type.getPatternConstraints().isEmpty());
366 List<LengthConstraint> typeLengths = type.getLengthConstraints();
367 assertEquals(1, typeLengths.size());
368 LengthConstraint length = typeLengths.get(0);
369 assertEquals(7L, length.getMin());
370 assertEquals(10L, length.getMax());
371 assertTrue(type.getRangeConstraints().isEmpty());
373 ExtendedType baseType1 = (ExtendedType) type.getBaseType();
374 QName baseType1QName = baseType1.getQName();
375 assertEquals("string-ext2", baseType1QName.getLocalName());
376 assertEquals("bar", baseType1QName.getPrefix());
377 assertEquals(barNS, baseType1QName.getNamespace());
378 assertEquals(barRev, baseType1QName.getRevision());
379 assertNull(baseType1.getUnits());
380 assertNull(baseType1.getDefaultValue());
381 assertTrue(baseType1.getPatternConstraints().isEmpty());
382 List<LengthConstraint> baseType2Lengths = baseType1.getLengthConstraints();
383 assertEquals(1, baseType2Lengths.size());
384 length = baseType2Lengths.get(0);
385 assertEquals(6L, length.getMin());
386 assertEquals(10L, length.getMax());
387 assertTrue(baseType1.getRangeConstraints().isEmpty());
389 ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
390 QName baseType2QName = baseType2.getQName();
391 assertEquals("string-ext1", baseType2QName.getLocalName());
392 assertEquals("bar", baseType2QName.getPrefix());
393 assertEquals(barNS, baseType2QName.getNamespace());
394 assertEquals(barRev, baseType2QName.getRevision());
395 assertNull(baseType2.getUnits());
396 assertNull(baseType2.getDefaultValue());
397 List<PatternConstraint> patterns = baseType2.getPatternConstraints();
398 assertEquals(1, patterns.size());
399 PatternConstraint pattern = patterns.iterator().next();
400 assertEquals("[a-k]*", pattern.getRegularExpression());
401 List<LengthConstraint> baseType3Lengths = baseType2.getLengthConstraints();
402 assertEquals(1, baseType3Lengths.size());
403 length = baseType3Lengths.get(0);
404 assertEquals(5L, length.getMin());
405 assertEquals(11L, length.getMax());
406 assertTrue(baseType2.getRangeConstraints().isEmpty());
408 assertTrue(baseType2.getBaseType() instanceof StringType);
412 public void testTypedefDecimal1() {
413 Module foo = TestUtils.findModule(modules, "foo");
414 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf");
416 ExtendedType type = (ExtendedType) testleaf.getType();
417 QName typeQName = type.getQName();
418 assertEquals("my-decimal-type", typeQName.getLocalName());
419 assertEquals("foo", typeQName.getPrefix());
420 assertEquals(fooNS, typeQName.getNamespace());
421 assertEquals(fooRev, typeQName.getRevision());
422 assertNull(type.getUnits());
423 assertNull(type.getDefaultValue());
424 assertEquals(4, (int) type.getFractionDigits());
425 assertTrue(type.getLengthConstraints().isEmpty());
426 assertTrue(type.getPatternConstraints().isEmpty());
427 assertTrue(type.getRangeConstraints().isEmpty());
429 ExtendedType typeBase = (ExtendedType) type.getBaseType();
430 QName typeBaseQName = typeBase.getQName();
431 assertEquals("my-decimal-type", typeBaseQName.getLocalName());
432 assertEquals("bar", typeBaseQName.getPrefix());
433 assertEquals(barNS, typeBaseQName.getNamespace());
434 assertEquals(barRev, typeBaseQName.getRevision());
435 assertNull(typeBase.getUnits());
436 assertNull(typeBase.getDefaultValue());
437 assertNull(typeBase.getFractionDigits());
438 assertTrue(typeBase.getLengthConstraints().isEmpty());
439 assertTrue(typeBase.getPatternConstraints().isEmpty());
440 assertTrue(typeBase.getRangeConstraints().isEmpty());
442 Decimal64 decimal = (Decimal64) typeBase.getBaseType();
443 assertEquals(6, (int) decimal.getFractionDigits());
447 public void testTypedefDecimal2() {
448 Module foo = TestUtils.findModule(modules, "foo");
449 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf2");
451 ExtendedType type = (ExtendedType) testleaf.getType();
452 QName typeQName = type.getQName();
453 assertEquals("my-decimal-type", typeQName.getLocalName());
454 assertEquals("bar", typeQName.getPrefix());
455 assertEquals(barNS, typeQName.getNamespace());
456 assertEquals(barRev, typeQName.getRevision());
457 assertNull(type.getUnits());
458 assertNull(type.getDefaultValue());
459 assertNull(type.getFractionDigits());
460 assertTrue(type.getLengthConstraints().isEmpty());
461 assertTrue(type.getPatternConstraints().isEmpty());
462 assertTrue(type.getRangeConstraints().isEmpty());
464 Decimal64 baseTypeDecimal = (Decimal64) type.getBaseType();
465 assertEquals(6, (int) baseTypeDecimal.getFractionDigits());
469 public void testTypedefUnion() {
470 Module foo = TestUtils.findModule(modules, "foo");
471 LeafSchemaNode unionleaf = (LeafSchemaNode) foo.getDataChildByName("union-leaf");
473 ExtendedType type = (ExtendedType) unionleaf.getType();
474 QName typeQName = type.getQName();
475 assertEquals("my-union-ext", typeQName.getLocalName());
476 assertEquals("bar", typeQName.getPrefix());
477 assertEquals(barNS, typeQName.getNamespace());
478 assertEquals(barRev, typeQName.getRevision());
479 assertNull(type.getUnits());
480 assertNull(type.getDefaultValue());
481 assertNull(type.getFractionDigits());
482 assertTrue(type.getLengthConstraints().isEmpty());
483 assertTrue(type.getPatternConstraints().isEmpty());
484 assertTrue(type.getRangeConstraints().isEmpty());
486 ExtendedType baseType = (ExtendedType) type.getBaseType();
487 QName baseTypeQName = baseType.getQName();
488 assertEquals("my-union", baseTypeQName.getLocalName());
489 assertEquals("bar", baseTypeQName.getPrefix());
490 assertEquals(barNS, baseTypeQName.getNamespace());
491 assertEquals(barRev, baseTypeQName.getRevision());
492 assertNull(baseType.getUnits());
493 assertNull(baseType.getDefaultValue());
494 assertNull(baseType.getFractionDigits());
495 assertTrue(baseType.getLengthConstraints().isEmpty());
496 assertTrue(baseType.getPatternConstraints().isEmpty());
497 assertTrue(baseType.getRangeConstraints().isEmpty());
499 UnionType unionType = (UnionType) baseType.getBaseType();
500 List<TypeDefinition<?>> unionTypes = unionType.getTypes();
501 assertEquals(2, unionTypes.size());
503 ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
504 QName unionType1QName = baseType.getQName();
505 assertEquals("my-union", unionType1QName.getLocalName());
506 assertEquals("bar", unionType1QName.getPrefix());
507 assertEquals(barNS, unionType1QName.getNamespace());
508 assertEquals(barRev, unionType1QName.getRevision());
509 assertNull(unionType1.getUnits());
510 assertNull(unionType1.getDefaultValue());
511 assertNull(unionType1.getFractionDigits());
512 assertTrue(unionType1.getLengthConstraints().isEmpty());
513 assertTrue(unionType1.getPatternConstraints().isEmpty());
514 List<RangeConstraint> ranges = unionType1.getRangeConstraints();
515 assertEquals(1, ranges.size());
516 RangeConstraint range = ranges.get(0);
517 assertEquals(1L, range.getMin());
518 assertEquals(100L, range.getMax());
519 assertTrue(unionType1.getBaseType() instanceof Int16);
521 assertTrue(unionTypes.get(1) instanceof Int32);
525 public void testNestedUnionResolving() {
526 Module foo = TestUtils.findModule(modules, "foo");
527 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("custom-union-leaf");
529 ExtendedType type = (ExtendedType) testleaf.getType();
530 QName testleafTypeQName = type.getQName();
531 assertEquals(bazNS, testleafTypeQName.getNamespace());
532 assertEquals(bazRev, testleafTypeQName.getRevision());
533 assertEquals("baz", testleafTypeQName.getPrefix());
534 assertEquals("union1", testleafTypeQName.getLocalName());
535 assertNull(type.getUnits());
536 assertNull(type.getDefaultValue());
537 assertNull(type.getFractionDigits());
538 assertTrue(type.getLengthConstraints().isEmpty());
539 assertTrue(type.getPatternConstraints().isEmpty());
540 assertTrue(type.getRangeConstraints().isEmpty());
542 ExtendedType typeBase = (ExtendedType) type.getBaseType();
543 QName typeBaseQName = typeBase.getQName();
544 assertEquals(bazNS, typeBaseQName.getNamespace());
545 assertEquals(bazRev, typeBaseQName.getRevision());
546 assertEquals("baz", typeBaseQName.getPrefix());
547 assertEquals("union2", typeBaseQName.getLocalName());
548 assertNull(typeBase.getUnits());
549 assertNull(typeBase.getDefaultValue());
550 assertNull(typeBase.getFractionDigits());
551 assertTrue(typeBase.getLengthConstraints().isEmpty());
552 assertTrue(typeBase.getPatternConstraints().isEmpty());
553 assertTrue(typeBase.getRangeConstraints().isEmpty());
555 UnionType union = (UnionType) typeBase.getBaseType();
556 List<TypeDefinition<?>> unionTypes = union.getTypes();
557 assertEquals(2, unionTypes.size());
558 assertTrue(unionTypes.get(0) instanceof Int32);
559 assertTrue(unionTypes.get(1) instanceof ExtendedType);
561 ExtendedType unionType1 = (ExtendedType) unionTypes.get(1);
562 QName uniontType1QName = unionType1.getQName();
563 assertEquals(barNS, uniontType1QName.getNamespace());
564 assertEquals(barRev, uniontType1QName.getRevision());
565 assertEquals("bar", uniontType1QName.getPrefix());
566 assertEquals("nested-union2", uniontType1QName.getLocalName());
567 assertNull(unionType1.getUnits());
568 assertNull(unionType1.getDefaultValue());
569 assertNull(unionType1.getFractionDigits());
570 assertTrue(unionType1.getLengthConstraints().isEmpty());
571 assertTrue(unionType1.getPatternConstraints().isEmpty());
572 assertTrue(unionType1.getRangeConstraints().isEmpty());
574 UnionType nestedUnion = (UnionType) unionType1.getBaseType();
575 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion.getTypes();
576 assertEquals(2, nestedUnion2Types.size());
577 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
578 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
580 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
581 QName myUnionExtQName = myUnionExt.getQName();
582 assertEquals(barNS, myUnionExtQName.getNamespace());
583 assertEquals(barRev, myUnionExtQName.getRevision());
584 assertEquals("bar", myUnionExtQName.getPrefix());
585 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
586 assertNull(myUnionExt.getUnits());
587 assertNull(myUnionExt.getDefaultValue());
588 assertNull(myUnionExt.getFractionDigits());
589 assertTrue(myUnionExt.getLengthConstraints().isEmpty());
590 assertTrue(myUnionExt.getPatternConstraints().isEmpty());
591 assertTrue(myUnionExt.getRangeConstraints().isEmpty());
593 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
594 QName myUnionQName = myUnion.getQName();
595 assertEquals(barNS, myUnionQName.getNamespace());
596 assertEquals(barRev, myUnionQName.getRevision());
597 assertEquals("bar", myUnionQName.getPrefix());
598 assertEquals("my-union", myUnionQName.getLocalName());
599 assertNull(myUnion.getUnits());
600 assertNull(myUnion.getDefaultValue());
601 assertNull(myUnion.getFractionDigits());
602 assertTrue(myUnion.getLengthConstraints().isEmpty());
603 assertTrue(myUnion.getPatternConstraints().isEmpty());
604 assertTrue(myUnion.getRangeConstraints().isEmpty());
606 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
607 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
608 assertEquals(2, myUnionBaseTypes.size());
609 assertTrue(myUnionBaseTypes.get(0) instanceof ExtendedType);
610 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
612 ExtendedType int16Ext = (ExtendedType) myUnionBaseTypes.get(0);
613 QName int16ExtQName = int16Ext.getQName();
614 assertEquals(barNS, int16ExtQName.getNamespace());
615 assertEquals(barRev, int16ExtQName.getRevision());
616 assertEquals("bar", int16ExtQName.getPrefix());
617 assertEquals("int16", int16ExtQName.getLocalName());
618 assertNull(int16Ext.getUnits());
619 assertNull(int16Ext.getDefaultValue());
620 assertNull(int16Ext.getFractionDigits());
621 assertTrue(int16Ext.getLengthConstraints().isEmpty());
622 assertTrue(int16Ext.getPatternConstraints().isEmpty());
623 List<RangeConstraint> ranges = int16Ext.getRangeConstraints();
624 assertEquals(1, ranges.size());
625 RangeConstraint range = ranges.get(0);
626 assertEquals(1L, range.getMin());
627 assertEquals(100L, range.getMax());
629 assertTrue(int16Ext.getBaseType() instanceof Int16);
633 public void testChoice() {
634 Module foo = TestUtils.findModule(modules, "foo");
635 ContainerSchemaNode transfer = (ContainerSchemaNode) foo.getDataChildByName("transfer");
636 ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how");
637 Set<ChoiceCaseNode> cases = how.getCases();
638 assertEquals(5, cases.size());
639 ChoiceCaseNode input = null;
640 ChoiceCaseNode output = null;
641 for (ChoiceCaseNode caseNode : cases) {
642 if ("input".equals(caseNode.getQName().getLocalName())) {
644 } else if ("output".equals(caseNode.getQName().getLocalName())) {
648 assertNotNull(input);
649 assertNotNull(input.getPath());
650 assertNotNull(output);
651 assertNotNull(output.getPath());
655 public void testDeviation() {
656 Module foo = TestUtils.findModule(modules, "foo");
657 Set<Deviation> deviations = foo.getDeviations();
658 assertEquals(1, deviations.size());
659 Deviation dev = deviations.iterator().next();
660 assertEquals("system/user ref", dev.getReference());
662 List<QName> path = new ArrayList<>();
663 path.add(new QName(barNS, barRev, "br", "interfaces"));
664 path.add(new QName(barNS, barRev, "br", "ifEntry"));
665 SchemaPath expectedPath = new SchemaPath(path, true);
667 assertEquals(expectedPath, dev.getTargetPath());
668 assertEquals(Deviate.ADD, dev.getDeviate());
672 public void testUnknownNode() {
673 Module baz = TestUtils.findModule(modules, "baz");
674 ContainerSchemaNode network = (ContainerSchemaNode) baz.getDataChildByName("network");
675 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
676 assertEquals(1, unknownNodes.size());
677 UnknownSchemaNode unknownNode = unknownNodes.get(0);
678 assertNotNull(unknownNode.getNodeType());
679 assertEquals("point", unknownNode.getNodeParameter());
683 public void testFeature() {
684 Module baz = TestUtils.findModule(modules, "baz");
685 Set<FeatureDefinition> features = baz.getFeatures();
686 assertEquals(1, features.size());
690 public void testExtension() {
691 Module baz = TestUtils.findModule(modules, "baz");
692 List<ExtensionDefinition> extensions = baz.getExtensionSchemaNodes();
693 assertEquals(1, extensions.size());
694 ExtensionDefinition extension = extensions.get(0);
695 assertEquals("name", extension.getArgument());
696 assertTrue(extension.isYinElement());
700 public void testNotification() {
701 Module baz = TestUtils.findModule(modules, "baz");
702 String expectedPrefix = "c";
704 Set<NotificationDefinition> notifications = baz.getNotifications();
705 assertEquals(1, notifications.size());
707 NotificationDefinition notification = notifications.iterator().next();
708 // test SchemaNode args
709 QName expectedQName = new QName(bazNS, bazRev, expectedPrefix, "event");
710 assertEquals(expectedQName, notification.getQName());
711 SchemaPath expectedPath = TestUtils.createPath(true, bazNS, bazRev, expectedPrefix, "event");
712 assertEquals(expectedPath, notification.getPath());
713 assertNull(notification.getDescription());
714 assertNull(notification.getReference());
715 assertEquals(Status.CURRENT, notification.getStatus());
716 assertEquals(0, notification.getUnknownSchemaNodes().size());
717 // test DataNodeContainer args
718 assertEquals(0, notification.getTypeDefinitions().size());
719 assertEquals(3, notification.getChildNodes().size());
720 assertEquals(0, notification.getGroupings().size());
721 assertEquals(0, notification.getUses().size());
723 LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
724 assertTrue(eventClass.getType() instanceof StringType);
725 LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
726 assertTrue(severity.getType() instanceof StringType);
730 public void testRpc() {
731 Module baz = TestUtils.findModule(modules, "baz");
733 Set<RpcDefinition> rpcs = baz.getRpcs();
734 assertEquals(1, rpcs.size());
736 RpcDefinition rpc = rpcs.iterator().next();
737 assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
738 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
742 public void testTypePath() throws ParseException {
743 Module bar = TestUtils.findModule(modules, "bar");
744 Set<TypeDefinition<?>> types = bar.getTypeDefinitions();
747 ExtendedType int32ext1 = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1");
748 QName int32TypedefQName = int32ext1.getQName();
750 assertEquals(barNS, int32TypedefQName.getNamespace());
751 assertEquals(barRev, int32TypedefQName.getRevision());
752 assertEquals("bar", int32TypedefQName.getPrefix());
753 assertEquals("int32-ext1", int32TypedefQName.getLocalName());
755 SchemaPath typeSchemaPath = int32ext1.getPath();
756 List<QName> typePath = typeSchemaPath.getPath();
757 assertEquals(1, typePath.size());
758 assertEquals(int32TypedefQName, typePath.get(0));
761 Int32 int32 = (Int32) int32ext1.getBaseType();
762 assertEquals(Int32.getInstance(), int32);
766 public void testTypePath2() throws ParseException {
767 Module bar = TestUtils.findModule(modules, "bar");
768 Set<TypeDefinition<?>> types = bar.getTypeDefinitions();
771 ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
772 QName myDecTypeQName = myDecType.getQName();
774 assertEquals(barNS, myDecTypeQName.getNamespace());
775 assertEquals(barRev, myDecTypeQName.getRevision());
776 assertEquals("bar", myDecTypeQName.getPrefix());
777 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
779 SchemaPath typeSchemaPath = myDecType.getPath();
780 List<QName> typePath = typeSchemaPath.getPath();
781 assertEquals(1, typePath.size());
782 assertEquals(myDecTypeQName, typePath.get(0));
784 // my-base-int32-type/int32
785 Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
786 QName dec64QName = dec64.getQName();
788 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
789 assertNull(dec64QName.getRevision());
790 assertEquals("", dec64QName.getPrefix());
791 assertEquals("decimal64", dec64QName.getLocalName());
793 SchemaPath dec64SchemaPath = dec64.getPath();
794 List<QName> dec64Path = dec64SchemaPath.getPath();
795 assertEquals(2, dec64Path.size());
796 assertEquals(myDecTypeQName, dec64Path.get(0));
797 assertEquals(dec64QName, dec64Path.get(1));
801 public void testParseMethod1() throws ParseException {
802 File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
803 File dependenciesDir = new File(getClass().getResource("/parse-methods").getPath());
804 YangModelParser parser = new YangParserImpl();
805 modules = parser.parseYangModels(yangFile, dependenciesDir);
806 assertEquals(6, modules.size());
810 public void testParseMethod2() throws ParseException {
811 File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
812 File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").getPath());
813 YangModelParser parser = new YangParserImpl();
814 modules = parser.parseYangModels(yangFile, dependenciesDir);
815 assertEquals(6, modules.size());
819 public void testSorting() throws FileNotFoundException {
820 // Correct order: m2, m4, m6, m8, m7, m6, m3, m1
821 File yangFile = new File(getClass().getResource("/sorting-test/m1.yang").getPath());
822 File dependenciesDir = new File(getClass().getResource("/sorting-test").getPath());
823 YangModelParser parser = new YangParserImpl();
824 modules = parser.parseYangModels(yangFile, dependenciesDir);
825 SchemaContext ctx = new SchemaContextImpl(modules);
827 assertSetEquals(modules, ctx.getModules());
830 parser = new YangParserImpl();
831 final File testDir = dependenciesDir;
832 final String[] fileList = testDir.list();
833 final List<File> testFiles = new ArrayList<>();
834 if (fileList == null) {
835 throw new FileNotFoundException(dependenciesDir.getAbsolutePath());
837 for (String fileName : fileList) {
838 testFiles.add(new File(testDir, fileName));
840 Set<Module> newModules = parser.parseYangModels(testFiles);
841 assertSetEquals(newModules, modules);
842 ctx = new SchemaContextImpl(newModules);
843 assertSetEquals(newModules, ctx.getModules());
845 newModules = parser.parseYangModels(testFiles, null);
846 assertSetEquals(newModules, modules);
847 ctx = new SchemaContextImpl(newModules);
848 assertSetEquals(newModules, ctx.getModules());
850 List<InputStream> streams = new ArrayList<>();
851 for (File f : testFiles) {
852 streams.add(new FileInputStream(f));
854 newModules = parser.parseYangModelsFromStreams(streams);
855 assertSetEquals(newModules, modules);
856 ctx = new SchemaContextImpl(newModules);
857 assertSetEquals(newModules, ctx.getModules());
860 for (File f : testFiles) {
861 streams.add(new FileInputStream(f));
863 newModules = parser.parseYangModelsFromStreams(streams, null);
864 assertSetEquals(newModules, modules);
865 ctx = new SchemaContextImpl(newModules);
866 assertSetEquals(newModules, ctx.getModules());
868 Map<File, Module> mapped = parser.parseYangModelsMapped(testFiles);
869 newModules = new LinkedHashSet<>(mapped.values());
870 assertSetEquals(newModules, modules);
871 ctx = new SchemaContextImpl(newModules);
872 assertSetEquals(newModules, ctx.getModules());
875 for (File f : testFiles) {
876 streams.add(new FileInputStream(f));
878 Map<InputStream, Module> mappedStreams = parser.parseYangModelsFromStreamsMapped(streams);
879 newModules = new LinkedHashSet<>(mappedStreams.values());
880 assertSetEquals(newModules, modules);
881 ctx = new SchemaContextImpl(newModules);
882 assertSetEquals(newModules, ctx.getModules());
885 private void checkOrder(Collection<Module> modules) {
886 Iterator<Module> it = modules.iterator();
887 Module m = it.next();
888 assertEquals("m2", m.getName());
890 assertEquals("m4", m.getName());
892 assertEquals("m6", m.getName());
894 assertEquals("m8", m.getName());
896 assertEquals("m7", m.getName());
898 assertEquals("m5", m.getName());
900 assertEquals("m3", m.getName());
902 assertEquals("m1", m.getName());
905 private void assertSetEquals(Set<Module> s1, Set<Module> s2) {
906 assertEquals(s1, s2);
907 Iterator<Module> it = s1.iterator();
908 for (Module m : s2) {
909 assertEquals(m, it.next());