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.HashSet;
20 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.yang.common.QName;
27 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
28 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
29 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.controller.yang.model.api.ChoiceNode;
31 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
32 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
34 import org.opendaylight.controller.yang.model.api.Deviation;
35 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
36 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
37 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
38 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
39 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
40 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
41 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
42 import org.opendaylight.controller.yang.model.api.Module;
43 import org.opendaylight.controller.yang.model.api.ModuleImport;
44 import org.opendaylight.controller.yang.model.api.MustDefinition;
45 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
46 import org.opendaylight.controller.yang.model.api.RpcDefinition;
47 import org.opendaylight.controller.yang.model.api.SchemaNode;
48 import org.opendaylight.controller.yang.model.api.SchemaPath;
49 import org.opendaylight.controller.yang.model.api.Status;
50 import org.opendaylight.controller.yang.model.api.TypeDefinition;
51 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
52 import org.opendaylight.controller.yang.model.api.UsesNode;
53 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
54 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
55 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
56 import org.opendaylight.controller.yang.model.util.Decimal64;
57 import org.opendaylight.controller.yang.model.util.ExtendedType;
58 import org.opendaylight.controller.yang.model.util.Int16;
59 import org.opendaylight.controller.yang.model.util.Int32;
60 import org.opendaylight.controller.yang.model.util.Int8;
61 import org.opendaylight.controller.yang.model.util.Leafref;
62 import org.opendaylight.controller.yang.model.util.StringType;
63 import org.opendaylight.controller.yang.model.util.Uint32;
64 import org.opendaylight.controller.yang.model.util.UnionType;
66 public class YangParserTest {
67 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
69 private Set<Module> modules;
72 public void init() throws FileNotFoundException {
73 modules = TestUtils.loadModules("src/test/resources/model");
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
223 .getDataChildByName("name");
224 StringType nameType = (StringType)name.getType();
225 QName nameQName = nameType.getQName();
227 assertEquals(baseYangTypeNS, nameQName.getNamespace());
228 assertNull(nameQName.getRevision());
229 assertEquals("", nameQName.getPrefix());
230 assertEquals("string", nameQName.getLocalName());
233 LeafSchemaNode count = (LeafSchemaNode) test
234 .getDataChildByName("count");
235 ExtendedType countType = (ExtendedType)count.getType();
236 QName countTypeQName = countType.getQName();
238 URI expectedNS = URI.create("urn:simple.types.data.demo");
239 Date expectedDate = simpleDateFormat.parse("2013-02-27");
240 assertEquals(expectedNS, countTypeQName.getNamespace());
241 assertEquals(expectedDate, countTypeQName.getRevision());
242 assertEquals("t2", countTypeQName.getPrefix());
243 assertEquals("int8", countTypeQName.getLocalName());
245 Int8 countTypeBase = (Int8)countType.getBaseType();
246 QName countTypeBaseQName = countTypeBase.getQName();
248 assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace());
249 assertNull(countTypeBaseQName.getRevision());
250 assertEquals("", countTypeBaseQName.getPrefix());
251 assertEquals("int8", countTypeBaseQName.getLocalName());
255 public void testAugmentResolving() {
257 Module module1 = TestUtils.findModule(modules, "types1");
259 Set<AugmentationSchema> module1Augmentations = module1
261 AugmentationSchema augment1 = module1Augmentations.iterator().next();
262 LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1
263 .getDataChildByName("ds0ChannelNumber");
264 assertTrue(augmentedLeafDefinition.isAugmenting());
267 Module module2 = TestUtils.findModule(modules, "types2");
269 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2
270 .getDataChildByName("interfaces");
271 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
272 .getDataChildByName("ifEntry");
273 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry
274 .getDataChildByName("augment-holder");
276 // augmentation defined in testfile1 and augmentation returned from
277 // augmented container have to be same
278 Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer
279 .getAvailableAugmentations();
280 AugmentationSchema augmentDefinition = augmentedContainerAugments
282 assertEquals(augment1, augmentDefinition);
284 LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer
285 .getDataChildByName("ds0ChannelNumber");
286 assertTrue(augmentedLeaf.isAugmenting());
287 assertEquals(augmentedLeafDefinition, augmentedLeaf);
289 Set<AugmentationSchema> ifEntryAugments = ifEntry
290 .getAvailableAugmentations();
291 assertEquals(2, ifEntryAugments.size());
294 Module module3 = TestUtils.findModule(modules, "types3");
296 Set<AugmentationSchema> module3Augmentations = module3
298 assertEquals(2, module3Augmentations.size());
299 AugmentationSchema augment3 = null;
300 for (AugmentationSchema as : module3Augmentations) {
301 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
305 ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
306 .getDataChildByName("augment-holder");
307 assertTrue(augmentedContainerDefinition.isAugmenting());
310 assertEquals(augmentedContainer, augmentedContainerDefinition);
311 assertEquals(augmentedLeaf, augmentedLeafDefinition);
315 public void testAugmentTarget() {
316 Module test = TestUtils.findModule(modules, "types2");
318 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
319 .getDataChildByName("interfaces");
320 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
321 .getDataChildByName("ifEntry");
322 Set<AugmentationSchema> augmentations = ifEntry
323 .getAvailableAugmentations();
324 assertEquals(2, augmentations.size());
326 AugmentationSchema augment = null;
327 for (AugmentationSchema as : augmentations) {
328 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
332 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment
333 .getDataChildByName("augment-holder");
334 assertNotNull(augmentHolder);
335 assertTrue(augmentHolder.isAugmenting());
336 QName augmentHolderQName = augmentHolder.getQName();
337 assertEquals("augment-holder", augmentHolderQName.getLocalName());
338 assertEquals("t3", augmentHolderQName.getPrefix());
339 assertEquals("Description for augment holder",
340 augmentHolder.getDescription());
344 public void testTypedefRangesResolving() {
345 Module testModule = TestUtils.findModule(modules, "types1");
347 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
348 .getDataChildByName("testleaf");
349 ExtendedType leafType = (ExtendedType) testleaf.getType();
350 assertEquals("my-type1", leafType.getQName().getLocalName());
351 assertEquals("t2", leafType.getQName().getPrefix());
352 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
353 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
354 assertEquals("t2", baseType.getQName().getPrefix());
356 List<RangeConstraint> ranges = leafType.getRanges();
357 assertEquals(1, ranges.size());
358 RangeConstraint range = ranges.get(0);
359 assertEquals(3L, range.getMin());
360 assertEquals(20L, range.getMax());
364 public void testTypedefPatternsResolving() {
365 Module testModule = TestUtils.findModule(modules, "types1");
367 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
368 .getDataChildByName("test-string-leaf");
369 ExtendedType testleafType = (ExtendedType) testleaf.getType();
370 QName testleafTypeQName = testleafType.getQName();
371 assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
372 assertEquals("t2", testleafTypeQName.getPrefix());
374 Set<String> expectedRegex = new HashSet<String>();
375 expectedRegex.add("[a-k]*");
376 expectedRegex.add("[b-u]*");
377 expectedRegex.add("[e-z]*");
379 Set<String> actualRegex = new HashSet<String>();
380 List<PatternConstraint> patterns = testleafType.getPatterns();
381 assertEquals(3, patterns.size());
382 for (PatternConstraint pc : patterns) {
383 actualRegex.add(pc.getRegularExpression());
385 assertEquals(expectedRegex, actualRegex);
387 TypeDefinition<?> baseType = testleafType.getBaseType();
388 assertEquals("my-string-type2", baseType.getQName().getLocalName());
390 List<LengthConstraint> lengths = testleafType.getLengths();
391 assertEquals(1, lengths.size());
393 LengthConstraint length = lengths.get(0);
394 assertEquals(5L, length.getMin());
395 assertEquals(10L, length.getMax());
399 public void testTypedefLengthsResolving() {
400 Module testModule = TestUtils.findModule(modules, "types1");
402 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
403 .getDataChildByName("leaf-with-length");
404 ExtendedType testleafType = (ExtendedType) testleaf.getType();
405 assertEquals("my-string-type", testleafType.getQName().getLocalName());
407 List<LengthConstraint> lengths = testleafType.getLengths();
408 assertEquals(1, lengths.size());
410 LengthConstraint length = lengths.get(0);
411 assertEquals(7L, length.getMin());
412 assertEquals(10L, length.getMax());
416 public void testTypeDef() {
417 Module testModule = TestUtils.findModule(modules, "types2");
419 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
420 .getDataChildByName("nested-type-leaf");
421 ExtendedType testleafType = (ExtendedType) testleaf.getType();
422 assertEquals("my-type1", testleafType.getQName().getLocalName());
424 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
425 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
427 ExtendedType int32Type = (ExtendedType) baseType.getBaseType();
428 Int32 int32TypeBase = (Int32)int32Type.getBaseType();
429 QName qname = int32TypeBase.getQName();
430 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), qname.getNamespace());
431 assertNull(qname.getRevision());
432 assertEquals("", qname.getPrefix());
433 assertEquals("int32", qname.getLocalName());
434 List<RangeConstraint> ranges = int32Type.getRanges();
435 assertEquals(1, ranges.size());
436 RangeConstraint range = ranges.get(0);
437 assertEquals(2L, range.getMin());
438 assertEquals(20L, range.getMax());
442 public void testTypedefDecimal1() {
443 Module testModule = TestUtils.findModule(modules, "types1");
445 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
446 .getDataChildByName("test-decimal-leaf");
447 ExtendedType type = (ExtendedType) testleaf.getType();
448 assertEquals(4, (int) type.getFractionDigits());
450 Decimal64 baseType = (Decimal64) type.getBaseType();
451 assertEquals(6, (int) baseType.getFractionDigits());
455 public void testTypedefDecimal2() {
456 Module testModule = TestUtils.findModule(modules, "types1");
458 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
459 .getDataChildByName("test-decimal-leaf2");
460 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
461 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
462 Decimal64 baseTypeCast = (Decimal64) baseType;
463 assertEquals(5, (int) baseTypeCast.getFractionDigits());
467 public void testTypedefUnion() {
468 Module testModule = TestUtils.findModule(modules, "types1");
470 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
471 .getDataChildByName("union-leaf");
472 ExtendedType testleafType = (ExtendedType) testleaf.getType();
473 assertEquals("my-union-ext", testleafType.getQName().getLocalName());
475 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
476 assertEquals("my-union", baseType.getQName().getLocalName());
478 UnionType unionBase = (UnionType) baseType.getBaseType();
480 List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
481 ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
482 List<RangeConstraint> ranges = unionType1.getRanges();
483 assertEquals(1, ranges.size());
484 RangeConstraint range = ranges.get(0);
485 assertEquals(1L, range.getMin());
486 assertEquals(100L, range.getMax());
488 assertTrue(unionType1.getBaseType() instanceof Int16);
489 assertTrue(unionTypes.get(1) instanceof Int32);
493 public void testNestedUnionResolving1() {
494 Module testModule = TestUtils.findModule(modules, "types1");
496 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
497 .getDataChildByName("nested-union-leaf");
499 ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
500 assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
502 ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
503 assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
505 UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
506 List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
507 assertEquals(2, unionTypes.size());
508 assertTrue(unionTypes.get(0) instanceof StringType);
509 assertTrue(unionTypes.get(1) instanceof ExtendedType);
511 ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
512 ExtendedType extendedUnionBase = (ExtendedType) extendedUnion
514 assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
516 UnionType extendedTargetUnion = (UnionType) extendedUnionBase
518 List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion
520 assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16);
521 assertTrue(extendedTargetTypes.get(1) instanceof Int32);
523 ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0);
524 assertTrue(int16.getBaseType() instanceof Int16);
525 List<RangeConstraint> ranges = int16.getRanges();
526 assertEquals(1, ranges.size());
527 RangeConstraint range = ranges.get(0);
528 assertEquals(1L, range.getMin());
529 assertEquals(100L, range.getMax());
533 public void testNestedUnionResolving2() {
534 Module testModule = TestUtils.findModule(modules, "types1");
536 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
537 .getDataChildByName("custom-union-leaf");
539 ExtendedType testleafType = (ExtendedType) testleaf.getType();
540 QName testleafTypeQName = testleafType.getQName();
541 assertEquals(URI.create("urn:simple.container.demo.test"),
542 testleafTypeQName.getNamespace());
543 assertEquals(TestUtils.createDate("2013-02-27"),
544 testleafTypeQName.getRevision());
545 assertEquals("t3", testleafTypeQName.getPrefix());
546 assertEquals("union1", testleafTypeQName.getLocalName());
548 ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
549 QName union2QName = union2.getQName();
550 assertEquals(URI.create("urn:simple.container.demo.test"),
551 union2QName.getNamespace());
552 assertEquals(TestUtils.createDate("2013-02-27"),
553 union2QName.getRevision());
554 assertEquals("t3", union2QName.getPrefix());
555 assertEquals("union2", union2QName.getLocalName());
557 UnionType union2Base = (UnionType) union2.getBaseType();
558 List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
559 assertEquals(2, unionTypes.size());
560 assertTrue(unionTypes.get(0) instanceof Int32);
561 assertTrue(unionTypes.get(1) instanceof ExtendedType);
563 ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
564 QName nestedUnion2QName = nestedUnion2.getQName();
565 assertEquals(URI.create("urn:simple.types.data.demo"),
566 nestedUnion2QName.getNamespace());
567 assertEquals(TestUtils.createDate("2013-02-27"),
568 nestedUnion2QName.getRevision());
569 assertEquals("t2", nestedUnion2QName.getPrefix());
570 assertEquals("nested-union2", nestedUnion2QName.getLocalName());
572 UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
573 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
574 assertEquals(2, nestedUnion2Types.size());
575 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
576 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
578 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
579 QName myUnionExtQName = myUnionExt.getQName();
580 assertEquals(URI.create("urn:simple.types.data.demo"),
581 myUnionExtQName.getNamespace());
582 assertEquals(TestUtils.createDate("2013-02-27"),
583 myUnionExtQName.getRevision());
584 assertEquals("t2", myUnionExtQName.getPrefix());
585 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
587 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
588 QName myUnionQName = myUnion.getQName();
589 assertEquals(URI.create("urn:simple.types.data.demo"),
590 myUnionQName.getNamespace());
591 assertEquals(TestUtils.createDate("2013-02-27"),
592 myUnionQName.getRevision());
593 assertEquals("t2", myUnionQName.getPrefix());
594 assertEquals("my-union", myUnionQName.getLocalName());
596 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
597 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
598 assertEquals(2, myUnionBaseTypes.size());
599 assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16);
600 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
601 ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0);
602 List<RangeConstraint> ranges = int16.getRanges();
603 assertEquals(1, ranges.size());
604 RangeConstraint range = ranges.get(0);
605 assertEquals(1L, range.getMin());
606 assertEquals(100L, range.getMax());
610 public void testRefine() {
611 Module testModule = TestUtils.findModule(modules, "types2");
613 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
614 .getDataChildByName("peer");
615 ContainerSchemaNode destination = (ContainerSchemaNode) peer
616 .getDataChildByName("destination");
617 Set<UsesNode> usesNodes = destination.getUses();
618 assertEquals(1, usesNodes.size());
619 UsesNode usesNode = usesNodes.iterator().next();
620 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
621 assertEquals(5, refines.size());
623 LeafSchemaNode refineLeaf = null;
624 ContainerSchemaNode refineContainer = null;
625 ListSchemaNode refineList = null;
626 GroupingDefinition refineGrouping = null;
627 TypeDefinition<?> typedef = null;
628 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
629 SchemaNode value = entry.getValue();
630 if (value instanceof LeafSchemaNode) {
631 refineLeaf = (LeafSchemaNode) value;
632 } else if (value instanceof ContainerSchemaNode) {
633 refineContainer = (ContainerSchemaNode) value;
634 } else if (value instanceof ListSchemaNode) {
635 refineList = (ListSchemaNode) value;
636 } else if (value instanceof GroupingDefinition) {
637 refineGrouping = (GroupingDefinition) value;
638 } else if (value instanceof TypeDefinition<?>) {
639 typedef = (TypeDefinition<?>) value;
644 assertNotNull(refineLeaf);
645 assertEquals("address", refineLeaf.getQName().getLocalName());
646 assertEquals("description of address defined by refine",
647 refineLeaf.getDescription());
648 assertEquals("address reference added by refine",
649 refineLeaf.getReference());
650 assertFalse(refineLeaf.isConfiguration());
651 assertTrue(refineLeaf.getConstraints().isMandatory());
652 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
653 .getMustConstraints();
654 assertEquals(1, leafMustConstraints.size());
655 MustDefinition leafMust = leafMustConstraints.iterator().next();
657 "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"",
658 leafMust.toString());
661 assertNotNull(refineContainer);
662 Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
663 .getMustConstraints();
664 assertTrue(mustConstraints.isEmpty());
665 assertEquals("description of port defined by refine",
666 refineContainer.getDescription());
667 assertEquals("port reference added by refine",
668 refineContainer.getReference());
669 assertFalse(refineContainer.isConfiguration());
670 assertTrue(refineContainer.isPresenceContainer());
673 assertNotNull(refineList);
674 assertEquals("description of addresses defined by refine",
675 refineList.getDescription());
676 assertEquals("addresses reference added by refine",
677 refineList.getReference());
678 assertFalse(refineList.isConfiguration());
679 assertEquals(2, (int) refineList.getConstraints().getMinElements());
680 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
682 // grouping target-inner
683 assertNotNull(refineGrouping);
684 Set<DataSchemaNode> refineGroupingChildren = refineGrouping
686 assertEquals(1, refineGroupingChildren.size());
687 LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren
689 assertEquals("inner-grouping-id", refineGroupingLeaf.getQName()
691 assertEquals("new target-inner grouping description",
692 refineGrouping.getDescription());
694 // typedef group-type
695 assertNotNull(typedef);
696 assertEquals("new group-type description", typedef.getDescription());
697 assertEquals("new group-type reference", typedef.getReference());
698 assertTrue(typedef.getBaseType() instanceof ExtendedType);
702 public void testChoice() {
703 Module testModule = TestUtils.findModule(modules, "types1");
704 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
705 .getDataChildByName("transfer");
706 ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
707 Set<ChoiceCaseNode> cases = how.getCases();
708 assertEquals(3, cases.size());
712 public void testAnyXml() {
713 Module testModule = TestUtils.findModule(modules, "types1");
714 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule
715 .getDataChildByName("data");
720 public void testDeviation() {
721 Module testModule = TestUtils.findModule(modules, "types1");
722 Set<Deviation> deviations = testModule.getDeviations();
723 assertEquals(1, deviations.size());
725 Deviation dev = deviations.iterator().next();
726 SchemaPath expectedPath = TestUtils.createPath(true, null, null,
727 "data", "system", "user");
728 assertEquals(expectedPath, dev.getTargetPath());
729 assertEquals(Deviate.ADD, dev.getDeviate());
733 public void testUnknownNode() {
734 Module testModule = TestUtils.findModule(modules, "types3");
735 ContainerSchemaNode network = (ContainerSchemaNode) testModule
736 .getDataChildByName("network");
737 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
738 assertEquals(1, unknownNodes.size());
739 UnknownSchemaNode unknownNode = unknownNodes.get(0);
740 assertNotNull(unknownNode.getNodeType());
741 assertEquals("point", unknownNode.getNodeParameter());
745 public void testFeature() {
746 Module testModule = TestUtils.findModule(modules, "types3");
747 Set<FeatureDefinition> features = testModule.getFeatures();
748 assertEquals(1, features.size());
752 public void testExtension() {
753 Module testModule = TestUtils.findModule(modules, "types3");
754 List<ExtensionDefinition> extensions = testModule
755 .getExtensionSchemaNodes();
756 assertEquals(1, extensions.size());
757 ExtensionDefinition extension = extensions.get(0);
758 assertEquals("name", extension.getArgument());
759 assertFalse(extension.isYinElement());
763 public void testNotification() {
764 Module testModule = TestUtils.findModule(modules, "types3");
765 URI expectedNamespace = URI.create("urn:simple.container.demo.test");
766 String expectedPrefix = "t3";
767 Date expectedRevision = TestUtils.createDate("2013-02-27");
769 Set<NotificationDefinition> notifications = testModule
771 assertEquals(1, notifications.size());
773 NotificationDefinition notification = notifications.iterator().next();
774 // test SchemaNode args
775 QName expectedQName = new QName(expectedNamespace, expectedRevision,
776 expectedPrefix, "event");
777 assertEquals(expectedQName, notification.getQName());
778 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
779 expectedRevision, expectedPrefix, "event");
780 assertEquals(expectedPath, notification.getPath());
781 assertNull(notification.getDescription());
782 assertNull(notification.getReference());
783 assertEquals(Status.CURRENT, notification.getStatus());
784 assertEquals(0, notification.getUnknownSchemaNodes().size());
785 // test DataNodeContainer args
786 assertEquals(0, notification.getTypeDefinitions().size());
787 assertEquals(3, notification.getChildNodes().size());
788 assertEquals(0, notification.getGroupings().size());
789 assertEquals(0, notification.getUses().size());
791 LeafSchemaNode eventClass = (LeafSchemaNode) notification
792 .getDataChildByName("event-class");
793 assertTrue(eventClass.getType() instanceof StringType);
794 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification
795 .getDataChildByName("reporting-entity");
796 assertNotNull(reportingEntity);
797 LeafSchemaNode severity = (LeafSchemaNode) notification
798 .getDataChildByName("severity");
799 assertTrue(severity.getType() instanceof StringType);
803 public void testRpc() {
804 Module testModule = TestUtils.findModule(modules, "types3");
806 Set<RpcDefinition> rpcs = testModule.getRpcs();
807 assertEquals(1, rpcs.size());
809 RpcDefinition rpc = rpcs.iterator().next();
810 assertEquals("Retrieve all or part of a specified configuration.",
811 rpc.getDescription());
812 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
814 ContainerSchemaNode input = rpc.getInput();
815 assertNotNull(input.getDataChildByName("source"));
816 assertNotNull(input.getDataChildByName("filter"));
817 ContainerSchemaNode output = rpc.getOutput();
818 assertNotNull(output.getDataChildByName("data"));
822 public void testGrouping() {
823 Module testModule = TestUtils.findModule(modules, "types2");
824 Set<GroupingDefinition> groupings = testModule.getGroupings();
825 assertEquals(1, groupings.size());
826 GroupingDefinition grouping = groupings.iterator().next();
827 Set<DataSchemaNode> children = grouping.getChildNodes();
828 assertEquals(5, children.size());
832 public void testAugmentNodesTypesSchemaPath() throws Exception {
833 Module testModule = TestUtils.findModule(modules, "types1");
834 Set<AugmentationSchema> augments = testModule.getAugmentations();
835 assertEquals(1, augments.size());
836 AugmentationSchema augment = augments.iterator().next();
838 LeafSchemaNode ifcId = (LeafSchemaNode) augment
839 .getDataChildByName("interface-id");
840 Leafref ifcIdType = (Leafref) ifcId.getType();
841 SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
842 List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
843 QName q0 = new QName(new URI("urn:simple.types.data.demo"),
844 simpleDateFormat.parse("2013-02-27"), "data", "interfaces");
845 QName q1 = new QName(new URI("urn:simple.types.data.demo"),
846 simpleDateFormat.parse("2013-02-27"), "data", "ifEntry");
847 QName q2 = new QName(new URI("urn:simple.container.demo.test"),
848 simpleDateFormat.parse("2013-02-27"), "data", "augment-holder");
849 QName q3 = new QName(new URI("urn:simple.container.demo"),
850 simpleDateFormat.parse("2013-02-27"), "data", "interface-id");
851 assertEquals(q0, ifcIdTypePath.get(0));
852 assertEquals(q1, ifcIdTypePath.get(1));
853 assertEquals(q2, ifcIdTypePath.get(2));
854 assertEquals(q3, ifcIdTypePath.get(3));
856 LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment
857 .getDataChildByName("higher-layer-if");
858 Leafref higherLayerType = (Leafref) higherLayer.getType();
859 SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
860 List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
861 assertEquals(q0, higherLayerTypePath.get(0));
862 assertEquals(q1, higherLayerTypePath.get(1));
863 assertEquals(q2, higherLayerTypePath.get(2));
864 q3 = new QName(new URI("urn:simple.container.demo"),
865 simpleDateFormat.parse("2013-02-27"), "data", "higher-layer-if");
866 assertEquals(q3, higherLayerTypePath.get(3));
870 public void testTypePath() throws ParseException {
871 Module test = TestUtils.findModule(modules, "types2");
872 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
874 // my-base-int32-type
875 ExtendedType int32Typedef = (ExtendedType)TestUtils.findTypedef(types, "my-base-int32-type");
876 QName int32TypedefQName = int32Typedef.getQName();
878 URI expectedNS = URI.create("urn:simple.types.data.demo");
879 Date expectedDate = simpleDateFormat.parse("2013-02-27");
880 assertEquals(expectedNS, int32TypedefQName.getNamespace());
881 assertEquals(expectedDate, int32TypedefQName.getRevision());
882 assertEquals("t2", int32TypedefQName.getPrefix());
883 assertEquals("my-base-int32-type", int32TypedefQName.getLocalName());
885 SchemaPath typeSchemaPath = int32Typedef.getPath();
886 List<QName> typePath = typeSchemaPath.getPath();
887 assertEquals(1, typePath.size());
888 assertEquals(int32TypedefQName, typePath.get(0));
890 // my-base-int32-type/int32
891 ExtendedType int32Ext = (ExtendedType)int32Typedef.getBaseType();
892 QName int32ExtQName = int32Ext.getQName();
894 assertEquals(expectedNS, int32ExtQName.getNamespace());
895 assertEquals(expectedDate, int32ExtQName.getRevision());
896 assertEquals("t2", int32ExtQName.getPrefix());
897 assertEquals("int32", int32ExtQName.getLocalName());
899 SchemaPath int32ExtSchemaPath = int32Ext.getPath();
900 List<QName> int32ExtPath = int32ExtSchemaPath.getPath();
901 assertEquals(2, int32ExtPath.size());
902 assertEquals(int32TypedefQName, int32ExtPath.get(0));
903 assertEquals(int32ExtQName, int32ExtPath.get(1));
905 // my-base-int32-type/int32/int32
906 Int32 int32 = (Int32)int32Ext.getBaseType();
907 QName int32QName = int32.getQName();
908 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), int32QName.getNamespace());
909 assertNull(int32QName.getRevision());
910 assertEquals("", int32QName.getPrefix());
911 assertEquals("int32", int32QName.getLocalName());
913 SchemaPath int32SchemaPath = int32.getPath();
914 List<QName> int32Path = int32SchemaPath.getPath();
915 assertEquals(3, int32Path.size());
916 assertEquals(int32TypedefQName, int32Path.get(0));
917 assertEquals(int32ExtQName, int32Path.get(1));
918 assertEquals(int32QName, int32Path.get(2));
922 public void testTypePath2() throws ParseException {
923 Module test = TestUtils.findModule(modules, "types2");
924 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
926 // my-base-int32-type
927 ExtendedType myDecType = (ExtendedType)TestUtils.findTypedef(types, "my-decimal-type");
928 QName myDecTypeQName = myDecType.getQName();
930 URI expectedNS = URI.create("urn:simple.types.data.demo");
931 Date expectedDate = simpleDateFormat.parse("2013-02-27");
932 assertEquals(expectedNS, myDecTypeQName.getNamespace());
933 assertEquals(expectedDate, myDecTypeQName.getRevision());
934 assertEquals("t2", myDecTypeQName.getPrefix());
935 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
937 SchemaPath typeSchemaPath = myDecType.getPath();
938 List<QName> typePath = typeSchemaPath.getPath();
939 assertEquals(1, typePath.size());
940 assertEquals(myDecTypeQName, typePath.get(0));
942 // my-base-int32-type/int32
943 Decimal64 dec64 = (Decimal64)myDecType.getBaseType();
944 QName dec64QName = dec64.getQName();
946 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
947 assertNull(dec64QName.getRevision());
948 assertEquals("", dec64QName.getPrefix());
949 assertEquals("decimal64", dec64QName.getLocalName());
951 SchemaPath dec64SchemaPath = dec64.getPath();
952 List<QName> dec64Path = dec64SchemaPath.getPath();
953 assertEquals(2, dec64Path.size());
954 assertEquals(myDecTypeQName, dec64Path.get(0));
955 assertEquals(dec64QName, dec64Path.get(1));