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.*;
11 import static org.junit.Assert.assertEquals;
14 import java.io.FileNotFoundException;
16 import java.text.ParseException;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
29 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Deviation;
33 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
34 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
35 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
37 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
41 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
42 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 import org.opendaylight.yangtools.yang.model.api.Status;
45 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
48 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
49 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
50 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
51 import org.opendaylight.yangtools.yang.model.util.Decimal64;
52 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
53 import org.opendaylight.yangtools.yang.model.util.Int16;
54 import org.opendaylight.yangtools.yang.model.util.Int32;
55 import org.opendaylight.yangtools.yang.model.util.StringType;
56 import org.opendaylight.yangtools.yang.model.util.Uint32;
57 import org.opendaylight.yangtools.yang.model.util.UnionType;
59 public class YangParserTest {
60 private final URI fooNS = URI.create("urn:opendaylight.foo");
61 private final URI barNS = URI.create("urn:opendaylight.bar");
62 private final URI bazNS = URI.create("urn:opendaylight.baz");
67 private Set<Module> modules;
70 public void init() throws FileNotFoundException, ParseException {
71 fooRev = TestUtils.simpleDateFormat.parse("2013-02-27");
72 barRev = TestUtils.simpleDateFormat.parse("2013-07-03");
73 bazRev = TestUtils.simpleDateFormat.parse("2013-02-27");
75 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
76 assertEquals(3, modules.size());
80 public void testHeaders() throws ParseException {
81 Module foo = TestUtils.findModule(modules, "foo");
83 assertEquals("foo", foo.getName());
84 assertEquals("1", foo.getYangVersion());
85 assertEquals(fooNS, foo.getNamespace());
86 assertEquals("foo", foo.getPrefix());
88 Set<ModuleImport> imports = foo.getImports();
89 assertEquals(2, imports.size());
91 ModuleImport import2 = TestUtils.findImport(imports, "br");
92 assertEquals("bar", import2.getModuleName());
93 assertEquals(barRev, import2.getRevision());
95 ModuleImport import3 = TestUtils.findImport(imports, "bz");
96 assertEquals("baz", import3.getModuleName());
97 assertEquals(bazRev, import3.getRevision());
99 assertEquals("opendaylight", foo.getOrganization());
100 assertEquals("http://www.opendaylight.org/", foo.getContact());
101 Date expectedRevision = TestUtils.createDate("2013-02-27");
102 assertEquals(expectedRevision, foo.getRevision());
103 assertEquals(" WILL BE DEFINED LATER", foo.getReference());
107 public void testOrderingTypedef() {
108 Module bar = TestUtils.findModule(modules, "bar");
109 Set<TypeDefinition<?>> typedefs = bar.getTypeDefinitions();
110 String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "my-decimal-type", "my-union",
111 "my-union-ext", "nested-union2", "string-ext1", "string-ext2", "string-ext3", "string-ext4" };
112 String[] actualOrder = new String[typedefs.size()];
115 for (TypeDefinition<?> type : typedefs) {
116 actualOrder[i] = type.getQName().getLocalName();
119 assertArrayEquals(expectedOrder, actualOrder);
123 public void testOrderingChildNodes() {
124 Module foo = TestUtils.findModule(modules, "foo");
125 AugmentationSchema augment1 = null;
126 for (AugmentationSchema as : foo.getAugmentations()) {
127 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
132 assertNotNull(augment1);
134 String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "odl", "schemas" };
135 String[] actualOrder = new String[expectedOrder.length];
138 for (DataSchemaNode augmentChild : augment1.getChildNodes()) {
139 actualOrder[i] = augmentChild.getQName().getLocalName();
143 assertArrayEquals(expectedOrder, actualOrder);
147 public void testOrderingNestedChildNodes1() {
148 Module foo = TestUtils.findModule(modules, "foo");
150 Set<DataSchemaNode> childNodes = foo.getChildNodes();
151 String[] expectedOrder = new String[] { "address", "addresses", "custom-union-leaf", "data", "datas",
152 "decimal-leaf", "decimal-leaf2", "ext", "how", "int32-leaf", "length-leaf", "mycont", "peer", "port",
153 "string-leaf", "transfer", "union-leaf" };
154 String[] actualOrder = new String[childNodes.size()];
157 for (DataSchemaNode child : childNodes) {
158 actualOrder[i] = child.getQName().getLocalName();
161 assertArrayEquals(expectedOrder, actualOrder);
165 public void testOrderingNestedChildNodes2() {
166 Module baz = TestUtils.findModule(modules, "baz");
167 Set<GroupingDefinition> groupings = baz.getGroupings();
168 assertEquals(1, groupings.size());
169 GroupingDefinition target = groupings.iterator().next();
171 Set<DataSchemaNode> childNodes = target.getChildNodes();
172 String[] expectedOrder = new String[] { "address", "addresses", "data", "how", "port" };
173 String[] actualOrder = new String[childNodes.size()];
176 for (DataSchemaNode child : childNodes) {
177 actualOrder[i] = child.getQName().getLocalName();
180 assertArrayEquals(expectedOrder, actualOrder);
184 public void testParseList() {
185 Module bar = TestUtils.findModule(modules, "bar");
186 URI expectedNamespace = URI.create("urn:opendaylight.bar");
187 String expectedPrefix = "bar";
189 ContainerSchemaNode interfaces = (ContainerSchemaNode) bar.getDataChildByName("interfaces");
191 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
192 // test SchemaNode args
193 QName expectedQName = new QName(expectedNamespace, barRev, expectedPrefix, "ifEntry");
194 assertEquals(expectedQName, ifEntry.getQName());
195 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, barRev, expectedPrefix, "interfaces",
197 assertEquals(expectedPath, ifEntry.getPath());
198 assertNull(ifEntry.getDescription());
199 assertNull(ifEntry.getReference());
200 assertEquals(Status.CURRENT, ifEntry.getStatus());
201 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
202 // test DataSchemaNode args
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 Set<ChoiceCaseNode> cases = how.getCases();
650 assertEquals(5, cases.size());
651 ChoiceCaseNode input = null;
652 ChoiceCaseNode output = null;
653 for (ChoiceCaseNode caseNode : cases) {
654 if ("input".equals(caseNode.getQName().getLocalName())) {
656 } else if ("output".equals(caseNode.getQName().getLocalName())) {
660 assertNotNull(input);
661 assertNotNull(input.getPath());
662 assertNotNull(output);
663 assertNotNull(output.getPath());
667 public void testDeviation() {
668 Module foo = TestUtils.findModule(modules, "foo");
669 Set<Deviation> deviations = foo.getDeviations();
670 assertEquals(1, deviations.size());
671 Deviation dev = deviations.iterator().next();
672 assertEquals("system/user ref", dev.getReference());
674 List<QName> path = new ArrayList<>();
675 path.add(new QName(barNS, barRev, "br", "interfaces"));
676 path.add(new QName(barNS, barRev, "br", "ifEntry"));
677 SchemaPath expectedPath = new SchemaPath(path, true);
679 assertEquals(expectedPath, dev.getTargetPath());
680 assertEquals(Deviate.ADD, dev.getDeviate());
684 public void testUnknownNode() {
685 Module baz = TestUtils.findModule(modules, "baz");
686 ContainerSchemaNode network = (ContainerSchemaNode) baz.getDataChildByName("network");
687 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
688 assertEquals(1, unknownNodes.size());
689 UnknownSchemaNode unknownNode = unknownNodes.get(0);
690 assertNotNull(unknownNode.getNodeType());
691 assertEquals("point", unknownNode.getNodeParameter());
695 public void testFeature() {
696 Module baz = TestUtils.findModule(modules, "baz");
697 Set<FeatureDefinition> features = baz.getFeatures();
698 assertEquals(1, features.size());
702 public void testExtension() {
703 Module baz = TestUtils.findModule(modules, "baz");
704 List<ExtensionDefinition> extensions = baz.getExtensionSchemaNodes();
705 assertEquals(1, extensions.size());
706 ExtensionDefinition extension = extensions.get(0);
707 assertEquals("name", extension.getArgument());
708 assertTrue(extension.isYinElement());
712 public void testNotification() {
713 Module baz = TestUtils.findModule(modules, "baz");
714 String expectedPrefix = "c";
716 Set<NotificationDefinition> notifications = baz.getNotifications();
717 assertEquals(1, notifications.size());
719 NotificationDefinition notification = notifications.iterator().next();
720 // test SchemaNode args
721 QName expectedQName = new QName(bazNS, bazRev, expectedPrefix, "event");
722 assertEquals(expectedQName, notification.getQName());
723 SchemaPath expectedPath = TestUtils.createPath(true, bazNS, bazRev, expectedPrefix, "event");
724 assertEquals(expectedPath, notification.getPath());
725 assertNull(notification.getDescription());
726 assertNull(notification.getReference());
727 assertEquals(Status.CURRENT, notification.getStatus());
728 assertEquals(0, notification.getUnknownSchemaNodes().size());
729 // test DataNodeContainer args
730 assertEquals(0, notification.getTypeDefinitions().size());
731 assertEquals(3, notification.getChildNodes().size());
732 assertEquals(0, notification.getGroupings().size());
733 assertEquals(0, notification.getUses().size());
735 LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
736 assertTrue(eventClass.getType() instanceof StringType);
737 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity");
738 assertNotNull(reportingEntity);
739 LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
740 assertTrue(severity.getType() instanceof StringType);
744 public void testRpc() {
745 Module baz = TestUtils.findModule(modules, "baz");
747 Set<RpcDefinition> rpcs = baz.getRpcs();
748 assertEquals(1, rpcs.size());
750 RpcDefinition rpc = rpcs.iterator().next();
751 assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
752 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
754 ContainerSchemaNode input = rpc.getInput();
755 assertNotNull(input.getDataChildByName("source"));
756 assertNotNull(input.getDataChildByName("filter"));
757 ContainerSchemaNode output = rpc.getOutput();
758 assertNotNull(output.getDataChildByName("data"));
762 public void testTypePath() throws ParseException {
763 Module bar = TestUtils.findModule(modules, "bar");
764 Set<TypeDefinition<?>> types = bar.getTypeDefinitions();
767 ExtendedType int32ext1 = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1");
768 QName int32TypedefQName = int32ext1.getQName();
770 assertEquals(barNS, int32TypedefQName.getNamespace());
771 assertEquals(barRev, int32TypedefQName.getRevision());
772 assertEquals("bar", int32TypedefQName.getPrefix());
773 assertEquals("int32-ext1", int32TypedefQName.getLocalName());
775 SchemaPath typeSchemaPath = int32ext1.getPath();
776 List<QName> typePath = typeSchemaPath.getPath();
777 assertEquals(1, typePath.size());
778 assertEquals(int32TypedefQName, typePath.get(0));
781 Int32 int32 = (Int32) int32ext1.getBaseType();
782 assertEquals(Int32.getInstance(), int32);
786 public void testTypePath2() throws ParseException {
787 Module bar = TestUtils.findModule(modules, "bar");
788 Set<TypeDefinition<?>> types = bar.getTypeDefinitions();
791 ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
792 QName myDecTypeQName = myDecType.getQName();
794 assertEquals(barNS, myDecTypeQName.getNamespace());
795 assertEquals(barRev, myDecTypeQName.getRevision());
796 assertEquals("bar", myDecTypeQName.getPrefix());
797 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
799 SchemaPath typeSchemaPath = myDecType.getPath();
800 List<QName> typePath = typeSchemaPath.getPath();
801 assertEquals(1, typePath.size());
802 assertEquals(myDecTypeQName, typePath.get(0));
804 // my-base-int32-type/int32
805 Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
806 QName dec64QName = dec64.getQName();
808 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
809 assertNull(dec64QName.getRevision());
810 assertEquals("", dec64QName.getPrefix());
811 assertEquals("decimal64", dec64QName.getLocalName());
813 SchemaPath dec64SchemaPath = dec64.getPath();
814 List<QName> dec64Path = dec64SchemaPath.getPath();
815 assertEquals(2, dec64Path.size());
816 assertEquals(myDecTypeQName, dec64Path.get(0));
817 assertEquals(dec64QName, dec64Path.get(1));
821 public void testParseMethod1() throws ParseException {
822 File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
823 File dependenciesDir = new File(getClass().getResource("/parse-methods").getPath());
824 YangModelParser parser = new YangParserImpl();
825 modules = parser.parseYangModels(yangFile, dependenciesDir);
826 assertEquals(6, modules.size());
830 public void testParseMethod2() throws ParseException {
831 File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
832 File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").getPath());
833 YangModelParser parser = new YangParserImpl();
834 modules = parser.parseYangModels(yangFile, dependenciesDir);
835 assertEquals(6, modules.size());