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