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.DataSchemaNode;
32 import org.opendaylight.controller.yang.model.api.Deviation;
33 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
34 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
35 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
36 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
37 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
38 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
40 import org.opendaylight.controller.yang.model.api.Module;
41 import org.opendaylight.controller.yang.model.api.ModuleImport;
42 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
43 import org.opendaylight.controller.yang.model.api.RpcDefinition;
44 import org.opendaylight.controller.yang.model.api.SchemaPath;
45 import org.opendaylight.controller.yang.model.api.Status;
46 import org.opendaylight.controller.yang.model.api.TypeDefinition;
47 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
48 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
49 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
50 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
51 import org.opendaylight.controller.yang.model.util.Decimal64;
52 import org.opendaylight.controller.yang.model.util.ExtendedType;
53 import org.opendaylight.controller.yang.model.util.Int16;
54 import org.opendaylight.controller.yang.model.util.Int32;
55 import org.opendaylight.controller.yang.model.util.Int8;
56 import org.opendaylight.controller.yang.model.util.Leafref;
57 import org.opendaylight.controller.yang.model.util.StringType;
58 import org.opendaylight.controller.yang.model.util.Uint32;
59 import org.opendaylight.controller.yang.model.util.UnionType;
61 public class YangParserTest {
62 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
63 private Set<Module> modules;
66 public void init() throws FileNotFoundException {
67 modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
68 assertEquals(3, modules.size());
72 public void testHeaders() {
73 Module test = TestUtils.findModule(modules, "types1");
75 assertEquals("types1", test.getName());
76 assertEquals("1", test.getYangVersion());
77 URI expectedNamespace = URI.create("urn:simple.container.demo");
78 assertEquals(expectedNamespace, test.getNamespace());
79 assertEquals("t1", test.getPrefix());
81 Set<ModuleImport> imports = test.getImports();
82 assertEquals(2, imports.size());
84 ModuleImport import2 = TestUtils.findImport(imports, "data");
85 assertEquals("types2", import2.getModuleName());
86 assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision());
88 ModuleImport import3 = TestUtils.findImport(imports, "t3");
89 assertEquals("types3", import3.getModuleName());
90 assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision());
92 assertEquals("opendaylight", test.getOrganization());
93 assertEquals("http://www.opendaylight.org/", test.getContact());
94 Date expectedRevision = TestUtils.createDate("2013-02-27");
95 assertEquals(expectedRevision, test.getRevision());
96 assertEquals(" WILL BE DEFINED LATER", test.getReference());
100 public void testOrderingTypedef() {
101 Module test = TestUtils.findModule(modules, "types2");
102 Set<TypeDefinition<?>> typedefs = test.getTypeDefinitions();
103 String[] expectedOrder = new String[] { "my-base-int32-type", "my-custom-string", "my-decimal-type",
104 "my-decimal-type-ext", "my-int-type", "my-int-type-ext", "my-int-type2", "my-string-type",
105 "my-string-type-ext", "my-string-type2", "my-type1", "my-union", "my-union-ext", "nested-union1",
107 String[] actualOrder = new String[typedefs.size()];
110 for (TypeDefinition<?> type : typedefs) {
111 actualOrder[i] = type.getQName().getLocalName();
114 assertArrayEquals(expectedOrder, actualOrder);
118 public void testOrderingChildNodes() {
119 Module test = TestUtils.findModule(modules, "types2");
120 Set<DataSchemaNode> childNodes = test.getChildNodes();
121 String[] expectedOrder = new String[] { "count", "if-name", "interfaces", "name", "nested-type-leaf", "peer",
123 String[] actualOrder = new String[childNodes.size()];
126 for (DataSchemaNode child : childNodes) {
127 actualOrder[i] = child.getQName().getLocalName();
130 assertArrayEquals(expectedOrder, actualOrder);
134 public void testOrderingNestedChildNodes() {
135 Module test = TestUtils.findModule(modules, "types2");
136 Set<GroupingDefinition> groupings = test.getGroupings();
137 assertEquals(1, groupings.size());
138 GroupingDefinition target = groupings.iterator().next();
140 Set<DataSchemaNode> childNodes = target.getChildNodes();
141 String[] expectedOrder = new String[] { "address", "addresses", "data", "how", "port" };
142 String[] actualOrder = new String[childNodes.size()];
145 for (DataSchemaNode child : childNodes) {
146 actualOrder[i] = child.getQName().getLocalName();
149 assertArrayEquals(expectedOrder, actualOrder);
153 public void testParseContainer() {
154 Module test = TestUtils.findModule(modules, "types2");
155 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
156 String expectedPrefix = "t2";
157 Date expectedRevision = TestUtils.createDate("2013-02-27");
159 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
160 // test SchemaNode args
161 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "interfaces");
162 assertEquals(expectedQName, interfaces.getQName());
163 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
165 assertEquals(expectedPath, interfaces.getPath());
166 assertNull(interfaces.getDescription());
167 assertNull(interfaces.getReference());
168 assertEquals(Status.CURRENT, interfaces.getStatus());
169 assertEquals(0, interfaces.getUnknownSchemaNodes().size());
170 // test DataSchemaNode args
171 assertFalse(interfaces.isAugmenting());
172 assertTrue(interfaces.isConfiguration());
173 ConstraintDefinition constraints = interfaces.getConstraints();
174 assertNull(constraints.getWhenCondition());
175 assertEquals(0, constraints.getMustConstraints().size());
176 assertFalse(constraints.isMandatory());
177 assertNull(constraints.getMinElements());
178 assertNull(constraints.getMaxElements());
179 // test AugmentationTarget args
180 assertEquals(0, interfaces.getAvailableAugmentations().size());
181 // test ContainerSchemaNode args
182 assertFalse(interfaces.isPresenceContainer());
183 // test DataNodeContainer args
184 assertEquals(0, interfaces.getTypeDefinitions().size());
185 assertEquals(1, interfaces.getChildNodes().size());
186 assertEquals(0, interfaces.getGroupings().size());
187 assertEquals(0, interfaces.getUses().size());
189 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
190 assertNotNull(ifEntry);
194 public void testParseList() {
195 Module test = TestUtils.findModule(modules, "types2");
196 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
197 String expectedPrefix = "t2";
198 Date expectedRevision = TestUtils.createDate("2013-02-27");
200 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
202 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
203 // test SchemaNode args
204 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "ifEntry");
205 assertEquals(expectedQName, ifEntry.getQName());
206 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
207 "interfaces", "ifEntry");
208 assertEquals(expectedPath, ifEntry.getPath());
209 assertNull(ifEntry.getDescription());
210 assertNull(ifEntry.getReference());
211 assertEquals(Status.CURRENT, ifEntry.getStatus());
212 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
213 // test DataSchemaNode args
214 assertFalse(ifEntry.isAugmenting());
215 assertTrue(ifEntry.isConfiguration());
216 ConstraintDefinition constraints = ifEntry.getConstraints();
217 assertNull(constraints.getWhenCondition());
218 assertEquals(0, constraints.getMustConstraints().size());
219 assertFalse(constraints.isMandatory());
220 assertEquals(1, (int) constraints.getMinElements());
221 assertEquals(11, (int) constraints.getMaxElements());
222 // test AugmentationTarget args
223 Set<AugmentationSchema> availableAugmentations = ifEntry.getAvailableAugmentations();
224 assertEquals(2, availableAugmentations.size());
225 // test ListSchemaNode args
226 List<QName> expectedKey = new ArrayList<QName>();
227 expectedKey.add(new QName(expectedNamespace, expectedRevision, expectedPrefix, "ifIndex"));
228 assertEquals(expectedKey, ifEntry.getKeyDefinition());
229 assertFalse(ifEntry.isUserOrdered());
230 // test DataNodeContainer args
231 assertEquals(0, ifEntry.getTypeDefinitions().size());
232 assertEquals(4, ifEntry.getChildNodes().size());
233 assertEquals(0, ifEntry.getGroupings().size());
234 assertEquals(0, ifEntry.getUses().size());
236 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
237 assertTrue(ifIndex.getType() instanceof Uint32);
238 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
239 assertTrue(ifMtu.getType() instanceof Int32);
243 public void testParseLeaf() throws ParseException {
244 Module test = TestUtils.findModule(modules, "types2");
247 LeafSchemaNode ifName = (LeafSchemaNode) test.getDataChildByName("if-name");
248 Leafref ifNameType = (Leafref) ifName.getType();
249 QName qname = ifNameType.getQName();
251 URI baseYangTypeNS = URI.create("urn:ietf:params:xml:ns:yang:1");
252 assertEquals(baseYangTypeNS, qname.getNamespace());
253 assertNull(qname.getRevision());
254 assertEquals("", qname.getPrefix());
255 assertEquals("leafref", qname.getLocalName());
258 LeafSchemaNode name = (LeafSchemaNode) test.getDataChildByName("name");
259 StringType nameType = (StringType) name.getType();
260 QName nameQName = nameType.getQName();
262 assertEquals(baseYangTypeNS, nameQName.getNamespace());
263 assertNull(nameQName.getRevision());
264 assertEquals("", nameQName.getPrefix());
265 assertEquals("string", nameQName.getLocalName());
268 LeafSchemaNode count = (LeafSchemaNode) test.getDataChildByName("count");
269 ExtendedType countType = (ExtendedType) count.getType();
270 QName countTypeQName = countType.getQName();
272 URI expectedNS = URI.create("urn:simple.types.data.demo");
273 Date expectedDate = simpleDateFormat.parse("2013-02-27");
274 assertEquals(expectedNS, countTypeQName.getNamespace());
275 assertEquals(expectedDate, countTypeQName.getRevision());
276 assertEquals("t2", countTypeQName.getPrefix());
277 assertEquals("int8", countTypeQName.getLocalName());
279 Int8 countTypeBase = (Int8) countType.getBaseType();
280 QName countTypeBaseQName = countTypeBase.getQName();
282 assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace());
283 assertNull(countTypeBaseQName.getRevision());
284 assertEquals("", countTypeBaseQName.getPrefix());
285 assertEquals("int8", countTypeBaseQName.getLocalName());
289 public void testAugmentResolving() {
291 Module module1 = TestUtils.findModule(modules, "types1");
293 Set<AugmentationSchema> module1Augmentations = module1.getAugmentations();
294 AugmentationSchema augment1 = module1Augmentations.iterator().next();
295 LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1.getDataChildByName("ds0ChannelNumber");
296 assertTrue(augmentedLeafDefinition.isAugmenting());
299 Module module2 = TestUtils.findModule(modules, "types2");
301 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName("interfaces");
302 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
303 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
305 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas");
306 LeafSchemaNode linkleaf = (LeafSchemaNode) schemas.getDataChildByName("linkleaf");
307 assertNotNull(linkleaf);
309 // augmentation defined in testfile1 and augmentation returned from
310 // augmented container have to be same
311 Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer.getAvailableAugmentations();
312 AugmentationSchema augmentDefinition = augmentedContainerAugments.iterator().next();
313 assertEquals(augment1, augmentDefinition);
315 LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer.getDataChildByName("ds0ChannelNumber");
316 assertTrue(augmentedLeaf.isAugmenting());
317 assertEquals(augmentedLeafDefinition, augmentedLeaf);
319 Set<AugmentationSchema> ifEntryAugments = ifEntry.getAvailableAugmentations();
320 assertEquals(2, ifEntryAugments.size());
323 Module module3 = TestUtils.findModule(modules, "types3");
325 Set<AugmentationSchema> module3Augmentations = module3.getAugmentations();
326 assertEquals(3, module3Augmentations.size());
327 AugmentationSchema augment3 = null;
328 for (AugmentationSchema as : module3Augmentations) {
329 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
333 ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
334 .getDataChildByName("augment-holder");
335 assertTrue(augmentedContainerDefinition.isAugmenting());
338 assertEquals(augmentedContainer, augmentedContainerDefinition);
339 assertEquals(augmentedLeaf, augmentedLeafDefinition);
343 public void testAugmentTarget() {
344 Module test = TestUtils.findModule(modules, "types2");
346 ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
347 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
348 Set<AugmentationSchema> augmentations = ifEntry.getAvailableAugmentations();
349 assertEquals(2, augmentations.size());
351 AugmentationSchema augment = null;
352 for (AugmentationSchema as : augmentations) {
353 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
357 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment.getDataChildByName("augment-holder");
358 assertNotNull(augmentHolder);
359 assertTrue(augmentHolder.isAugmenting());
360 QName augmentHolderQName = augmentHolder.getQName();
361 assertEquals("augment-holder", augmentHolderQName.getLocalName());
362 assertEquals("t3", augmentHolderQName.getPrefix());
363 assertEquals("Description for augment holder", augmentHolder.getDescription());
367 public void testTypedefRangesResolving() throws ParseException {
368 Module testModule = TestUtils.findModule(modules, "types1");
370 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("testleaf");
371 ExtendedType leafType = (ExtendedType) testleaf.getType();
372 QName leafTypeQName = leafType.getQName();
373 assertEquals("my-type1", leafTypeQName.getLocalName());
374 assertEquals("t1", leafTypeQName.getPrefix());
375 assertEquals(URI.create("urn:simple.container.demo"), leafTypeQName.getNamespace());
376 Date expectedDate = simpleDateFormat.parse("2013-02-27");
377 assertEquals(expectedDate, leafTypeQName.getRevision());
378 assertEquals(1, leafType.getRanges().size());
380 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
381 QName baseTypeQName = baseType.getQName();
382 assertEquals("my-type1", baseTypeQName.getLocalName());
383 assertEquals("t2", baseTypeQName.getPrefix());
384 assertEquals(URI.create("urn:simple.types.data.demo"), baseTypeQName.getNamespace());
385 assertEquals(expectedDate, baseTypeQName.getRevision());
386 assertEquals(2, baseType.getRanges().size());
388 List<RangeConstraint> ranges = leafType.getRanges();
389 assertEquals(1, ranges.size());
390 RangeConstraint range = ranges.get(0);
391 assertEquals(12L, range.getMin());
392 assertEquals(20L, range.getMax());
396 public void testTypedefPatternsResolving() {
397 Module testModule = TestUtils.findModule(modules, "types1");
399 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-string-leaf");
400 ExtendedType testleafType = (ExtendedType) testleaf.getType();
401 QName testleafTypeQName = testleafType.getQName();
402 assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
403 assertEquals("t2", testleafTypeQName.getPrefix());
405 List<PatternConstraint> patterns = testleafType.getPatterns();
406 assertEquals(1, patterns.size());
407 PatternConstraint pattern = patterns.iterator().next();
408 assertEquals("[e-z]*", pattern.getRegularExpression());
410 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
411 assertEquals("my-string-type2", baseType.getQName().getLocalName());
413 patterns = baseType.getPatterns();
414 assertEquals(1, patterns.size());
415 pattern = patterns.iterator().next();
416 assertEquals("[b-u]*", pattern.getRegularExpression());
418 List<LengthConstraint> lengths = testleafType.getLengths();
419 assertTrue(lengths.isEmpty());
423 public void testTypedefLengthsResolving() {
424 Module testModule = TestUtils.findModule(modules, "types1");
426 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("leaf-with-length");
427 ExtendedType testleafType = (ExtendedType) testleaf.getType();
428 assertEquals("my-string-type", testleafType.getQName().getLocalName());
430 List<LengthConstraint> lengths = testleafType.getLengths();
431 assertEquals(1, lengths.size());
433 LengthConstraint length = lengths.get(0);
434 assertEquals(7L, length.getMin());
435 assertEquals(10L, length.getMax());
439 public void testTypeDef() {
440 Module testModule = TestUtils.findModule(modules, "types2");
442 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("nested-type-leaf");
443 ExtendedType testleafType = (ExtendedType) testleaf.getType();
444 assertEquals("my-type1", testleafType.getQName().getLocalName());
446 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
447 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
449 Int32 int32Type = (Int32) baseType.getBaseType();
450 QName qname = int32Type.getQName();
451 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), qname.getNamespace());
452 assertNull(qname.getRevision());
453 assertEquals("", qname.getPrefix());
454 assertEquals("int32", qname.getLocalName());
455 List<RangeConstraint> ranges = baseType.getRanges();
456 assertEquals(1, ranges.size());
457 RangeConstraint range = ranges.get(0);
458 assertEquals(2L, range.getMin());
459 assertEquals(20L, range.getMax());
463 public void testTypedefDecimal1() {
464 Module testModule = TestUtils.findModule(modules, "types1");
466 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-decimal-leaf");
467 ExtendedType type = (ExtendedType) testleaf.getType();
468 assertEquals(4, (int) type.getFractionDigits());
470 ExtendedType typeBase = (ExtendedType) type.getBaseType();
471 assertEquals("my-decimal-type", typeBase.getQName().getLocalName());
472 assertNull(typeBase.getFractionDigits());
474 Decimal64 decimal = (Decimal64) typeBase.getBaseType();
475 assertEquals(6, (int) decimal.getFractionDigits());
479 public void testTypedefDecimal2() {
480 Module testModule = TestUtils.findModule(modules, "types1");
482 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("test-decimal-leaf2");
483 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
484 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
485 Decimal64 baseTypeCast = (Decimal64) baseType;
486 assertEquals(5, (int) baseTypeCast.getFractionDigits());
490 public void testTypedefUnion() {
491 Module testModule = TestUtils.findModule(modules, "types1");
493 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("union-leaf");
494 ExtendedType testleafType = (ExtendedType) testleaf.getType();
495 assertEquals("my-union-ext", testleafType.getQName().getLocalName());
497 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
498 assertEquals("my-union", baseType.getQName().getLocalName());
500 UnionType unionBase = (UnionType) baseType.getBaseType();
502 List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
503 ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
504 List<RangeConstraint> ranges = unionType1.getRanges();
505 assertEquals(1, ranges.size());
506 RangeConstraint range = ranges.get(0);
507 assertEquals(1L, range.getMin());
508 assertEquals(100L, range.getMax());
510 assertTrue(unionType1.getBaseType() instanceof Int16);
511 assertTrue(unionTypes.get(1) instanceof Int32);
515 public void testNestedUnionResolving1() {
516 Module testModule = TestUtils.findModule(modules, "types1");
518 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("nested-union-leaf");
520 ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
521 assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
523 ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
524 assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
526 UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
527 List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
528 assertEquals(2, unionTypes.size());
529 assertTrue(unionTypes.get(0) instanceof StringType);
530 assertTrue(unionTypes.get(1) instanceof ExtendedType);
532 ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
533 ExtendedType extendedUnionBase = (ExtendedType) extendedUnion.getBaseType();
534 assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
536 UnionType extendedTargetUnion = (UnionType) extendedUnionBase.getBaseType();
537 List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion.getTypes();
538 assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16);
539 assertTrue(extendedTargetTypes.get(1) instanceof Int32);
541 ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0);
542 assertTrue(int16.getBaseType() instanceof Int16);
543 List<RangeConstraint> ranges = int16.getRanges();
544 assertEquals(1, ranges.size());
545 RangeConstraint range = ranges.get(0);
546 assertEquals(1L, range.getMin());
547 assertEquals(100L, range.getMax());
551 public void testNestedUnionResolving2() {
552 Module testModule = TestUtils.findModule(modules, "types1");
554 LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("custom-union-leaf");
556 ExtendedType testleafType = (ExtendedType) testleaf.getType();
557 QName testleafTypeQName = testleafType.getQName();
558 assertEquals(URI.create("urn:simple.container.demo.test"), testleafTypeQName.getNamespace());
559 assertEquals(TestUtils.createDate("2013-02-27"), testleafTypeQName.getRevision());
560 assertEquals("t3", testleafTypeQName.getPrefix());
561 assertEquals("union1", testleafTypeQName.getLocalName());
563 ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
564 QName union2QName = union2.getQName();
565 assertEquals(URI.create("urn:simple.container.demo.test"), union2QName.getNamespace());
566 assertEquals(TestUtils.createDate("2013-02-27"), union2QName.getRevision());
567 assertEquals("t3", union2QName.getPrefix());
568 assertEquals("union2", union2QName.getLocalName());
570 UnionType union2Base = (UnionType) union2.getBaseType();
571 List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
572 assertEquals(2, unionTypes.size());
573 assertTrue(unionTypes.get(0) instanceof Int32);
574 assertTrue(unionTypes.get(1) instanceof ExtendedType);
576 ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
577 QName nestedUnion2QName = nestedUnion2.getQName();
578 assertEquals(URI.create("urn:simple.types.data.demo"), nestedUnion2QName.getNamespace());
579 assertEquals(TestUtils.createDate("2013-02-27"), nestedUnion2QName.getRevision());
580 assertEquals("t2", nestedUnion2QName.getPrefix());
581 assertEquals("nested-union2", nestedUnion2QName.getLocalName());
583 UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
584 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
585 assertEquals(2, nestedUnion2Types.size());
586 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
587 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
589 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
590 QName myUnionExtQName = myUnionExt.getQName();
591 assertEquals(URI.create("urn:simple.types.data.demo"), myUnionExtQName.getNamespace());
592 assertEquals(TestUtils.createDate("2013-02-27"), myUnionExtQName.getRevision());
593 assertEquals("t2", myUnionExtQName.getPrefix());
594 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
596 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
597 QName myUnionQName = myUnion.getQName();
598 assertEquals(URI.create("urn:simple.types.data.demo"), myUnionQName.getNamespace());
599 assertEquals(TestUtils.createDate("2013-02-27"), myUnionQName.getRevision());
600 assertEquals("t2", myUnionQName.getPrefix());
601 assertEquals("my-union", myUnionQName.getLocalName());
603 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
604 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
605 assertEquals(2, myUnionBaseTypes.size());
606 assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16);
607 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
608 ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0);
609 List<RangeConstraint> ranges = int16.getRanges();
610 assertEquals(1, ranges.size());
611 RangeConstraint range = ranges.get(0);
612 assertEquals(1L, range.getMin());
613 assertEquals(100L, range.getMax());
617 public void testChoice() {
618 Module testModule = TestUtils.findModule(modules, "types1");
619 ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName("transfer");
620 ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
621 Set<ChoiceCaseNode> cases = how.getCases();
622 assertEquals(5, cases.size());
623 ChoiceCaseNode input = null;
624 ChoiceCaseNode output = null;
625 for (ChoiceCaseNode caseNode : cases) {
626 if ("input".equals(caseNode.getQName().getLocalName())) {
628 } else if ("output".equals(caseNode.getQName().getLocalName())) {
632 assertNotNull(input);
633 assertNotNull(input.getPath());
634 assertNotNull(output);
635 assertNotNull(output.getPath());
639 public void testAnyXml() {
640 Module testModule = TestUtils.findModule(modules, "types1");
641 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
646 public void testDeviation() {
647 Module testModule = TestUtils.findModule(modules, "types1");
648 Set<Deviation> deviations = testModule.getDeviations();
649 assertEquals(1, deviations.size());
651 Deviation dev = deviations.iterator().next();
652 SchemaPath expectedPath = TestUtils.createPath(true, null, null, "data", "system", "user");
653 assertEquals(expectedPath, dev.getTargetPath());
654 assertEquals(Deviate.ADD, dev.getDeviate());
658 public void testUnknownNode() {
659 Module testModule = TestUtils.findModule(modules, "types3");
660 ContainerSchemaNode network = (ContainerSchemaNode) testModule.getDataChildByName("network");
661 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
662 assertEquals(1, unknownNodes.size());
663 UnknownSchemaNode unknownNode = unknownNodes.get(0);
664 assertNotNull(unknownNode.getNodeType());
665 assertEquals("point", unknownNode.getNodeParameter());
669 public void testFeature() {
670 Module testModule = TestUtils.findModule(modules, "types3");
671 Set<FeatureDefinition> features = testModule.getFeatures();
672 assertEquals(1, features.size());
676 public void testExtension() {
677 Module testModule = TestUtils.findModule(modules, "types3");
678 List<ExtensionDefinition> extensions = testModule.getExtensionSchemaNodes();
679 assertEquals(1, extensions.size());
680 ExtensionDefinition extension = extensions.get(0);
681 assertEquals("name", extension.getArgument());
682 assertFalse(extension.isYinElement());
686 public void testNotification() {
687 Module testModule = TestUtils.findModule(modules, "types3");
688 URI expectedNamespace = URI.create("urn:simple.container.demo.test");
689 String expectedPrefix = "t3";
690 Date expectedRevision = TestUtils.createDate("2013-02-27");
692 Set<NotificationDefinition> notifications = testModule.getNotifications();
693 assertEquals(1, notifications.size());
695 NotificationDefinition notification = notifications.iterator().next();
696 // test SchemaNode args
697 QName expectedQName = new QName(expectedNamespace, expectedRevision, expectedPrefix, "event");
698 assertEquals(expectedQName, notification.getQName());
699 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, expectedRevision, expectedPrefix,
701 assertEquals(expectedPath, notification.getPath());
702 assertNull(notification.getDescription());
703 assertNull(notification.getReference());
704 assertEquals(Status.CURRENT, notification.getStatus());
705 assertEquals(0, notification.getUnknownSchemaNodes().size());
706 // test DataNodeContainer args
707 assertEquals(0, notification.getTypeDefinitions().size());
708 assertEquals(3, notification.getChildNodes().size());
709 assertEquals(0, notification.getGroupings().size());
710 assertEquals(0, notification.getUses().size());
712 LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
713 assertTrue(eventClass.getType() instanceof StringType);
714 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity");
715 assertNotNull(reportingEntity);
716 LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
717 assertTrue(severity.getType() instanceof StringType);
721 public void testRpc() {
722 Module testModule = TestUtils.findModule(modules, "types3");
724 Set<RpcDefinition> rpcs = testModule.getRpcs();
725 assertEquals(1, rpcs.size());
727 RpcDefinition rpc = rpcs.iterator().next();
728 assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
729 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
731 ContainerSchemaNode input = rpc.getInput();
732 assertNotNull(input.getDataChildByName("source"));
733 assertNotNull(input.getDataChildByName("filter"));
734 ContainerSchemaNode output = rpc.getOutput();
735 assertNotNull(output.getDataChildByName("data"));
739 public void testAugmentNodesTypesSchemaPath() throws Exception {
740 Module testModule = TestUtils.findModule(modules, "types1");
741 Set<AugmentationSchema> augments = testModule.getAugmentations();
742 assertEquals(1, augments.size());
743 AugmentationSchema augment = augments.iterator().next();
745 LeafSchemaNode ifcId = (LeafSchemaNode) augment.getDataChildByName("interface-id");
746 Leafref ifcIdType = (Leafref) ifcId.getType();
747 SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
748 List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
750 URI types1URI = URI.create("urn:simple.container.demo");
751 URI types2URI = URI.create("urn:simple.types.data.demo");
752 URI types3URI = URI.create("urn:simple.container.demo.test");
753 Date expectedDate = simpleDateFormat.parse("2013-02-27");
755 QName q0 = new QName(types2URI, expectedDate, "data", "interfaces");
756 QName q1 = new QName(types2URI, expectedDate, "data", "ifEntry");
757 QName q2 = new QName(types3URI, expectedDate, "data", "augment-holder");
758 QName q3 = new QName(types1URI, expectedDate, "data", "interface-id");
759 assertEquals(q0, ifcIdTypePath.get(0));
760 assertEquals(q1, ifcIdTypePath.get(1));
761 assertEquals(q2, ifcIdTypePath.get(2));
762 assertEquals(q3, ifcIdTypePath.get(3));
764 LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment.getDataChildByName("higher-layer-if");
765 Leafref higherLayerType = (Leafref) higherLayer.getType();
766 SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
767 List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
768 assertEquals(q0, higherLayerTypePath.get(0));
769 assertEquals(q1, higherLayerTypePath.get(1));
770 assertEquals(q2, higherLayerTypePath.get(2));
771 q3 = new QName(types1URI, expectedDate, "data", "higher-layer-if");
772 assertEquals(q3, higherLayerTypePath.get(3));
774 LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type");
775 ExtendedType leafType = (ExtendedType) myType.getType();
777 testModule = TestUtils.findModule(modules, "types2");
778 TypeDefinition<?> typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "my-type1");
780 assertEquals(typedef, leafType);
784 public void testTypePath() throws ParseException {
785 Module test = TestUtils.findModule(modules, "types2");
786 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
788 // my-base-int32-type
789 ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types, "my-base-int32-type");
790 QName int32TypedefQName = int32Typedef.getQName();
792 URI expectedNS = URI.create("urn:simple.types.data.demo");
793 Date expectedDate = simpleDateFormat.parse("2013-02-27");
794 assertEquals(expectedNS, int32TypedefQName.getNamespace());
795 assertEquals(expectedDate, int32TypedefQName.getRevision());
796 assertEquals("t2", int32TypedefQName.getPrefix());
797 assertEquals("my-base-int32-type", int32TypedefQName.getLocalName());
799 SchemaPath typeSchemaPath = int32Typedef.getPath();
800 List<QName> typePath = typeSchemaPath.getPath();
801 assertEquals(1, typePath.size());
802 assertEquals(int32TypedefQName, typePath.get(0));
804 // my-base-int32-type/int32
805 Int32 int32 = (Int32) int32Typedef.getBaseType();
806 QName int32QName = int32.getQName();
807 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), int32QName.getNamespace());
808 assertNull(int32QName.getRevision());
809 assertEquals("", int32QName.getPrefix());
810 assertEquals("int32", int32QName.getLocalName());
812 SchemaPath int32SchemaPath = int32.getPath();
813 List<QName> int32Path = int32SchemaPath.getPath();
814 assertEquals(3, int32Path.size());
815 assertEquals(int32TypedefQName, int32Path.get(0));
816 assertEquals(int32QName, int32Path.get(2));
820 public void testTypePath2() throws ParseException {
821 Module test = TestUtils.findModule(modules, "types2");
822 Set<TypeDefinition<?>> types = test.getTypeDefinitions();
824 // my-base-int32-type
825 ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
826 QName myDecTypeQName = myDecType.getQName();
828 URI expectedNS = URI.create("urn:simple.types.data.demo");
829 Date expectedDate = simpleDateFormat.parse("2013-02-27");
830 assertEquals(expectedNS, myDecTypeQName.getNamespace());
831 assertEquals(expectedDate, myDecTypeQName.getRevision());
832 assertEquals("t2", myDecTypeQName.getPrefix());
833 assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
835 SchemaPath typeSchemaPath = myDecType.getPath();
836 List<QName> typePath = typeSchemaPath.getPath();
837 assertEquals(1, typePath.size());
838 assertEquals(myDecTypeQName, typePath.get(0));
840 // my-base-int32-type/int32
841 Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
842 QName dec64QName = dec64.getQName();
844 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
845 assertNull(dec64QName.getRevision());
846 assertEquals("", dec64QName.getPrefix());
847 assertEquals("decimal64", dec64QName.getLocalName());
849 SchemaPath dec64SchemaPath = dec64.getPath();
850 List<QName> dec64Path = dec64SchemaPath.getPath();
851 assertEquals(2, dec64Path.size());
852 assertEquals(myDecTypeQName, dec64Path.get(0));
853 assertEquals(dec64QName, dec64Path.get(1));