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;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.yang.common.QName;
25 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
27 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
28 import org.opendaylight.controller.yang.model.api.ChoiceNode;
29 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
30 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.controller.yang.model.api.Deviation;
32 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
33 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
34 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
35 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
36 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
37 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
38 import org.opendaylight.controller.yang.model.api.Module;
39 import org.opendaylight.controller.yang.model.api.ModuleImport;
40 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
41 import org.opendaylight.controller.yang.model.api.RpcDefinition;
42 import org.opendaylight.controller.yang.model.api.SchemaPath;
43 import org.opendaylight.controller.yang.model.api.Status;
44 import org.opendaylight.controller.yang.model.api.TypeDefinition;
45 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
46 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
47 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
48 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
49 import org.opendaylight.controller.yang.model.util.Decimal64;
50 import org.opendaylight.controller.yang.model.util.ExtendedType;
51 import org.opendaylight.controller.yang.model.util.Int16;
52 import org.opendaylight.controller.yang.model.util.Int32;
53 import org.opendaylight.controller.yang.model.util.Int8;
54 import org.opendaylight.controller.yang.model.util.Leafref;
55 import org.opendaylight.controller.yang.model.util.StringType;
56 import org.opendaylight.controller.yang.model.util.Uint32;
57 import org.opendaylight.controller.yang.model.util.UnionType;
59 public class YangParserTest {
60 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
61 private Set<Module> modules;
64 public void init() throws FileNotFoundException {
65 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
66 assertEquals(3, modules.size());
70 public void testHeaders() {
71 Module test = TestUtils.findModule(modules, "types1");
73 assertEquals("types1", test.getName());
74 assertEquals("1", test.getYangVersion());
75 URI expectedNamespace = URI.create("urn:simple.container.demo");
76 assertEquals(expectedNamespace, test.getNamespace());
77 assertEquals("t1", test.getPrefix());
79 Set<ModuleImport> imports = test.getImports();
80 assertEquals(2, imports.size());
82 ModuleImport import2 = TestUtils.findImport(imports, "data");
83 assertEquals("types2", import2.getModuleName());
84 assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision());
86 ModuleImport import3 = TestUtils.findImport(imports, "t3");
87 assertEquals("types3", import3.getModuleName());
88 assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision());
90 assertEquals("opendaylight", test.getOrganization());
91 assertEquals("http://www.opendaylight.org/", test.getContact());
92 Date expectedRevision = TestUtils.createDate("2013-02-27");
93 assertEquals(expectedRevision, test.getRevision());
94 assertEquals(" WILL BE DEFINED LATER", test.getReference());
98 public void testParseContainer() {
99 Module test = TestUtils.findModule(modules, "types2");
100 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
101 String expectedPrefix = "t2";
102 Date expectedRevision = TestUtils.createDate("2013-02-27");
104 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
105 // test SchemaNode args
106 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "interfaces");
107 assertEquals(expectedQName, interfaces.getQName());
108 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
110 assertEquals(expectedPath, interfaces.getPath());
111 assertNull(interfaces.getDescription());
112 assertNull(interfaces.getReference());
113 assertEquals(Status.CURRENT, interfaces.getStatus());
114 assertEquals(0, interfaces.getUnknownSchemaNodes().size());
115 // test DataSchemaNode args
116 assertFalse(interfaces.isAugmenting());
117 assertTrue(interfaces.isConfiguration());
118 ConstraintDefinition constraints = interfaces.getConstraints();
119 assertNull(constraints.getWhenCondition());
120 assertEquals(0, constraints.getMustConstraints().size());
121 assertFalse(constraints.isMandatory());
122 assertNull(constraints.getMinElements());
123 assertNull(constraints.getMaxElements());
124 // test AugmentationTarget args
125 assertEquals(0, interfaces.getAvailableAugmentations().size());
126 // test ContainerSchemaNode args
127 assertFalse(interfaces.isPresenceContainer());
128 // test DataNodeContainer args
129 assertEquals(0, interfaces.getTypeDefinitions().size());
130 assertEquals(1, interfaces.getChildNodes().size());
131 assertEquals(0, interfaces.getGroupings().size());
132 assertEquals(0, interfaces.getUses().size());
134 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
135 assertNotNull(ifEntry);
139 public void testParseList() {
140 Module test = TestUtils.findModule(modules, "types2");
141 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
142 String expectedPrefix = "t2";
143 Date expectedRevision = TestUtils.createDate("2013-02-27");
145 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
147 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
148 // test SchemaNode args
149 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "ifEntry");
150 assertEquals(expectedQName, ifEntry.getQName());
151 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
152 "interfaces", "ifEntry");
153 assertEquals(expectedPath, ifEntry.getPath());
154 assertNull(ifEntry.getDescription());
155 assertNull(ifEntry.getReference());
156 assertEquals(Status.CURRENT, ifEntry.getStatus());
157 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
158 // test DataSchemaNode args
159 assertFalse(ifEntry.isAugmenting());
160 assertTrue(ifEntry.isConfiguration());
161 ConstraintDefinition constraints = ifEntry.getConstraints();
162 assertNull(constraints.getWhenCondition());
163 assertEquals(0, constraints.getMustConstraints().size());
164 assertFalse(constraints.isMandatory());
165 assertEquals(1, (int) constraints.getMinElements());
166 assertEquals(11, (int) constraints.getMaxElements());
167 // test AugmentationTarget args
168 Set<AugmentationSchema> availableAugmentations = ifEntry.getAvailableAugmentations();
169 assertEquals(2, availableAugmentations.size());
170 // test ListSchemaNode args
171 List<QName> expectedKey = new ArrayList<QName>();
172 expectedKey.add(new QName(expectedNamespace, expectedRevision, expectedPrefix, "ifIndex"));
173 assertEquals(expectedKey, ifEntry.getKeyDefinition());
174 assertFalse(ifEntry.isUserOrdered());
175 // test DataNodeContainer args
176 assertEquals(0, ifEntry.getTypeDefinitions().size());
177 assertEquals(4, ifEntry.getChildNodes().size());
178 assertEquals(0, ifEntry.getGroupings().size());
179 assertEquals(0, ifEntry.getUses().size());
181 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
182 assertTrue(ifIndex.getType() instanceof Uint32);
183 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
184 assertTrue(ifMtu.getType() instanceof Int32);
188 public void testParseLeaf() throws ParseException {
189 Module test = TestUtils.findModule(modules, "types2");
192 LeafSchemaNode ifName = (LeafSchemaNode) test.getDataChildByName("if-name");
193 Leafref ifNameType = (Leafref) ifName.getType();
194 QName qname = ifNameType.getQName();
196 URI baseYangTypeNS = URI.create("urn:ietf:params:xml:ns:yang:1");
197 assertEquals(baseYangTypeNS, qname.getNamespace());
198 assertNull(qname.getRevision());
199 assertEquals("", qname.getPrefix());
200 assertEquals("leafref", qname.getLocalName());
203 LeafSchemaNode name = (LeafSchemaNode) test.getDataChildByName("name");
204 StringType nameType = (StringType) name.getType();
205 QName nameQName = nameType.getQName();
207 assertEquals(baseYangTypeNS, nameQName.getNamespace());
208 assertNull(nameQName.getRevision());
209 assertEquals("", nameQName.getPrefix());
210 assertEquals("string", nameQName.getLocalName());
213 LeafSchemaNode count = (LeafSchemaNode) test.getDataChildByName("count");
214 ExtendedType countType = (ExtendedType) count.getType();
215 QName countTypeQName = countType.getQName();
217 URI expectedNS = URI.create("urn:simple.types.data.demo");
218 Date expectedDate = simpleDateFormat.parse("2013-02-27");
219 assertEquals(expectedNS, countTypeQName.getNamespace());
220 assertEquals(expectedDate, countTypeQName.getRevision());
221 assertEquals("t2", countTypeQName.getPrefix());
222 assertEquals("int8", countTypeQName.getLocalName());
224 Int8 countTypeBase = (Int8) countType.getBaseType();
225 QName countTypeBaseQName = countTypeBase.getQName();
227 assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace());
228 assertNull(countTypeBaseQName.getRevision());
229 assertEquals("", countTypeBaseQName.getPrefix());
230 assertEquals("int8", countTypeBaseQName.getLocalName());
234 public void testAugmentResolving() {
236 Module module1 = TestUtils.findModule(modules, "types1");
238 Set<AugmentationSchema> module1Augmentations = module1.getAugmentations();
239 AugmentationSchema augment1 = module1Augmentations.iterator().next();
240 LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1.getDataChildByName("ds0ChannelNumber");
241 assertTrue(augmentedLeafDefinition.isAugmenting());
244 Module module2 = TestUtils.findModule(modules, "types2");
246 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
247 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
248 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
250 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas");
251 LeafSchemaNode linkleaf = (LeafSchemaNode) schemas.getDataChildByName("linkleaf");
252 assertNotNull(linkleaf);
254 // augmentation defined in testfile1 and augmentation returned from
255 // augmented container have to be same
256 Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer.getAvailableAugmentations();
257 AugmentationSchema augmentDefinition = augmentedContainerAugments.iterator().next();
258 assertEquals(augment1, augmentDefinition);
260 LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber");
261 assertTrue(augmentedLeaf.isAugmenting());
262 assertEquals(augmentedLeafDefinition, augmentedLeaf);
264 Set<AugmentationSchema> ifEntryAugments = ifEntry.getAvailableAugmentations();
265 assertEquals(2, ifEntryAugments.size());
268 Module module3 = TestUtils.findModule(modules, "types3");
270 Set<AugmentationSchema> module3Augmentations = module3.getAugmentations();
271 assertEquals(3, module3Augmentations.size());
272 AugmentationSchema augment3 = null;
273 for (AugmentationSchema as : module3Augmentations) {
274 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
278 ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
279 .getDataChildByName("augment-holder");
280 assertTrue(augmentedContainerDefinition.isAugmenting());
283 assertEquals(augmentedContainer, augmentedContainerDefinition);
284 assertEquals(augmentedLeaf, augmentedLeafDefinition);
288 public void testAugmentTarget() {
289 Module test = TestUtils.findModule(modules, "types2");
291 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
292 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
293 Set<AugmentationSchema> augmentations = ifEntry.getAvailableAugmentations();
294 assertEquals(2, augmentations.size());
296 AugmentationSchema augment = null;
297 for (AugmentationSchema as : augmentations) {
298 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
302 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment.getDataChildByName("augment-holder");
303 assertNotNull(augmentHolder);
304 assertTrue(augmentHolder.isAugmenting());
305 QName augmentHolderQName = augmentHolder.getQName();
306 assertEquals("augment-holder", augmentHolderQName.getLocalName());
307 assertEquals("t3", augmentHolderQName.getPrefix());
308 assertEquals("Description for augment holder", augmentHolder.getDescription());
312 public void testTypedefRangesResolving() throws ParseException {
313 Module testModule = TestUtils.findModule(modules, "types1");
315 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("testleaf");
316 ExtendedType leafType = (ExtendedType) testleaf.getType();
317 QName leafTypeQName = leafType.getQName();
318 assertEquals("my-type1", leafTypeQName.getLocalName());
319 assertEquals("t1", leafTypeQName.getPrefix());
320 assertEquals(URI.create("urn:simple.container.demo"), leafTypeQName.getNamespace());
321 Date expectedDate = simpleDateFormat.parse("2013-02-27");
322 assertEquals(expectedDate, leafTypeQName.getRevision());
323 assertEquals(1, leafType.getRanges().size());
325 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
326 QName baseTypeQName = baseType.getQName();
327 assertEquals("my-type1", baseTypeQName.getLocalName());
328 assertEquals("t2", baseTypeQName.getPrefix());
329 assertEquals(URI.create("urn:simple.types.data.demo"), baseTypeQName.getNamespace());
330 assertEquals(expectedDate, baseTypeQName.getRevision());
331 assertEquals(2, baseType.getRanges().size());
333 List<RangeConstraint> ranges = leafType.getRanges();
334 assertEquals(1, ranges.size());
335 RangeConstraint range = ranges.get(0);
336 assertEquals(12L, range.getMin());
337 assertEquals(20L, range.getMax());
341 public void testTypedefPatternsResolving() {
342 Module testModule = TestUtils.findModule(modules, "types1");
344 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-string-leaf");
345 ExtendedType testleafType = (ExtendedType) testleaf.getType();
346 QName testleafTypeQName = testleafType.getQName();
347 assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
348 assertEquals("t2", testleafTypeQName.getPrefix());
350 List<PatternConstraint> patterns = testleafType.getPatterns();
351 assertEquals(1, patterns.size());
352 PatternConstraint pattern = patterns.iterator().next();
353 assertEquals("[e-z]*", pattern.getRegularExpression());
355 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
356 assertEquals("my-string-type2", baseType.getQName().getLocalName());
358 patterns = baseType.getPatterns();
359 assertEquals(1, patterns.size());
360 pattern = patterns.iterator().next();
361 assertEquals("[b-u]*", pattern.getRegularExpression());
363 List<LengthConstraint> lengths = testleafType.getLengths();
364 assertTrue(lengths.isEmpty());
368 public void testTypedefLengthsResolving() {
369 Module testModule = TestUtils.findModule(modules, "types1");
371 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("leaf-with-length");
372 ExtendedType testleafType = (ExtendedType) testleaf.getType();
373 assertEquals("my-string-type", testleafType.getQName().getLocalName());
375 List<LengthConstraint> lengths = testleafType.getLengths();
376 assertEquals(1, lengths.size());
378 LengthConstraint length = lengths.get(0);
379 assertEquals(7L, length.getMin());
380 assertEquals(10L, length.getMax());
384 public void testTypeDef() {
385 Module testModule = TestUtils.findModule(modules, "types2");
387 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("nested-type-leaf");
388 ExtendedType testleafType = (ExtendedType) testleaf.getType();
389 assertEquals("my-type1", testleafType.getQName().getLocalName());
391 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
392 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
394 Int32 int32Type = (Int32) baseType.getBaseType();
395 QName qname = int32Type.getQName();
396 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), qname.getNamespace());
397 assertNull(qname.getRevision());
398 assertEquals("", qname.getPrefix());
399 assertEquals("int32", qname.getLocalName());
400 List<RangeConstraint> ranges = baseType.getRanges();
401 assertEquals(1, ranges.size());
402 RangeConstraint range = ranges.get(0);
403 assertEquals(2L, range.getMin());
404 assertEquals(20L, range.getMax());
408 public void testTypedefDecimal1() {
409 Module testModule = TestUtils.findModule(modules, "types1");
411 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-decimal-leaf");
412 ExtendedType type = (ExtendedType) testleaf.getType();
413 assertEquals(4, (int) type.getFractionDigits());
415 ExtendedType typeBase = (ExtendedType) type.getBaseType();
416 assertEquals("my-decimal-type", typeBase.getQName().getLocalName());
417 assertNull(typeBase.getFractionDigits());
419 Decimal64 decimal = (Decimal64) typeBase.getBaseType();
420 assertEquals(6, (int) decimal.getFractionDigits());
424 public void testTypedefDecimal2() {
425 Module testModule = TestUtils.findModule(modules, "types1");
427 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-decimal-leaf2");
428 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
429 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
430 Decimal64 baseTypeCast = (Decimal64) baseType;
431 assertEquals(5, (int) baseTypeCast.getFractionDigits());
435 public void testTypedefUnion() {
436 Module testModule = TestUtils.findModule(modules, "types1");
438 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("union-leaf");
439 ExtendedType testleafType = (ExtendedType) testleaf.getType();
440 assertEquals("my-union-ext", testleafType.getQName().getLocalName());
442 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
443 assertEquals("my-union", baseType.getQName().getLocalName());
445 UnionType unionBase = (UnionType) baseType.getBaseType();
447 List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
448 ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
449 List<RangeConstraint> ranges = unionType1.getRanges();
450 assertEquals(1, ranges.size());
451 RangeConstraint range = ranges.get(0);
452 assertEquals(1L, range.getMin());
453 assertEquals(100L, range.getMax());
455 assertTrue(unionType1.getBaseType() instanceof Int16);
456 assertTrue(unionTypes.get(1) instanceof Int32);
460 public void testNestedUnionResolving1() {
461 Module testModule = TestUtils.findModule(modules, "types1");
463 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("nested-union-leaf");
465 ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
466 assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
468 ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
469 assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
471 UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
472 List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
473 assertEquals(2, unionTypes.size());
474 assertTrue(unionTypes.get(0) instanceof StringType);
475 assertTrue(unionTypes.get(1) instanceof ExtendedType);
477 ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
478 ExtendedType extendedUnionBase = (ExtendedType) extendedUnion.getBaseType();
479 assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
481 UnionType extendedTargetUnion = (UnionType) extendedUnionBase.getBaseType();
482 List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion.getTypes();
483 assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16);
484 assertTrue(extendedTargetTypes.get(1) instanceof Int32);
486 ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0);
487 assertTrue(int16.getBaseType() instanceof Int16);
488 List<RangeConstraint> ranges = int16.getRanges();
489 assertEquals(1, ranges.size());
490 RangeConstraint range = ranges.get(0);
491 assertEquals(1L, range.getMin());
492 assertEquals(100L, range.getMax());
496 public void testNestedUnionResolving2() {
497 Module testModule = TestUtils.findModule(modules, "types1");
499 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("custom-union-leaf");
501 ExtendedType testleafType = (ExtendedType) testleaf.getType();
502 QName testleafTypeQName = testleafType.getQName();
503 assertEquals(URI.create("urn:simple.container.demo.test"), testleafTypeQName.getNamespace());
504 assertEquals(TestUtils.createDate("2013-02-27"), testleafTypeQName.getRevision());
505 assertEquals("t3", testleafTypeQName.getPrefix());
506 assertEquals("union1", testleafTypeQName.getLocalName());
508 ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
509 QName union2QName = union2.getQName();
510 assertEquals(URI.create("urn:simple.container.demo.test"), union2QName.getNamespace());
511 assertEquals(TestUtils.createDate("2013-02-27"), union2QName.getRevision());
512 assertEquals("t3", union2QName.getPrefix());
513 assertEquals("union2", union2QName.getLocalName());
515 UnionType union2Base = (UnionType) union2.getBaseType();
516 List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
517 assertEquals(2, unionTypes.size());
518 assertTrue(unionTypes.get(0) instanceof Int32);
519 assertTrue(unionTypes.get(1) instanceof ExtendedType);
521 ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
522 QName nestedUnion2QName = nestedUnion2.getQName();
523 assertEquals(URI.create("urn:simple.types.data.demo"), nestedUnion2QName.getNamespace());
524 assertEquals(TestUtils.createDate("2013-02-27"), nestedUnion2QName.getRevision());
525 assertEquals("t2", nestedUnion2QName.getPrefix());
526 assertEquals("nested-union2", nestedUnion2QName.getLocalName());
528 UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
529 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
530 assertEquals(2, nestedUnion2Types.size());
531 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
532 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
534 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
535 QName myUnionExtQName = myUnionExt.getQName();
536 assertEquals(URI.create("urn:simple.types.data.demo"), myUnionExtQName.getNamespace());
537 assertEquals(TestUtils.createDate("2013-02-27"), myUnionExtQName.getRevision());
538 assertEquals("t2", myUnionExtQName.getPrefix());
539 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
541 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
542 QName myUnionQName = myUnion.getQName();
543 assertEquals(URI.create("urn:simple.types.data.demo"), myUnionQName.getNamespace());
544 assertEquals(TestUtils.createDate("2013-02-27"), myUnionQName.getRevision());
545 assertEquals("t2", myUnionQName.getPrefix());
546 assertEquals("my-union", myUnionQName.getLocalName());
548 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
549 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
550 assertEquals(2, myUnionBaseTypes.size());
551 assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16);
552 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
553 ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0);
554 List<RangeConstraint> ranges = int16.getRanges();
555 assertEquals(1, ranges.size());
556 RangeConstraint range = ranges.get(0);
557 assertEquals(1L, range.getMin());
558 assertEquals(100L, range.getMax());
562 public void testChoice() {
563 Module testModule = TestUtils.findModule(modules, "types1");
564 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("transfer");
565 ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
566 Set<ChoiceCaseNode> cases = how.getCases();
567 assertEquals(5, cases.size());
568 ChoiceCaseNode input = null;
569 ChoiceCaseNode output = null;
570 for(ChoiceCaseNode caseNode : cases) {
571 if("input".equals(caseNode.getQName().getLocalName())) {
573 } else if("output".equals(caseNode.getQName().getLocalName())) {
577 assertNotNull(input);
578 assertNotNull(input.getPath());
579 assertNotNull(output);
580 assertNotNull(output.getPath());
584 public void testAnyXml() {
585 Module testModule = TestUtils.findModule(modules, "types1");
586 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
591 public void testDeviation() {
592 Module testModule = TestUtils.findModule(modules, "types1");
593 Set<Deviation> deviations = testModule.getDeviations();
594 assertEquals(1, deviations.size());
596 Deviation dev = deviations.iterator().next();
597 SchemaPath expectedPath = TestUtils.createPath(true, null, null, "data", "system", "user");
598 assertEquals(expectedPath, dev.getTargetPath());
599 assertEquals(Deviate.ADD, dev.getDeviate());
603 public void testUnknownNode() {
604 Module testModule = TestUtils.findModule(modules, "types3");
605 ContainerSchemaNode network = (ContainerSchemaNode) testModule.getDataChildByName("network");
606 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
607 assertEquals(1, unknownNodes.size());
608 UnknownSchemaNode unknownNode = unknownNodes.get(0);
609 assertNotNull(unknownNode.getNodeType());
610 assertEquals("point", unknownNode.getNodeParameter());
614 public void testFeature() {
615 Module testModule = TestUtils.findModule(modules, "types3");
616 Set<FeatureDefinition> features = testModule.getFeatures();
617 assertEquals(1, features.size());
621 public void testExtension() {
622 Module testModule = TestUtils.findModule(modules, "types3");
623 List<ExtensionDefinition> extensions = testModule.getExtensionSchemaNodes();
624 assertEquals(1, extensions.size());
625 ExtensionDefinition extension = extensions.get(0);
626 assertEquals("name", extension.getArgument());
627 assertFalse(extension.isYinElement());
631 public void testNotification() {
632 Module testModule = TestUtils.findModule(modules, "types3");
633 URI expectedNamespace = URI.create("urn:simple.container.demo.test");
634 String expectedPrefix = "t3";
635 Date expectedRevision = TestUtils.createDate("2013-02-27");
637 Set<NotificationDefinition> notifications = testModule.getNotifications();
638 assertEquals(1, notifications.size());
640 NotificationDefinition notification = notifications.iterator().next();
641 // test SchemaNode args
642 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "event");
643 assertEquals(expectedQName, notification.getQName());
644 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
646 assertEquals(expectedPath, notification.getPath());
647 assertNull(notification.getDescription());
648 assertNull(notification.getReference());
649 assertEquals(Status.CURRENT, notification.getStatus());
650 assertEquals(0, notification.getUnknownSchemaNodes().size());
651 // test DataNodeContainer args
652 assertEquals(0, notification.getTypeDefinitions().size());
653 assertEquals(3, notification.getChildNodes().size());
654 assertEquals(0, notification.getGroupings().size());
655 assertEquals(0, notification.getUses().size());
657 LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
658 assertTrue(eventClass.getType() instanceof StringType);
659 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity");
660 assertNotNull(reportingEntity);
661 LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
662 assertTrue(severity.getType() instanceof StringType);
666 public void testRpc() {
667 Module testModule = TestUtils.findModule(modules, "types3");
669 Set<RpcDefinition> rpcs = testModule.getRpcs();
670 assertEquals(1, rpcs.size());
672 RpcDefinition rpc = rpcs.iterator().next();
673 assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
674 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
676 ContainerSchemaNode input = rpc.getInput();
677 assertNotNull(input.getDataChildByName("source"));
678 assertNotNull(input.getDataChildByName("filter"));
679 ContainerSchemaNode output = rpc.getOutput();
680 assertNotNull(output.getDataChildByName("data"));
684 public void testAugmentNodesTypesSchemaPath() throws Exception {
685 Module testModule = TestUtils.findModule(modules, "types1");
686 Set<AugmentationSchema> augments = testModule.getAugmentations();
687 assertEquals(1, augments.size());
688 AugmentationSchema augment = augments.iterator().next();
690 LeafSchemaNode ifcId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
691 Leafref ifcIdType = (Leafref) ifcId.getType();
692 SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
693 List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
695 URI types1URI = URI.create("urn:simple.container.demo");
696 URI types2URI = URI.create("urn:simple.types.data.demo");
697 URI types3URI = URI.create("urn:simple.container.demo.test");
698 Date expectedDate = simpleDateFormat.parse("2013-02-27");
700 QName q0 = new QName(types2URI, expectedDate, "data", "interfaces");
701 QName q1 = new QName(types2URI, expectedDate, "data", "ifEntry");
702 QName q2 = new QName(types3URI, expectedDate, "data", "augment-holder");
703 QName q3 = new QName(types1URI, expectedDate, "data", "interface-id");
704 assertEquals(q0, ifcIdTypePath.get(0));
705 assertEquals(q1, ifcIdTypePath.get(1));
706 assertEquals(q2, ifcIdTypePath.get(2));
707 assertEquals(q3, ifcIdTypePath.get(3));
709 LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment.getDataChildByName("higher-layer-if");
710 Leafref higherLayerType = (Leafref) higherLayer.getType();
711 SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
712 List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
713 assertEquals(q0, higherLayerTypePath.get(0));
714 assertEquals(q1, higherLayerTypePath.get(1));
715 assertEquals(q2, higherLayerTypePath.get(2));
716 q3 = new QName(types1URI, expectedDate, "data", "higher-layer-if");
717 assertEquals(q3, higherLayerTypePath.get(3));
719 LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
720 ExtendedType leafType = (ExtendedType) myType.getType();
722 testModule = TestUtils.findModule(modules, "types2");
723 TypeDefinition<?> typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "my-type1");
725 assertEquals(typedef, leafType);
729 public void testTypePath() throws ParseException {
730 Module test = TestUtils.findModule(modules, "types2");
731 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
733 // my-base-int32-type
734 ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types, "my-base-int32-type");
735 QName int32TypedefQName = int32Typedef.getQName();
737 URI expectedNS = URI.create("urn:simple.types.data.demo");
738 Date expectedDate = simpleDateFormat.parse("2013-02-27");
739 assertEquals(expectedNS, int32TypedefQName.getNamespace());
740 assertEquals(expectedDate, int32TypedefQName.getRevision());
741 assertEquals("t2", int32TypedefQName.getPrefix());
742 assertEquals("my-base-int32-type", int32TypedefQName.getLocalName());
744 SchemaPath typeSchemaPath = int32Typedef.getPath();
745 List<QName> typePath = typeSchemaPath.getPath();
746 assertEquals(1, typePath.size());
747 assertEquals(int32TypedefQName, typePath.get(0));
749 // my-base-int32-type/int32
750 Int32 int32 = (Int32) int32Typedef.getBaseType();
751 QName int32QName = int32.getQName();
752 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), int32QName.getNamespace());
753 assertNull(int32QName.getRevision());
754 assertEquals("", int32QName.getPrefix());
755 assertEquals("int32", int32QName.getLocalName());
757 SchemaPath int32SchemaPath = int32.getPath();
758 List<QName> int32Path = int32SchemaPath.getPath();
759 assertEquals(3, int32Path.size());
760 assertEquals(int32TypedefQName, int32Path.get(0));
761 assertEquals(int32QName, int32Path.get(2));
765 public void testTypePath2() throws ParseException {
766 Module test = TestUtils.findModule(modules, "types2");
767 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
769 // my-base-int32-type
770 ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
771 QName myDecTypeQName = myDecType.getQName();
773 URI expectedNS = URI.create("urn:simple.types.data.demo");
774 Date expectedDate = simpleDateFormat.parse("2013-02-27");
775 assertEquals(expectedNS, myDecTypeQName.getNamespace());
776 assertEquals(expectedDate, myDecTypeQName.getRevision());
777 assertEquals("t2", myDecTypeQName.getPrefix());
778 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
780 SchemaPath typeSchemaPath = myDecType.getPath();
781 List<QName> typePath = typeSchemaPath.getPath();
782 assertEquals(1, typePath.size());
783 assertEquals(myDecTypeQName, typePath.get(0));
785 // my-base-int32-type/int32
786 Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
787 QName dec64QName = dec64.getQName();
789 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
790 assertNull(dec64QName.getRevision());
791 assertEquals("", dec64QName.getPrefix());
792 assertEquals("decimal64", dec64QName.getLocalName());
794 SchemaPath dec64SchemaPath = dec64.getPath();
795 List<QName> dec64Path = dec64SchemaPath.getPath();
796 assertEquals(2, dec64Path.size());
797 assertEquals(myDecTypeQName, dec64Path.get(0));
798 assertEquals(dec64QName, dec64Path.get(1));