Added YANG typedef support in the YANG parser
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / model / parser / impl / YangModelParserTest.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.model.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.model.api.type.IntegerTypeDefinition;
19 import org.opendaylight.controller.model.api.type.PatternConstraint;
20 import org.opendaylight.controller.model.api.type.RangeConstraint;
21 import org.opendaylight.controller.model.util.Decimal64;
22 import org.opendaylight.controller.model.util.Int32;
23 import org.opendaylight.controller.model.util.StringType;
24 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
25 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
28 import org.opendaylight.controller.yang.model.api.Module;
29 import org.opendaylight.controller.yang.model.api.TypeDefinition;
30 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
31
32 public class YangModelParserTest {
33
34     private final String testFile1 = "src/test/resources/model/testfile1.yang";
35     private final String testFile2 = "src/test/resources/model/testfile2.yang";
36     private YangModelParser tested;
37
38     @Before
39     public void init() {
40         tested = new YangModelParserImpl();
41     }
42
43     @Test
44     public void testAugment() {
45         Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
46         assertEquals(2, modules.size());
47
48         Module m2 = null;
49         for(Module m : modules) {
50             if(m.getName().equals("types2")) {
51                 m2 = m;
52             }
53         }
54         assertNotNull(m2);
55
56         AugmentationSchema augment = m2.getAugmentations().iterator().next();
57         assertNotNull(augment);
58     }
59
60     @Test
61     public void testAugmentTarget() {
62         Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
63         assertEquals(2, modules.size());
64
65         Module m1 = null;
66         for(Module m : modules) {
67             if(m.getName().equals("types1")) {
68                 m1 = m;
69             }
70         }
71         assertNotNull(m1);
72
73         ContainerSchemaNode container = (ContainerSchemaNode)m1.getDataChildByName("interfaces");
74         assertNotNull(container);
75
76         ListSchemaNode list = (ListSchemaNode)container.getDataChildByName("ifEntry");
77         assertNotNull(list);
78         assertEquals(1, list.getAvailableAugmentations().size());
79
80         LeafSchemaNode leaf = (LeafSchemaNode)list.getDataChildByName("ds0ChannelNumber");
81         assertNotNull(leaf);
82     }
83
84     @Test
85     public void testTypedefRangesResolving() {
86         Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
87         assertEquals(2, modules.size());
88
89         Module m1 = null;
90         for(Module m : modules) {
91             if(m.getName().equals("types1")) {
92                 m1 = m;
93             }
94         }
95         assertNotNull(m1);
96
97         LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("testleaf");
98         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
99         assertTrue(testleaf.getType().getBaseType() instanceof Int32);
100         Int32 baseTypeCast = (Int32)baseType;
101         List<RangeConstraint> ranges = baseTypeCast.getRangeStatements();
102         assertEquals(2, ranges.size());
103         RangeConstraint range = ranges.get(0);
104         assertEquals(2L, range.getMin());
105         assertEquals(20L, range.getMax());
106     }
107
108     @Test
109     public void testTypedefPatternsResolving() {
110         Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
111         assertEquals(2, modules.size());
112
113         Module m1 = null;
114         for(Module m : modules) {
115             if(m.getName().equals("types1")) {
116                 m1 = m;
117             }
118         }
119         assertNotNull(m1);
120
121         LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-string-leaf");
122         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
123         assertTrue(testleaf.getType().getBaseType() instanceof StringType);
124         StringType baseTypeCast = (StringType)baseType;
125
126         Set<String> expectedRegularExpressions = new HashSet<String>();
127         expectedRegularExpressions.add("[a-k]*");
128         expectedRegularExpressions.add("[b-u]*");
129         expectedRegularExpressions.add("[e-z]*");
130
131         Set<String> actualRegularExpressions = new HashSet<String>();
132         List<PatternConstraint> patterns = baseTypeCast.getPatterns();
133         for(PatternConstraint pc : patterns) {
134             actualRegularExpressions.add(pc.getRegularExpression());
135         }
136
137         assertEquals(expectedRegularExpressions, actualRegularExpressions);
138     }
139
140     @Test
141     public void testTypedefLengthsResolving() {
142         Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
143         assertEquals(2, modules.size());
144
145         Module m1 = null;
146         for(Module m : modules) {
147             if(m.getName().equals("types1")) {
148                 m1 = m;
149             }
150         }
151         assertNotNull(m1);
152
153         LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-int-leaf");
154         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
155         assertTrue(testleaf.getType().getBaseType() instanceof IntegerTypeDefinition);
156         Int32 baseTypeCast = (Int32)baseType;
157
158         Long[][] expectedRanges = new Long[3][2];
159         expectedRanges[0] = new Long[]{10L, 20L};
160         expectedRanges[1] = new Long[]{12L, 18L};
161         expectedRanges[2] = new Long[]{14L, 16L};
162
163         List<RangeConstraint> actualRanges = baseTypeCast.getRangeStatements();
164         assertEquals(3, actualRanges.size());
165         for(int i = 0; i < actualRanges.size(); i++) {
166             assertEquals(expectedRanges[i][0], actualRanges.get(i).getMin());
167             assertEquals(expectedRanges[i][1], actualRanges.get(i).getMax());
168         }
169     }
170
171     @Test
172     public void testTypeDef() {
173         Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
174         assertEquals(2, modules.size());
175
176         Module m2 = null;
177         for(Module m : modules) {
178             if(m.getName().equals("types2")) {
179                 m2 = m;
180             }
181         }
182         assertNotNull(m2);
183
184         LeafSchemaNode testleaf = (LeafSchemaNode)m2.getDataChildByName("nested-type-leaf");
185         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
186         assertTrue(testleaf.getType().getBaseType() instanceof Int32);
187         Int32 baseTypeCast = (Int32)baseType;
188         List<RangeConstraint> ranges = baseTypeCast.getRangeStatements();
189         assertEquals(2, ranges.size());
190         RangeConstraint range = ranges.get(0);
191         assertEquals(2L, range.getMin());
192         assertEquals(20L, range.getMax());
193     }
194
195     @Test
196     public void testTypedefDecimal1() {
197         Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
198         assertEquals(2, modules.size());
199
200         Module m1 = null;
201         for(Module m : modules) {
202             if(m.getName().equals("types1")) {
203                 m1 = m;
204             }
205         }
206         assertNotNull(m1);
207
208         LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf");
209         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
210         assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
211         Decimal64 baseTypeCast = (Decimal64)baseType;
212         assertEquals(4, (int)baseTypeCast.getFractionDigits());
213     }
214
215     @Test
216     public void testTypedefDecimal2() {
217         Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
218         assertEquals(2, modules.size());
219
220         Module m1 = null;
221         for(Module m : modules) {
222             if(m.getName().equals("types1")) {
223                 m1 = m;
224             }
225         }
226         assertNotNull(m1);
227
228         LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf2");
229         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
230         assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
231         Decimal64 baseTypeCast = (Decimal64)baseType;
232         assertEquals(5, (int)baseTypeCast.getFractionDigits());
233     }
234
235 }