Bug 2480: Union objects are generated incorrectly when using bits type
[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.Arrays;
20 import java.util.List;
21 import java.util.Set;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.Status;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
32 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
33 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
34 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
36 import org.opendaylight.yangtools.yang.model.util.BitsType;
37 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
38 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
39 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
40 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
41 import org.opendaylight.yangtools.yang.model.util.UnionType;
42
43 public class TypesResolutionTest {
44     private Set<Module> testedModules;
45
46     @Before
47     public void init() throws Exception {
48         File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
49         File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
50         YangContextParser parser = new YangParserImpl();
51         testedModules = parser.parseFile(yangFile, dependenciesDir).getModules();
52         assertEquals(4, testedModules.size());
53     }
54
55     @Test
56     public void testIPVersion() {
57         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
58         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
59         assertEquals(14, typedefs.size());
60
61         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
62         assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
63         assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
64
65         EnumerationType enumType = (EnumerationType) type.getBaseType();
66         List<EnumPair> values = enumType.getValues();
67         assertEquals(3, values.size());
68
69         EnumPair value0 = values.get(0);
70         assertEquals("unknown", value0.getName());
71         assertEquals(0, (int) value0.getValue());
72         assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
73
74         EnumPair value1 = values.get(1);
75         assertEquals("ipv4", value1.getName());
76         assertEquals(1, (int) value1.getValue());
77         assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
78
79         EnumPair value2 = values.get(2);
80         assertEquals("ipv6", value2.getName());
81         assertEquals(2, (int) value2.getValue());
82         assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
83     }
84
85     @Test
86     public void testEnumeration() {
87         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
88         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
89
90         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
91         EnumerationType enumType = (EnumerationType) type.getBaseType();
92         List<EnumPair> values = enumType.getValues();
93         assertEquals(4, values.size());
94
95         EnumPair value0 = values.get(0);
96         assertEquals("unknown", value0.getName());
97         assertEquals(0, (int) value0.getValue());
98         assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
99
100         EnumPair value1 = values.get(1);
101         assertEquals("ipv4", value1.getName());
102         assertEquals(19, (int) value1.getValue());
103         assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
104
105         EnumPair value2 = values.get(2);
106         assertEquals("ipv6", value2.getName());
107         assertEquals(7, (int) value2.getValue());
108         assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
109
110         EnumPair value3 = values.get(3);
111         assertEquals("default", value3.getName());
112         assertEquals(20, (int) value3.getValue());
113         assertEquals("default ip", 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         assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
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])" + "(%[\\p{N}\\p{L}]+)?$";
128         assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
129
130         ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
131         assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
132         List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
133         expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
134                 + "((([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}"
135                 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
136         assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
137
138         expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
139                 + "(%.+)?$";
140         assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
141     }
142
143     @Test
144     public void testDomainName() {
145         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
146         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
147         ExtendedType type = (ExtendedType) TestUtils.findTypedef(typedefs, "domain-name");
148         assertTrue(type.getBaseType() instanceof StringTypeDefinition);
149         List<PatternConstraint> patterns = type.getPatternConstraints();
150         assertEquals(1, patterns.size());
151         String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
152                 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
153         assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
154
155         List<LengthConstraint> lengths = type.getLengthConstraints();
156         assertEquals(1, lengths.size());
157         LengthConstraint length = type.getLengthConstraints().get(0);
158         assertEquals(BigInteger.ONE, length.getMin());
159         assertEquals(BigInteger.valueOf(253), length.getMax());
160     }
161
162     @Test
163     public void testInstanceIdentifier1() {
164         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
165         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf1");
166         InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
167         assertFalse(leafType.requireInstance());
168         assertEquals(1, leaf.getUnknownSchemaNodes().size());
169     }
170
171     @Test
172     public void testInstanceIdentifier2() {
173         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
174         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf2");
175         InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
176         assertTrue(leafType.requireInstance());
177     }
178
179     @Test
180     public void testIdentity() {
181         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
182         Set<IdentitySchemaNode> identities = tested.getIdentities();
183         assertEquals(5, identities.size());
184         IdentitySchemaNode cryptoAlg = null;
185         IdentitySchemaNode cryptoBase = null;
186         IdentitySchemaNode cryptoId = null;
187         for (IdentitySchemaNode id : identities) {
188             if (id.getQName().getLocalName().equals("crypto-alg")) {
189                 cryptoAlg = id;
190             } else if ("crypto-base".equals(id.getQName().getLocalName())) {
191                 cryptoBase = id;
192             } else if ("crypto-id".equals(id.getQName().getLocalName())) {
193                 cryptoId = id;
194             }
195         }
196         assertNotNull(cryptoAlg);
197         IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
198         assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
199         assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
200         assertNull(baseIdentity.getBaseIdentity());
201
202         assertNotNull(cryptoBase);
203         assertNull(cryptoBase.getBaseIdentity());
204         assertEquals(3, cryptoBase.getDerivedIdentities().size());
205
206         assertNotNull(cryptoId);
207         assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
208     }
209
210     @Test
211     public void testBitsType1() {
212         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
213         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("mybits");
214         BitsType leafType = (BitsType) leaf.getType();
215         List<Bit> bits = leafType.getBits();
216         assertEquals(3, bits.size());
217
218         Bit bit1 = bits.get(0);
219         assertEquals("disable-nagle", bit1.getName());
220         assertEquals(0L, (long) bit1.getPosition());
221
222         Bit bit2 = bits.get(1);
223         assertEquals("auto-sense-speed", bit2.getName());
224         assertEquals(1L, (long) bit2.getPosition());
225
226         Bit bit3 = bits.get(2);
227         assertEquals("10-Mb-only", bit3.getName());
228         assertEquals(2L, (long) bit3.getPosition());
229     }
230
231     @Test
232     public void testBitsType2() {
233         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
234         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
235         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
236
237         BitsType bitsType = (BitsType) testedType.getBaseType();
238         List<Bit> bits = bitsType.getBits();
239         assertEquals(5, bits.size());
240
241         Bit bit0 = bits.get(0);
242         assertEquals(0L, (long) bit0.getPosition());
243
244         Bit bit1 = bits.get(1);
245         assertEquals(500L, (long) bit1.getPosition());
246
247         Bit bit2 = bits.get(2);
248         assertEquals(501L, (long) bit2.getPosition());
249
250         Bit bit3 = bits.get(3);
251         assertEquals(365L, (long) bit3.getPosition());
252
253         Bit bit4 = bits.get(4);
254         assertEquals(502L, (long) bit4.getPosition());
255     }
256
257     @Test
258     public void testIanaTimezones() {
259         Module tested = TestUtils.findModule(testedModules, "iana-timezones");
260         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
261         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
262
263         String expectedDesc = "A timezone location as defined by the IANA timezone";
264         assertTrue(testedType.getDescription().contains(expectedDesc));
265         assertTrue(testedType.getReference().isEmpty());
266         assertEquals(Status.CURRENT, testedType.getStatus());
267
268         QName testedTypeQName = testedType.getQName();
269         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
270         assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
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("object-identifier-128", testedTypeQName.getLocalName());
303
304         ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
305         patterns = testedTypeBase.getPatternConstraints();
306         assertEquals(1, patterns.size());
307
308         pattern = patterns.get(0);
309         assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
310                 pattern.getRegularExpression());
311
312         QName testedTypeBaseQName = testedTypeBase.getQName();
313         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
314         assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
315         assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
316     }
317
318     @Test
319     public void testIdentityref() {
320         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
321         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
322         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
323         IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
324         QName identity = baseType.getIdentity().getQName();
325         assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
326         assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
327         assertEquals("service-type", identity.getLocalName());
328
329         LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName("type");
330         assertNotNull(type);
331     }
332
333     @Test
334     public void testUnionWithExt() throws Exception {
335         File extdef = new File(getClass().getResource("/types/union-with-ext/extdef.yang").toURI());
336         File unionbug = new File(getClass().getResource("/types/union-with-ext/unionbug.yang").toURI());
337         File inet = new File(getClass().getResource("/ietf/ietf-inet-types@2010-09-24.yang").toURI());
338         YangContextParser parser = new YangParserImpl();
339         parser.parseFiles(Arrays.asList(extdef, unionbug, inet));
340     }
341
342     @Test
343     public void testUnionWithBits() throws Exception {
344         File unionbits = new File(getClass().getResource("/types/union-with-bits/union-bits-model.yang").toURI());
345         YangContextParser parser = new YangParserImpl();
346         parser.parseFiles(Arrays.asList(unionbits));
347     }
348
349 }