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.SimpleDateFormat;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.HashSet;
19 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.yang.common.QName;
26 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
27 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
28 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.controller.yang.model.api.ChoiceNode;
30 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
31 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
33 import org.opendaylight.controller.yang.model.api.Deviation;
34 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
35 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
36 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
37 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
38 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
39 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
40 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
41 import org.opendaylight.controller.yang.model.api.Module;
42 import org.opendaylight.controller.yang.model.api.ModuleImport;
43 import org.opendaylight.controller.yang.model.api.MustDefinition;
44 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
45 import org.opendaylight.controller.yang.model.api.RpcDefinition;
46 import org.opendaylight.controller.yang.model.api.SchemaNode;
47 import org.opendaylight.controller.yang.model.api.SchemaPath;
48 import org.opendaylight.controller.yang.model.api.Status;
49 import org.opendaylight.controller.yang.model.api.TypeDefinition;
50 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
51 import org.opendaylight.controller.yang.model.api.UsesNode;
52 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
53 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
54 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
55 import org.opendaylight.controller.yang.model.util.Decimal64;
56 import org.opendaylight.controller.yang.model.util.ExtendedType;
57 import org.opendaylight.controller.yang.model.util.Int16;
58 import org.opendaylight.controller.yang.model.util.Int32;
59 import org.opendaylight.controller.yang.model.util.Leafref;
60 import org.opendaylight.controller.yang.model.util.StringType;
61 import org.opendaylight.controller.yang.model.util.Uint32;
62 import org.opendaylight.controller.yang.model.util.UnionType;
64 public class YangParserTest {
65 private Set<Module> modules;
68 public void init() throws FileNotFoundException {
69 modules = TestUtils.loadModules("src/test/resources/model");
70 assertEquals(3, modules.size());
74 public void testHeaders() {
75 Module test = TestUtils.findModule(modules, "types1");
77 assertEquals("types1", test.getName());
78 assertEquals("1", test.getYangVersion());
79 URI expectedNamespace = URI.create("urn:simple.container.demo");
80 assertEquals(expectedNamespace, test.getNamespace());
81 assertEquals("t1", test.getPrefix());
83 Set<ModuleImport> imports = test.getImports();
84 assertEquals(2, imports.size());
86 ModuleImport import2 = TestUtils.findImport(imports, "data");
87 assertEquals("types2", import2.getModuleName());
88 assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision());
90 ModuleImport import3 = TestUtils.findImport(imports, "t3");
91 assertEquals("types3", import3.getModuleName());
92 assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision());
94 assertEquals("opendaylight", test.getOrganization());
95 assertEquals("http://www.opendaylight.org/", test.getContact());
96 Date expectedRevision = TestUtils.createDate("2013-02-27");
97 assertEquals(expectedRevision, test.getRevision());
98 assertEquals(" WILL BE DEFINED LATER", test.getReference());
102 public void testParseContainer() {
103 Module test = TestUtils.findModule(modules, "types2");
104 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
105 String expectedPrefix = "t2";
106 Date expectedRevision = TestUtils.createDate("2013-02-27");
108 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
109 .getDataChildByName("interfaces");
110 // test SchemaNode args
111 QName expectedQName = new QName(expectedNamespace, expectedRevision,
112 expectedPrefix, "interfaces");
113 assertEquals(expectedQName, interfaces.getQName());
114 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
115 expectedRevision, expectedPrefix, "interfaces");
116 assertEquals(expectedPath, interfaces.getPath());
117 assertNull(interfaces.getDescription());
118 assertNull(interfaces.getReference());
119 assertEquals(Status.CURRENT, interfaces.getStatus());
120 assertEquals(0, interfaces.getUnknownSchemaNodes().size());
121 // test DataSchemaNode args
122 assertFalse(interfaces.isAugmenting());
123 assertFalse(interfaces.isConfiguration());
124 ConstraintDefinition constraints = interfaces.getConstraints();
125 assertNull(constraints.getWhenCondition());
126 assertEquals(0, constraints.getMustConstraints().size());
127 assertFalse(constraints.isMandatory());
128 assertNull(constraints.getMinElements());
129 assertNull(constraints.getMaxElements());
130 // test AugmentationTarget args
131 assertEquals(0, interfaces.getAvailableAugmentations().size());
132 // test ContainerSchemaNode args
133 assertFalse(interfaces.isPresenceContainer());
134 // test DataNodeContainer args
135 assertEquals(0, interfaces.getTypeDefinitions().size());
136 assertEquals(1, interfaces.getChildNodes().size());
137 assertEquals(0, interfaces.getGroupings().size());
138 assertEquals(0, interfaces.getUses().size());
140 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
141 .getDataChildByName("ifEntry");
142 assertNotNull(ifEntry);
146 public void testParseList() {
147 Module test = TestUtils.findModule(modules, "types2");
148 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
149 String expectedPrefix = "t2";
150 Date expectedRevision = TestUtils.createDate("2013-02-27");
152 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
153 .getDataChildByName("interfaces");
155 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
156 .getDataChildByName("ifEntry");
157 // test SchemaNode args
158 QName expectedQName = new QName(expectedNamespace, expectedRevision,
159 expectedPrefix, "ifEntry");
160 assertEquals(expectedQName, ifEntry.getQName());
161 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
162 expectedRevision, expectedPrefix, "interfaces", "ifEntry");
163 assertEquals(expectedPath, ifEntry.getPath());
164 assertNull(ifEntry.getDescription());
165 assertNull(ifEntry.getReference());
166 assertEquals(Status.CURRENT, ifEntry.getStatus());
167 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
168 // test DataSchemaNode args
169 assertFalse(ifEntry.isAugmenting());
170 assertFalse(ifEntry.isConfiguration());
171 ConstraintDefinition constraints = ifEntry.getConstraints();
172 assertNull(constraints.getWhenCondition());
173 assertEquals(0, constraints.getMustConstraints().size());
174 assertFalse(constraints.isMandatory());
175 assertEquals(1, (int) constraints.getMinElements());
176 assertEquals(11, (int) constraints.getMaxElements());
177 // test AugmentationTarget args
178 Set<AugmentationSchema> availableAugmentations = ifEntry
179 .getAvailableAugmentations();
180 assertEquals(2, availableAugmentations.size());
181 // test ListSchemaNode args
182 List<QName> expectedKey = new ArrayList<QName>();
183 expectedKey.add(new QName(expectedNamespace, expectedRevision,
184 expectedPrefix, "ifIndex"));
185 assertEquals(expectedKey, ifEntry.getKeyDefinition());
186 assertFalse(ifEntry.isUserOrdered());
187 // test DataNodeContainer args
188 assertEquals(0, ifEntry.getTypeDefinitions().size());
189 assertEquals(4, ifEntry.getChildNodes().size());
190 assertEquals(0, ifEntry.getGroupings().size());
191 assertEquals(0, ifEntry.getUses().size());
193 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry
194 .getDataChildByName("ifIndex");
195 assertTrue(ifIndex.getType() instanceof Uint32);
196 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry
197 .getDataChildByName("ifMtu");
198 assertTrue(ifMtu.getType() instanceof Int32);
202 public void testAugmentResolving() {
204 Module module1 = TestUtils.findModule(modules, "types1");
206 Set<AugmentationSchema> module1Augmentations = module1
208 AugmentationSchema augment1 = module1Augmentations.iterator().next();
209 LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1
210 .getDataChildByName("ds0ChannelNumber");
211 assertTrue(augmentedLeafDefinition.isAugmenting());
214 Module module2 = TestUtils.findModule(modules, "types2");
216 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2
217 .getDataChildByName("interfaces");
218 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
219 .getDataChildByName("ifEntry");
220 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry
221 .getDataChildByName("augment-holder");
223 // augmentation defined in testfile1 and augmentation returned from
224 // augmented container have to be same
225 Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer
226 .getAvailableAugmentations();
227 AugmentationSchema augmentDefinition = augmentedContainerAugments
229 assertEquals(augment1, augmentDefinition);
231 LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer
232 .getDataChildByName("ds0ChannelNumber");
233 assertTrue(augmentedLeaf.isAugmenting());
234 assertEquals(augmentedLeafDefinition, augmentedLeaf);
236 Set<AugmentationSchema> ifEntryAugments = ifEntry
237 .getAvailableAugmentations();
238 assertEquals(2, ifEntryAugments.size());
241 Module module3 = TestUtils.findModule(modules, "types3");
243 Set<AugmentationSchema> module3Augmentations = module3
245 assertEquals(2, module3Augmentations.size());
246 AugmentationSchema augment3 = null;
247 for (AugmentationSchema as : module3Augmentations) {
248 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
252 ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
253 .getDataChildByName("augment-holder");
254 assertTrue(augmentedContainerDefinition.isAugmenting());
257 assertEquals(augmentedContainer, augmentedContainerDefinition);
258 assertEquals(augmentedLeaf, augmentedLeafDefinition);
262 public void testAugmentTarget() {
263 Module test = TestUtils.findModule(modules, "types2");
265 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
266 .getDataChildByName("interfaces");
267 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
268 .getDataChildByName("ifEntry");
269 Set<AugmentationSchema> augmentations = ifEntry
270 .getAvailableAugmentations();
271 assertEquals(2, augmentations.size());
273 AugmentationSchema augment = null;
274 for (AugmentationSchema as : augmentations) {
275 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
279 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment
280 .getDataChildByName("augment-holder");
281 assertNotNull(augmentHolder);
282 assertTrue(augmentHolder.isAugmenting());
283 QName augmentHolderQName = augmentHolder.getQName();
284 assertEquals("augment-holder", augmentHolderQName.getLocalName());
285 assertEquals("t3", augmentHolderQName.getPrefix());
286 assertEquals("Description for augment holder",
287 augmentHolder.getDescription());
291 public void testTypedefRangesResolving() {
292 Module testModule = TestUtils.findModule(modules, "types1");
294 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
295 .getDataChildByName("testleaf");
296 ExtendedType leafType = (ExtendedType) testleaf.getType();
297 assertEquals("my-type1", leafType.getQName().getLocalName());
298 assertEquals("t2", leafType.getQName().getPrefix());
299 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
300 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
301 assertEquals("t2", baseType.getQName().getPrefix());
303 List<RangeConstraint> ranges = leafType.getRanges();
304 assertEquals(1, ranges.size());
305 RangeConstraint range = ranges.get(0);
306 assertEquals(3L, range.getMin());
307 assertEquals(20L, range.getMax());
311 public void testTypedefPatternsResolving() {
312 Module testModule = TestUtils.findModule(modules, "types1");
314 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
315 .getDataChildByName("test-string-leaf");
316 ExtendedType testleafType = (ExtendedType) testleaf.getType();
317 QName testleafTypeQName = testleafType.getQName();
318 assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
319 assertEquals("t2", testleafTypeQName.getPrefix());
321 Set<String> expectedRegex = new HashSet<String>();
322 expectedRegex.add("[a-k]*");
323 expectedRegex.add("[b-u]*");
324 expectedRegex.add("[e-z]*");
326 Set<String> actualRegex = new HashSet<String>();
327 List<PatternConstraint> patterns = testleafType.getPatterns();
328 assertEquals(3, patterns.size());
329 for (PatternConstraint pc : patterns) {
330 actualRegex.add(pc.getRegularExpression());
332 assertEquals(expectedRegex, actualRegex);
334 TypeDefinition<?> baseType = testleafType.getBaseType();
335 assertEquals("my-string-type2", baseType.getQName().getLocalName());
337 List<LengthConstraint> lengths = testleafType.getLengths();
338 assertEquals(1, lengths.size());
340 LengthConstraint length = lengths.get(0);
341 assertEquals(5L, length.getMin());
342 assertEquals(10L, length.getMax());
346 public void testTypedefLengthsResolving() {
347 Module testModule = TestUtils.findModule(modules, "types1");
349 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
350 .getDataChildByName("leaf-with-length");
351 ExtendedType testleafType = (ExtendedType) testleaf.getType();
352 assertEquals("my-string-type", testleafType.getQName().getLocalName());
354 List<LengthConstraint> lengths = testleafType.getLengths();
355 assertEquals(1, lengths.size());
357 LengthConstraint length = lengths.get(0);
358 assertEquals(7L, length.getMin());
359 assertEquals(10L, length.getMax());
363 public void testTypeDef() {
364 Module testModule = TestUtils.findModule(modules, "types2");
366 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
367 .getDataChildByName("nested-type-leaf");
368 ExtendedType testleafType = (ExtendedType) testleaf.getType();
369 assertEquals("my-type1", testleafType.getQName().getLocalName());
371 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
372 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
374 Int32 int32base = (Int32) baseType.getBaseType();
375 List<RangeConstraint> ranges = int32base.getRangeStatements();
376 assertEquals(1, ranges.size());
377 RangeConstraint range = ranges.get(0);
378 assertEquals(2L, range.getMin());
379 assertEquals(20L, range.getMax());
383 public void testTypedefDecimal1() {
384 Module testModule = TestUtils.findModule(modules, "types1");
386 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
387 .getDataChildByName("test-decimal-leaf");
388 ExtendedType type = (ExtendedType) testleaf.getType();
389 assertEquals(4, (int) type.getFractionDigits());
391 Decimal64 baseType = (Decimal64) type.getBaseType();
392 assertEquals(6, (int) baseType.getFractionDigits());
396 public void testTypedefDecimal2() {
397 Module testModule = TestUtils.findModule(modules, "types1");
399 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
400 .getDataChildByName("test-decimal-leaf2");
401 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
402 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
403 Decimal64 baseTypeCast = (Decimal64) baseType;
404 assertEquals(5, (int) baseTypeCast.getFractionDigits());
408 public void testTypedefUnion() {
409 Module testModule = TestUtils.findModule(modules, "types1");
411 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
412 .getDataChildByName("union-leaf");
413 ExtendedType testleafType = (ExtendedType) testleaf.getType();
414 assertEquals("my-union-ext", testleafType.getQName().getLocalName());
416 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
417 assertEquals("my-union", baseType.getQName().getLocalName());
419 UnionType unionBase = (UnionType) baseType.getBaseType();
421 List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
422 Int16 unionType1 = (Int16) unionTypes.get(0);
423 List<RangeConstraint> ranges = unionType1.getRangeStatements();
424 assertEquals(1, ranges.size());
425 RangeConstraint range = ranges.get(0);
426 assertEquals(1L, range.getMin());
427 assertEquals(100L, range.getMax());
429 assertTrue(unionTypes.get(0) instanceof Int16);
430 assertTrue(unionTypes.get(1) instanceof Int32);
434 public void testNestedUnionResolving1() {
435 Module testModule = TestUtils.findModule(modules, "types1");
437 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
438 .getDataChildByName("nested-union-leaf");
440 ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
441 assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
443 ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
444 assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
446 UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
447 List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
448 assertEquals(2, unionTypes.size());
449 assertTrue(unionTypes.get(0) instanceof StringType);
450 assertTrue(unionTypes.get(1) instanceof ExtendedType);
452 ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
453 ExtendedType extendedUnionBase = (ExtendedType) extendedUnion
455 assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
457 UnionType extendedTargetUnion = (UnionType) extendedUnionBase
459 List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion
461 assertTrue(extendedTargetTypes.get(0) instanceof Int16);
462 assertTrue(extendedTargetTypes.get(1) instanceof Int32);
464 Int16 int16 = (Int16) extendedTargetTypes.get(0);
465 List<RangeConstraint> ranges = int16.getRangeStatements();
466 assertEquals(1, ranges.size());
467 RangeConstraint range = ranges.get(0);
468 assertEquals(1L, range.getMin());
469 assertEquals(100L, range.getMax());
473 public void testNestedUnionResolving2() {
474 Module testModule = TestUtils.findModule(modules, "types1");
476 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
477 .getDataChildByName("custom-union-leaf");
479 ExtendedType testleafType = (ExtendedType) testleaf.getType();
480 QName testleafTypeQName = testleafType.getQName();
481 assertEquals(URI.create("urn:simple.container.demo.test"),
482 testleafTypeQName.getNamespace());
483 assertEquals(TestUtils.createDate("2013-02-27"),
484 testleafTypeQName.getRevision());
485 assertEquals("t3", testleafTypeQName.getPrefix());
486 assertEquals("union1", testleafTypeQName.getLocalName());
488 ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
489 QName union2QName = union2.getQName();
490 assertEquals(URI.create("urn:simple.container.demo.test"),
491 union2QName.getNamespace());
492 assertEquals(TestUtils.createDate("2013-02-27"),
493 union2QName.getRevision());
494 assertEquals("t3", union2QName.getPrefix());
495 assertEquals("union2", union2QName.getLocalName());
497 UnionType union2Base = (UnionType) union2.getBaseType();
498 List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
499 assertEquals(2, unionTypes.size());
500 assertTrue(unionTypes.get(0) instanceof Int32);
501 assertTrue(unionTypes.get(1) instanceof ExtendedType);
503 ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
504 QName nestedUnion2QName = nestedUnion2.getQName();
505 assertEquals(URI.create("urn:simple.types.data.demo"),
506 nestedUnion2QName.getNamespace());
507 assertEquals(TestUtils.createDate("2013-02-27"),
508 nestedUnion2QName.getRevision());
509 assertEquals("t2", nestedUnion2QName.getPrefix());
510 assertEquals("nested-union2", nestedUnion2QName.getLocalName());
512 UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
513 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
514 assertEquals(2, nestedUnion2Types.size());
515 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
516 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
518 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
519 QName myUnionExtQName = myUnionExt.getQName();
520 assertEquals(URI.create("urn:simple.types.data.demo"),
521 myUnionExtQName.getNamespace());
522 assertEquals(TestUtils.createDate("2013-02-27"),
523 myUnionExtQName.getRevision());
524 assertEquals("t2", myUnionExtQName.getPrefix());
525 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
527 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
528 QName myUnionQName = myUnion.getQName();
529 assertEquals(URI.create("urn:simple.types.data.demo"),
530 myUnionQName.getNamespace());
531 assertEquals(TestUtils.createDate("2013-02-27"),
532 myUnionQName.getRevision());
533 assertEquals("t2", myUnionQName.getPrefix());
534 assertEquals("my-union", myUnionQName.getLocalName());
536 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
537 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
538 assertEquals(2, myUnionBaseTypes.size());
539 assertTrue(myUnionBaseTypes.get(0) instanceof Int16);
540 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
541 Int16 int16 = (Int16) myUnionBaseTypes.get(0);
542 List<RangeConstraint> ranges = int16.getRangeStatements();
543 assertEquals(1, ranges.size());
544 RangeConstraint range = ranges.get(0);
545 assertEquals(1L, range.getMin());
546 assertEquals(100L, range.getMax());
550 public void testRefine() {
551 Module testModule = TestUtils.findModule(modules, "types2");
553 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
554 .getDataChildByName("peer");
555 ContainerSchemaNode destination = (ContainerSchemaNode) peer
556 .getDataChildByName("destination");
557 Set<UsesNode> usesNodes = destination.getUses();
558 assertEquals(1, usesNodes.size());
559 UsesNode usesNode = usesNodes.iterator().next();
560 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
561 assertEquals(5, refines.size());
563 LeafSchemaNode refineLeaf = null;
564 ContainerSchemaNode refineContainer = null;
565 ListSchemaNode refineList = null;
566 GroupingDefinition refineGrouping = null;
567 TypeDefinition<?> typedef = null;
568 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
569 SchemaNode value = entry.getValue();
570 if (value instanceof LeafSchemaNode) {
571 refineLeaf = (LeafSchemaNode) value;
572 } else if (value instanceof ContainerSchemaNode) {
573 refineContainer = (ContainerSchemaNode) value;
574 } else if (value instanceof ListSchemaNode) {
575 refineList = (ListSchemaNode) value;
576 } else if (value instanceof GroupingDefinition) {
577 refineGrouping = (GroupingDefinition) value;
578 } else if (value instanceof TypeDefinition<?>) {
579 typedef = (TypeDefinition<?>) value;
584 assertNotNull(refineLeaf);
585 assertEquals("address", refineLeaf.getQName().getLocalName());
586 assertEquals("description of address defined by refine",
587 refineLeaf.getDescription());
588 assertEquals("address reference added by refine",
589 refineLeaf.getReference());
590 assertFalse(refineLeaf.isConfiguration());
591 assertTrue(refineLeaf.getConstraints().isMandatory());
592 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
593 .getMustConstraints();
594 assertEquals(1, leafMustConstraints.size());
595 MustDefinition leafMust = leafMustConstraints.iterator().next();
597 "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"",
598 leafMust.toString());
601 assertNotNull(refineContainer);
602 Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
603 .getMustConstraints();
604 assertTrue(mustConstraints.isEmpty());
605 assertEquals("description of port defined by refine",
606 refineContainer.getDescription());
607 assertEquals("port reference added by refine",
608 refineContainer.getReference());
609 assertFalse(refineContainer.isConfiguration());
610 assertTrue(refineContainer.isPresenceContainer());
613 assertNotNull(refineList);
614 assertEquals("description of addresses defined by refine",
615 refineList.getDescription());
616 assertEquals("addresses reference added by refine",
617 refineList.getReference());
618 assertFalse(refineList.isConfiguration());
619 assertEquals(2, (int) refineList.getConstraints().getMinElements());
620 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
622 // grouping target-inner
623 assertNotNull(refineGrouping);
624 Set<DataSchemaNode> refineGroupingChildren = refineGrouping
626 assertEquals(1, refineGroupingChildren.size());
627 LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren
629 assertEquals("inner-grouping-id", refineGroupingLeaf.getQName()
631 assertEquals("new target-inner grouping description",
632 refineGrouping.getDescription());
634 // typedef group-type
635 assertNotNull(typedef);
636 assertEquals("new group-type description", typedef.getDescription());
637 assertEquals("new group-type reference", typedef.getReference());
638 assertTrue(typedef.getBaseType() instanceof ExtendedType);
642 public void testChoice() {
643 Module testModule = TestUtils.findModule(modules, "types1");
644 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
645 .getDataChildByName("transfer");
646 ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
647 Set<ChoiceCaseNode> cases = how.getCases();
648 assertEquals(3, cases.size());
652 public void testAnyXml() {
653 Module testModule = TestUtils.findModule(modules, "types1");
654 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule
655 .getDataChildByName("data");
660 public void testDeviation() {
661 Module testModule = TestUtils.findModule(modules, "types1");
662 Set<Deviation> deviations = testModule.getDeviations();
663 assertEquals(1, deviations.size());
665 Deviation dev = deviations.iterator().next();
666 SchemaPath expectedPath = TestUtils.createPath(true, null, null,
667 "data", "system", "user");
668 assertEquals(expectedPath, dev.getTargetPath());
669 assertEquals(Deviate.ADD, dev.getDeviate());
673 public void testUnknownNode() {
674 Module testModule = TestUtils.findModule(modules, "types3");
675 ContainerSchemaNode network = (ContainerSchemaNode) testModule
676 .getDataChildByName("network");
677 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
678 assertEquals(1, unknownNodes.size());
679 UnknownSchemaNode unknownNode = unknownNodes.get(0);
680 assertNotNull(unknownNode.getNodeType());
681 assertEquals("point", unknownNode.getNodeParameter());
685 public void testFeature() {
686 Module testModule = TestUtils.findModule(modules, "types3");
687 Set<FeatureDefinition> features = testModule.getFeatures();
688 assertEquals(1, features.size());
692 public void testExtension() {
693 Module testModule = TestUtils.findModule(modules, "types3");
694 List<ExtensionDefinition> extensions = testModule
695 .getExtensionSchemaNodes();
696 assertEquals(1, extensions.size());
697 ExtensionDefinition extension = extensions.get(0);
698 assertEquals("name", extension.getArgument());
699 assertFalse(extension.isYinElement());
703 public void testNotification() {
704 Module testModule = TestUtils.findModule(modules, "types3");
705 URI expectedNamespace = URI.create("urn:simple.container.demo.test");
706 String expectedPrefix = "t3";
707 Date expectedRevision = TestUtils.createDate("2013-02-27");
709 Set<NotificationDefinition> notifications = testModule
711 assertEquals(1, notifications.size());
713 NotificationDefinition notification = notifications.iterator().next();
714 // test SchemaNode args
715 QName expectedQName = new QName(expectedNamespace, expectedRevision,
716 expectedPrefix, "event");
717 assertEquals(expectedQName, notification.getQName());
718 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
719 expectedRevision, expectedPrefix, "event");
720 assertEquals(expectedPath, notification.getPath());
721 assertNull(notification.getDescription());
722 assertNull(notification.getReference());
723 assertEquals(Status.CURRENT, notification.getStatus());
724 assertEquals(0, notification.getUnknownSchemaNodes().size());
725 // test DataNodeContainer args
726 assertEquals(0, notification.getTypeDefinitions().size());
727 assertEquals(3, notification.getChildNodes().size());
728 assertEquals(0, notification.getGroupings().size());
729 assertEquals(0, notification.getUses().size());
731 LeafSchemaNode eventClass = (LeafSchemaNode) notification
732 .getDataChildByName("event-class");
733 assertTrue(eventClass.getType() instanceof StringType);
734 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification
735 .getDataChildByName("reporting-entity");
736 assertNotNull(reportingEntity);
737 LeafSchemaNode severity = (LeafSchemaNode) notification
738 .getDataChildByName("severity");
739 assertTrue(severity.getType() instanceof StringType);
743 public void testRpc() {
744 Module testModule = TestUtils.findModule(modules, "types3");
746 Set<RpcDefinition> rpcs = testModule.getRpcs();
747 assertEquals(1, rpcs.size());
749 RpcDefinition rpc = rpcs.iterator().next();
750 assertEquals("Retrieve all or part of a specified configuration.",
751 rpc.getDescription());
752 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
754 ContainerSchemaNode input = rpc.getInput();
755 assertNotNull(input.getDataChildByName("source"));
756 assertNotNull(input.getDataChildByName("filter"));
757 ContainerSchemaNode output = rpc.getOutput();
758 assertNotNull(output.getDataChildByName("data"));
762 public void testGrouping() {
763 Module testModule = TestUtils.findModule(modules, "types2");
764 Set<GroupingDefinition> groupings = testModule.getGroupings();
765 assertEquals(1, groupings.size());
766 GroupingDefinition grouping = groupings.iterator().next();
767 Set<DataSchemaNode> children = grouping.getChildNodes();
768 assertEquals(5, children.size());
772 public void testAugmentNodesTypesSchemaPath() throws Exception {
773 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
774 Module testModule = TestUtils.findModule(modules, "types1");
775 Set<AugmentationSchema> augments = testModule.getAugmentations();
776 assertEquals(1, augments.size());
777 AugmentationSchema augment = augments.iterator().next();
779 LeafSchemaNode ifcId = (LeafSchemaNode) augment
780 .getDataChildByName("interface-id");
781 Leafref ifcIdType = (Leafref) ifcId.getType();
782 SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
783 List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
784 QName q0 = new QName(new URI("urn:simple.types.data.demo"),
785 simpleDateFormat.parse("2013-02-27"), "data", "interfaces");
786 QName q1 = new QName(new URI("urn:simple.types.data.demo"),
787 simpleDateFormat.parse("2013-02-27"), "data", "ifEntry");
788 QName q2 = new QName(new URI("urn:simple.container.demo.test"),
789 simpleDateFormat.parse("2013-02-27"), "data", "augment-holder");
790 QName q3 = new QName(new URI("urn:simple.container.demo"),
791 simpleDateFormat.parse("2013-02-27"), "data", "interface-id");
792 assertEquals(q0, ifcIdTypePath.get(0));
793 assertEquals(q1, ifcIdTypePath.get(1));
794 assertEquals(q2, ifcIdTypePath.get(2));
795 assertEquals(q3, ifcIdTypePath.get(3));
797 LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment
798 .getDataChildByName("higher-layer-if");
799 Leafref higherLayerType = (Leafref) higherLayer.getType();
800 SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
801 List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
802 assertEquals(q0, higherLayerTypePath.get(0));
803 assertEquals(q1, higherLayerTypePath.get(1));
804 assertEquals(q2, higherLayerTypePath.get(2));
805 q3 = new QName(new URI("urn:simple.container.demo"),
806 simpleDateFormat.parse("2013-02-27"), "data", "higher-layer-if");
807 assertEquals(q3, higherLayerTypePath.get(3));