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