ba31e51922a62c2d3fc6b352a2e3aa8110d3d216
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / parser / impl / YangParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.yang.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.io.FileNotFoundException;
13 import java.net.URI;
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;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.yang.common.QName;
26 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
27 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
28 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.controller.yang.model.api.ChoiceNode;
30 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
31 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
33 import org.opendaylight.controller.yang.model.api.Deviation;
34 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
35 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
36 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
37 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
38 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
39 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
40 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
41 import org.opendaylight.controller.yang.model.api.Module;
42 import org.opendaylight.controller.yang.model.api.ModuleImport;
43 import org.opendaylight.controller.yang.model.api.MustDefinition;
44 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
45 import org.opendaylight.controller.yang.model.api.RpcDefinition;
46 import org.opendaylight.controller.yang.model.api.SchemaNode;
47 import org.opendaylight.controller.yang.model.api.SchemaPath;
48 import org.opendaylight.controller.yang.model.api.Status;
49 import org.opendaylight.controller.yang.model.api.TypeDefinition;
50 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
51 import org.opendaylight.controller.yang.model.api.UsesNode;
52 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
53 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
54 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
55 import org.opendaylight.controller.yang.model.util.Decimal64;
56 import org.opendaylight.controller.yang.model.util.ExtendedType;
57 import org.opendaylight.controller.yang.model.util.Int16;
58 import org.opendaylight.controller.yang.model.util.Int32;
59 import org.opendaylight.controller.yang.model.util.Int8;
60 import org.opendaylight.controller.yang.model.util.Leafref;
61 import org.opendaylight.controller.yang.model.util.StringType;
62 import org.opendaylight.controller.yang.model.util.Uint32;
63 import org.opendaylight.controller.yang.model.util.UnionType;
64
65 public class YangParserTest {
66     private final DateFormat simpleDateFormat = new SimpleDateFormat(
67             "yyyy-MM-dd");
68
69     private Set<Module> modules;
70
71     @Before
72     public void init() throws FileNotFoundException {
73         modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
74         assertEquals(3, modules.size());
75     }
76
77     @Test
78     public void testHeaders() {
79         Module test = TestUtils.findModule(modules, "types1");
80
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());
86
87         Set<ModuleImport> imports = test.getImports();
88         assertEquals(2, imports.size());
89
90         ModuleImport import2 = TestUtils.findImport(imports, "data");
91         assertEquals("types2", import2.getModuleName());
92         assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision());
93
94         ModuleImport import3 = TestUtils.findImport(imports, "t3");
95         assertEquals("types3", import3.getModuleName());
96         assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision());
97
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());
103     }
104
105     @Test
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");
111
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());
143
144         ListSchemaNode ifEntry = (ListSchemaNode) interfaces
145                 .getDataChildByName("ifEntry");
146         assertNotNull(ifEntry);
147     }
148
149     @Test
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");
155
156         ContainerSchemaNode interfaces = (ContainerSchemaNode) test
157                 .getDataChildByName("interfaces");
158
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());
196
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);
203     }
204
205     @Test
206     public void testParseLeaf() throws ParseException {
207         Module test = TestUtils.findModule(modules, "types2");
208
209         // leaf if-name
210         LeafSchemaNode ifName = (LeafSchemaNode) test
211                 .getDataChildByName("if-name");
212         Leafref ifNameType = (Leafref) ifName.getType();
213         QName qname = ifNameType.getQName();
214
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());
220
221         // leaf name
222         LeafSchemaNode name = (LeafSchemaNode) test.getDataChildByName("name");
223         StringType nameType = (StringType) name.getType();
224         QName nameQName = nameType.getQName();
225
226         assertEquals(baseYangTypeNS, nameQName.getNamespace());
227         assertNull(nameQName.getRevision());
228         assertEquals("", nameQName.getPrefix());
229         assertEquals("string", nameQName.getLocalName());
230
231         // leaf count
232         LeafSchemaNode count = (LeafSchemaNode) test
233                 .getDataChildByName("count");
234         ExtendedType countType = (ExtendedType) count.getType();
235         QName countTypeQName = countType.getQName();
236
237         URI expectedNS = URI.create("urn:simple.types.data.demo");
238         Date expectedDate = simpleDateFormat.parse("2013-02-27");
239         assertEquals(expectedNS, countTypeQName.getNamespace());
240         assertEquals(expectedDate, countTypeQName.getRevision());
241         assertEquals("t2", countTypeQName.getPrefix());
242         assertEquals("int8", countTypeQName.getLocalName());
243
244         Int8 countTypeBase = (Int8) countType.getBaseType();
245         QName countTypeBaseQName = countTypeBase.getQName();
246
247         assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace());
248         assertNull(countTypeBaseQName.getRevision());
249         assertEquals("", countTypeBaseQName.getPrefix());
250         assertEquals("int8", countTypeBaseQName.getLocalName());
251     }
252
253     @Test
254     public void testAugmentResolving() {
255         // testfile1
256         Module module1 = TestUtils.findModule(modules, "types1");
257
258         Set<AugmentationSchema> module1Augmentations = module1
259                 .getAugmentations();
260         AugmentationSchema augment1 = module1Augmentations.iterator().next();
261         LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1
262                 .getDataChildByName("ds0ChannelNumber");
263         assertTrue(augmentedLeafDefinition.isAugmenting());
264
265         // testfile2
266         Module module2 = TestUtils.findModule(modules, "types2");
267
268         ContainerSchemaNode interfaces = (ContainerSchemaNode) module2
269                 .getDataChildByName("interfaces");
270         ListSchemaNode ifEntry = (ListSchemaNode) interfaces
271                 .getDataChildByName("ifEntry");
272         ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry
273                 .getDataChildByName("augment-holder");
274
275         // augmentation defined in testfile1 and augmentation returned from
276         // augmented container have to be same
277         Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer
278                 .getAvailableAugmentations();
279         AugmentationSchema augmentDefinition = augmentedContainerAugments
280                 .iterator().next();
281         assertEquals(augment1, augmentDefinition);
282
283         LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer
284                 .getDataChildByName("ds0ChannelNumber");
285         assertTrue(augmentedLeaf.isAugmenting());
286         assertEquals(augmentedLeafDefinition, augmentedLeaf);
287
288         Set<AugmentationSchema> ifEntryAugments = ifEntry
289                 .getAvailableAugmentations();
290         assertEquals(2, ifEntryAugments.size());
291
292         // testfile3
293         Module module3 = TestUtils.findModule(modules, "types3");
294
295         Set<AugmentationSchema> module3Augmentations = module3
296                 .getAugmentations();
297         assertEquals(2, module3Augmentations.size());
298         AugmentationSchema augment3 = null;
299         for (AugmentationSchema as : module3Augmentations) {
300             if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
301                 augment3 = as;
302             }
303         }
304         ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
305                 .getDataChildByName("augment-holder");
306         assertTrue(augmentedContainerDefinition.isAugmenting());
307
308         // check
309         assertEquals(augmentedContainer, augmentedContainerDefinition);
310         assertEquals(augmentedLeaf, augmentedLeafDefinition);
311     }
312
313     @Test
314     public void testAugmentTarget() {
315         Module test = TestUtils.findModule(modules, "types2");
316
317         ContainerSchemaNode interfaces = (ContainerSchemaNode) test
318                 .getDataChildByName("interfaces");
319         ListSchemaNode ifEntry = (ListSchemaNode) interfaces
320                 .getDataChildByName("ifEntry");
321         Set<AugmentationSchema> augmentations = ifEntry
322                 .getAvailableAugmentations();
323         assertEquals(2, augmentations.size());
324
325         AugmentationSchema augment = null;
326         for (AugmentationSchema as : augmentations) {
327             if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
328                 augment = as;
329             }
330         }
331         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment
332                 .getDataChildByName("augment-holder");
333         assertNotNull(augmentHolder);
334         assertTrue(augmentHolder.isAugmenting());
335         QName augmentHolderQName = augmentHolder.getQName();
336         assertEquals("augment-holder", augmentHolderQName.getLocalName());
337         assertEquals("t3", augmentHolderQName.getPrefix());
338         assertEquals("Description for augment holder",
339                 augmentHolder.getDescription());
340     }
341
342     @Test
343     public void testTypedefRangesResolving() throws ParseException {
344         Module testModule = TestUtils.findModule(modules, "types1");
345
346         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
347                 .getDataChildByName("testleaf");
348         ExtendedType leafType = (ExtendedType) testleaf.getType();
349         QName leafTypeQName = leafType.getQName();
350         assertEquals("my-type1", leafTypeQName.getLocalName());
351         assertEquals("t1", leafTypeQName.getPrefix());
352         assertEquals(URI.create("urn:simple.container.demo"),
353                 leafTypeQName.getNamespace());
354         Date expectedDate = simpleDateFormat.parse("2013-02-27");
355         assertEquals(expectedDate, leafTypeQName.getRevision());
356         assertEquals(1, leafType.getRanges().size());
357
358         ExtendedType baseType = (ExtendedType) leafType.getBaseType();
359         QName baseTypeQName = baseType.getQName();
360         assertEquals("my-type1", baseTypeQName.getLocalName());
361         assertEquals("t2", baseTypeQName.getPrefix());
362         assertEquals(URI.create("urn:simple.types.data.demo"),
363                 baseTypeQName.getNamespace());
364         assertEquals(expectedDate, baseTypeQName.getRevision());
365         assertEquals(2, baseType.getRanges().size());
366
367         List<RangeConstraint> ranges = leafType.getRanges();
368         assertEquals(1, ranges.size());
369         RangeConstraint range = ranges.get(0);
370         assertEquals(3L, range.getMin());
371         assertEquals(20L, range.getMax());
372     }
373
374     @Test
375     public void testTypedefPatternsResolving() {
376         Module testModule = TestUtils.findModule(modules, "types1");
377
378         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
379                 .getDataChildByName("test-string-leaf");
380         ExtendedType testleafType = (ExtendedType) testleaf.getType();
381         QName testleafTypeQName = testleafType.getQName();
382         assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
383         assertEquals("t2", testleafTypeQName.getPrefix());
384
385         List<PatternConstraint> patterns = testleafType.getPatterns();
386         assertEquals(1, patterns.size());
387         PatternConstraint pattern = patterns.iterator().next();
388         assertEquals("[e-z]*", pattern.getRegularExpression());
389
390         ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
391         assertEquals("my-string-type2", baseType.getQName().getLocalName());
392
393         patterns = baseType.getPatterns();
394         assertEquals(1, patterns.size());
395         pattern = patterns.iterator().next();
396         assertEquals("[b-u]*", pattern.getRegularExpression());
397
398         List<LengthConstraint> lengths = testleafType.getLengths();
399         assertTrue(lengths.isEmpty());
400     }
401
402     @Test
403     public void testTypedefLengthsResolving() {
404         Module testModule = TestUtils.findModule(modules, "types1");
405
406         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
407                 .getDataChildByName("leaf-with-length");
408         ExtendedType testleafType = (ExtendedType) testleaf.getType();
409         assertEquals("my-string-type", testleafType.getQName().getLocalName());
410
411         List<LengthConstraint> lengths = testleafType.getLengths();
412         assertEquals(1, lengths.size());
413
414         LengthConstraint length = lengths.get(0);
415         assertEquals(7L, length.getMin());
416         assertEquals(10L, length.getMax());
417     }
418
419     @Test
420     public void testTypeDef() {
421         Module testModule = TestUtils.findModule(modules, "types2");
422
423         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
424                 .getDataChildByName("nested-type-leaf");
425         ExtendedType testleafType = (ExtendedType) testleaf.getType();
426         assertEquals("my-type1", testleafType.getQName().getLocalName());
427
428         ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
429         assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
430
431         Int32 int32Type = (Int32) baseType.getBaseType();
432         QName qname = int32Type.getQName();
433         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
434                 qname.getNamespace());
435         assertNull(qname.getRevision());
436         assertEquals("", qname.getPrefix());
437         assertEquals("int32", qname.getLocalName());
438         List<RangeConstraint> ranges = baseType.getRanges();
439         assertEquals(1, ranges.size());
440         RangeConstraint range = ranges.get(0);
441         assertEquals(2L, range.getMin());
442         assertEquals(20L, range.getMax());
443     }
444
445     @Test
446     public void testTypedefDecimal1() {
447         Module testModule = TestUtils.findModule(modules, "types1");
448
449         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
450                 .getDataChildByName("test-decimal-leaf");
451         ExtendedType type = (ExtendedType) testleaf.getType();
452         assertEquals(4, (int) type.getFractionDigits());
453
454         ExtendedType typeBase = (ExtendedType) type.getBaseType();
455         assertEquals("my-decimal-type", typeBase.getQName().getLocalName());
456         assertNull(typeBase.getFractionDigits());
457
458         Decimal64 decimal = (Decimal64) typeBase.getBaseType();
459         assertEquals(6, (int) decimal.getFractionDigits());
460     }
461
462     @Test
463     public void testTypedefDecimal2() {
464         Module testModule = TestUtils.findModule(modules, "types1");
465
466         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
467                 .getDataChildByName("test-decimal-leaf2");
468         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
469         assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
470         Decimal64 baseTypeCast = (Decimal64) baseType;
471         assertEquals(5, (int) baseTypeCast.getFractionDigits());
472     }
473
474     @Test
475     public void testTypedefUnion() {
476         Module testModule = TestUtils.findModule(modules, "types1");
477
478         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
479                 .getDataChildByName("union-leaf");
480         ExtendedType testleafType = (ExtendedType) testleaf.getType();
481         assertEquals("my-union-ext", testleafType.getQName().getLocalName());
482
483         ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
484         assertEquals("my-union", baseType.getQName().getLocalName());
485
486         UnionType unionBase = (UnionType) baseType.getBaseType();
487
488         List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
489         ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
490         List<RangeConstraint> ranges = unionType1.getRanges();
491         assertEquals(1, ranges.size());
492         RangeConstraint range = ranges.get(0);
493         assertEquals(1L, range.getMin());
494         assertEquals(100L, range.getMax());
495
496         assertTrue(unionType1.getBaseType() instanceof Int16);
497         assertTrue(unionTypes.get(1) instanceof Int32);
498     }
499
500     @Test
501     public void testNestedUnionResolving1() {
502         Module testModule = TestUtils.findModule(modules, "types1");
503
504         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
505                 .getDataChildByName("nested-union-leaf");
506
507         ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
508         assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
509
510         ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
511         assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
512
513         UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
514         List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
515         assertEquals(2, unionTypes.size());
516         assertTrue(unionTypes.get(0) instanceof StringType);
517         assertTrue(unionTypes.get(1) instanceof ExtendedType);
518
519         ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
520         ExtendedType extendedUnionBase = (ExtendedType) extendedUnion
521                 .getBaseType();
522         assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
523
524         UnionType extendedTargetUnion = (UnionType) extendedUnionBase
525                 .getBaseType();
526         List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion
527                 .getTypes();
528         assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16);
529         assertTrue(extendedTargetTypes.get(1) instanceof Int32);
530
531         ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0);
532         assertTrue(int16.getBaseType() instanceof Int16);
533         List<RangeConstraint> ranges = int16.getRanges();
534         assertEquals(1, ranges.size());
535         RangeConstraint range = ranges.get(0);
536         assertEquals(1L, range.getMin());
537         assertEquals(100L, range.getMax());
538     }
539
540     @Test
541     public void testNestedUnionResolving2() {
542         Module testModule = TestUtils.findModule(modules, "types1");
543
544         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
545                 .getDataChildByName("custom-union-leaf");
546
547         ExtendedType testleafType = (ExtendedType) testleaf.getType();
548         QName testleafTypeQName = testleafType.getQName();
549         assertEquals(URI.create("urn:simple.container.demo.test"),
550                 testleafTypeQName.getNamespace());
551         assertEquals(TestUtils.createDate("2013-02-27"),
552                 testleafTypeQName.getRevision());
553         assertEquals("t3", testleafTypeQName.getPrefix());
554         assertEquals("union1", testleafTypeQName.getLocalName());
555
556         ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
557         QName union2QName = union2.getQName();
558         assertEquals(URI.create("urn:simple.container.demo.test"),
559                 union2QName.getNamespace());
560         assertEquals(TestUtils.createDate("2013-02-27"),
561                 union2QName.getRevision());
562         assertEquals("t3", union2QName.getPrefix());
563         assertEquals("union2", union2QName.getLocalName());
564
565         UnionType union2Base = (UnionType) union2.getBaseType();
566         List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
567         assertEquals(2, unionTypes.size());
568         assertTrue(unionTypes.get(0) instanceof Int32);
569         assertTrue(unionTypes.get(1) instanceof ExtendedType);
570
571         ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
572         QName nestedUnion2QName = nestedUnion2.getQName();
573         assertEquals(URI.create("urn:simple.types.data.demo"),
574                 nestedUnion2QName.getNamespace());
575         assertEquals(TestUtils.createDate("2013-02-27"),
576                 nestedUnion2QName.getRevision());
577         assertEquals("t2", nestedUnion2QName.getPrefix());
578         assertEquals("nested-union2", nestedUnion2QName.getLocalName());
579
580         UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
581         List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
582         assertEquals(2, nestedUnion2Types.size());
583         assertTrue(nestedUnion2Types.get(0) instanceof StringType);
584         assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
585
586         ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
587         QName myUnionExtQName = myUnionExt.getQName();
588         assertEquals(URI.create("urn:simple.types.data.demo"),
589                 myUnionExtQName.getNamespace());
590         assertEquals(TestUtils.createDate("2013-02-27"),
591                 myUnionExtQName.getRevision());
592         assertEquals("t2", myUnionExtQName.getPrefix());
593         assertEquals("my-union-ext", myUnionExtQName.getLocalName());
594
595         ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
596         QName myUnionQName = myUnion.getQName();
597         assertEquals(URI.create("urn:simple.types.data.demo"),
598                 myUnionQName.getNamespace());
599         assertEquals(TestUtils.createDate("2013-02-27"),
600                 myUnionQName.getRevision());
601         assertEquals("t2", myUnionQName.getPrefix());
602         assertEquals("my-union", myUnionQName.getLocalName());
603
604         UnionType myUnionBase = (UnionType) myUnion.getBaseType();
605         List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
606         assertEquals(2, myUnionBaseTypes.size());
607         assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16);
608         assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
609         ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0);
610         List<RangeConstraint> ranges = int16.getRanges();
611         assertEquals(1, ranges.size());
612         RangeConstraint range = ranges.get(0);
613         assertEquals(1L, range.getMin());
614         assertEquals(100L, range.getMax());
615     }
616
617     @Test
618     public void testRefine() {
619         Module testModule = TestUtils.findModule(modules, "types2");
620
621         ContainerSchemaNode peer = (ContainerSchemaNode) testModule
622                 .getDataChildByName("peer");
623         ContainerSchemaNode destination = (ContainerSchemaNode) peer
624                 .getDataChildByName("destination");
625         Set<UsesNode> usesNodes = destination.getUses();
626         assertEquals(1, usesNodes.size());
627         UsesNode usesNode = usesNodes.iterator().next();
628         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
629         assertEquals(5, refines.size());
630
631         LeafSchemaNode refineLeaf = null;
632         ContainerSchemaNode refineContainer = null;
633         ListSchemaNode refineList = null;
634         GroupingDefinition refineGrouping = null;
635         TypeDefinition<?> typedef = null;
636         for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
637             SchemaNode value = entry.getValue();
638             if (value instanceof LeafSchemaNode) {
639                 refineLeaf = (LeafSchemaNode) value;
640             } else if (value instanceof ContainerSchemaNode) {
641                 refineContainer = (ContainerSchemaNode) value;
642             } else if (value instanceof ListSchemaNode) {
643                 refineList = (ListSchemaNode) value;
644             } else if (value instanceof GroupingDefinition) {
645                 refineGrouping = (GroupingDefinition) value;
646             } else if (value instanceof TypeDefinition<?>) {
647                 typedef = (TypeDefinition<?>) value;
648             }
649         }
650
651         // leaf address
652         assertNotNull(refineLeaf);
653         assertEquals("address", refineLeaf.getQName().getLocalName());
654         assertEquals("description of address defined by refine",
655                 refineLeaf.getDescription());
656         assertEquals("address reference added by refine",
657                 refineLeaf.getReference());
658         assertFalse(refineLeaf.isConfiguration());
659         assertTrue(refineLeaf.getConstraints().isMandatory());
660         Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
661                 .getMustConstraints();
662         assertEquals(1, leafMustConstraints.size());
663         MustDefinition leafMust = leafMustConstraints.iterator().next();
664         assertEquals(
665                 "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"",
666                 leafMust.toString());
667
668         // container port
669         assertNotNull(refineContainer);
670         Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
671                 .getMustConstraints();
672         assertTrue(mustConstraints.isEmpty());
673         assertEquals("description of port defined by refine",
674                 refineContainer.getDescription());
675         assertEquals("port reference added by refine",
676                 refineContainer.getReference());
677         assertFalse(refineContainer.isConfiguration());
678         assertTrue(refineContainer.isPresenceContainer());
679
680         // list addresses
681         assertNotNull(refineList);
682         assertEquals("description of addresses defined by refine",
683                 refineList.getDescription());
684         assertEquals("addresses reference added by refine",
685                 refineList.getReference());
686         assertFalse(refineList.isConfiguration());
687         assertEquals(2, (int) refineList.getConstraints().getMinElements());
688         assertEquals(12, (int) refineList.getConstraints().getMaxElements());
689
690         // grouping target-inner
691         assertNotNull(refineGrouping);
692         Set<DataSchemaNode> refineGroupingChildren = refineGrouping
693                 .getChildNodes();
694         assertEquals(1, refineGroupingChildren.size());
695         LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren
696                 .iterator().next();
697         assertEquals("inner-grouping-id", refineGroupingLeaf.getQName()
698                 .getLocalName());
699         assertEquals("new target-inner grouping description",
700                 refineGrouping.getDescription());
701
702         // typedef group-type
703         assertNotNull(typedef);
704         assertEquals("new group-type description", typedef.getDescription());
705         assertEquals("new group-type reference", typedef.getReference());
706         assertTrue(typedef.getBaseType() instanceof ExtendedType);
707     }
708
709     @Test
710     public void testChoice() {
711         Module testModule = TestUtils.findModule(modules, "types1");
712         ContainerSchemaNode peer = (ContainerSchemaNode) testModule
713                 .getDataChildByName("transfer");
714         ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
715         Set<ChoiceCaseNode> cases = how.getCases();
716         assertEquals(3, cases.size());
717     }
718
719     @Test
720     public void testAnyXml() {
721         Module testModule = TestUtils.findModule(modules, "types1");
722         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule
723                 .getDataChildByName("data");
724         assertNotNull(data);
725     }
726
727     @Test
728     public void testDeviation() {
729         Module testModule = TestUtils.findModule(modules, "types1");
730         Set<Deviation> deviations = testModule.getDeviations();
731         assertEquals(1, deviations.size());
732
733         Deviation dev = deviations.iterator().next();
734         SchemaPath expectedPath = TestUtils.createPath(true, null, null,
735                 "data", "system", "user");
736         assertEquals(expectedPath, dev.getTargetPath());
737         assertEquals(Deviate.ADD, dev.getDeviate());
738     }
739
740     @Test
741     public void testUnknownNode() {
742         Module testModule = TestUtils.findModule(modules, "types3");
743         ContainerSchemaNode network = (ContainerSchemaNode) testModule
744                 .getDataChildByName("network");
745         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
746         assertEquals(1, unknownNodes.size());
747         UnknownSchemaNode unknownNode = unknownNodes.get(0);
748         assertNotNull(unknownNode.getNodeType());
749         assertEquals("point", unknownNode.getNodeParameter());
750     }
751
752     @Test
753     public void testFeature() {
754         Module testModule = TestUtils.findModule(modules, "types3");
755         Set<FeatureDefinition> features = testModule.getFeatures();
756         assertEquals(1, features.size());
757     }
758
759     @Test
760     public void testExtension() {
761         Module testModule = TestUtils.findModule(modules, "types3");
762         List<ExtensionDefinition> extensions = testModule
763                 .getExtensionSchemaNodes();
764         assertEquals(1, extensions.size());
765         ExtensionDefinition extension = extensions.get(0);
766         assertEquals("name", extension.getArgument());
767         assertFalse(extension.isYinElement());
768     }
769
770     @Test
771     public void testNotification() {
772         Module testModule = TestUtils.findModule(modules, "types3");
773         URI expectedNamespace = URI.create("urn:simple.container.demo.test");
774         String expectedPrefix = "t3";
775         Date expectedRevision = TestUtils.createDate("2013-02-27");
776
777         Set<NotificationDefinition> notifications = testModule
778                 .getNotifications();
779         assertEquals(1, notifications.size());
780
781         NotificationDefinition notification = notifications.iterator().next();
782         // test SchemaNode args
783         QName expectedQName = new QName(expectedNamespace, expectedRevision,
784                 expectedPrefix, "event");
785         assertEquals(expectedQName, notification.getQName());
786         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
787                 expectedRevision, expectedPrefix, "event");
788         assertEquals(expectedPath, notification.getPath());
789         assertNull(notification.getDescription());
790         assertNull(notification.getReference());
791         assertEquals(Status.CURRENT, notification.getStatus());
792         assertEquals(0, notification.getUnknownSchemaNodes().size());
793         // test DataNodeContainer args
794         assertEquals(0, notification.getTypeDefinitions().size());
795         assertEquals(3, notification.getChildNodes().size());
796         assertEquals(0, notification.getGroupings().size());
797         assertEquals(0, notification.getUses().size());
798
799         LeafSchemaNode eventClass = (LeafSchemaNode) notification
800                 .getDataChildByName("event-class");
801         assertTrue(eventClass.getType() instanceof StringType);
802         AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification
803                 .getDataChildByName("reporting-entity");
804         assertNotNull(reportingEntity);
805         LeafSchemaNode severity = (LeafSchemaNode) notification
806                 .getDataChildByName("severity");
807         assertTrue(severity.getType() instanceof StringType);
808     }
809
810     @Test
811     public void testRpc() {
812         Module testModule = TestUtils.findModule(modules, "types3");
813
814         Set<RpcDefinition> rpcs = testModule.getRpcs();
815         assertEquals(1, rpcs.size());
816
817         RpcDefinition rpc = rpcs.iterator().next();
818         assertEquals("Retrieve all or part of a specified configuration.",
819                 rpc.getDescription());
820         assertEquals("RFC 6241, Section 7.1", rpc.getReference());
821
822         ContainerSchemaNode input = rpc.getInput();
823         assertNotNull(input.getDataChildByName("source"));
824         assertNotNull(input.getDataChildByName("filter"));
825         ContainerSchemaNode output = rpc.getOutput();
826         assertNotNull(output.getDataChildByName("data"));
827     }
828
829     @Test
830     public void testGrouping() {
831         Module testModule = TestUtils.findModule(modules, "types2");
832         Set<GroupingDefinition> groupings = testModule.getGroupings();
833         assertEquals(1, groupings.size());
834         GroupingDefinition grouping = groupings.iterator().next();
835         Set<DataSchemaNode> children = grouping.getChildNodes();
836         assertEquals(5, children.size());
837     }
838
839     @Test
840     public void testAugmentNodesTypesSchemaPath() throws Exception {
841         Module testModule = TestUtils.findModule(modules, "types1");
842         Set<AugmentationSchema> augments = testModule.getAugmentations();
843         assertEquals(1, augments.size());
844         AugmentationSchema augment = augments.iterator().next();
845
846         LeafSchemaNode ifcId = (LeafSchemaNode) augment
847                 .getDataChildByName("interface-id");
848         Leafref ifcIdType = (Leafref) ifcId.getType();
849         SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
850         List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
851         
852         URI types1URI = URI.create("urn:simple.container.demo");
853         URI types2URI = URI.create("urn:simple.types.data.demo");
854         URI types3URI = URI.create("urn:simple.container.demo.test");
855         Date expectedDate = simpleDateFormat.parse("2013-02-27");
856         
857         QName q0 = new QName(types2URI,
858                 expectedDate, "data", "interfaces");
859         QName q1 = new QName(types2URI,
860                 expectedDate, "data", "ifEntry");
861         QName q2 = new QName(types3URI,
862                 expectedDate, "data", "augment-holder");
863         QName q3 = new QName(types1URI,
864                 expectedDate, "data", "interface-id");
865         assertEquals(q0, ifcIdTypePath.get(0));
866         assertEquals(q1, ifcIdTypePath.get(1));
867         assertEquals(q2, ifcIdTypePath.get(2));
868         assertEquals(q3, ifcIdTypePath.get(3));
869
870         LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment
871                 .getDataChildByName("higher-layer-if");
872         Leafref higherLayerType = (Leafref) higherLayer.getType();
873         SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
874         List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
875         assertEquals(q0, higherLayerTypePath.get(0));
876         assertEquals(q1, higherLayerTypePath.get(1));
877         assertEquals(q2, higherLayerTypePath.get(2));
878         q3 = new QName(types1URI,
879                 expectedDate, "data", "higher-layer-if");
880         assertEquals(q3, higherLayerTypePath.get(3));
881         
882         LeafSchemaNode myType = (LeafSchemaNode) augment
883                 .getDataChildByName("my-type");
884         ExtendedType leafType = (ExtendedType)myType.getType();
885         
886         testModule = TestUtils.findModule(modules, "types2");
887         TypeDefinition<?> typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "my-type1");
888         
889         assertEquals(typedef, leafType);
890     }
891
892     @Test
893     public void testTypePath() throws ParseException {
894         Module test = TestUtils.findModule(modules, "types2");
895         Set<TypeDefinition<?>> types = test.getTypeDefinitions();
896
897         // my-base-int32-type
898         ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types,
899                 "my-base-int32-type");
900         QName int32TypedefQName = int32Typedef.getQName();
901
902         URI expectedNS = URI.create("urn:simple.types.data.demo");
903         Date expectedDate = simpleDateFormat.parse("2013-02-27");
904         assertEquals(expectedNS, int32TypedefQName.getNamespace());
905         assertEquals(expectedDate, int32TypedefQName.getRevision());
906         assertEquals("t2", int32TypedefQName.getPrefix());
907         assertEquals("my-base-int32-type", int32TypedefQName.getLocalName());
908
909         SchemaPath typeSchemaPath = int32Typedef.getPath();
910         List<QName> typePath = typeSchemaPath.getPath();
911         assertEquals(1, typePath.size());
912         assertEquals(int32TypedefQName, typePath.get(0));
913
914         // my-base-int32-type/int32
915         Int32 int32 = (Int32) int32Typedef.getBaseType();
916         QName int32QName = int32.getQName();
917         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
918                 int32QName.getNamespace());
919         assertNull(int32QName.getRevision());
920         assertEquals("", int32QName.getPrefix());
921         assertEquals("int32", int32QName.getLocalName());
922
923         SchemaPath int32SchemaPath = int32.getPath();
924         List<QName> int32Path = int32SchemaPath.getPath();
925         assertEquals(3, int32Path.size());
926         assertEquals(int32TypedefQName, int32Path.get(0));
927         assertEquals(int32QName, int32Path.get(2));
928     }
929
930     @Test
931     public void testTypePath2() throws ParseException {
932         Module test = TestUtils.findModule(modules, "types2");
933         Set<TypeDefinition<?>> types = test.getTypeDefinitions();
934
935         // my-base-int32-type
936         ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types,
937                 "my-decimal-type");
938         QName myDecTypeQName = myDecType.getQName();
939
940         URI expectedNS = URI.create("urn:simple.types.data.demo");
941         Date expectedDate = simpleDateFormat.parse("2013-02-27");
942         assertEquals(expectedNS, myDecTypeQName.getNamespace());
943         assertEquals(expectedDate, myDecTypeQName.getRevision());
944         assertEquals("t2", myDecTypeQName.getPrefix());
945         assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
946
947         SchemaPath typeSchemaPath = myDecType.getPath();
948         List<QName> typePath = typeSchemaPath.getPath();
949         assertEquals(1, typePath.size());
950         assertEquals(myDecTypeQName, typePath.get(0));
951
952         // my-base-int32-type/int32
953         Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
954         QName dec64QName = dec64.getQName();
955
956         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
957                 dec64QName.getNamespace());
958         assertNull(dec64QName.getRevision());
959         assertEquals("", dec64QName.getPrefix());
960         assertEquals("decimal64", dec64QName.getLocalName());
961
962         SchemaPath dec64SchemaPath = dec64.getPath();
963         List<QName> dec64Path = dec64SchemaPath.getPath();
964         assertEquals(2, dec64Path.size());
965         assertEquals(myDecTypeQName, dec64Path.get(0));
966         assertEquals(dec64QName, dec64Path.get(1));
967     }
968
969 }