When a node is going down, remove edges in both directions associated with the node.
[controller.git] / opendaylight / sal / yang-prototype / yang / 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.Set;
21
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.LeafSchemaNode;
38 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
39 import org.opendaylight.controller.yang.model.api.Module;
40 import org.opendaylight.controller.yang.model.api.ModuleImport;
41 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
42 import org.opendaylight.controller.yang.model.api.RpcDefinition;
43 import org.opendaylight.controller.yang.model.api.SchemaPath;
44 import org.opendaylight.controller.yang.model.api.Status;
45 import org.opendaylight.controller.yang.model.api.TypeDefinition;
46 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
47 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
48 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
49 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
50 import org.opendaylight.controller.yang.model.util.Decimal64;
51 import org.opendaylight.controller.yang.model.util.ExtendedType;
52 import org.opendaylight.controller.yang.model.util.Int16;
53 import org.opendaylight.controller.yang.model.util.Int32;
54 import org.opendaylight.controller.yang.model.util.StringType;
55 import org.opendaylight.controller.yang.model.util.Uint32;
56 import org.opendaylight.controller.yang.model.util.UnionType;
57
58 public class YangParserTest {
59
60     private final URI nodesNS = URI.create("urn:simple.nodes.test");
61     private final URI typesNS = URI.create("urn:simple.types.test");
62     private final URI customNS = URI.create("urn:custom.nodes.test");
63     private Date nodesRev;
64     private Date typesRev;
65     private Date customRev;
66
67     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
68     private Set<Module> modules;
69
70     @Before
71     public void init() throws FileNotFoundException, ParseException {
72         nodesRev = simpleDateFormat.parse("2013-02-27");
73         typesRev = simpleDateFormat.parse("2013-07-03");
74         customRev = simpleDateFormat.parse("2013-02-27");
75
76         modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
77         assertEquals(3, modules.size());
78     }
79
80     @Test
81     public void testHeaders() throws ParseException {
82         Module test = TestUtils.findModule(modules, "nodes");
83
84         assertEquals("nodes", test.getName());
85         assertEquals("1", test.getYangVersion());
86         assertEquals(nodesNS, test.getNamespace());
87         assertEquals("n", test.getPrefix());
88
89         Set<ModuleImport> imports = test.getImports();
90         assertEquals(2, imports.size());
91
92         ModuleImport import2 = TestUtils.findImport(imports, "t");
93         assertEquals("types", import2.getModuleName());
94         assertEquals(typesRev, import2.getRevision());
95
96         ModuleImport import3 = TestUtils.findImport(imports, "c");
97         assertEquals("custom", import3.getModuleName());
98         assertEquals(customRev, import3.getRevision());
99
100         assertEquals("opendaylight", test.getOrganization());
101         assertEquals("http://www.opendaylight.org/", test.getContact());
102         Date expectedRevision = TestUtils.createDate("2013-02-27");
103         assertEquals(expectedRevision, test.getRevision());
104         assertEquals(" WILL BE DEFINED LATER", test.getReference());
105     }
106
107     @Test
108     public void testOrderingTypedef() {
109         Module test = TestUtils.findModule(modules, "types");
110         Set<TypeDefinition<?>> typedefs = test.getTypeDefinitions();
111         String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "my-decimal-type", "my-union",
112                 "my-union-ext", "nested-union2", "string-ext1", "string-ext2", "string-ext3", "string-ext4" };
113         String[] actualOrder = new String[typedefs.size()];
114
115         int i = 0;
116         for (TypeDefinition<?> type : typedefs) {
117             actualOrder[i] = type.getQName().getLocalName();
118             i++;
119         }
120         assertArrayEquals(expectedOrder, actualOrder);
121     }
122
123     @Test
124     public void testOrderingChildNodes() {
125         Module test = TestUtils.findModule(modules, "nodes");
126         AugmentationSchema augment1 = null;
127         for (AugmentationSchema as : test.getAugmentations()) {
128             if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
129                 augment1 = as;
130                 break;
131             }
132         }
133         assertNotNull(augment1);
134
135         String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "odl", "schemas" };
136         String[] actualOrder = new String[expectedOrder.length];
137
138         int i = 0;
139         for (DataSchemaNode augmentChild : augment1.getChildNodes()) {
140             actualOrder[i] = augmentChild.getQName().getLocalName();
141             i++;
142         }
143
144         assertArrayEquals(expectedOrder, actualOrder);
145     }
146
147     @Test
148     public void testOrderingNestedChildNodes1() {
149         Module test = TestUtils.findModule(modules, "nodes");
150
151         Set<DataSchemaNode> childNodes = test.getChildNodes();
152         String[] expectedOrder = new String[] { "address", "addresses", "custom-union-leaf", "data", "datas",
153                 "decimal-leaf", "decimal-leaf2", "ext", "how", "int32-leaf", "length-leaf", "mycont", "peer", "port",
154                 "string-leaf", "transfer", "union-leaf" };
155         String[] actualOrder = new String[childNodes.size()];
156
157         int i = 0;
158         for (DataSchemaNode child : childNodes) {
159             actualOrder[i] = child.getQName().getLocalName();
160             i++;
161         }
162         assertArrayEquals(expectedOrder, actualOrder);
163     }
164
165     @Test
166     public void testOrderingNestedChildNodes2() {
167         Module test = TestUtils.findModule(modules, "custom");
168         Set<GroupingDefinition> groupings = test.getGroupings();
169         assertEquals(1, groupings.size());
170         GroupingDefinition target = groupings.iterator().next();
171
172         Set<DataSchemaNode> childNodes = target.getChildNodes();
173         String[] expectedOrder = new String[] { "address", "addresses", "data", "how", "port" };
174         String[] actualOrder = new String[childNodes.size()];
175
176         int i = 0;
177         for (DataSchemaNode child : childNodes) {
178             actualOrder[i] = child.getQName().getLocalName();
179             i++;
180         }
181         assertArrayEquals(expectedOrder, actualOrder);
182     }
183
184     @Test
185     public void testParseContainer() {
186         Module test = TestUtils.findModule(modules, "types");
187         URI expectedNamespace = URI.create("urn:simple.types.test");
188         String expectedPrefix = "t";
189
190         ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
191         // test SchemaNode args
192         QName expectedQName = new QName(expectedNamespace, typesRev, expectedPrefix, "interfaces");
193         assertEquals(expectedQName, interfaces.getQName());
194         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, typesRev, expectedPrefix, "interfaces");
195         assertEquals(expectedPath, interfaces.getPath());
196         assertNull(interfaces.getDescription());
197         assertNull(interfaces.getReference());
198         assertEquals(Status.CURRENT, interfaces.getStatus());
199         assertEquals(0, interfaces.getUnknownSchemaNodes().size());
200         // test DataSchemaNode args
201         assertFalse(interfaces.isAugmenting());
202         assertTrue(interfaces.isConfiguration());
203         ConstraintDefinition constraints = interfaces.getConstraints();
204         assertNull(constraints.getWhenCondition());
205         assertEquals(0, constraints.getMustConstraints().size());
206         assertFalse(constraints.isMandatory());
207         assertNull(constraints.getMinElements());
208         assertNull(constraints.getMaxElements());
209         // test AugmentationTarget args
210         assertEquals(0, interfaces.getAvailableAugmentations().size());
211         // test ContainerSchemaNode args
212         assertFalse(interfaces.isPresenceContainer());
213         // test DataNodeContainer args
214         assertEquals(0, interfaces.getTypeDefinitions().size());
215         assertEquals(1, interfaces.getChildNodes().size());
216         assertEquals(1, interfaces.getGroupings().size());
217         assertEquals(0, interfaces.getUses().size());
218
219         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
220         assertNotNull(ifEntry);
221     }
222
223     @Test
224     public void testParseList() {
225         Module test = TestUtils.findModule(modules, "types");
226         URI expectedNamespace = URI.create("urn:simple.types.test");
227         String expectedPrefix = "t";
228
229         ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
230
231         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
232         // test SchemaNode args
233         QName expectedQName = new QName(expectedNamespace, typesRev, expectedPrefix, "ifEntry");
234         assertEquals(expectedQName, ifEntry.getQName());
235         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, typesRev, expectedPrefix, "interfaces",
236                 "ifEntry");
237         assertEquals(expectedPath, ifEntry.getPath());
238         assertNull(ifEntry.getDescription());
239         assertNull(ifEntry.getReference());
240         assertEquals(Status.CURRENT, ifEntry.getStatus());
241         assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
242         // test DataSchemaNode args
243         assertFalse(ifEntry.isAugmenting());
244         assertTrue(ifEntry.isConfiguration());
245         ConstraintDefinition constraints = ifEntry.getConstraints();
246         assertNull(constraints.getWhenCondition());
247         assertEquals(0, constraints.getMustConstraints().size());
248         assertFalse(constraints.isMandatory());
249         assertEquals(1, (int) constraints.getMinElements());
250         assertEquals(11, (int) constraints.getMaxElements());
251         // test AugmentationTarget args
252         Set<AugmentationSchema> availableAugmentations = ifEntry.getAvailableAugmentations();
253         assertEquals(2, availableAugmentations.size());
254         // test ListSchemaNode args
255         List<QName> expectedKey = new ArrayList<QName>();
256         expectedKey.add(new QName(expectedNamespace, typesRev, expectedPrefix, "ifIndex"));
257         assertEquals(expectedKey, ifEntry.getKeyDefinition());
258         assertFalse(ifEntry.isUserOrdered());
259         // test DataNodeContainer args
260         assertEquals(0, ifEntry.getTypeDefinitions().size());
261         assertEquals(4, ifEntry.getChildNodes().size());
262         assertEquals(0, ifEntry.getGroupings().size());
263         assertEquals(0, ifEntry.getUses().size());
264
265         LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
266         assertTrue(ifIndex.getType() instanceof Uint32);
267         LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
268         assertTrue(ifMtu.getType() instanceof Int32);
269     }
270
271     @Test
272     public void testTypedefRangesResolving() throws ParseException {
273         Module testModule = TestUtils.findModule(modules, "nodes");
274         LeafSchemaNode int32Leaf = (LeafSchemaNode) testModule.getDataChildByName("int32-leaf");
275
276         ExtendedType leafType = (ExtendedType) int32Leaf.getType();
277         QName leafTypeQName = leafType.getQName();
278         assertEquals("int32-ext2", leafTypeQName.getLocalName());
279         assertEquals("n", leafTypeQName.getPrefix());
280         assertEquals(nodesNS, leafTypeQName.getNamespace());
281         assertEquals(nodesRev, leafTypeQName.getRevision());
282         assertNull(leafType.getUnits());
283         assertNull(leafType.getDefaultValue());
284         assertTrue(leafType.getLengths().isEmpty());
285         assertTrue(leafType.getPatterns().isEmpty());
286         List<RangeConstraint> ranges = leafType.getRanges();
287         assertEquals(1, ranges.size());
288         RangeConstraint range = ranges.get(0);
289         assertEquals(12L, range.getMin());
290         assertEquals(20L, range.getMax());
291
292         ExtendedType baseType = (ExtendedType) leafType.getBaseType();
293         QName baseTypeQName = baseType.getQName();
294         assertEquals("int32-ext2", baseTypeQName.getLocalName());
295         assertEquals("t", baseTypeQName.getPrefix());
296         assertEquals(typesNS, baseTypeQName.getNamespace());
297         assertEquals(typesRev, baseTypeQName.getRevision());
298         assertEquals("mile", baseType.getUnits());
299         assertEquals("11", baseType.getDefaultValue());
300         assertTrue(leafType.getLengths().isEmpty());
301         assertTrue(leafType.getPatterns().isEmpty());
302         List<RangeConstraint> baseTypeRanges = baseType.getRanges();
303         assertEquals(2, baseTypeRanges.size());
304         RangeConstraint baseTypeRange1 = baseTypeRanges.get(0);
305         assertEquals(3L, baseTypeRange1.getMin());
306         assertEquals(9L, baseTypeRange1.getMax());
307         RangeConstraint baseTypeRange2 = baseTypeRanges.get(1);
308         assertEquals(11L, baseTypeRange2.getMin());
309         assertEquals(20L, baseTypeRange2.getMax());
310
311         ExtendedType base = (ExtendedType) baseType.getBaseType();
312         QName baseQName = base.getQName();
313         assertEquals("int32-ext1", baseQName.getLocalName());
314         assertEquals("t", baseQName.getPrefix());
315         assertEquals(typesNS, baseQName.getNamespace());
316         assertEquals(typesRev, baseQName.getRevision());
317         assertNull(base.getUnits());
318         assertNull(base.getDefaultValue());
319         assertTrue(leafType.getLengths().isEmpty());
320         assertTrue(leafType.getPatterns().isEmpty());
321         List<RangeConstraint> baseRanges = base.getRanges();
322         assertEquals(1, baseRanges.size());
323         RangeConstraint baseRange = baseRanges.get(0);
324         assertEquals(2L, baseRange.getMin());
325         assertEquals(20L, baseRange.getMax());
326
327         assertTrue(base.getBaseType() instanceof Int32);
328     }
329
330     @Test
331     public void testTypedefPatternsResolving() {
332         Module testModule = TestUtils.findModule(modules, "nodes");
333         LeafSchemaNode stringleaf = (LeafSchemaNode) testModule.getDataChildByName("string-leaf");
334
335         ExtendedType type = (ExtendedType) stringleaf.getType();
336         QName typeQName = type.getQName();
337         assertEquals("string-ext4", typeQName.getLocalName());
338         assertEquals("t", typeQName.getPrefix());
339         assertEquals(typesNS, typeQName.getNamespace());
340         assertEquals(typesRev, typeQName.getRevision());
341         assertNull(type.getUnits());
342         assertNull(type.getDefaultValue());
343         List<PatternConstraint> patterns = type.getPatterns();
344         assertEquals(1, patterns.size());
345         PatternConstraint pattern = patterns.iterator().next();
346         assertEquals("[e-z]*", pattern.getRegularExpression());
347         assertTrue(type.getLengths().isEmpty());
348         assertTrue(type.getRanges().isEmpty());
349
350         ExtendedType baseType1 = (ExtendedType) type.getBaseType();
351         QName baseType1QName = baseType1.getQName();
352         assertEquals("string-ext3", baseType1QName.getLocalName());
353         assertEquals("t", baseType1QName.getPrefix());
354         assertEquals(typesNS, baseType1QName.getNamespace());
355         assertEquals(typesRev, baseType1QName.getRevision());
356         assertNull(baseType1.getUnits());
357         assertNull(baseType1.getDefaultValue());
358         patterns = baseType1.getPatterns();
359         assertEquals(1, patterns.size());
360         pattern = patterns.iterator().next();
361         assertEquals("[b-u]*", pattern.getRegularExpression());
362         assertTrue(baseType1.getLengths().isEmpty());
363         assertTrue(baseType1.getRanges().isEmpty());
364
365         ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
366         QName baseType2QName = baseType2.getQName();
367         assertEquals("string-ext2", baseType2QName.getLocalName());
368         assertEquals("t", baseType2QName.getPrefix());
369         assertEquals(typesNS, baseType2QName.getNamespace());
370         assertEquals(typesRev, baseType2QName.getRevision());
371         assertNull(baseType2.getUnits());
372         assertNull(baseType2.getDefaultValue());
373         assertTrue(baseType2.getPatterns().isEmpty());
374         List<LengthConstraint> baseType2Lengths = baseType2.getLengths();
375         assertEquals(1, baseType2Lengths.size());
376         LengthConstraint length = baseType2Lengths.get(0);
377         assertEquals(6L, length.getMin());
378         assertEquals(10L, length.getMax());
379         assertTrue(baseType2.getRanges().isEmpty());
380
381         ExtendedType baseType3 = (ExtendedType) baseType2.getBaseType();
382         QName baseType3QName = baseType3.getQName();
383         assertEquals("string-ext1", baseType3QName.getLocalName());
384         assertEquals("t", baseType3QName.getPrefix());
385         assertEquals(typesNS, baseType3QName.getNamespace());
386         assertEquals(typesRev, baseType3QName.getRevision());
387         assertNull(baseType3.getUnits());
388         assertNull(baseType3.getDefaultValue());
389         patterns = baseType3.getPatterns();
390         assertEquals(1, patterns.size());
391         pattern = patterns.iterator().next();
392         assertEquals("[a-k]*", pattern.getRegularExpression());
393         List<LengthConstraint> baseType3Lengths = baseType3.getLengths();
394         assertEquals(1, baseType3Lengths.size());
395         length = baseType3Lengths.get(0);
396         assertEquals(5L, length.getMin());
397         assertEquals(11L, length.getMax());
398         assertTrue(baseType3.getRanges().isEmpty());
399
400         assertTrue(baseType3.getBaseType() instanceof StringType);
401     }
402
403     @Test
404     public void testTypedefLengthsResolving() {
405         Module testModule = TestUtils.findModule(modules, "nodes");
406
407         LeafSchemaNode lengthLeaf = (LeafSchemaNode) testModule.getDataChildByName("length-leaf");
408         ExtendedType type = (ExtendedType) lengthLeaf.getType();
409
410         QName typeQName = type.getQName();
411         assertEquals("string-ext2", typeQName.getLocalName());
412         assertEquals("n", typeQName.getPrefix());
413         assertEquals(nodesNS, typeQName.getNamespace());
414         assertEquals(nodesRev, typeQName.getRevision());
415         assertNull(type.getUnits());
416         assertNull(type.getDefaultValue());
417         assertTrue(type.getPatterns().isEmpty());
418         List<LengthConstraint> typeLengths = type.getLengths();
419         assertEquals(1, typeLengths.size());
420         LengthConstraint length = typeLengths.get(0);
421         assertEquals(7L, length.getMin());
422         assertEquals(10L, length.getMax());
423         assertTrue(type.getRanges().isEmpty());
424
425         ExtendedType baseType1 = (ExtendedType) type.getBaseType();
426         QName baseType1QName = baseType1.getQName();
427         assertEquals("string-ext2", baseType1QName.getLocalName());
428         assertEquals("t", baseType1QName.getPrefix());
429         assertEquals(typesNS, baseType1QName.getNamespace());
430         assertEquals(typesRev, baseType1QName.getRevision());
431         assertNull(baseType1.getUnits());
432         assertNull(baseType1.getDefaultValue());
433         assertTrue(baseType1.getPatterns().isEmpty());
434         List<LengthConstraint> baseType2Lengths = baseType1.getLengths();
435         assertEquals(1, baseType2Lengths.size());
436         length = baseType2Lengths.get(0);
437         assertEquals(6L, length.getMin());
438         assertEquals(10L, length.getMax());
439         assertTrue(baseType1.getRanges().isEmpty());
440
441         ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
442         QName baseType2QName = baseType2.getQName();
443         assertEquals("string-ext1", baseType2QName.getLocalName());
444         assertEquals("t", baseType2QName.getPrefix());
445         assertEquals(typesNS, baseType2QName.getNamespace());
446         assertEquals(typesRev, baseType2QName.getRevision());
447         assertNull(baseType2.getUnits());
448         assertNull(baseType2.getDefaultValue());
449         List<PatternConstraint> patterns = baseType2.getPatterns();
450         assertEquals(1, patterns.size());
451         PatternConstraint pattern = patterns.iterator().next();
452         assertEquals("[a-k]*", pattern.getRegularExpression());
453         List<LengthConstraint> baseType3Lengths = baseType2.getLengths();
454         assertEquals(1, baseType3Lengths.size());
455         length = baseType3Lengths.get(0);
456         assertEquals(5L, length.getMin());
457         assertEquals(11L, length.getMax());
458         assertTrue(baseType2.getRanges().isEmpty());
459
460         assertTrue(baseType2.getBaseType() instanceof StringType);
461     }
462
463     @Test
464     public void testTypedefDecimal1() {
465         Module testModule = TestUtils.findModule(modules, "nodes");
466         LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("decimal-leaf");
467
468         ExtendedType type = (ExtendedType) testleaf.getType();
469         QName typeQName = type.getQName();
470         assertEquals("my-decimal-type", typeQName.getLocalName());
471         assertEquals("n", typeQName.getPrefix());
472         assertEquals(nodesNS, typeQName.getNamespace());
473         assertEquals(nodesRev, typeQName.getRevision());
474         assertNull(type.getUnits());
475         assertNull(type.getDefaultValue());
476         assertEquals(4, (int) type.getFractionDigits());
477         assertTrue(type.getLengths().isEmpty());
478         assertTrue(type.getPatterns().isEmpty());
479         assertTrue(type.getRanges().isEmpty());
480
481         ExtendedType typeBase = (ExtendedType) type.getBaseType();
482         QName typeBaseQName = typeBase.getQName();
483         assertEquals("my-decimal-type", typeBaseQName.getLocalName());
484         assertEquals("t", typeBaseQName.getPrefix());
485         assertEquals(typesNS, typeBaseQName.getNamespace());
486         assertEquals(typesRev, typeBaseQName.getRevision());
487         assertNull(typeBase.getUnits());
488         assertNull(typeBase.getDefaultValue());
489         assertNull(typeBase.getFractionDigits());
490         assertTrue(typeBase.getLengths().isEmpty());
491         assertTrue(typeBase.getPatterns().isEmpty());
492         assertTrue(typeBase.getRanges().isEmpty());
493
494         Decimal64 decimal = (Decimal64) typeBase.getBaseType();
495         assertEquals(6, (int) decimal.getFractionDigits());
496     }
497
498     @Test
499     public void testTypedefDecimal2() {
500         Module testModule = TestUtils.findModule(modules, "nodes");
501         LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("decimal-leaf2");
502
503         ExtendedType type = (ExtendedType) testleaf.getType();
504         QName typeQName = type.getQName();
505         assertEquals("my-decimal-type", typeQName.getLocalName());
506         assertEquals("t", typeQName.getPrefix());
507         assertEquals(typesNS, typeQName.getNamespace());
508         assertEquals(typesRev, typeQName.getRevision());
509         assertNull(type.getUnits());
510         assertNull(type.getDefaultValue());
511         assertNull(type.getFractionDigits());
512         assertTrue(type.getLengths().isEmpty());
513         assertTrue(type.getPatterns().isEmpty());
514         assertTrue(type.getRanges().isEmpty());
515
516         Decimal64 baseTypeDecimal = (Decimal64) type.getBaseType();
517         assertEquals(6, (int) baseTypeDecimal.getFractionDigits());
518     }
519
520     @Test
521     public void testTypedefUnion() {
522         Module testModule = TestUtils.findModule(modules, "nodes");
523         LeafSchemaNode unionleaf = (LeafSchemaNode) testModule.getDataChildByName("union-leaf");
524
525         ExtendedType type = (ExtendedType) unionleaf.getType();
526         QName typeQName = type.getQName();
527         assertEquals("my-union-ext", typeQName.getLocalName());
528         assertEquals("t", typeQName.getPrefix());
529         assertEquals(typesNS, typeQName.getNamespace());
530         assertEquals(typesRev, typeQName.getRevision());
531         assertNull(type.getUnits());
532         assertNull(type.getDefaultValue());
533         assertNull(type.getFractionDigits());
534         assertTrue(type.getLengths().isEmpty());
535         assertTrue(type.getPatterns().isEmpty());
536         assertTrue(type.getRanges().isEmpty());
537
538         ExtendedType baseType = (ExtendedType) type.getBaseType();
539         QName baseTypeQName = baseType.getQName();
540         assertEquals("my-union", baseTypeQName.getLocalName());
541         assertEquals("t", baseTypeQName.getPrefix());
542         assertEquals(typesNS, baseTypeQName.getNamespace());
543         assertEquals(typesRev, baseTypeQName.getRevision());
544         assertNull(baseType.getUnits());
545         assertNull(baseType.getDefaultValue());
546         assertNull(baseType.getFractionDigits());
547         assertTrue(baseType.getLengths().isEmpty());
548         assertTrue(baseType.getPatterns().isEmpty());
549         assertTrue(baseType.getRanges().isEmpty());
550
551         UnionType unionType = (UnionType) baseType.getBaseType();
552         List<TypeDefinition<?>> unionTypes = unionType.getTypes();
553         assertEquals(2, unionTypes.size());
554
555         ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
556         QName unionType1QName = baseType.getQName();
557         assertEquals("my-union", unionType1QName.getLocalName());
558         assertEquals("t", unionType1QName.getPrefix());
559         assertEquals(typesNS, unionType1QName.getNamespace());
560         assertEquals(typesRev, unionType1QName.getRevision());
561         assertNull(unionType1.getUnits());
562         assertNull(unionType1.getDefaultValue());
563         assertNull(unionType1.getFractionDigits());
564         assertTrue(unionType1.getLengths().isEmpty());
565         assertTrue(unionType1.getPatterns().isEmpty());
566         List<RangeConstraint> ranges = unionType1.getRanges();
567         assertEquals(1, ranges.size());
568         RangeConstraint range = ranges.get(0);
569         assertEquals(1L, range.getMin());
570         assertEquals(100L, range.getMax());
571         assertTrue(unionType1.getBaseType() instanceof Int16);
572
573         assertTrue(unionTypes.get(1) instanceof Int32);
574     }
575
576     @Test
577     public void testNestedUnionResolving() {
578         Module testModule = TestUtils.findModule(modules, "nodes");
579         LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("custom-union-leaf");
580
581         ExtendedType type = (ExtendedType) testleaf.getType();
582         QName testleafTypeQName = type.getQName();
583         assertEquals(customNS, testleafTypeQName.getNamespace());
584         assertEquals(customRev, testleafTypeQName.getRevision());
585         assertEquals("c", testleafTypeQName.getPrefix());
586         assertEquals("union1", testleafTypeQName.getLocalName());
587         assertNull(type.getUnits());
588         assertNull(type.getDefaultValue());
589         assertNull(type.getFractionDigits());
590         assertTrue(type.getLengths().isEmpty());
591         assertTrue(type.getPatterns().isEmpty());
592         assertTrue(type.getRanges().isEmpty());
593
594         ExtendedType typeBase = (ExtendedType) type.getBaseType();
595         QName typeBaseQName = typeBase.getQName();
596         assertEquals(customNS, typeBaseQName.getNamespace());
597         assertEquals(customRev, typeBaseQName.getRevision());
598         assertEquals("c", typeBaseQName.getPrefix());
599         assertEquals("union2", typeBaseQName.getLocalName());
600         assertNull(typeBase.getUnits());
601         assertNull(typeBase.getDefaultValue());
602         assertNull(typeBase.getFractionDigits());
603         assertTrue(typeBase.getLengths().isEmpty());
604         assertTrue(typeBase.getPatterns().isEmpty());
605         assertTrue(typeBase.getRanges().isEmpty());
606
607         UnionType union = (UnionType) typeBase.getBaseType();
608         List<TypeDefinition<?>> unionTypes = union.getTypes();
609         assertEquals(2, unionTypes.size());
610         assertTrue(unionTypes.get(0) instanceof Int32);
611         assertTrue(unionTypes.get(1) instanceof ExtendedType);
612
613         ExtendedType unionType1 = (ExtendedType) unionTypes.get(1);
614         QName uniontType1QName = unionType1.getQName();
615         assertEquals(typesNS, uniontType1QName.getNamespace());
616         assertEquals(typesRev, uniontType1QName.getRevision());
617         assertEquals("t", uniontType1QName.getPrefix());
618         assertEquals("nested-union2", uniontType1QName.getLocalName());
619         assertNull(unionType1.getUnits());
620         assertNull(unionType1.getDefaultValue());
621         assertNull(unionType1.getFractionDigits());
622         assertTrue(unionType1.getLengths().isEmpty());
623         assertTrue(unionType1.getPatterns().isEmpty());
624         assertTrue(unionType1.getRanges().isEmpty());
625
626         UnionType nestedUnion = (UnionType) unionType1.getBaseType();
627         List<TypeDefinition<?>> nestedUnion2Types = nestedUnion.getTypes();
628         assertEquals(2, nestedUnion2Types.size());
629         assertTrue(nestedUnion2Types.get(0) instanceof StringType);
630         assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
631
632         ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
633         QName myUnionExtQName = myUnionExt.getQName();
634         assertEquals(typesNS, myUnionExtQName.getNamespace());
635         assertEquals(typesRev, myUnionExtQName.getRevision());
636         assertEquals("t", myUnionExtQName.getPrefix());
637         assertEquals("my-union-ext", myUnionExtQName.getLocalName());
638         assertNull(myUnionExt.getUnits());
639         assertNull(myUnionExt.getDefaultValue());
640         assertNull(myUnionExt.getFractionDigits());
641         assertTrue(myUnionExt.getLengths().isEmpty());
642         assertTrue(myUnionExt.getPatterns().isEmpty());
643         assertTrue(myUnionExt.getRanges().isEmpty());
644
645         ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
646         QName myUnionQName = myUnion.getQName();
647         assertEquals(typesNS, myUnionQName.getNamespace());
648         assertEquals(typesRev, myUnionQName.getRevision());
649         assertEquals("t", myUnionQName.getPrefix());
650         assertEquals("my-union", myUnionQName.getLocalName());
651         assertNull(myUnion.getUnits());
652         assertNull(myUnion.getDefaultValue());
653         assertNull(myUnion.getFractionDigits());
654         assertTrue(myUnion.getLengths().isEmpty());
655         assertTrue(myUnion.getPatterns().isEmpty());
656         assertTrue(myUnion.getRanges().isEmpty());
657
658         UnionType myUnionBase = (UnionType) myUnion.getBaseType();
659         List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
660         assertEquals(2, myUnionBaseTypes.size());
661         assertTrue(myUnionBaseTypes.get(0) instanceof ExtendedType);
662         assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
663
664         ExtendedType int16Ext = (ExtendedType) myUnionBaseTypes.get(0);
665         QName int16ExtQName = int16Ext.getQName();
666         assertEquals(typesNS, int16ExtQName.getNamespace());
667         assertEquals(typesRev, int16ExtQName.getRevision());
668         assertEquals("t", int16ExtQName.getPrefix());
669         assertEquals("int16", int16ExtQName.getLocalName());
670         assertNull(int16Ext.getUnits());
671         assertNull(int16Ext.getDefaultValue());
672         assertNull(int16Ext.getFractionDigits());
673         assertTrue(int16Ext.getLengths().isEmpty());
674         assertTrue(int16Ext.getPatterns().isEmpty());
675         List<RangeConstraint> ranges = int16Ext.getRanges();
676         assertEquals(1, ranges.size());
677         RangeConstraint range = ranges.get(0);
678         assertEquals(1L, range.getMin());
679         assertEquals(100L, range.getMax());
680
681         assertTrue(int16Ext.getBaseType() instanceof Int16);
682     }
683
684     @Test
685     public void testChoice() {
686         Module testModule = TestUtils.findModule(modules, "nodes");
687         ContainerSchemaNode transfer = (ContainerSchemaNode) testModule.getDataChildByName("transfer");
688         ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how");
689         Set<ChoiceCaseNode> cases = how.getCases();
690         assertEquals(5, cases.size());
691         ChoiceCaseNode input = null;
692         ChoiceCaseNode output = null;
693         for (ChoiceCaseNode caseNode : cases) {
694             if ("input".equals(caseNode.getQName().getLocalName())) {
695                 input = caseNode;
696             } else if ("output".equals(caseNode.getQName().getLocalName())) {
697                 output = caseNode;
698             }
699         }
700         assertNotNull(input);
701         assertNotNull(input.getPath());
702         assertNotNull(output);
703         assertNotNull(output.getPath());
704     }
705
706     @Test
707     public void testAnyXml() {
708         Module testModule = TestUtils.findModule(modules, "nodes");
709         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("datas");
710         assertNotNull("anyxml data not found", data);
711
712         // test SchemaNode args
713         QName qname = data.getQName();
714         assertEquals("datas", qname.getLocalName());
715         assertEquals("n", qname.getPrefix());
716         assertEquals(nodesNS, qname.getNamespace());
717         assertEquals(nodesRev, qname.getRevision());
718         assertTrue(data.getDescription().contains("Copy of the source typesstore subset that matched"));
719         assertNull(data.getReference());
720         assertEquals(Status.OBSOLETE, data.getStatus());
721         assertEquals(0, data.getUnknownSchemaNodes().size());
722         // test DataSchemaNode args
723         assertFalse(data.isAugmenting());
724         assertTrue(data.isConfiguration());
725         ConstraintDefinition constraints = data.getConstraints();
726         assertNull(constraints.getWhenCondition());
727         assertEquals(0, constraints.getMustConstraints().size());
728         assertFalse(constraints.isMandatory());
729         assertNull(constraints.getMinElements());
730         assertNull(constraints.getMaxElements());
731     }
732
733     @Test
734     public void testDeviation() {
735         Module testModule = TestUtils.findModule(modules, "nodes");
736         Set<Deviation> deviations = testModule.getDeviations();
737         assertEquals(1, deviations.size());
738         Deviation dev = deviations.iterator().next();
739
740         assertEquals("system/user ref", dev.getReference());
741
742         List<QName> path = new ArrayList<QName>();
743         path.add(new QName(typesNS, typesRev, "t", "interfaces"));
744         path.add(new QName(typesNS, typesRev, "t", "ifEntry"));
745         SchemaPath expectedPath = new SchemaPath(path, true);
746
747         assertEquals(expectedPath, dev.getTargetPath());
748         assertEquals(Deviate.ADD, dev.getDeviate());
749     }
750
751     @Test
752     public void testUnknownNode() {
753         Module testModule = TestUtils.findModule(modules, "custom");
754         ContainerSchemaNode network = (ContainerSchemaNode) testModule.getDataChildByName("network");
755         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
756         assertEquals(1, unknownNodes.size());
757         UnknownSchemaNode unknownNode = unknownNodes.get(0);
758         assertNotNull(unknownNode.getNodeType());
759         assertEquals("point", unknownNode.getNodeParameter());
760     }
761
762     @Test
763     public void testFeature() {
764         Module testModule = TestUtils.findModule(modules, "custom");
765         Set<FeatureDefinition> features = testModule.getFeatures();
766         assertEquals(1, features.size());
767     }
768
769     @Test
770     public void testExtension() {
771         Module testModule = TestUtils.findModule(modules, "custom");
772         List<ExtensionDefinition> extensions = testModule.getExtensionSchemaNodes();
773         assertEquals(1, extensions.size());
774         ExtensionDefinition extension = extensions.get(0);
775         assertEquals("name", extension.getArgument());
776         assertTrue(extension.isYinElement());
777     }
778
779     @Test
780     public void testNotification() {
781         Module testModule = TestUtils.findModule(modules, "custom");
782         String expectedPrefix = "c";
783
784         Set<NotificationDefinition> notifications = testModule.getNotifications();
785         assertEquals(1, notifications.size());
786
787         NotificationDefinition notification = notifications.iterator().next();
788         // test SchemaNode args
789         QName expectedQName = new QName(customNS, customRev, expectedPrefix, "event");
790         assertEquals(expectedQName, notification.getQName());
791         SchemaPath expectedPath = TestUtils.createPath(true, customNS, customRev, expectedPrefix, "event");
792         assertEquals(expectedPath, notification.getPath());
793         assertNull(notification.getDescription());
794         assertNull(notification.getReference());
795         assertEquals(Status.CURRENT, notification.getStatus());
796         assertEquals(0, notification.getUnknownSchemaNodes().size());
797         // test DataNodeContainer args
798         assertEquals(0, notification.getTypeDefinitions().size());
799         assertEquals(3, notification.getChildNodes().size());
800         assertEquals(0, notification.getGroupings().size());
801         assertEquals(0, notification.getUses().size());
802
803         LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
804         assertTrue(eventClass.getType() instanceof StringType);
805         AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity");
806         assertNotNull(reportingEntity);
807         LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
808         assertTrue(severity.getType() instanceof StringType);
809     }
810
811     @Test
812     public void testRpc() {
813         Module testModule = TestUtils.findModule(modules, "custom");
814
815         Set<RpcDefinition> rpcs = testModule.getRpcs();
816         assertEquals(1, rpcs.size());
817
818         RpcDefinition rpc = rpcs.iterator().next();
819         assertEquals("Retrieve all or part of a specified configuration.", 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 testTypePath() throws ParseException {
831         Module test = TestUtils.findModule(modules, "types");
832         Set<TypeDefinition<?>> types = test.getTypeDefinitions();
833
834         // my-base-int32-type
835         ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1");
836         QName int32TypedefQName = int32Typedef.getQName();
837
838         assertEquals(typesNS, int32TypedefQName.getNamespace());
839         assertEquals(typesRev, int32TypedefQName.getRevision());
840         assertEquals("t", int32TypedefQName.getPrefix());
841         assertEquals("int32-ext1", int32TypedefQName.getLocalName());
842
843         SchemaPath typeSchemaPath = int32Typedef.getPath();
844         List<QName> typePath = typeSchemaPath.getPath();
845         assertEquals(1, typePath.size());
846         assertEquals(int32TypedefQName, typePath.get(0));
847
848         // my-base-int32-type/int32
849         Int32 int32 = (Int32) int32Typedef.getBaseType();
850         QName int32QName = int32.getQName();
851         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), int32QName.getNamespace());
852         assertNull(int32QName.getRevision());
853         assertEquals("", int32QName.getPrefix());
854         assertEquals("int32", int32QName.getLocalName());
855
856         SchemaPath int32SchemaPath = int32.getPath();
857         List<QName> int32Path = int32SchemaPath.getPath();
858         assertEquals(3, int32Path.size());
859         assertEquals(int32TypedefQName, int32Path.get(0));
860         assertEquals(int32QName, int32Path.get(2));
861     }
862
863     @Test
864     public void testTypePath2() throws ParseException {
865         Module test = TestUtils.findModule(modules, "types");
866         Set<TypeDefinition<?>> types = test.getTypeDefinitions();
867
868         // my-base-int32-type
869         ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
870         QName myDecTypeQName = myDecType.getQName();
871
872         assertEquals(typesNS, myDecTypeQName.getNamespace());
873         assertEquals(typesRev, myDecTypeQName.getRevision());
874         assertEquals("t", myDecTypeQName.getPrefix());
875         assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
876
877         SchemaPath typeSchemaPath = myDecType.getPath();
878         List<QName> typePath = typeSchemaPath.getPath();
879         assertEquals(1, typePath.size());
880         assertEquals(myDecTypeQName, typePath.get(0));
881
882         // my-base-int32-type/int32
883         Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
884         QName dec64QName = dec64.getQName();
885
886         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
887         assertNull(dec64QName.getRevision());
888         assertEquals("", dec64QName.getPrefix());
889         assertEquals("decimal64", dec64QName.getLocalName());
890
891         SchemaPath dec64SchemaPath = dec64.getPath();
892         List<QName> dec64Path = dec64SchemaPath.getPath();
893         assertEquals(2, dec64Path.size());
894         assertEquals(myDecTypeQName, dec64Path.get(0));
895         assertEquals(dec64QName, dec64Path.get(1));
896     }
897
898 }