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.FileNotFoundException;
15 import java.text.ParseException;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
28 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Deviation;
32 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
33 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
34 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
35 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
36 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
40 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
41 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.model.api.Status;
44 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
47 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
48 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
49 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
50 import org.opendaylight.yangtools.yang.model.util.Decimal64;
51 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
52 import org.opendaylight.yangtools.yang.model.util.Int16;
53 import org.opendaylight.yangtools.yang.model.util.Int32;
54 import org.opendaylight.yangtools.yang.model.util.StringType;
55 import org.opendaylight.yangtools.yang.model.util.Uint32;
56 import org.opendaylight.yangtools.yang.model.util.UnionType;
58 public class YangParserTest {
59 private final URI fooNS = URI.create("urn:opendaylight.foo");
60 private final URI barNS = URI.create("urn:opendaylight.bar");
61 private final URI bazNS = URI.create("urn:opendaylight.baz");
66 private Set<Module> modules;
69 public void init() throws FileNotFoundException, ParseException {
70 fooRev = TestUtils.simpleDateFormat.parse("2013-02-27");
71 barRev = TestUtils.simpleDateFormat.parse("2013-07-03");
72 bazRev = TestUtils.simpleDateFormat.parse("2013-02-27");
74 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
75 assertEquals(3, modules.size());
79 public void testHeaders() throws ParseException {
80 Module foo = TestUtils.findModule(modules, "foo");
82 assertEquals("foo", foo.getName());
83 assertEquals("1", foo.getYangVersion());
84 assertEquals(fooNS, foo.getNamespace());
85 assertEquals("foo", foo.getPrefix());
87 Set<ModuleImport> imports = foo.getImports();
88 assertEquals(2, imports.size());
90 ModuleImport import2 = TestUtils.findImport(imports, "br");
91 assertEquals("bar", import2.getModuleName());
92 assertEquals(barRev, import2.getRevision());
94 ModuleImport import3 = TestUtils.findImport(imports, "bz");
95 assertEquals("baz", import3.getModuleName());
96 assertEquals(bazRev, import3.getRevision());
98 assertEquals("opendaylight", foo.getOrganization());
99 assertEquals("http://www.opendaylight.org/", foo.getContact());
100 Date expectedRevision = TestUtils.createDate("2013-02-27");
101 assertEquals(expectedRevision, foo.getRevision());
102 assertEquals(" WILL BE DEFINED LATER", foo.getReference());
106 public void testOrderingTypedef() {
107 Module bar = TestUtils.findModule(modules, "bar");
108 Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
109 String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "my-decimal-type", "my-union",
110 "my-union-ext", "nested-union2", "string-ext1", "string-ext2", "string-ext3", "string-ext4" };
111 String[] actualOrder = new String[typedefs.size()];
114 for (TypeDefinition<?> type : typedefs) {
115 actualOrder[i] = type.getQName().getLocalName();
118 assertArrayEquals(expectedOrder, actualOrder);
122 public void testOrderingChildNodes() {
123 Module foo = TestUtils.findModule(modules, "foo");
124 AugmentationSchema augment1 = null;
125 for (AugmentationSchema as : foo.getAugmentations()) {
126 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
131 assertNotNull(augment1);
133 String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "odl", "schemas" };
134 String[] actualOrder = new String[expectedOrder.length];
137 for (DataSchemaNode augmentChild : augment1.getChildNodes()) {
138 actualOrder[i] = augmentChild.getQName().getLocalName();
142 assertArrayEquals(expectedOrder, actualOrder);
146 public void testOrderingNestedChildNodes1() {
147 Module foo = TestUtils.findModule(modules, "foo");
149 Set<DataSchemaNode> childNodes = foo.getChildNodes();
150 String[] expectedOrder = new String[] { "address", "addresses", "custom-union-leaf", "data", "datas",
151 "decimal-leaf", "decimal-leaf2", "ext", "how", "int32-leaf", "length-leaf", "mycont", "peer", "port",
152 "string-leaf", "transfer", "union-leaf" };
153 String[] actualOrder = new String[childNodes.size()];
156 for (DataSchemaNode child : childNodes) {
157 actualOrder[i] = child.getQName().getLocalName();
160 assertArrayEquals(expectedOrder, actualOrder);
164 public void testOrderingNestedChildNodes2() {
165 Module baz = TestUtils.findModule(modules, "baz");
166 Set<GroupingDefinition> groupings = baz.getGroupings();
167 assertEquals(1, groupings.size());
168 GroupingDefinition target = groupings.iterator().next();
170 Set<DataSchemaNode> childNodes = target.getChildNodes();
171 String[] expectedOrder = new String[] { "address", "addresses", "data", "how", "port" };
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 testParseList() {
184 Module bar = TestUtils.findModule(modules, "bar");
185 URI expectedNamespace = URI.create("urn:opendaylight.bar");
186 String expectedPrefix = "bar";
188 ContainerSchemaNode interfaces = (ContainerSchemaNode) bar.getDataChildByName("interfaces");
190 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
191 // test SchemaNode args
192 QName expectedQName = new QName(expectedNamespace, barRev, expectedPrefix, "ifEntry");
193 assertEquals(expectedQName, ifEntry.getQName());
194 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, barRev, expectedPrefix, "interfaces",
196 assertEquals(expectedPath, ifEntry.getPath());
197 assertNull(ifEntry.getDescription());
198 assertNull(ifEntry.getReference());
199 assertEquals(Status.CURRENT, ifEntry.getStatus());
200 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
201 // test DataSchemaNode args
202 assertEquals(interfaces, ifEntry.getParent());
203 assertFalse(ifEntry.isAugmenting());
204 assertTrue(ifEntry.isConfiguration());
205 ConstraintDefinition constraints = ifEntry.getConstraints();
206 assertNull(constraints.getWhenCondition());
207 assertEquals(0, constraints.getMustConstraints().size());
208 assertFalse(constraints.isMandatory());
209 assertEquals(1, (int) constraints.getMinElements());
210 assertEquals(11, (int) constraints.getMaxElements());
211 // test AugmentationTarget args
212 Set<AugmentationSchema> availableAugmentations = ifEntry.getAvailableAugmentations();
213 assertEquals(2, availableAugmentations.size());
214 // test ListSchemaNode args
215 List<QName> expectedKey = new ArrayList<>();
216 expectedKey.add(new QName(expectedNamespace, barRev, expectedPrefix, "ifIndex"));
217 assertEquals(expectedKey, ifEntry.getKeyDefinition());
218 assertFalse(ifEntry.isUserOrdered());
219 // test DataNodeContainer args
220 assertEquals(0, ifEntry.getTypeDefinitions().size());
221 assertEquals(4, ifEntry.getChildNodes().size());
222 assertEquals(0, ifEntry.getGroupings().size());
223 assertEquals(0, ifEntry.getUses().size());
225 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
226 assertTrue(ifIndex.getType() instanceof Uint32);
227 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
228 assertTrue(ifMtu.getType() instanceof Int32);
232 public void testTypedefRangesResolving() throws ParseException {
233 Module foo = TestUtils.findModule(modules, "foo");
234 LeafSchemaNode int32Leaf = (LeafSchemaNode) foo.getDataChildByName("int32-leaf");
236 ExtendedType leafType = (ExtendedType) int32Leaf.getType();
237 QName leafTypeQName = leafType.getQName();
238 assertEquals("int32-ext2", leafTypeQName.getLocalName());
239 assertEquals("foo", leafTypeQName.getPrefix());
240 assertEquals(fooNS, leafTypeQName.getNamespace());
241 assertEquals(fooRev, leafTypeQName.getRevision());
242 assertNull(leafType.getUnits());
243 assertNull(leafType.getDefaultValue());
244 assertTrue(leafType.getLengthConstraints().isEmpty());
245 assertTrue(leafType.getPatternConstraints().isEmpty());
246 List<RangeConstraint> ranges = leafType.getRangeConstraints();
247 assertEquals(1, ranges.size());
248 RangeConstraint range = ranges.get(0);
249 assertEquals(12L, range.getMin());
250 assertEquals(20L, range.getMax());
252 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
253 QName baseTypeQName = baseType.getQName();
254 assertEquals("int32-ext2", baseTypeQName.getLocalName());
255 assertEquals("bar", baseTypeQName.getPrefix());
256 assertEquals(barNS, baseTypeQName.getNamespace());
257 assertEquals(barRev, baseTypeQName.getRevision());
258 assertEquals("mile", baseType.getUnits());
259 assertEquals("11", baseType.getDefaultValue());
260 assertTrue(leafType.getLengthConstraints().isEmpty());
261 assertTrue(leafType.getPatternConstraints().isEmpty());
262 List<RangeConstraint> baseTypeRanges = baseType.getRangeConstraints();
263 assertEquals(2, baseTypeRanges.size());
264 RangeConstraint baseTypeRange1 = baseTypeRanges.get(0);
265 assertEquals(3L, baseTypeRange1.getMin());
266 assertEquals(9L, baseTypeRange1.getMax());
267 RangeConstraint baseTypeRange2 = baseTypeRanges.get(1);
268 assertEquals(11L, baseTypeRange2.getMin());
269 assertEquals(20L, baseTypeRange2.getMax());
271 ExtendedType base = (ExtendedType) baseType.getBaseType();
272 QName baseQName = base.getQName();
273 assertEquals("int32-ext1", baseQName.getLocalName());
274 assertEquals("bar", baseQName.getPrefix());
275 assertEquals(barNS, baseQName.getNamespace());
276 assertEquals(barRev, baseQName.getRevision());
277 assertNull(base.getUnits());
278 assertNull(base.getDefaultValue());
279 assertTrue(leafType.getLengthConstraints().isEmpty());
280 assertTrue(leafType.getPatternConstraints().isEmpty());
281 List<RangeConstraint> baseRanges = base.getRangeConstraints();
282 assertEquals(1, baseRanges.size());
283 RangeConstraint baseRange = baseRanges.get(0);
284 assertEquals(2L, baseRange.getMin());
285 assertEquals(20L, baseRange.getMax());
287 assertTrue(base.getBaseType() instanceof Int32);
291 public void testTypedefPatternsResolving() {
292 Module foo = TestUtils.findModule(modules, "foo");
293 LeafSchemaNode stringleaf = (LeafSchemaNode) foo.getDataChildByName("string-leaf");
295 ExtendedType type = (ExtendedType) stringleaf.getType();
296 QName typeQName = type.getQName();
297 assertEquals("string-ext4", typeQName.getLocalName());
298 assertEquals("bar", typeQName.getPrefix());
299 assertEquals(barNS, typeQName.getNamespace());
300 assertEquals(barRev, typeQName.getRevision());
301 assertNull(type.getUnits());
302 assertNull(type.getDefaultValue());
303 List<PatternConstraint> patterns = type.getPatternConstraints();
304 assertEquals(1, patterns.size());
305 PatternConstraint pattern = patterns.iterator().next();
306 assertEquals("[e-z]*", pattern.getRegularExpression());
307 assertTrue(type.getLengthConstraints().isEmpty());
308 assertTrue(type.getRangeConstraints().isEmpty());
310 ExtendedType baseType1 = (ExtendedType) type.getBaseType();
311 QName baseType1QName = baseType1.getQName();
312 assertEquals("string-ext3", baseType1QName.getLocalName());
313 assertEquals("bar", baseType1QName.getPrefix());
314 assertEquals(barNS, baseType1QName.getNamespace());
315 assertEquals(barRev, baseType1QName.getRevision());
316 assertNull(baseType1.getUnits());
317 assertNull(baseType1.getDefaultValue());
318 patterns = baseType1.getPatternConstraints();
319 assertEquals(1, patterns.size());
320 pattern = patterns.iterator().next();
321 assertEquals("[b-u]*", pattern.getRegularExpression());
322 assertTrue(baseType1.getLengthConstraints().isEmpty());
323 assertTrue(baseType1.getRangeConstraints().isEmpty());
325 ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
326 QName baseType2QName = baseType2.getQName();
327 assertEquals("string-ext2", baseType2QName.getLocalName());
328 assertEquals("bar", baseType2QName.getPrefix());
329 assertEquals(barNS, baseType2QName.getNamespace());
330 assertEquals(barRev, baseType2QName.getRevision());
331 assertNull(baseType2.getUnits());
332 assertNull(baseType2.getDefaultValue());
333 assertTrue(baseType2.getPatternConstraints().isEmpty());
334 List<LengthConstraint> baseType2Lengths = baseType2.getLengthConstraints();
335 assertEquals(1, baseType2Lengths.size());
336 LengthConstraint length = baseType2Lengths.get(0);
337 assertEquals(6L, length.getMin());
338 assertEquals(10L, length.getMax());
339 assertTrue(baseType2.getRangeConstraints().isEmpty());
341 ExtendedType baseType3 = (ExtendedType) baseType2.getBaseType();
342 QName baseType3QName = baseType3.getQName();
343 assertEquals("string-ext1", baseType3QName.getLocalName());
344 assertEquals("bar", baseType3QName.getPrefix());
345 assertEquals(barNS, baseType3QName.getNamespace());
346 assertEquals(barRev, baseType3QName.getRevision());
347 assertNull(baseType3.getUnits());
348 assertNull(baseType3.getDefaultValue());
349 patterns = baseType3.getPatternConstraints();
350 assertEquals(1, patterns.size());
351 pattern = patterns.iterator().next();
352 assertEquals("[a-k]*", pattern.getRegularExpression());
353 List<LengthConstraint> baseType3Lengths = baseType3.getLengthConstraints();
354 assertEquals(1, baseType3Lengths.size());
355 length = baseType3Lengths.get(0);
356 assertEquals(5L, length.getMin());
357 assertEquals(11L, length.getMax());
358 assertTrue(baseType3.getRangeConstraints().isEmpty());
360 assertTrue(baseType3.getBaseType() instanceof StringType);
364 public void testTypedefLengthsResolving() {
365 Module foo = TestUtils.findModule(modules, "foo");
367 LeafSchemaNode lengthLeaf = (LeafSchemaNode) foo.getDataChildByName("length-leaf");
368 ExtendedType type = (ExtendedType) lengthLeaf.getType();
370 QName typeQName = type.getQName();
371 assertEquals("string-ext2", typeQName.getLocalName());
372 assertEquals("foo", typeQName.getPrefix());
373 assertEquals(fooNS, typeQName.getNamespace());
374 assertEquals(fooRev, typeQName.getRevision());
375 assertNull(type.getUnits());
376 assertNull(type.getDefaultValue());
377 assertTrue(type.getPatternConstraints().isEmpty());
378 List<LengthConstraint> typeLengths = type.getLengthConstraints();
379 assertEquals(1, typeLengths.size());
380 LengthConstraint length = typeLengths.get(0);
381 assertEquals(7L, length.getMin());
382 assertEquals(10L, length.getMax());
383 assertTrue(type.getRangeConstraints().isEmpty());
385 ExtendedType baseType1 = (ExtendedType) type.getBaseType();
386 QName baseType1QName = baseType1.getQName();
387 assertEquals("string-ext2", baseType1QName.getLocalName());
388 assertEquals("bar", baseType1QName.getPrefix());
389 assertEquals(barNS, baseType1QName.getNamespace());
390 assertEquals(barRev, baseType1QName.getRevision());
391 assertNull(baseType1.getUnits());
392 assertNull(baseType1.getDefaultValue());
393 assertTrue(baseType1.getPatternConstraints().isEmpty());
394 List<LengthConstraint> baseType2Lengths = baseType1.getLengthConstraints();
395 assertEquals(1, baseType2Lengths.size());
396 length = baseType2Lengths.get(0);
397 assertEquals(6L, length.getMin());
398 assertEquals(10L, length.getMax());
399 assertTrue(baseType1.getRangeConstraints().isEmpty());
401 ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
402 QName baseType2QName = baseType2.getQName();
403 assertEquals("string-ext1", baseType2QName.getLocalName());
404 assertEquals("bar", baseType2QName.getPrefix());
405 assertEquals(barNS, baseType2QName.getNamespace());
406 assertEquals(barRev, baseType2QName.getRevision());
407 assertNull(baseType2.getUnits());
408 assertNull(baseType2.getDefaultValue());
409 List<PatternConstraint> patterns = baseType2.getPatternConstraints();
410 assertEquals(1, patterns.size());
411 PatternConstraint pattern = patterns.iterator().next();
412 assertEquals("[a-k]*", pattern.getRegularExpression());
413 List<LengthConstraint> baseType3Lengths = baseType2.getLengthConstraints();
414 assertEquals(1, baseType3Lengths.size());
415 length = baseType3Lengths.get(0);
416 assertEquals(5L, length.getMin());
417 assertEquals(11L, length.getMax());
418 assertTrue(baseType2.getRangeConstraints().isEmpty());
420 assertTrue(baseType2.getBaseType() instanceof StringType);
424 public void testTypedefDecimal1() {
425 Module foo = TestUtils.findModule(modules, "foo");
426 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf");
428 ExtendedType type = (ExtendedType) testleaf.getType();
429 QName typeQName = type.getQName();
430 assertEquals("my-decimal-type", typeQName.getLocalName());
431 assertEquals("foo", typeQName.getPrefix());
432 assertEquals(fooNS, typeQName.getNamespace());
433 assertEquals(fooRev, typeQName.getRevision());
434 assertNull(type.getUnits());
435 assertNull(type.getDefaultValue());
436 assertEquals(4, (int) type.getFractionDigits());
437 assertTrue(type.getLengthConstraints().isEmpty());
438 assertTrue(type.getPatternConstraints().isEmpty());
439 assertTrue(type.getRangeConstraints().isEmpty());
441 ExtendedType typeBase = (ExtendedType) type.getBaseType();
442 QName typeBaseQName = typeBase.getQName();
443 assertEquals("my-decimal-type", typeBaseQName.getLocalName());
444 assertEquals("bar", typeBaseQName.getPrefix());
445 assertEquals(barNS, typeBaseQName.getNamespace());
446 assertEquals(barRev, typeBaseQName.getRevision());
447 assertNull(typeBase.getUnits());
448 assertNull(typeBase.getDefaultValue());
449 assertNull(typeBase.getFractionDigits());
450 assertTrue(typeBase.getLengthConstraints().isEmpty());
451 assertTrue(typeBase.getPatternConstraints().isEmpty());
452 assertTrue(typeBase.getRangeConstraints().isEmpty());
454 Decimal64 decimal = (Decimal64) typeBase.getBaseType();
455 assertEquals(6, (int) decimal.getFractionDigits());
459 public void testTypedefDecimal2() {
460 Module foo = TestUtils.findModule(modules, "foo");
461 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf2");
463 ExtendedType type = (ExtendedType) testleaf.getType();
464 QName typeQName = type.getQName();
465 assertEquals("my-decimal-type", typeQName.getLocalName());
466 assertEquals("bar", typeQName.getPrefix());
467 assertEquals(barNS, typeQName.getNamespace());
468 assertEquals(barRev, typeQName.getRevision());
469 assertNull(type.getUnits());
470 assertNull(type.getDefaultValue());
471 assertNull(type.getFractionDigits());
472 assertTrue(type.getLengthConstraints().isEmpty());
473 assertTrue(type.getPatternConstraints().isEmpty());
474 assertTrue(type.getRangeConstraints().isEmpty());
476 Decimal64 baseTypeDecimal = (Decimal64) type.getBaseType();
477 assertEquals(6, (int) baseTypeDecimal.getFractionDigits());
481 public void testTypedefUnion() {
482 Module foo = TestUtils.findModule(modules, "foo");
483 LeafSchemaNode unionleaf = (LeafSchemaNode) foo.getDataChildByName("union-leaf");
485 ExtendedType type = (ExtendedType) unionleaf.getType();
486 QName typeQName = type.getQName();
487 assertEquals("my-union-ext", typeQName.getLocalName());
488 assertEquals("bar", typeQName.getPrefix());
489 assertEquals(barNS, typeQName.getNamespace());
490 assertEquals(barRev, typeQName.getRevision());
491 assertNull(type.getUnits());
492 assertNull(type.getDefaultValue());
493 assertNull(type.getFractionDigits());
494 assertTrue(type.getLengthConstraints().isEmpty());
495 assertTrue(type.getPatternConstraints().isEmpty());
496 assertTrue(type.getRangeConstraints().isEmpty());
498 ExtendedType baseType = (ExtendedType) type.getBaseType();
499 QName baseTypeQName = baseType.getQName();
500 assertEquals("my-union", baseTypeQName.getLocalName());
501 assertEquals("bar", baseTypeQName.getPrefix());
502 assertEquals(barNS, baseTypeQName.getNamespace());
503 assertEquals(barRev, baseTypeQName.getRevision());
504 assertNull(baseType.getUnits());
505 assertNull(baseType.getDefaultValue());
506 assertNull(baseType.getFractionDigits());
507 assertTrue(baseType.getLengthConstraints().isEmpty());
508 assertTrue(baseType.getPatternConstraints().isEmpty());
509 assertTrue(baseType.getRangeConstraints().isEmpty());
511 UnionType unionType = (UnionType) baseType.getBaseType();
512 List<TypeDefinition<?>> unionTypes = unionType.getTypes();
513 assertEquals(2, unionTypes.size());
515 ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
516 QName unionType1QName = baseType.getQName();
517 assertEquals("my-union", unionType1QName.getLocalName());
518 assertEquals("bar", unionType1QName.getPrefix());
519 assertEquals(barNS, unionType1QName.getNamespace());
520 assertEquals(barRev, unionType1QName.getRevision());
521 assertNull(unionType1.getUnits());
522 assertNull(unionType1.getDefaultValue());
523 assertNull(unionType1.getFractionDigits());
524 assertTrue(unionType1.getLengthConstraints().isEmpty());
525 assertTrue(unionType1.getPatternConstraints().isEmpty());
526 List<RangeConstraint> ranges = unionType1.getRangeConstraints();
527 assertEquals(1, ranges.size());
528 RangeConstraint range = ranges.get(0);
529 assertEquals(1L, range.getMin());
530 assertEquals(100L, range.getMax());
531 assertTrue(unionType1.getBaseType() instanceof Int16);
533 assertTrue(unionTypes.get(1) instanceof Int32);
537 public void testNestedUnionResolving() {
538 Module foo = TestUtils.findModule(modules, "foo");
539 LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("custom-union-leaf");
541 ExtendedType type = (ExtendedType) testleaf.getType();
542 QName testleafTypeQName = type.getQName();
543 assertEquals(bazNS, testleafTypeQName.getNamespace());
544 assertEquals(bazRev, testleafTypeQName.getRevision());
545 assertEquals("baz", testleafTypeQName.getPrefix());
546 assertEquals("union1", testleafTypeQName.getLocalName());
547 assertNull(type.getUnits());
548 assertNull(type.getDefaultValue());
549 assertNull(type.getFractionDigits());
550 assertTrue(type.getLengthConstraints().isEmpty());
551 assertTrue(type.getPatternConstraints().isEmpty());
552 assertTrue(type.getRangeConstraints().isEmpty());
554 ExtendedType typeBase = (ExtendedType) type.getBaseType();
555 QName typeBaseQName = typeBase.getQName();
556 assertEquals(bazNS, typeBaseQName.getNamespace());
557 assertEquals(bazRev, typeBaseQName.getRevision());
558 assertEquals("baz", typeBaseQName.getPrefix());
559 assertEquals("union2", typeBaseQName.getLocalName());
560 assertNull(typeBase.getUnits());
561 assertNull(typeBase.getDefaultValue());
562 assertNull(typeBase.getFractionDigits());
563 assertTrue(typeBase.getLengthConstraints().isEmpty());
564 assertTrue(typeBase.getPatternConstraints().isEmpty());
565 assertTrue(typeBase.getRangeConstraints().isEmpty());
567 UnionType union = (UnionType) typeBase.getBaseType();
568 List<TypeDefinition<?>> unionTypes = union.getTypes();
569 assertEquals(2, unionTypes.size());
570 assertTrue(unionTypes.get(0) instanceof Int32);
571 assertTrue(unionTypes.get(1) instanceof ExtendedType);
573 ExtendedType unionType1 = (ExtendedType) unionTypes.get(1);
574 QName uniontType1QName = unionType1.getQName();
575 assertEquals(barNS, uniontType1QName.getNamespace());
576 assertEquals(barRev, uniontType1QName.getRevision());
577 assertEquals("bar", uniontType1QName.getPrefix());
578 assertEquals("nested-union2", uniontType1QName.getLocalName());
579 assertNull(unionType1.getUnits());
580 assertNull(unionType1.getDefaultValue());
581 assertNull(unionType1.getFractionDigits());
582 assertTrue(unionType1.getLengthConstraints().isEmpty());
583 assertTrue(unionType1.getPatternConstraints().isEmpty());
584 assertTrue(unionType1.getRangeConstraints().isEmpty());
586 UnionType nestedUnion = (UnionType) unionType1.getBaseType();
587 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion.getTypes();
588 assertEquals(2, nestedUnion2Types.size());
589 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
590 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
592 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
593 QName myUnionExtQName = myUnionExt.getQName();
594 assertEquals(barNS, myUnionExtQName.getNamespace());
595 assertEquals(barRev, myUnionExtQName.getRevision());
596 assertEquals("bar", myUnionExtQName.getPrefix());
597 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
598 assertNull(myUnionExt.getUnits());
599 assertNull(myUnionExt.getDefaultValue());
600 assertNull(myUnionExt.getFractionDigits());
601 assertTrue(myUnionExt.getLengthConstraints().isEmpty());
602 assertTrue(myUnionExt.getPatternConstraints().isEmpty());
603 assertTrue(myUnionExt.getRangeConstraints().isEmpty());
605 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
606 QName myUnionQName = myUnion.getQName();
607 assertEquals(barNS, myUnionQName.getNamespace());
608 assertEquals(barRev, myUnionQName.getRevision());
609 assertEquals("bar", myUnionQName.getPrefix());
610 assertEquals("my-union", myUnionQName.getLocalName());
611 assertNull(myUnion.getUnits());
612 assertNull(myUnion.getDefaultValue());
613 assertNull(myUnion.getFractionDigits());
614 assertTrue(myUnion.getLengthConstraints().isEmpty());
615 assertTrue(myUnion.getPatternConstraints().isEmpty());
616 assertTrue(myUnion.getRangeConstraints().isEmpty());
618 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
619 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
620 assertEquals(2, myUnionBaseTypes.size());
621 assertTrue(myUnionBaseTypes.get(0) instanceof ExtendedType);
622 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
624 ExtendedType int16Ext = (ExtendedType) myUnionBaseTypes.get(0);
625 QName int16ExtQName = int16Ext.getQName();
626 assertEquals(barNS, int16ExtQName.getNamespace());
627 assertEquals(barRev, int16ExtQName.getRevision());
628 assertEquals("bar", int16ExtQName.getPrefix());
629 assertEquals("int16", int16ExtQName.getLocalName());
630 assertNull(int16Ext.getUnits());
631 assertNull(int16Ext.getDefaultValue());
632 assertNull(int16Ext.getFractionDigits());
633 assertTrue(int16Ext.getLengthConstraints().isEmpty());
634 assertTrue(int16Ext.getPatternConstraints().isEmpty());
635 List<RangeConstraint> ranges = int16Ext.getRangeConstraints();
636 assertEquals(1, ranges.size());
637 RangeConstraint range = ranges.get(0);
638 assertEquals(1L, range.getMin());
639 assertEquals(100L, range.getMax());
641 assertTrue(int16Ext.getBaseType() instanceof Int16);
645 public void testChoice() {
646 Module foo = TestUtils.findModule(modules, "foo");
647 ContainerSchemaNode transfer = (ContainerSchemaNode) foo.getDataChildByName("transfer");
648 ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how");
649 assertEquals(transfer, how.getParent());
650 Set<ChoiceCaseNode> cases = how.getCases();
651 assertEquals(5, cases.size());
652 ChoiceCaseNode input = null;
653 ChoiceCaseNode output = null;
654 for (ChoiceCaseNode caseNode : cases) {
655 if ("input".equals(caseNode.getQName().getLocalName())) {
657 } else if ("output".equals(caseNode.getQName().getLocalName())) {
661 assertNotNull(input);
662 assertNotNull(input.getPath());
663 assertNotNull(output);
664 assertNotNull(output.getPath());
668 public void testDeviation() {
669 Module foo = TestUtils.findModule(modules, "foo");
670 Set<Deviation> deviations = foo.getDeviations();
671 assertEquals(1, deviations.size());
672 Deviation dev = deviations.iterator().next();
673 assertEquals("system/user ref", dev.getReference());
675 List<QName> path = new ArrayList<>();
676 path.add(new QName(barNS, barRev, "br", "interfaces"));
677 path.add(new QName(barNS, barRev, "br", "ifEntry"));
678 SchemaPath expectedPath = new SchemaPath(path, true);
680 assertEquals(expectedPath, dev.getTargetPath());
681 assertEquals(Deviate.ADD, dev.getDeviate());
685 public void testUnknownNode() {
686 Module baz = TestUtils.findModule(modules, "baz");
687 ContainerSchemaNode network = (ContainerSchemaNode) baz.getDataChildByName("network");
688 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
689 assertEquals(1, unknownNodes.size());
690 UnknownSchemaNode unknownNode = unknownNodes.get(0);
691 assertNotNull(unknownNode.getNodeType());
692 assertEquals("point", unknownNode.getNodeParameter());
696 public void testFeature() {
697 Module baz = TestUtils.findModule(modules, "baz");
698 Set<FeatureDefinition> features = baz.getFeatures();
699 assertEquals(1, features.size());
703 public void testExtension() {
704 Module baz = TestUtils.findModule(modules, "baz");
705 List<ExtensionDefinition> extensions = baz.getExtensionSchemaNodes();
706 assertEquals(1, extensions.size());
707 ExtensionDefinition extension = extensions.get(0);
708 assertEquals("name", extension.getArgument());
709 assertTrue(extension.isYinElement());
713 public void testNotification() {
714 Module baz = TestUtils.findModule(modules, "baz");
715 String expectedPrefix = "c";
717 Set<NotificationDefinition> notifications = baz.getNotifications();
718 assertEquals(1, notifications.size());
720 NotificationDefinition notification = notifications.iterator().next();
721 // test SchemaNode args
722 QName expectedQName = new QName(bazNS, bazRev, expectedPrefix, "event");
723 assertEquals(expectedQName, notification.getQName());
724 SchemaPath expectedPath = TestUtils.createPath(true, bazNS, bazRev, expectedPrefix, "event");
725 assertEquals(expectedPath, notification.getPath());
726 assertNull(notification.getDescription());
727 assertNull(notification.getReference());
728 assertEquals(Status.CURRENT, notification.getStatus());
729 assertEquals(0, notification.getUnknownSchemaNodes().size());
730 // test DataNodeContainer args
731 assertEquals(0, notification.getTypeDefinitions().size());
732 assertEquals(3, notification.getChildNodes().size());
733 assertEquals(0, notification.getGroupings().size());
734 assertEquals(0, notification.getUses().size());
736 LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
737 assertEquals(notification, eventClass.getParent());
738 assertTrue(eventClass.getType() instanceof StringType);
739 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity");
740 assertEquals(notification, reportingEntity.getParent());
741 LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
742 assertEquals(notification, severity.getParent());
743 assertTrue(severity.getType() instanceof StringType);
747 public void testRpc() {
748 Module baz = TestUtils.findModule(modules, "baz");
750 Set<RpcDefinition> rpcs = baz.getRpcs();
751 assertEquals(1, rpcs.size());
753 RpcDefinition rpc = rpcs.iterator().next();
754 assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
755 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
757 ContainerSchemaNode input = rpc.getInput();
758 assertEquals(rpc, input.getParent());
759 DataSchemaNode source = input.getDataChildByName("source");
760 assertEquals(input, source.getParent());
761 DataSchemaNode filter = input.getDataChildByName("filter");
762 assertEquals(input, filter.getParent());
763 ContainerSchemaNode output = rpc.getOutput();
764 assertEquals(rpc, output.getParent());
765 DataSchemaNode data = output.getDataChildByName("data");
766 assertEquals(output, data.getParent());
770 public void testTypePath() throws ParseException {
771 Module bar = TestUtils.findModule(modules, "bar");
772 Set<TypeDefinition<?>> types = bar.getTypeDefinitions();
775 ExtendedType int32ext1 = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1");
776 QName int32TypedefQName = int32ext1.getQName();
778 assertEquals(barNS, int32TypedefQName.getNamespace());
779 assertEquals(barRev, int32TypedefQName.getRevision());
780 assertEquals("bar", int32TypedefQName.getPrefix());
781 assertEquals("int32-ext1", int32TypedefQName.getLocalName());
783 SchemaPath typeSchemaPath = int32ext1.getPath();
784 List<QName> typePath = typeSchemaPath.getPath();
785 assertEquals(1, typePath.size());
786 assertEquals(int32TypedefQName, typePath.get(0));
789 Int32 int32 = (Int32) int32ext1.getBaseType();
790 assertEquals(Int32.getInstance(), int32);
794 public void testTypePath2() throws ParseException {
795 Module bar = TestUtils.findModule(modules, "bar");
796 Set<TypeDefinition<?>> types = bar.getTypeDefinitions();
799 ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
800 QName myDecTypeQName = myDecType.getQName();
802 assertEquals(barNS, myDecTypeQName.getNamespace());
803 assertEquals(barRev, myDecTypeQName.getRevision());
804 assertEquals("bar", myDecTypeQName.getPrefix());
805 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
807 SchemaPath typeSchemaPath = myDecType.getPath();
808 List<QName> typePath = typeSchemaPath.getPath();
809 assertEquals(1, typePath.size());
810 assertEquals(myDecTypeQName, typePath.get(0));
812 // my-base-int32-type/int32
813 Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
814 QName dec64QName = dec64.getQName();
816 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
817 assertNull(dec64QName.getRevision());
818 assertEquals("", dec64QName.getPrefix());
819 assertEquals("decimal64", dec64QName.getLocalName());
821 SchemaPath dec64SchemaPath = dec64.getPath();
822 List<QName> dec64Path = dec64SchemaPath.getPath();
823 assertEquals(2, dec64Path.size());
824 assertEquals(myDecTypeQName, dec64Path.get(0));
825 assertEquals(dec64QName, dec64Path.get(1));
829 public void testParseMethod1() throws ParseException {
830 File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
831 File dependenciesDir = new File(getClass().getResource("/parse-methods").getPath());
832 YangModelParser parser = new YangParserImpl();
833 modules = parser.parseYangModels(yangFile, dependenciesDir);
834 assertEquals(6, modules.size());
838 public void testParseMethod2() throws ParseException {
839 File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
840 File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").getPath());
841 YangModelParser parser = new YangParserImpl();
842 modules = parser.parseYangModels(yangFile, dependenciesDir);
843 assertEquals(6, modules.size());