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.controller.yang.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.FileNotFoundException;
14 import java.text.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.yang.common.QName;
26 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
27 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
28 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.controller.yang.model.api.ChoiceNode;
30 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
31 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
33 import org.opendaylight.controller.yang.model.api.Deviation;
34 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
35 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
36 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
37 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
38 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
39 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
40 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
41 import org.opendaylight.controller.yang.model.api.Module;
42 import org.opendaylight.controller.yang.model.api.ModuleImport;
43 import org.opendaylight.controller.yang.model.api.MustDefinition;
44 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
45 import org.opendaylight.controller.yang.model.api.RpcDefinition;
46 import org.opendaylight.controller.yang.model.api.SchemaNode;
47 import org.opendaylight.controller.yang.model.api.SchemaPath;
48 import org.opendaylight.controller.yang.model.api.Status;
49 import org.opendaylight.controller.yang.model.api.TypeDefinition;
50 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
51 import org.opendaylight.controller.yang.model.api.UsesNode;
52 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
53 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
54 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
55 import org.opendaylight.controller.yang.model.util.Decimal64;
56 import org.opendaylight.controller.yang.model.util.ExtendedType;
57 import org.opendaylight.controller.yang.model.util.Int16;
58 import org.opendaylight.controller.yang.model.util.Int32;
59 import org.opendaylight.controller.yang.model.util.Int8;
60 import org.opendaylight.controller.yang.model.util.Leafref;
61 import org.opendaylight.controller.yang.model.util.StringType;
62 import org.opendaylight.controller.yang.model.util.Uint32;
63 import org.opendaylight.controller.yang.model.util.UnionType;
65 public class YangParserTest {
66 private final DateFormat simpleDateFormat = new SimpleDateFormat(
69 private Set<Module> modules;
72 public void init() throws FileNotFoundException {
73 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
74 assertEquals(3, modules.size());
78 public void testHeaders() {
79 Module test = TestUtils.findModule(modules, "types1");
81 assertEquals("types1", test.getName());
82 assertEquals("1", test.getYangVersion());
83 URI expectedNamespace = URI.create("urn:simple.container.demo");
84 assertEquals(expectedNamespace, test.getNamespace());
85 assertEquals("t1", test.getPrefix());
87 Set<ModuleImport> imports = test.getImports();
88 assertEquals(2, imports.size());
90 ModuleImport import2 = TestUtils.findImport(imports, "data");
91 assertEquals("types2", import2.getModuleName());
92 assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision());
94 ModuleImport import3 = TestUtils.findImport(imports, "t3");
95 assertEquals("types3", import3.getModuleName());
96 assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision());
98 assertEquals("opendaylight", test.getOrganization());
99 assertEquals("http://www.opendaylight.org/", test.getContact());
100 Date expectedRevision = TestUtils.createDate("2013-02-27");
101 assertEquals(expectedRevision, test.getRevision());
102 assertEquals(" WILL BE DEFINED LATER", test.getReference());
106 public void testParseContainer() {
107 Module test = TestUtils.findModule(modules, "types2");
108 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
109 String expectedPrefix = "t2";
110 Date expectedRevision = TestUtils.createDate("2013-02-27");
112 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
113 .getDataChildByName("interfaces");
114 // test SchemaNode args
115 QName expectedQName = new QName(expectedNamespace, expectedRevision,
116 expectedPrefix, "interfaces");
117 assertEquals(expectedQName, interfaces.getQName());
118 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
119 expectedRevision, expectedPrefix, "interfaces");
120 assertEquals(expectedPath, interfaces.getPath());
121 assertNull(interfaces.getDescription());
122 assertNull(interfaces.getReference());
123 assertEquals(Status.CURRENT, interfaces.getStatus());
124 assertEquals(0, interfaces.getUnknownSchemaNodes().size());
125 // test DataSchemaNode args
126 assertFalse(interfaces.isAugmenting());
127 assertFalse(interfaces.isConfiguration());
128 ConstraintDefinition constraints = interfaces.getConstraints();
129 assertNull(constraints.getWhenCondition());
130 assertEquals(0, constraints.getMustConstraints().size());
131 assertFalse(constraints.isMandatory());
132 assertNull(constraints.getMinElements());
133 assertNull(constraints.getMaxElements());
134 // test AugmentationTarget args
135 assertEquals(0, interfaces.getAvailableAugmentations().size());
136 // test ContainerSchemaNode args
137 assertFalse(interfaces.isPresenceContainer());
138 // test DataNodeContainer args
139 assertEquals(0, interfaces.getTypeDefinitions().size());
140 assertEquals(1, interfaces.getChildNodes().size());
141 assertEquals(0, interfaces.getGroupings().size());
142 assertEquals(0, interfaces.getUses().size());
144 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
145 .getDataChildByName("ifEntry");
146 assertNotNull(ifEntry);
150 public void testParseList() {
151 Module test = TestUtils.findModule(modules, "types2");
152 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
153 String expectedPrefix = "t2";
154 Date expectedRevision = TestUtils.createDate("2013-02-27");
156 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
157 .getDataChildByName("interfaces");
159 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
160 .getDataChildByName("ifEntry");
161 // test SchemaNode args
162 QName expectedQName = new QName(expectedNamespace, expectedRevision,
163 expectedPrefix, "ifEntry");
164 assertEquals(expectedQName, ifEntry.getQName());
165 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
166 expectedRevision, expectedPrefix, "interfaces", "ifEntry");
167 assertEquals(expectedPath, ifEntry.getPath());
168 assertNull(ifEntry.getDescription());
169 assertNull(ifEntry.getReference());
170 assertEquals(Status.CURRENT, ifEntry.getStatus());
171 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
172 // test DataSchemaNode args
173 assertFalse(ifEntry.isAugmenting());
174 assertFalse(ifEntry.isConfiguration());
175 ConstraintDefinition constraints = ifEntry.getConstraints();
176 assertNull(constraints.getWhenCondition());
177 assertEquals(0, constraints.getMustConstraints().size());
178 assertFalse(constraints.isMandatory());
179 assertEquals(1, (int) constraints.getMinElements());
180 assertEquals(11, (int) constraints.getMaxElements());
181 // test AugmentationTarget args
182 Set<AugmentationSchema> availableAugmentations = ifEntry
183 .getAvailableAugmentations();
184 assertEquals(2, availableAugmentations.size());
185 // test ListSchemaNode args
186 List<QName> expectedKey = new ArrayList<QName>();
187 expectedKey.add(new QName(expectedNamespace, expectedRevision,
188 expectedPrefix, "ifIndex"));
189 assertEquals(expectedKey, ifEntry.getKeyDefinition());
190 assertFalse(ifEntry.isUserOrdered());
191 // test DataNodeContainer args
192 assertEquals(0, ifEntry.getTypeDefinitions().size());
193 assertEquals(4, ifEntry.getChildNodes().size());
194 assertEquals(0, ifEntry.getGroupings().size());
195 assertEquals(0, ifEntry.getUses().size());
197 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry
198 .getDataChildByName("ifIndex");
199 assertTrue(ifIndex.getType() instanceof Uint32);
200 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry
201 .getDataChildByName("ifMtu");
202 assertTrue(ifMtu.getType() instanceof Int32);
206 public void testParseLeaf() throws ParseException {
207 Module test = TestUtils.findModule(modules, "types2");
210 LeafSchemaNode ifName = (LeafSchemaNode) test
211 .getDataChildByName("if-name");
212 Leafref ifNameType = (Leafref) ifName.getType();
213 QName qname = ifNameType.getQName();
215 URI baseYangTypeNS = URI.create("urn:ietf:params:xml:ns:yang:1");
216 assertEquals(baseYangTypeNS, qname.getNamespace());
217 assertNull(qname.getRevision());
218 assertEquals("", qname.getPrefix());
219 assertEquals("leafref", qname.getLocalName());
222 LeafSchemaNode name = (LeafSchemaNode) test.getDataChildByName("name");
223 StringType nameType = (StringType) name.getType();
224 QName nameQName = nameType.getQName();
226 assertEquals(baseYangTypeNS, nameQName.getNamespace());
227 assertNull(nameQName.getRevision());
228 assertEquals("", nameQName.getPrefix());
229 assertEquals("string", nameQName.getLocalName());
232 LeafSchemaNode count = (LeafSchemaNode) test
233 .getDataChildByName("count");
234 ExtendedType countType = (ExtendedType) count.getType();
235 QName countTypeQName = countType.getQName();
237 URI expectedNS = URI.create("urn:simple.types.data.demo");
238 Date expectedDate = simpleDateFormat.parse("2013-02-27");
239 assertEquals(expectedNS, countTypeQName.getNamespace());
240 assertEquals(expectedDate, countTypeQName.getRevision());
241 assertEquals("t2", countTypeQName.getPrefix());
242 assertEquals("int8", countTypeQName.getLocalName());
244 Int8 countTypeBase = (Int8) countType.getBaseType();
245 QName countTypeBaseQName = countTypeBase.getQName();
247 assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace());
248 assertNull(countTypeBaseQName.getRevision());
249 assertEquals("", countTypeBaseQName.getPrefix());
250 assertEquals("int8", countTypeBaseQName.getLocalName());
254 public void testAugmentResolving() {
256 Module module1 = TestUtils.findModule(modules, "types1");
258 Set<AugmentationSchema> module1Augmentations = module1
260 AugmentationSchema augment1 = module1Augmentations.iterator().next();
261 LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1
262 .getDataChildByName("ds0ChannelNumber");
263 assertTrue(augmentedLeafDefinition.isAugmenting());
266 Module module2 = TestUtils.findModule(modules, "types2");
268 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2
269 .getDataChildByName("interfaces");
270 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
271 .getDataChildByName("ifEntry");
272 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry
273 .getDataChildByName("augment-holder");
275 // augmentation defined in testfile1 and augmentation returned from
276 // augmented container have to be same
277 Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer
278 .getAvailableAugmentations();
279 AugmentationSchema augmentDefinition = augmentedContainerAugments
281 assertEquals(augment1, augmentDefinition);
283 LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer
284 .getDataChildByName("ds0ChannelNumber");
285 assertTrue(augmentedLeaf.isAugmenting());
286 assertEquals(augmentedLeafDefinition, augmentedLeaf);
288 Set<AugmentationSchema> ifEntryAugments = ifEntry
289 .getAvailableAugmentations();
290 assertEquals(2, ifEntryAugments.size());
293 Module module3 = TestUtils.findModule(modules, "types3");
295 Set<AugmentationSchema> module3Augmentations = module3
297 assertEquals(2, module3Augmentations.size());
298 AugmentationSchema augment3 = null;
299 for (AugmentationSchema as : module3Augmentations) {
300 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
304 ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
305 .getDataChildByName("augment-holder");
306 assertTrue(augmentedContainerDefinition.isAugmenting());
309 assertEquals(augmentedContainer, augmentedContainerDefinition);
310 assertEquals(augmentedLeaf, augmentedLeafDefinition);
314 public void testAugmentTarget() {
315 Module test = TestUtils.findModule(modules, "types2");
317 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
318 .getDataChildByName("interfaces");
319 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
320 .getDataChildByName("ifEntry");
321 Set<AugmentationSchema> augmentations = ifEntry
322 .getAvailableAugmentations();
323 assertEquals(2, augmentations.size());
325 AugmentationSchema augment = null;
326 for (AugmentationSchema as : augmentations) {
327 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
331 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment
332 .getDataChildByName("augment-holder");
333 assertNotNull(augmentHolder);
334 assertTrue(augmentHolder.isAugmenting());
335 QName augmentHolderQName = augmentHolder.getQName();
336 assertEquals("augment-holder", augmentHolderQName.getLocalName());
337 assertEquals("t3", augmentHolderQName.getPrefix());
338 assertEquals("Description for augment holder",
339 augmentHolder.getDescription());
343 public void testTypedefRangesResolving() throws ParseException {
344 Module testModule = TestUtils.findModule(modules, "types1");
346 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
347 .getDataChildByName("testleaf");
348 ExtendedType leafType = (ExtendedType) testleaf.getType();
349 QName leafTypeQName = leafType.getQName();
350 assertEquals("my-type1", leafTypeQName.getLocalName());
351 assertEquals("t1", leafTypeQName.getPrefix());
352 assertEquals(URI.create("urn:simple.container.demo"),
353 leafTypeQName.getNamespace());
354 Date expectedDate = simpleDateFormat.parse("2013-02-27");
355 assertEquals(expectedDate, leafTypeQName.getRevision());
356 assertEquals(1, leafType.getRanges().size());
358 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
359 QName baseTypeQName = baseType.getQName();
360 assertEquals("my-type1", baseTypeQName.getLocalName());
361 assertEquals("t2", baseTypeQName.getPrefix());
362 assertEquals(URI.create("urn:simple.types.data.demo"),
363 baseTypeQName.getNamespace());
364 assertEquals(expectedDate, baseTypeQName.getRevision());
365 assertEquals(2, baseType.getRanges().size());
367 List<RangeConstraint> ranges = leafType.getRanges();
368 assertEquals(1, ranges.size());
369 RangeConstraint range = ranges.get(0);
370 assertEquals(3L, range.getMin());
371 assertEquals(20L, range.getMax());
375 public void testTypedefPatternsResolving() {
376 Module testModule = TestUtils.findModule(modules, "types1");
378 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
379 .getDataChildByName("test-string-leaf");
380 ExtendedType testleafType = (ExtendedType) testleaf.getType();
381 QName testleafTypeQName = testleafType.getQName();
382 assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
383 assertEquals("t2", testleafTypeQName.getPrefix());
385 List<PatternConstraint> patterns = testleafType.getPatterns();
386 assertEquals(1, patterns.size());
387 PatternConstraint pattern = patterns.iterator().next();
388 assertEquals("[e-z]*", pattern.getRegularExpression());
390 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
391 assertEquals("my-string-type2", baseType.getQName().getLocalName());
393 patterns = baseType.getPatterns();
394 assertEquals(1, patterns.size());
395 pattern = patterns.iterator().next();
396 assertEquals("[b-u]*", pattern.getRegularExpression());
398 List<LengthConstraint> lengths = testleafType.getLengths();
399 assertTrue(lengths.isEmpty());
403 public void testTypedefLengthsResolving() {
404 Module testModule = TestUtils.findModule(modules, "types1");
406 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
407 .getDataChildByName("leaf-with-length");
408 ExtendedType testleafType = (ExtendedType) testleaf.getType();
409 assertEquals("my-string-type", testleafType.getQName().getLocalName());
411 List<LengthConstraint> lengths = testleafType.getLengths();
412 assertEquals(1, lengths.size());
414 LengthConstraint length = lengths.get(0);
415 assertEquals(7L, length.getMin());
416 assertEquals(10L, length.getMax());
420 public void testTypeDef() {
421 Module testModule = TestUtils.findModule(modules, "types2");
423 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
424 .getDataChildByName("nested-type-leaf");
425 ExtendedType testleafType = (ExtendedType) testleaf.getType();
426 assertEquals("my-type1", testleafType.getQName().getLocalName());
428 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
429 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
431 Int32 int32Type = (Int32) baseType.getBaseType();
432 QName qname = int32Type.getQName();
433 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
434 qname.getNamespace());
435 assertNull(qname.getRevision());
436 assertEquals("", qname.getPrefix());
437 assertEquals("int32", qname.getLocalName());
438 List<RangeConstraint> ranges = baseType.getRanges();
439 assertEquals(1, ranges.size());
440 RangeConstraint range = ranges.get(0);
441 assertEquals(2L, range.getMin());
442 assertEquals(20L, range.getMax());
446 public void testTypedefDecimal1() {
447 Module testModule = TestUtils.findModule(modules, "types1");
449 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
450 .getDataChildByName("test-decimal-leaf");
451 ExtendedType type = (ExtendedType) testleaf.getType();
452 assertEquals(4, (int) type.getFractionDigits());
454 ExtendedType typeBase = (ExtendedType) type.getBaseType();
455 assertEquals("my-decimal-type", typeBase.getQName().getLocalName());
456 assertNull(typeBase.getFractionDigits());
458 Decimal64 decimal = (Decimal64) typeBase.getBaseType();
459 assertEquals(6, (int) decimal.getFractionDigits());
463 public void testTypedefDecimal2() {
464 Module testModule = TestUtils.findModule(modules, "types1");
466 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
467 .getDataChildByName("test-decimal-leaf2");
468 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
469 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
470 Decimal64 baseTypeCast = (Decimal64) baseType;
471 assertEquals(5, (int) baseTypeCast.getFractionDigits());
475 public void testTypedefUnion() {
476 Module testModule = TestUtils.findModule(modules, "types1");
478 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
479 .getDataChildByName("union-leaf");
480 ExtendedType testleafType = (ExtendedType) testleaf.getType();
481 assertEquals("my-union-ext", testleafType.getQName().getLocalName());
483 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
484 assertEquals("my-union", baseType.getQName().getLocalName());
486 UnionType unionBase = (UnionType) baseType.getBaseType();
488 List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
489 ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
490 List<RangeConstraint> ranges = unionType1.getRanges();
491 assertEquals(1, ranges.size());
492 RangeConstraint range = ranges.get(0);
493 assertEquals(1L, range.getMin());
494 assertEquals(100L, range.getMax());
496 assertTrue(unionType1.getBaseType() instanceof Int16);
497 assertTrue(unionTypes.get(1) instanceof Int32);
501 public void testNestedUnionResolving1() {
502 Module testModule = TestUtils.findModule(modules, "types1");
504 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
505 .getDataChildByName("nested-union-leaf");
507 ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
508 assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
510 ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
511 assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
513 UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
514 List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
515 assertEquals(2, unionTypes.size());
516 assertTrue(unionTypes.get(0) instanceof StringType);
517 assertTrue(unionTypes.get(1) instanceof ExtendedType);
519 ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
520 ExtendedType extendedUnionBase = (ExtendedType) extendedUnion
522 assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
524 UnionType extendedTargetUnion = (UnionType) extendedUnionBase
526 List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion
528 assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16);
529 assertTrue(extendedTargetTypes.get(1) instanceof Int32);
531 ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0);
532 assertTrue(int16.getBaseType() instanceof Int16);
533 List<RangeConstraint> ranges = int16.getRanges();
534 assertEquals(1, ranges.size());
535 RangeConstraint range = ranges.get(0);
536 assertEquals(1L, range.getMin());
537 assertEquals(100L, range.getMax());
541 public void testNestedUnionResolving2() {
542 Module testModule = TestUtils.findModule(modules, "types1");
544 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
545 .getDataChildByName("custom-union-leaf");
547 ExtendedType testleafType = (ExtendedType) testleaf.getType();
548 QName testleafTypeQName = testleafType.getQName();
549 assertEquals(URI.create("urn:simple.container.demo.test"),
550 testleafTypeQName.getNamespace());
551 assertEquals(TestUtils.createDate("2013-02-27"),
552 testleafTypeQName.getRevision());
553 assertEquals("t3", testleafTypeQName.getPrefix());
554 assertEquals("union1", testleafTypeQName.getLocalName());
556 ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
557 QName union2QName = union2.getQName();
558 assertEquals(URI.create("urn:simple.container.demo.test"),
559 union2QName.getNamespace());
560 assertEquals(TestUtils.createDate("2013-02-27"),
561 union2QName.getRevision());
562 assertEquals("t3", union2QName.getPrefix());
563 assertEquals("union2", union2QName.getLocalName());
565 UnionType union2Base = (UnionType) union2.getBaseType();
566 List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
567 assertEquals(2, unionTypes.size());
568 assertTrue(unionTypes.get(0) instanceof Int32);
569 assertTrue(unionTypes.get(1) instanceof ExtendedType);
571 ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
572 QName nestedUnion2QName = nestedUnion2.getQName();
573 assertEquals(URI.create("urn:simple.types.data.demo"),
574 nestedUnion2QName.getNamespace());
575 assertEquals(TestUtils.createDate("2013-02-27"),
576 nestedUnion2QName.getRevision());
577 assertEquals("t2", nestedUnion2QName.getPrefix());
578 assertEquals("nested-union2", nestedUnion2QName.getLocalName());
580 UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
581 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
582 assertEquals(2, nestedUnion2Types.size());
583 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
584 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
586 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
587 QName myUnionExtQName = myUnionExt.getQName();
588 assertEquals(URI.create("urn:simple.types.data.demo"),
589 myUnionExtQName.getNamespace());
590 assertEquals(TestUtils.createDate("2013-02-27"),
591 myUnionExtQName.getRevision());
592 assertEquals("t2", myUnionExtQName.getPrefix());
593 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
595 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
596 QName myUnionQName = myUnion.getQName();
597 assertEquals(URI.create("urn:simple.types.data.demo"),
598 myUnionQName.getNamespace());
599 assertEquals(TestUtils.createDate("2013-02-27"),
600 myUnionQName.getRevision());
601 assertEquals("t2", myUnionQName.getPrefix());
602 assertEquals("my-union", myUnionQName.getLocalName());
604 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
605 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
606 assertEquals(2, myUnionBaseTypes.size());
607 assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16);
608 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
609 ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0);
610 List<RangeConstraint> ranges = int16.getRanges();
611 assertEquals(1, ranges.size());
612 RangeConstraint range = ranges.get(0);
613 assertEquals(1L, range.getMin());
614 assertEquals(100L, range.getMax());
618 public void testRefine() {
619 Module testModule = TestUtils.findModule(modules, "types2");
621 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
622 .getDataChildByName("peer");
623 ContainerSchemaNode destination = (ContainerSchemaNode) peer
624 .getDataChildByName("destination");
625 Set<UsesNode> usesNodes = destination.getUses();
626 assertEquals(1, usesNodes.size());
627 UsesNode usesNode = usesNodes.iterator().next();
628 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
629 assertEquals(5, refines.size());
631 LeafSchemaNode refineLeaf = null;
632 ContainerSchemaNode refineContainer = null;
633 ListSchemaNode refineList = null;
634 GroupingDefinition refineGrouping = null;
635 TypeDefinition<?> typedef = null;
636 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
637 SchemaNode value = entry.getValue();
638 if (value instanceof LeafSchemaNode) {
639 refineLeaf = (LeafSchemaNode) value;
640 } else if (value instanceof ContainerSchemaNode) {
641 refineContainer = (ContainerSchemaNode) value;
642 } else if (value instanceof ListSchemaNode) {
643 refineList = (ListSchemaNode) value;
644 } else if (value instanceof GroupingDefinition) {
645 refineGrouping = (GroupingDefinition) value;
646 } else if (value instanceof TypeDefinition<?>) {
647 typedef = (TypeDefinition<?>) value;
652 assertNotNull(refineLeaf);
653 assertEquals("address", refineLeaf.getQName().getLocalName());
654 assertEquals("description of address defined by refine",
655 refineLeaf.getDescription());
656 assertEquals("address reference added by refine",
657 refineLeaf.getReference());
658 assertFalse(refineLeaf.isConfiguration());
659 assertTrue(refineLeaf.getConstraints().isMandatory());
660 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
661 .getMustConstraints();
662 assertEquals(1, leafMustConstraints.size());
663 MustDefinition leafMust = leafMustConstraints.iterator().next();
665 "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"",
666 leafMust.toString());
669 assertNotNull(refineContainer);
670 Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
671 .getMustConstraints();
672 assertTrue(mustConstraints.isEmpty());
673 assertEquals("description of port defined by refine",
674 refineContainer.getDescription());
675 assertEquals("port reference added by refine",
676 refineContainer.getReference());
677 assertFalse(refineContainer.isConfiguration());
678 assertTrue(refineContainer.isPresenceContainer());
681 assertNotNull(refineList);
682 assertEquals("description of addresses defined by refine",
683 refineList.getDescription());
684 assertEquals("addresses reference added by refine",
685 refineList.getReference());
686 assertFalse(refineList.isConfiguration());
687 assertEquals(2, (int) refineList.getConstraints().getMinElements());
688 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
690 // grouping target-inner
691 assertNotNull(refineGrouping);
692 Set<DataSchemaNode> refineGroupingChildren = refineGrouping
694 assertEquals(1, refineGroupingChildren.size());
695 LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren
697 assertEquals("inner-grouping-id", refineGroupingLeaf.getQName()
699 assertEquals("new target-inner grouping description",
700 refineGrouping.getDescription());
702 // typedef group-type
703 assertNotNull(typedef);
704 assertEquals("new group-type description", typedef.getDescription());
705 assertEquals("new group-type reference", typedef.getReference());
706 assertTrue(typedef.getBaseType() instanceof ExtendedType);
710 public void testChoice() {
711 Module testModule = TestUtils.findModule(modules, "types1");
712 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
713 .getDataChildByName("transfer");
714 ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
715 Set<ChoiceCaseNode> cases = how.getCases();
716 assertEquals(3, cases.size());
720 public void testAnyXml() {
721 Module testModule = TestUtils.findModule(modules, "types1");
722 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule
723 .getDataChildByName("data");
728 public void testDeviation() {
729 Module testModule = TestUtils.findModule(modules, "types1");
730 Set<Deviation> deviations = testModule.getDeviations();
731 assertEquals(1, deviations.size());
733 Deviation dev = deviations.iterator().next();
734 SchemaPath expectedPath = TestUtils.createPath(true, null, null,
735 "data", "system", "user");
736 assertEquals(expectedPath, dev.getTargetPath());
737 assertEquals(Deviate.ADD, dev.getDeviate());
741 public void testUnknownNode() {
742 Module testModule = TestUtils.findModule(modules, "types3");
743 ContainerSchemaNode network = (ContainerSchemaNode) testModule
744 .getDataChildByName("network");
745 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
746 assertEquals(1, unknownNodes.size());
747 UnknownSchemaNode unknownNode = unknownNodes.get(0);
748 assertNotNull(unknownNode.getNodeType());
749 assertEquals("point", unknownNode.getNodeParameter());
753 public void testFeature() {
754 Module testModule = TestUtils.findModule(modules, "types3");
755 Set<FeatureDefinition> features = testModule.getFeatures();
756 assertEquals(1, features.size());
760 public void testExtension() {
761 Module testModule = TestUtils.findModule(modules, "types3");
762 List<ExtensionDefinition> extensions = testModule
763 .getExtensionSchemaNodes();
764 assertEquals(1, extensions.size());
765 ExtensionDefinition extension = extensions.get(0);
766 assertEquals("name", extension.getArgument());
767 assertFalse(extension.isYinElement());
771 public void testNotification() {
772 Module testModule = TestUtils.findModule(modules, "types3");
773 URI expectedNamespace = URI.create("urn:simple.container.demo.test");
774 String expectedPrefix = "t3";
775 Date expectedRevision = TestUtils.createDate("2013-02-27");
777 Set<NotificationDefinition> notifications = testModule
779 assertEquals(1, notifications.size());
781 NotificationDefinition notification = notifications.iterator().next();
782 // test SchemaNode args
783 QName expectedQName = new QName(expectedNamespace, expectedRevision,
784 expectedPrefix, "event");
785 assertEquals(expectedQName, notification.getQName());
786 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
787 expectedRevision, expectedPrefix, "event");
788 assertEquals(expectedPath, notification.getPath());
789 assertNull(notification.getDescription());
790 assertNull(notification.getReference());
791 assertEquals(Status.CURRENT, notification.getStatus());
792 assertEquals(0, notification.getUnknownSchemaNodes().size());
793 // test DataNodeContainer args
794 assertEquals(0, notification.getTypeDefinitions().size());
795 assertEquals(3, notification.getChildNodes().size());
796 assertEquals(0, notification.getGroupings().size());
797 assertEquals(0, notification.getUses().size());
799 LeafSchemaNode eventClass = (LeafSchemaNode) notification
800 .getDataChildByName("event-class");
801 assertTrue(eventClass.getType() instanceof StringType);
802 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification
803 .getDataChildByName("reporting-entity");
804 assertNotNull(reportingEntity);
805 LeafSchemaNode severity = (LeafSchemaNode) notification
806 .getDataChildByName("severity");
807 assertTrue(severity.getType() instanceof StringType);
811 public void testRpc() {
812 Module testModule = TestUtils.findModule(modules, "types3");
814 Set<RpcDefinition> rpcs = testModule.getRpcs();
815 assertEquals(1, rpcs.size());
817 RpcDefinition rpc = rpcs.iterator().next();
818 assertEquals("Retrieve all or part of a specified configuration.",
819 rpc.getDescription());
820 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
822 ContainerSchemaNode input = rpc.getInput();
823 assertNotNull(input.getDataChildByName("source"));
824 assertNotNull(input.getDataChildByName("filter"));
825 ContainerSchemaNode output = rpc.getOutput();
826 assertNotNull(output.getDataChildByName("data"));
830 public void testGrouping() {
831 Module testModule = TestUtils.findModule(modules, "types2");
832 Set<GroupingDefinition> groupings = testModule.getGroupings();
833 assertEquals(1, groupings.size());
834 GroupingDefinition grouping = groupings.iterator().next();
835 Set<DataSchemaNode> children = grouping.getChildNodes();
836 assertEquals(5, children.size());
840 public void testAugmentNodesTypesSchemaPath() throws Exception {
841 Module testModule = TestUtils.findModule(modules, "types1");
842 Set<AugmentationSchema> augments = testModule.getAugmentations();
843 assertEquals(1, augments.size());
844 AugmentationSchema augment = augments.iterator().next();
846 LeafSchemaNode ifcId = (LeafSchemaNode) augment
847 .getDataChildByName("interface-id");
848 Leafref ifcIdType = (Leafref) ifcId.getType();
849 SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
850 List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
851 QName q0 = new QName(new URI("urn:simple.types.data.demo"),
852 simpleDateFormat.parse("2013-02-27"), "data", "interfaces");
853 QName q1 = new QName(new URI("urn:simple.types.data.demo"),
854 simpleDateFormat.parse("2013-02-27"), "data", "ifEntry");
855 QName q2 = new QName(new URI("urn:simple.container.demo.test"),
856 simpleDateFormat.parse("2013-02-27"), "data", "augment-holder");
857 QName q3 = new QName(new URI("urn:simple.container.demo"),
858 simpleDateFormat.parse("2013-02-27"), "data", "interface-id");
859 assertEquals(q0, ifcIdTypePath.get(0));
860 assertEquals(q1, ifcIdTypePath.get(1));
861 assertEquals(q2, ifcIdTypePath.get(2));
862 assertEquals(q3, ifcIdTypePath.get(3));
864 LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment
865 .getDataChildByName("higher-layer-if");
866 Leafref higherLayerType = (Leafref) higherLayer.getType();
867 SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
868 List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
869 assertEquals(q0, higherLayerTypePath.get(0));
870 assertEquals(q1, higherLayerTypePath.get(1));
871 assertEquals(q2, higherLayerTypePath.get(2));
872 q3 = new QName(new URI("urn:simple.container.demo"),
873 simpleDateFormat.parse("2013-02-27"), "data", "higher-layer-if");
874 assertEquals(q3, higherLayerTypePath.get(3));
878 public void testTypePath() throws ParseException {
879 Module test = TestUtils.findModule(modules, "types2");
880 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
882 // my-base-int32-type
883 ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types,
884 "my-base-int32-type");
885 QName int32TypedefQName = int32Typedef.getQName();
887 URI expectedNS = URI.create("urn:simple.types.data.demo");
888 Date expectedDate = simpleDateFormat.parse("2013-02-27");
889 assertEquals(expectedNS, int32TypedefQName.getNamespace());
890 assertEquals(expectedDate, int32TypedefQName.getRevision());
891 assertEquals("t2", int32TypedefQName.getPrefix());
892 assertEquals("my-base-int32-type", int32TypedefQName.getLocalName());
894 SchemaPath typeSchemaPath = int32Typedef.getPath();
895 List<QName> typePath = typeSchemaPath.getPath();
896 assertEquals(1, typePath.size());
897 assertEquals(int32TypedefQName, typePath.get(0));
899 // my-base-int32-type/int32
900 Int32 int32 = (Int32) int32Typedef.getBaseType();
901 QName int32QName = int32.getQName();
902 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
903 int32QName.getNamespace());
904 assertNull(int32QName.getRevision());
905 assertEquals("", int32QName.getPrefix());
906 assertEquals("int32", int32QName.getLocalName());
908 SchemaPath int32SchemaPath = int32.getPath();
909 List<QName> int32Path = int32SchemaPath.getPath();
910 assertEquals(3, int32Path.size());
911 assertEquals(int32TypedefQName, int32Path.get(0));
912 assertEquals(int32QName, int32Path.get(2));
916 public void testTypePath2() throws ParseException {
917 Module test = TestUtils.findModule(modules, "types2");
918 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
920 // my-base-int32-type
921 ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types,
923 QName myDecTypeQName = myDecType.getQName();
925 URI expectedNS = URI.create("urn:simple.types.data.demo");
926 Date expectedDate = simpleDateFormat.parse("2013-02-27");
927 assertEquals(expectedNS, myDecTypeQName.getNamespace());
928 assertEquals(expectedDate, myDecTypeQName.getRevision());
929 assertEquals("t2", myDecTypeQName.getPrefix());
930 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
932 SchemaPath typeSchemaPath = myDecType.getPath();
933 List<QName> typePath = typeSchemaPath.getPath();
934 assertEquals(1, typePath.size());
935 assertEquals(myDecTypeQName, typePath.get(0));
937 // my-base-int32-type/int32
938 Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
939 QName dec64QName = dec64.getQName();
941 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
942 dec64QName.getNamespace());
943 assertNull(dec64QName.getRevision());
944 assertEquals("", dec64QName.getPrefix());
945 assertEquals("decimal64", dec64QName.getLocalName());
947 SchemaPath dec64SchemaPath = dec64.getPath();
948 List<QName> dec64Path = dec64SchemaPath.getPath();
949 assertEquals(2, dec64Path.size());
950 assertEquals(myDecTypeQName, dec64Path.get(0));
951 assertEquals(dec64QName, dec64Path.get(1));