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("yyyy-MM-dd");
68 private Set<Module> modules;
71 public void init() throws FileNotFoundException {
72 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
73 assertEquals(3, modules.size());
77 public void testHeaders() {
78 Module test = TestUtils.findModule(modules, "types1");
80 assertEquals("types1", test.getName());
81 assertEquals("1", test.getYangVersion());
82 URI expectedNamespace = URI.create("urn:simple.container.demo");
83 assertEquals(expectedNamespace, test.getNamespace());
84 assertEquals("t1", test.getPrefix());
86 Set<ModuleImport> imports = test.getImports();
87 assertEquals(2, imports.size());
89 ModuleImport import2 = TestUtils.findImport(imports, "data");
90 assertEquals("types2", import2.getModuleName());
91 assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision());
93 ModuleImport import3 = TestUtils.findImport(imports, "t3");
94 assertEquals("types3", import3.getModuleName());
95 assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision());
97 assertEquals("opendaylight", test.getOrganization());
98 assertEquals("http://www.opendaylight.org/", test.getContact());
99 Date expectedRevision = TestUtils.createDate("2013-02-27");
100 assertEquals(expectedRevision, test.getRevision());
101 assertEquals(" WILL BE DEFINED LATER", test.getReference());
105 public void testParseContainer() {
106 Module test = TestUtils.findModule(modules, "types2");
107 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
108 String expectedPrefix = "t2";
109 Date expectedRevision = TestUtils.createDate("2013-02-27");
111 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
112 // test SchemaNode args
113 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "interfaces");
114 assertEquals(expectedQName, interfaces.getQName());
115 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
117 assertEquals(expectedPath, interfaces.getPath());
118 assertNull(interfaces.getDescription());
119 assertNull(interfaces.getReference());
120 assertEquals(Status.CURRENT, interfaces.getStatus());
121 assertEquals(0, interfaces.getUnknownSchemaNodes().size());
122 // test DataSchemaNode args
123 assertFalse(interfaces.isAugmenting());
124 assertFalse(interfaces.isConfiguration());
125 ConstraintDefinition constraints = interfaces.getConstraints();
126 assertNull(constraints.getWhenCondition());
127 assertEquals(0, constraints.getMustConstraints().size());
128 assertFalse(constraints.isMandatory());
129 assertNull(constraints.getMinElements());
130 assertNull(constraints.getMaxElements());
131 // test AugmentationTarget args
132 assertEquals(0, interfaces.getAvailableAugmentations().size());
133 // test ContainerSchemaNode args
134 assertFalse(interfaces.isPresenceContainer());
135 // test DataNodeContainer args
136 assertEquals(0, interfaces.getTypeDefinitions().size());
137 assertEquals(1, interfaces.getChildNodes().size());
138 assertEquals(0, interfaces.getGroupings().size());
139 assertEquals(0, interfaces.getUses().size());
141 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
142 assertNotNull(ifEntry);
146 public void testParseList() {
147 Module test = TestUtils.findModule(modules, "types2");
148 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
149 String expectedPrefix = "t2";
150 Date expectedRevision = TestUtils.createDate("2013-02-27");
152 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
154 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
155 // test SchemaNode args
156 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "ifEntry");
157 assertEquals(expectedQName, ifEntry.getQName());
158 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
159 "interfaces", "ifEntry");
160 assertEquals(expectedPath, ifEntry.getPath());
161 assertNull(ifEntry.getDescription());
162 assertNull(ifEntry.getReference());
163 assertEquals(Status.CURRENT, ifEntry.getStatus());
164 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
165 // test DataSchemaNode args
166 assertFalse(ifEntry.isAugmenting());
167 assertFalse(ifEntry.isConfiguration());
168 ConstraintDefinition constraints = ifEntry.getConstraints();
169 assertNull(constraints.getWhenCondition());
170 assertEquals(0, constraints.getMustConstraints().size());
171 assertFalse(constraints.isMandatory());
172 assertEquals(1, (int) constraints.getMinElements());
173 assertEquals(11, (int) constraints.getMaxElements());
174 // test AugmentationTarget args
175 Set<AugmentationSchema> availableAugmentations = ifEntry.getAvailableAugmentations();
176 assertEquals(2, availableAugmentations.size());
177 // test ListSchemaNode args
178 List<QName> expectedKey = new ArrayList<QName>();
179 expectedKey.add(new QName(expectedNamespace, expectedRevision, expectedPrefix, "ifIndex"));
180 assertEquals(expectedKey, ifEntry.getKeyDefinition());
181 assertFalse(ifEntry.isUserOrdered());
182 // test DataNodeContainer args
183 assertEquals(0, ifEntry.getTypeDefinitions().size());
184 assertEquals(4, ifEntry.getChildNodes().size());
185 assertEquals(0, ifEntry.getGroupings().size());
186 assertEquals(0, ifEntry.getUses().size());
188 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
189 assertTrue(ifIndex.getType() instanceof Uint32);
190 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
191 assertTrue(ifMtu.getType() instanceof Int32);
195 public void testParseLeaf() throws ParseException {
196 Module test = TestUtils.findModule(modules, "types2");
199 LeafSchemaNode ifName = (LeafSchemaNode) test.getDataChildByName("if-name");
200 Leafref ifNameType = (Leafref) ifName.getType();
201 QName qname = ifNameType.getQName();
203 URI baseYangTypeNS = URI.create("urn:ietf:params:xml:ns:yang:1");
204 assertEquals(baseYangTypeNS, qname.getNamespace());
205 assertNull(qname.getRevision());
206 assertEquals("", qname.getPrefix());
207 assertEquals("leafref", qname.getLocalName());
210 LeafSchemaNode name = (LeafSchemaNode) test.getDataChildByName("name");
211 StringType nameType = (StringType) name.getType();
212 QName nameQName = nameType.getQName();
214 assertEquals(baseYangTypeNS, nameQName.getNamespace());
215 assertNull(nameQName.getRevision());
216 assertEquals("", nameQName.getPrefix());
217 assertEquals("string", nameQName.getLocalName());
220 LeafSchemaNode count = (LeafSchemaNode) test.getDataChildByName("count");
221 ExtendedType countType = (ExtendedType) count.getType();
222 QName countTypeQName = countType.getQName();
224 URI expectedNS = URI.create("urn:simple.types.data.demo");
225 Date expectedDate = simpleDateFormat.parse("2013-02-27");
226 assertEquals(expectedNS, countTypeQName.getNamespace());
227 assertEquals(expectedDate, countTypeQName.getRevision());
228 assertEquals("t2", countTypeQName.getPrefix());
229 assertEquals("int8", countTypeQName.getLocalName());
231 Int8 countTypeBase = (Int8) countType.getBaseType();
232 QName countTypeBaseQName = countTypeBase.getQName();
234 assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace());
235 assertNull(countTypeBaseQName.getRevision());
236 assertEquals("", countTypeBaseQName.getPrefix());
237 assertEquals("int8", countTypeBaseQName.getLocalName());
241 public void testAugmentResolving() {
243 Module module1 = TestUtils.findModule(modules, "types1");
245 Set<AugmentationSchema> module1Augmentations = module1.getAugmentations();
246 AugmentationSchema augment1 = module1Augmentations.iterator().next();
247 LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1.getDataChildByName("ds0ChannelNumber");
248 assertTrue(augmentedLeafDefinition.isAugmenting());
251 Module module2 = TestUtils.findModule(modules, "types2");
253 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
254 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
255 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
257 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas");
258 LeafSchemaNode linkleaf = (LeafSchemaNode) schemas.getDataChildByName("linkleaf");
259 assertNotNull(linkleaf);
261 // augmentation defined in testfile1 and augmentation returned from
262 // augmented container have to be same
263 Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer.getAvailableAugmentations();
264 AugmentationSchema augmentDefinition = augmentedContainerAugments.iterator().next();
265 assertEquals(augment1, augmentDefinition);
267 LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber");
268 assertTrue(augmentedLeaf.isAugmenting());
269 assertEquals(augmentedLeafDefinition, augmentedLeaf);
271 Set<AugmentationSchema> ifEntryAugments = ifEntry.getAvailableAugmentations();
272 assertEquals(2, ifEntryAugments.size());
275 Module module3 = TestUtils.findModule(modules, "types3");
277 Set<AugmentationSchema> module3Augmentations = module3.getAugmentations();
278 assertEquals(3, module3Augmentations.size());
279 AugmentationSchema augment3 = null;
280 for (AugmentationSchema as : module3Augmentations) {
281 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
285 ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
286 .getDataChildByName("augment-holder");
287 assertTrue(augmentedContainerDefinition.isAugmenting());
290 assertEquals(augmentedContainer, augmentedContainerDefinition);
291 assertEquals(augmentedLeaf, augmentedLeafDefinition);
295 public void testAugmentTarget() {
296 Module test = TestUtils.findModule(modules, "types2");
298 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
299 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
300 Set<AugmentationSchema> augmentations = ifEntry.getAvailableAugmentations();
301 assertEquals(2, augmentations.size());
303 AugmentationSchema augment = null;
304 for (AugmentationSchema as : augmentations) {
305 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
309 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment.getDataChildByName("augment-holder");
310 assertNotNull(augmentHolder);
311 assertTrue(augmentHolder.isAugmenting());
312 QName augmentHolderQName = augmentHolder.getQName();
313 assertEquals("augment-holder", augmentHolderQName.getLocalName());
314 assertEquals("t3", augmentHolderQName.getPrefix());
315 assertEquals("Description for augment holder", augmentHolder.getDescription());
319 public void testTypedefRangesResolving() throws ParseException {
320 Module testModule = TestUtils.findModule(modules, "types1");
322 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("testleaf");
323 ExtendedType leafType = (ExtendedType) testleaf.getType();
324 QName leafTypeQName = leafType.getQName();
325 assertEquals("my-type1", leafTypeQName.getLocalName());
326 assertEquals("t1", leafTypeQName.getPrefix());
327 assertEquals(URI.create("urn:simple.container.demo"), leafTypeQName.getNamespace());
328 Date expectedDate = simpleDateFormat.parse("2013-02-27");
329 assertEquals(expectedDate, leafTypeQName.getRevision());
330 assertEquals(1, leafType.getRanges().size());
332 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
333 QName baseTypeQName = baseType.getQName();
334 assertEquals("my-type1", baseTypeQName.getLocalName());
335 assertEquals("t2", baseTypeQName.getPrefix());
336 assertEquals(URI.create("urn:simple.types.data.demo"), baseTypeQName.getNamespace());
337 assertEquals(expectedDate, baseTypeQName.getRevision());
338 assertEquals(2, baseType.getRanges().size());
340 List<RangeConstraint> ranges = leafType.getRanges();
341 assertEquals(1, ranges.size());
342 RangeConstraint range = ranges.get(0);
343 assertEquals(12L, range.getMin());
344 assertEquals(20L, range.getMax());
348 public void testTypedefPatternsResolving() {
349 Module testModule = TestUtils.findModule(modules, "types1");
351 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-string-leaf");
352 ExtendedType testleafType = (ExtendedType) testleaf.getType();
353 QName testleafTypeQName = testleafType.getQName();
354 assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
355 assertEquals("t2", testleafTypeQName.getPrefix());
357 List<PatternConstraint> patterns = testleafType.getPatterns();
358 assertEquals(1, patterns.size());
359 PatternConstraint pattern = patterns.iterator().next();
360 assertEquals("[e-z]*", pattern.getRegularExpression());
362 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
363 assertEquals("my-string-type2", baseType.getQName().getLocalName());
365 patterns = baseType.getPatterns();
366 assertEquals(1, patterns.size());
367 pattern = patterns.iterator().next();
368 assertEquals("[b-u]*", pattern.getRegularExpression());
370 List<LengthConstraint> lengths = testleafType.getLengths();
371 assertTrue(lengths.isEmpty());
375 public void testTypedefLengthsResolving() {
376 Module testModule = TestUtils.findModule(modules, "types1");
378 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("leaf-with-length");
379 ExtendedType testleafType = (ExtendedType) testleaf.getType();
380 assertEquals("my-string-type", testleafType.getQName().getLocalName());
382 List<LengthConstraint> lengths = testleafType.getLengths();
383 assertEquals(1, lengths.size());
385 LengthConstraint length = lengths.get(0);
386 assertEquals(7L, length.getMin());
387 assertEquals(10L, length.getMax());
391 public void testTypeDef() {
392 Module testModule = TestUtils.findModule(modules, "types2");
394 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("nested-type-leaf");
395 ExtendedType testleafType = (ExtendedType) testleaf.getType();
396 assertEquals("my-type1", testleafType.getQName().getLocalName());
398 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
399 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
401 Int32 int32Type = (Int32) baseType.getBaseType();
402 QName qname = int32Type.getQName();
403 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), qname.getNamespace());
404 assertNull(qname.getRevision());
405 assertEquals("", qname.getPrefix());
406 assertEquals("int32", qname.getLocalName());
407 List<RangeConstraint> ranges = baseType.getRanges();
408 assertEquals(1, ranges.size());
409 RangeConstraint range = ranges.get(0);
410 assertEquals(2L, range.getMin());
411 assertEquals(20L, range.getMax());
415 public void testTypedefDecimal1() {
416 Module testModule = TestUtils.findModule(modules, "types1");
418 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-decimal-leaf");
419 ExtendedType type = (ExtendedType) testleaf.getType();
420 assertEquals(4, (int) type.getFractionDigits());
422 ExtendedType typeBase = (ExtendedType) type.getBaseType();
423 assertEquals("my-decimal-type", typeBase.getQName().getLocalName());
424 assertNull(typeBase.getFractionDigits());
426 Decimal64 decimal = (Decimal64) typeBase.getBaseType();
427 assertEquals(6, (int) decimal.getFractionDigits());
431 public void testTypedefDecimal2() {
432 Module testModule = TestUtils.findModule(modules, "types1");
434 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-decimal-leaf2");
435 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
436 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
437 Decimal64 baseTypeCast = (Decimal64) baseType;
438 assertEquals(5, (int) baseTypeCast.getFractionDigits());
442 public void testTypedefUnion() {
443 Module testModule = TestUtils.findModule(modules, "types1");
445 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("union-leaf");
446 ExtendedType testleafType = (ExtendedType) testleaf.getType();
447 assertEquals("my-union-ext", testleafType.getQName().getLocalName());
449 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
450 assertEquals("my-union", baseType.getQName().getLocalName());
452 UnionType unionBase = (UnionType) baseType.getBaseType();
454 List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
455 ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
456 List<RangeConstraint> ranges = unionType1.getRanges();
457 assertEquals(1, ranges.size());
458 RangeConstraint range = ranges.get(0);
459 assertEquals(1L, range.getMin());
460 assertEquals(100L, range.getMax());
462 assertTrue(unionType1.getBaseType() instanceof Int16);
463 assertTrue(unionTypes.get(1) instanceof Int32);
467 public void testNestedUnionResolving1() {
468 Module testModule = TestUtils.findModule(modules, "types1");
470 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("nested-union-leaf");
472 ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
473 assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
475 ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
476 assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
478 UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
479 List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
480 assertEquals(2, unionTypes.size());
481 assertTrue(unionTypes.get(0) instanceof StringType);
482 assertTrue(unionTypes.get(1) instanceof ExtendedType);
484 ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
485 ExtendedType extendedUnionBase = (ExtendedType) extendedUnion.getBaseType();
486 assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
488 UnionType extendedTargetUnion = (UnionType) extendedUnionBase.getBaseType();
489 List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion.getTypes();
490 assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16);
491 assertTrue(extendedTargetTypes.get(1) instanceof Int32);
493 ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0);
494 assertTrue(int16.getBaseType() instanceof Int16);
495 List<RangeConstraint> ranges = int16.getRanges();
496 assertEquals(1, ranges.size());
497 RangeConstraint range = ranges.get(0);
498 assertEquals(1L, range.getMin());
499 assertEquals(100L, range.getMax());
503 public void testNestedUnionResolving2() {
504 Module testModule = TestUtils.findModule(modules, "types1");
506 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("custom-union-leaf");
508 ExtendedType testleafType = (ExtendedType) testleaf.getType();
509 QName testleafTypeQName = testleafType.getQName();
510 assertEquals(URI.create("urn:simple.container.demo.test"), testleafTypeQName.getNamespace());
511 assertEquals(TestUtils.createDate("2013-02-27"), testleafTypeQName.getRevision());
512 assertEquals("t3", testleafTypeQName.getPrefix());
513 assertEquals("union1", testleafTypeQName.getLocalName());
515 ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
516 QName union2QName = union2.getQName();
517 assertEquals(URI.create("urn:simple.container.demo.test"), union2QName.getNamespace());
518 assertEquals(TestUtils.createDate("2013-02-27"), union2QName.getRevision());
519 assertEquals("t3", union2QName.getPrefix());
520 assertEquals("union2", union2QName.getLocalName());
522 UnionType union2Base = (UnionType) union2.getBaseType();
523 List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
524 assertEquals(2, unionTypes.size());
525 assertTrue(unionTypes.get(0) instanceof Int32);
526 assertTrue(unionTypes.get(1) instanceof ExtendedType);
528 ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
529 QName nestedUnion2QName = nestedUnion2.getQName();
530 assertEquals(URI.create("urn:simple.types.data.demo"), nestedUnion2QName.getNamespace());
531 assertEquals(TestUtils.createDate("2013-02-27"), nestedUnion2QName.getRevision());
532 assertEquals("t2", nestedUnion2QName.getPrefix());
533 assertEquals("nested-union2", nestedUnion2QName.getLocalName());
535 UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
536 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
537 assertEquals(2, nestedUnion2Types.size());
538 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
539 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
541 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
542 QName myUnionExtQName = myUnionExt.getQName();
543 assertEquals(URI.create("urn:simple.types.data.demo"), myUnionExtQName.getNamespace());
544 assertEquals(TestUtils.createDate("2013-02-27"), myUnionExtQName.getRevision());
545 assertEquals("t2", myUnionExtQName.getPrefix());
546 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
548 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
549 QName myUnionQName = myUnion.getQName();
550 assertEquals(URI.create("urn:simple.types.data.demo"), myUnionQName.getNamespace());
551 assertEquals(TestUtils.createDate("2013-02-27"), myUnionQName.getRevision());
552 assertEquals("t2", myUnionQName.getPrefix());
553 assertEquals("my-union", myUnionQName.getLocalName());
555 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
556 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
557 assertEquals(2, myUnionBaseTypes.size());
558 assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16);
559 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
560 ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0);
561 List<RangeConstraint> ranges = int16.getRanges();
562 assertEquals(1, ranges.size());
563 RangeConstraint range = ranges.get(0);
564 assertEquals(1L, range.getMin());
565 assertEquals(100L, range.getMax());
569 public void testRefine() {
570 Module testModule = TestUtils.findModule(modules, "types2");
572 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("peer");
573 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
574 Set<UsesNode> usesNodes = destination.getUses();
575 assertEquals(1, usesNodes.size());
576 UsesNode usesNode = usesNodes.iterator().next();
577 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
578 assertEquals(5, refines.size());
580 LeafSchemaNode refineLeaf = null;
581 ContainerSchemaNode refineContainer = null;
582 ListSchemaNode refineList = null;
583 GroupingDefinition refineGrouping = null;
584 TypeDefinition<?> typedef = null;
585 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
586 SchemaNode value = entry.getValue();
587 if (value instanceof LeafSchemaNode) {
588 refineLeaf = (LeafSchemaNode) value;
589 } else if (value instanceof ContainerSchemaNode) {
590 refineContainer = (ContainerSchemaNode) value;
591 } else if (value instanceof ListSchemaNode) {
592 refineList = (ListSchemaNode) value;
593 } else if (value instanceof GroupingDefinition) {
594 refineGrouping = (GroupingDefinition) value;
595 } else if (value instanceof TypeDefinition<?>) {
596 typedef = (TypeDefinition<?>) value;
601 assertNotNull(refineLeaf);
602 assertEquals("address", refineLeaf.getQName().getLocalName());
603 assertEquals("description of address defined by refine", refineLeaf.getDescription());
604 assertEquals("address reference added by refine", refineLeaf.getReference());
605 assertFalse(refineLeaf.isConfiguration());
606 assertTrue(refineLeaf.getConstraints().isMandatory());
607 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
608 assertEquals(1, leafMustConstraints.size());
609 MustDefinition leafMust = leafMustConstraints.iterator().next();
610 assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
613 assertNotNull(refineContainer);
614 Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
615 assertTrue(mustConstraints.isEmpty());
616 assertEquals("description of port defined by refine", refineContainer.getDescription());
617 assertEquals("port reference added by refine", refineContainer.getReference());
618 assertFalse(refineContainer.isConfiguration());
619 assertTrue(refineContainer.isPresenceContainer());
622 assertNotNull(refineList);
623 assertEquals("description of addresses defined by refine", refineList.getDescription());
624 assertEquals("addresses reference added by refine", refineList.getReference());
625 assertFalse(refineList.isConfiguration());
626 assertEquals(2, (int) refineList.getConstraints().getMinElements());
627 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
629 // grouping target-inner
630 assertNotNull(refineGrouping);
631 Set<DataSchemaNode> refineGroupingChildren = refineGrouping.getChildNodes();
632 assertEquals(1, refineGroupingChildren.size());
633 LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren.iterator().next();
634 assertEquals("inner-grouping-id", refineGroupingLeaf.getQName().getLocalName());
635 assertEquals("new target-inner grouping description", refineGrouping.getDescription());
637 // typedef group-type
638 assertNotNull(typedef);
639 assertEquals("new group-type description", typedef.getDescription());
640 assertEquals("new group-type reference", typedef.getReference());
641 assertTrue(typedef.getBaseType() instanceof ExtendedType);
645 public void testChoice() {
646 Module testModule = TestUtils.findModule(modules, "types1");
647 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("transfer");
648 ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
649 Set<ChoiceCaseNode> cases = how.getCases();
650 assertEquals(3, cases.size());
654 public void testAnyXml() {
655 Module testModule = TestUtils.findModule(modules, "types1");
656 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
661 public void testDeviation() {
662 Module testModule = TestUtils.findModule(modules, "types1");
663 Set<Deviation> deviations = testModule.getDeviations();
664 assertEquals(1, deviations.size());
666 Deviation dev = deviations.iterator().next();
667 SchemaPath expectedPath = TestUtils.createPath(true, null, null, "data", "system", "user");
668 assertEquals(expectedPath, dev.getTargetPath());
669 assertEquals(Deviate.ADD, dev.getDeviate());
673 public void testUnknownNode() {
674 Module testModule = TestUtils.findModule(modules, "types3");
675 ContainerSchemaNode network = (ContainerSchemaNode) testModule.getDataChildByName("network");
676 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
677 assertEquals(1, unknownNodes.size());
678 UnknownSchemaNode unknownNode = unknownNodes.get(0);
679 assertNotNull(unknownNode.getNodeType());
680 assertEquals("point", unknownNode.getNodeParameter());
684 public void testFeature() {
685 Module testModule = TestUtils.findModule(modules, "types3");
686 Set<FeatureDefinition> features = testModule.getFeatures();
687 assertEquals(1, features.size());
691 public void testExtension() {
692 Module testModule = TestUtils.findModule(modules, "types3");
693 List<ExtensionDefinition> extensions = testModule.getExtensionSchemaNodes();
694 assertEquals(1, extensions.size());
695 ExtensionDefinition extension = extensions.get(0);
696 assertEquals("name", extension.getArgument());
697 assertFalse(extension.isYinElement());
701 public void testNotification() {
702 Module testModule = TestUtils.findModule(modules, "types3");
703 URI expectedNamespace = URI.create("urn:simple.container.demo.test");
704 String expectedPrefix = "t3";
705 Date expectedRevision = TestUtils.createDate("2013-02-27");
707 Set<NotificationDefinition> notifications = testModule.getNotifications();
708 assertEquals(1, notifications.size());
710 NotificationDefinition notification = notifications.iterator().next();
711 // test SchemaNode args
712 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "event");
713 assertEquals(expectedQName, notification.getQName());
714 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
716 assertEquals(expectedPath, notification.getPath());
717 assertNull(notification.getDescription());
718 assertNull(notification.getReference());
719 assertEquals(Status.CURRENT, notification.getStatus());
720 assertEquals(0, notification.getUnknownSchemaNodes().size());
721 // test DataNodeContainer args
722 assertEquals(0, notification.getTypeDefinitions().size());
723 assertEquals(3, notification.getChildNodes().size());
724 assertEquals(0, notification.getGroupings().size());
725 assertEquals(0, notification.getUses().size());
727 LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
728 assertTrue(eventClass.getType() instanceof StringType);
729 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity");
730 assertNotNull(reportingEntity);
731 LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
732 assertTrue(severity.getType() instanceof StringType);
736 public void testRpc() {
737 Module testModule = TestUtils.findModule(modules, "types3");
739 Set<RpcDefinition> rpcs = testModule.getRpcs();
740 assertEquals(1, rpcs.size());
742 RpcDefinition rpc = rpcs.iterator().next();
743 assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
744 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
746 ContainerSchemaNode input = rpc.getInput();
747 assertNotNull(input.getDataChildByName("source"));
748 assertNotNull(input.getDataChildByName("filter"));
749 ContainerSchemaNode output = rpc.getOutput();
750 assertNotNull(output.getDataChildByName("data"));
754 public void testGrouping() {
755 Module testModule = TestUtils.findModule(modules, "types2");
756 Set<GroupingDefinition> groupings = testModule.getGroupings();
757 assertEquals(1, groupings.size());
758 GroupingDefinition grouping = groupings.iterator().next();
759 Set<DataSchemaNode> children = grouping.getChildNodes();
760 assertEquals(5, children.size());
764 public void testAugmentNodesTypesSchemaPath() throws Exception {
765 Module testModule = TestUtils.findModule(modules, "types1");
766 Set<AugmentationSchema> augments = testModule.getAugmentations();
767 assertEquals(1, augments.size());
768 AugmentationSchema augment = augments.iterator().next();
770 LeafSchemaNode ifcId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
771 Leafref ifcIdType = (Leafref) ifcId.getType();
772 SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
773 List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
775 URI types1URI = URI.create("urn:simple.container.demo");
776 URI types2URI = URI.create("urn:simple.types.data.demo");
777 URI types3URI = URI.create("urn:simple.container.demo.test");
778 Date expectedDate = simpleDateFormat.parse("2013-02-27");
780 QName q0 = new QName(types2URI, expectedDate, "data", "interfaces");
781 QName q1 = new QName(types2URI, expectedDate, "data", "ifEntry");
782 QName q2 = new QName(types3URI, expectedDate, "data", "augment-holder");
783 QName q3 = new QName(types1URI, expectedDate, "data", "interface-id");
784 assertEquals(q0, ifcIdTypePath.get(0));
785 assertEquals(q1, ifcIdTypePath.get(1));
786 assertEquals(q2, ifcIdTypePath.get(2));
787 assertEquals(q3, ifcIdTypePath.get(3));
789 LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment.getDataChildByName("higher-layer-if");
790 Leafref higherLayerType = (Leafref) higherLayer.getType();
791 SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
792 List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
793 assertEquals(q0, higherLayerTypePath.get(0));
794 assertEquals(q1, higherLayerTypePath.get(1));
795 assertEquals(q2, higherLayerTypePath.get(2));
796 q3 = new QName(types1URI, expectedDate, "data", "higher-layer-if");
797 assertEquals(q3, higherLayerTypePath.get(3));
799 LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
800 ExtendedType leafType = (ExtendedType) myType.getType();
802 testModule = TestUtils.findModule(modules, "types2");
803 TypeDefinition<?> typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "my-type1");
805 assertEquals(typedef, leafType);
809 public void testTypePath() throws ParseException {
810 Module test = TestUtils.findModule(modules, "types2");
811 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
813 // my-base-int32-type
814 ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types, "my-base-int32-type");
815 QName int32TypedefQName = int32Typedef.getQName();
817 URI expectedNS = URI.create("urn:simple.types.data.demo");
818 Date expectedDate = simpleDateFormat.parse("2013-02-27");
819 assertEquals(expectedNS, int32TypedefQName.getNamespace());
820 assertEquals(expectedDate, int32TypedefQName.getRevision());
821 assertEquals("t2", int32TypedefQName.getPrefix());
822 assertEquals("my-base-int32-type", int32TypedefQName.getLocalName());
824 SchemaPath typeSchemaPath = int32Typedef.getPath();
825 List<QName> typePath = typeSchemaPath.getPath();
826 assertEquals(1, typePath.size());
827 assertEquals(int32TypedefQName, typePath.get(0));
829 // my-base-int32-type/int32
830 Int32 int32 = (Int32) int32Typedef.getBaseType();
831 QName int32QName = int32.getQName();
832 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), int32QName.getNamespace());
833 assertNull(int32QName.getRevision());
834 assertEquals("", int32QName.getPrefix());
835 assertEquals("int32", int32QName.getLocalName());
837 SchemaPath int32SchemaPath = int32.getPath();
838 List<QName> int32Path = int32SchemaPath.getPath();
839 assertEquals(3, int32Path.size());
840 assertEquals(int32TypedefQName, int32Path.get(0));
841 assertEquals(int32QName, int32Path.get(2));
845 public void testTypePath2() throws ParseException {
846 Module test = TestUtils.findModule(modules, "types2");
847 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
849 // my-base-int32-type
850 ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
851 QName myDecTypeQName = myDecType.getQName();
853 URI expectedNS = URI.create("urn:simple.types.data.demo");
854 Date expectedDate = simpleDateFormat.parse("2013-02-27");
855 assertEquals(expectedNS, myDecTypeQName.getNamespace());
856 assertEquals(expectedDate, myDecTypeQName.getRevision());
857 assertEquals("t2", myDecTypeQName.getPrefix());
858 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
860 SchemaPath typeSchemaPath = myDecType.getPath();
861 List<QName> typePath = typeSchemaPath.getPath();
862 assertEquals(1, typePath.size());
863 assertEquals(myDecTypeQName, typePath.get(0));
865 // my-base-int32-type/int32
866 Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
867 QName dec64QName = dec64.getQName();
869 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
870 assertNull(dec64QName.getRevision());
871 assertEquals("", dec64QName.getPrefix());
872 assertEquals("decimal64", dec64QName.getLocalName());
874 SchemaPath dec64SchemaPath = dec64.getPath();
875 List<QName> dec64Path = dec64SchemaPath.getPath();
876 assertEquals(2, dec64Path.size());
877 assertEquals(myDecTypeQName, dec64Path.get(0));
878 assertEquals(dec64QName, dec64Path.get(1));