Implemented refine statement parsing.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / model / parser / impl / TypesResolutionTest.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.net.URI;
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.common.QName;
19 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
20 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.controller.yang.model.api.Module;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;
25 import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair;
26 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
27 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
28 import org.opendaylight.controller.yang.model.util.BitsType;
29 import org.opendaylight.controller.yang.model.util.EnumerationType;
30 import org.opendaylight.controller.yang.model.util.ExtendedType;
31 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
32 import org.opendaylight.controller.yang.model.util.StringType;
33 import org.opendaylight.controller.yang.model.util.UnionType;
34
35 public class TypesResolutionTest {
36
37     private Set<Module> testedModules;
38
39     @Before
40     public void init() {
41         testedModules = TestUtils.loadModules("src/test/resources/types");
42     }
43
44     @Test
45     public void testIPVersion() {
46         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
47         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
48         assertEquals(14, typedefs.size());
49
50         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
51         assertTrue(type.getDescription().contains(
52                 "This value represents the version of the IP protocol."));
53         assertTrue(type.getReference().contains(
54                 "RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
55
56         EnumerationType enumType = (EnumerationType) type.getBaseType();
57         List<EnumPair> values = enumType.getValues();
58         assertEquals(3, values.size());
59
60         EnumPair value0 = values.get(0);
61         assertEquals("unknown", value0.getName());
62         assertEquals(0, (int) value0.getValue());
63         assertEquals(
64                 "An unknown or unspecified version of the Internet protocol.",
65                 value0.getDescription());
66
67         EnumPair value1 = values.get(1);
68         assertEquals("ipv4", value1.getName());
69         assertEquals(1, (int) value1.getValue());
70         assertEquals("The IPv4 protocol as defined in RFC 791.",
71                 value1.getDescription());
72
73         EnumPair value2 = values.get(2);
74         assertEquals("ipv6", value2.getName());
75         assertEquals(2, (int) value2.getValue());
76         assertEquals("The IPv6 protocol as defined in RFC 2460.",
77                 value2.getDescription());
78     }
79
80     @Test
81     public void testEnumeration() {
82         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
83         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
84
85         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
86         EnumerationType enumType = (EnumerationType) type.getBaseType();
87         List<EnumPair> values = enumType.getValues();
88         assertEquals(4, values.size());
89
90         EnumPair value0 = values.get(0);
91         assertEquals("unknown", value0.getName());
92         assertEquals(0, (int) value0.getValue());
93         assertEquals(
94                 "An unknown or unspecified version of the Internet protocol.",
95                 value0.getDescription());
96
97         EnumPair value1 = values.get(1);
98         assertEquals("ipv4", value1.getName());
99         assertEquals(19, (int) value1.getValue());
100         assertEquals("The IPv4 protocol as defined in RFC 791.",
101                 value1.getDescription());
102
103         EnumPair value2 = values.get(2);
104         assertEquals("ipv6", value2.getName());
105         assertEquals(7, (int) value2.getValue());
106         assertEquals("The IPv6 protocol as defined in RFC 2460.",
107                 value2.getDescription());
108
109         EnumPair value3 = values.get(3);
110         assertEquals("default", value3.getName());
111         assertEquals(20, (int) value3.getValue());
112         assertEquals("default ip",
113                 value3.getDescription());
114     }
115
116     @Test
117     public void testIpAddress() {
118         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
119         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
120         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
121         UnionType baseType = (UnionType) type.getBaseType();
122         List<TypeDefinition<?>> unionTypes = baseType.getTypes();
123
124         ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
125         StringType ipv4Base = (StringType) ipv4.getBaseType();
126         String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
127                 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
128                 + "(%[\\p{N}\\p{L}]+)?";
129         assertEquals(expectedPattern, ipv4Base.getPatterns().get(0)
130                 .getRegularExpression());
131
132         ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
133         StringType ipv6Base = (StringType) ipv6.getBaseType();
134         List<PatternConstraint> ipv6Patterns = ipv6Base.getPatterns();
135         expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
136                 + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|"
137                 + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
138                 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))"
139                 + "(%[\\p{N}\\p{L}]+)?";
140         assertEquals(expectedPattern, ipv6Patterns.get(0)
141                 .getRegularExpression());
142
143         expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
144                 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
145         assertEquals(expectedPattern, ipv6Patterns.get(1)
146                 .getRegularExpression());
147     }
148
149     @Test
150     public void testDomainName() {
151         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
152         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
153         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "domain-name");
154         StringType baseType = (StringType) type.getBaseType();
155         List<PatternConstraint> patterns = baseType.getPatterns();
156         assertEquals(1, patterns.size());
157         String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
158                 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)"
159                 + "|\\.";
160         assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
161
162         List<LengthConstraint> lengths = baseType.getLengthStatements();
163         assertEquals(1, lengths.size());
164         LengthConstraint length = baseType.getLengthStatements().get(0);
165         assertEquals(1L, length.getMin());
166         assertEquals(253L, length.getMax());
167     }
168
169     @Test
170     public void testInstanceIdentifier1() {
171         Module tested = TestUtils
172                 .findModule(testedModules, "custom-types-test");
173         LeafSchemaNode leaf = (LeafSchemaNode) tested
174                 .getDataChildByName("inst-id-leaf1");
175         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
176         assertFalse(leafType.requireInstance());
177     }
178
179     @Test
180     public void testInstanceIdentifier2() {
181         Module tested = TestUtils
182                 .findModule(testedModules, "custom-types-test");
183         LeafSchemaNode leaf = (LeafSchemaNode) tested
184                 .getDataChildByName("inst-id-leaf2");
185         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
186         assertTrue(leafType.requireInstance());
187     }
188
189     @Test
190     public void testIdentity() {
191         Module tested = TestUtils
192                 .findModule(testedModules, "custom-types-test");
193         Set<IdentitySchemaNode> identities = tested.getIdentities();
194         IdentitySchemaNode testedIdentity = null;
195         for (IdentitySchemaNode id : identities) {
196             if (id.getQName().getLocalName().equals("crypto-alg")) {
197                 testedIdentity = id;
198                 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
199                 assertEquals("crypto-base", baseIdentity.getQName()
200                         .getLocalName());
201                 assertNull(baseIdentity.getBaseIdentity());
202             }
203         }
204         assertNotNull(testedIdentity);
205     }
206
207     @Test
208     public void testBitsType1() {
209         Module tested = TestUtils
210                 .findModule(testedModules, "custom-types-test");
211         LeafSchemaNode leaf = (LeafSchemaNode) tested
212                 .getDataChildByName("mybits");
213         BitsType leafType = (BitsType) leaf.getType();
214         List<Bit> bits = leafType.getBits();
215         assertEquals(3, bits.size());
216
217         Bit bit1 = bits.get(0);
218         assertEquals("disable-nagle", bit1.getName());
219         assertEquals(0L, (long) bit1.getPosition());
220
221         Bit bit2 = bits.get(1);
222         assertEquals("auto-sense-speed", bit2.getName());
223         assertEquals(1L, (long) bit2.getPosition());
224
225         Bit bit3 = bits.get(2);
226         assertEquals("10-Mb-only", bit3.getName());
227         assertEquals(2L, (long) bit3.getPosition());
228     }
229
230     @Test
231     public void testBitsType2() {
232         Module tested = TestUtils
233                 .findModule(testedModules, "custom-types-test");
234         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
235         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
236                 "access-operations-type");
237
238         BitsType bitsType = (BitsType) testedType.getBaseType();
239         List<Bit> bits = bitsType.getBits();
240         assertEquals(5, bits.size());
241
242         Bit bit0 = bits.get(0);
243         assertEquals(0L, (long) bit0.getPosition());
244
245         Bit bit1 = bits.get(1);
246         assertEquals(500L, (long) bit1.getPosition());
247
248         Bit bit2 = bits.get(2);
249         assertEquals(501L, (long) bit2.getPosition());
250
251         Bit bit3 = bits.get(3);
252         assertEquals(365L, (long) bit3.getPosition());
253
254         Bit bit4 = bits.get(4);
255         assertEquals(502L, (long) bit4.getPosition());
256     }
257
258     @Test
259     public void testIanaTimezones() {
260         Module tested = TestUtils.findModule(testedModules, "iana-timezones");
261         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
262         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
263                 "iana-timezone");
264
265         String expectedDesc = "A timezone location as defined by the IANA timezone\n       database (http://www.iana.org/time-zones)";
266         assertEquals(expectedDesc, testedType.getDescription());
267         assertNull(testedType.getReference());
268         assertEquals(Status.CURRENT, testedType.getStatus());
269
270         QName testedTypeQName = testedType.getQName();
271         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"),
272                 testedTypeQName.getNamespace());
273         assertEquals(TestUtils.createDate("2012-07-09"),
274                 testedTypeQName.getRevision());
275         assertEquals("ianatz", testedTypeQName.getPrefix());
276         assertEquals("iana-timezone", testedTypeQName.getLocalName());
277
278         EnumerationType enumType = (EnumerationType) testedType.getBaseType();
279         List<EnumPair> values = enumType.getValues();
280         assertEquals(415, values.size()); // 0-414
281
282         EnumPair enum168 = values.get(168);
283         assertEquals("America/Danmarkshavn", enum168.getName());
284         assertEquals(168, (int) enum168.getValue());
285         assertEquals("east coast, north of Scoresbysund",
286                 enum168.getDescription());
287
288         EnumPair enum374 = values.get(374);
289         assertEquals("America/Indiana/Winamac", enum374.getName());
290         assertEquals(374, (int) enum374.getValue());
291         assertEquals("Eastern Time - Indiana - Pulaski County",
292                 enum374.getDescription());
293     }
294
295     @Test
296     public void testObjectId128() {
297         Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
298         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
299         ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(
300                 typedefs, "object-identifier-128");
301
302         QName testedTypeQName = testedType.getQName();
303         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
304                 testedTypeQName.getNamespace());
305         assertEquals(TestUtils.createDate("2010-09-24"),
306                 testedTypeQName.getRevision());
307         assertEquals("yang", testedTypeQName.getPrefix());
308         assertEquals("object-identifier-128", testedTypeQName.getLocalName());
309
310         ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
311
312         List<PatternConstraint> patterns = testedTypeBase.getPatterns();
313         assertEquals(2, patterns.size());
314         PatternConstraint pattern1 = patterns.get(0);
315         assertEquals("\\d*(\\.\\d*){1,127}", pattern1.getRegularExpression());
316         PatternConstraint pattern2 = patterns.get(1);
317         assertEquals(
318                 "(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
319                 pattern2.getRegularExpression());
320
321         QName testedTypeBaseQName = testedTypeBase.getQName();
322         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
323                 testedTypeBaseQName.getNamespace());
324         assertEquals(TestUtils.createDate("2010-09-24"),
325                 testedTypeBaseQName.getRevision());
326         assertEquals("yang", testedTypeBaseQName.getPrefix());
327         assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
328     }
329
330 }