Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / 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.stmt.retest;
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 import static org.junit.Assert.fail;
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.InstanceIdentifierType;
40 import org.opendaylight.yangtools.yang.model.util.UnionType;
41 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
43 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
44 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
47
48 public class TypesResolutionTest {
49     private Set<Module> testedModules;
50
51     @Before
52     public void init() throws Exception {
53         File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
54         File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
55         YangContextParser parser = new YangParserImpl();
56         testedModules = parser.parseFile(yangFile, dependenciesDir).getModules();
57         assertEquals(4, testedModules.size());
58     }
59
60     @Test
61     public void testIPVersion() {
62         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
63         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
64         assertEquals(14, typedefs.size());
65
66         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
67         assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
68         assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
69
70         EnumerationType enumType = (EnumerationType) type.getBaseType();
71         List<EnumPair> values = enumType.getValues();
72         assertEquals(3, values.size());
73
74         EnumPair value0 = values.get(0);
75         assertEquals("unknown", value0.getName());
76         assertEquals(0, (int) value0.getValue());
77         assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
78
79         EnumPair value1 = values.get(1);
80         assertEquals("ipv4", value1.getName());
81         assertEquals(1, (int) value1.getValue());
82         assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
83
84         EnumPair value2 = values.get(2);
85         assertEquals("ipv6", value2.getName());
86         assertEquals(2, (int) value2.getValue());
87         assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
88     }
89
90     @Test
91     public void testEnumeration() {
92         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
93         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
94
95         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
96         EnumerationType enumType = (EnumerationType) type.getBaseType();
97         List<EnumPair> values = enumType.getValues();
98         assertEquals(4, values.size());
99
100         EnumPair value0 = values.get(0);
101         assertEquals("unknown", value0.getName());
102         assertEquals(0, (int) value0.getValue());
103         assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
104
105         EnumPair value1 = values.get(1);
106         assertEquals("ipv4", value1.getName());
107         assertEquals(19, (int) value1.getValue());
108         assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
109
110         EnumPair value2 = values.get(2);
111         assertEquals("ipv6", value2.getName());
112         assertEquals(7, (int) value2.getValue());
113         assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
114
115         EnumPair value3 = values.get(3);
116         assertEquals("default", value3.getName());
117         assertEquals(20, (int) value3.getValue());
118         assertEquals("default ip", value3.getDescription());
119     }
120
121     @Test
122     public void testIpAddress() {
123         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
124         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
125         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
126         UnionType baseType = (UnionType) type.getBaseType();
127         List<TypeDefinition<?>> unionTypes = baseType.getTypes();
128
129         ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
130         assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
131         String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
132                 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
133         assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
134
135         ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
136         assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
137         List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
138         expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
139                 + "((([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}"
140                 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
141         assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
142
143         expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
144                 + "(%.+)?$";
145         assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
146     }
147
148     @Test
149     public void testDomainName() {
150         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
151         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
152         ExtendedType type = (ExtendedType) TestUtils.findTypedef(typedefs, "domain-name");
153         assertTrue(type.getBaseType() instanceof StringTypeDefinition);
154         List<PatternConstraint> patterns = type.getPatternConstraints();
155         assertEquals(1, patterns.size());
156         String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
157                 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
158         assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
159
160         List<LengthConstraint> lengths = type.getLengthConstraints();
161         assertEquals(1, lengths.size());
162         LengthConstraint length = type.getLengthConstraints().get(0);
163         assertEquals(BigInteger.ONE, length.getMin());
164         assertEquals(BigInteger.valueOf(253), length.getMax());
165     }
166
167     @Test
168     public void testInstanceIdentifier1() {
169         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
170         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf1");
171         InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
172         assertFalse(leafType.requireInstance());
173         assertEquals(1, leaf.getUnknownSchemaNodes().size());
174     }
175
176     @Test
177     public void testInstanceIdentifier2() {
178         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
179         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf2");
180         InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
181         assertTrue(leafType.requireInstance());
182     }
183
184     @Test
185     public void testIdentity() {
186         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
187         Set<IdentitySchemaNode> identities = tested.getIdentities();
188         assertEquals(5, identities.size());
189         IdentitySchemaNode cryptoAlg = null;
190         IdentitySchemaNode cryptoBase = null;
191         IdentitySchemaNode cryptoId = null;
192         for (IdentitySchemaNode id : identities) {
193             if (id.getQName().getLocalName().equals("crypto-alg")) {
194                 cryptoAlg = id;
195             } else if ("crypto-base".equals(id.getQName().getLocalName())) {
196                 cryptoBase = id;
197             } else if ("crypto-id".equals(id.getQName().getLocalName())) {
198                 cryptoId = id;
199             }
200         }
201         assertNotNull(cryptoAlg);
202         IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
203         assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
204         assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
205         assertNull(baseIdentity.getBaseIdentity());
206
207         assertNotNull(cryptoBase);
208         assertNull(cryptoBase.getBaseIdentity());
209         assertEquals(3, cryptoBase.getDerivedIdentities().size());
210
211         assertNotNull(cryptoId);
212         assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
213     }
214
215     @Test
216     public void testBitsType1() {
217         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
218         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("mybits");
219         BitsType leafType = (BitsType) leaf.getType();
220         List<Bit> bits = leafType.getBits();
221         assertEquals(3, bits.size());
222
223         Bit bit1 = bits.get(0);
224         assertEquals("disable-nagle", bit1.getName());
225         assertEquals(0L, (long) bit1.getPosition());
226
227         Bit bit2 = bits.get(1);
228         assertEquals("auto-sense-speed", bit2.getName());
229         assertEquals(1L, (long) bit2.getPosition());
230
231         Bit bit3 = bits.get(2);
232         assertEquals("10-Mb-only", bit3.getName());
233         assertEquals(2L, (long) bit3.getPosition());
234     }
235
236     @Test
237     public void testBitsType2() {
238         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
239         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
240         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
241
242         BitsType bitsType = (BitsType) testedType.getBaseType();
243         List<Bit> bits = bitsType.getBits();
244         assertEquals(5, bits.size());
245
246         Bit bit0 = bits.get(0);
247         assertEquals(0L, (long) bit0.getPosition());
248
249         Bit bit1 = bits.get(1);
250         assertEquals(500L, (long) bit1.getPosition());
251
252         Bit bit2 = bits.get(2);
253         assertEquals(501L, (long) bit2.getPosition());
254
255         Bit bit3 = bits.get(3);
256         assertEquals(365L, (long) bit3.getPosition());
257
258         Bit bit4 = bits.get(4);
259         assertEquals(502L, (long) bit4.getPosition());
260     }
261
262     @Test
263     public void testIanaTimezones() {
264         Module tested = TestUtils.findModule(testedModules, "iana-timezones");
265         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
266         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
267
268         String expectedDesc = "A timezone location as defined by the IANA timezone";
269         assertTrue(testedType.getDescription().contains(expectedDesc));
270         assertTrue(testedType.getReference().isEmpty());
271         assertEquals(Status.CURRENT, testedType.getStatus());
272
273         QName testedTypeQName = testedType.getQName();
274         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
275         assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
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", enum168.getDescription());
286
287         EnumPair enum374 = values.get(374);
288         assertEquals("America/Indiana/Winamac", enum374.getName());
289         assertEquals(374, (int) enum374.getValue());
290         assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
291     }
292
293     @Test
294     public void testObjectId128() {
295         Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
296         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
297         ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(typedefs, "object-identifier-128");
298
299         List<PatternConstraint> patterns = testedType.getPatternConstraints();
300         assertEquals(1, patterns.size());
301         PatternConstraint pattern = patterns.get(0);
302         assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
303
304         QName testedTypeQName = testedType.getQName();
305         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
306         assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
307         assertEquals("object-identifier-128", testedTypeQName.getLocalName());
308
309         ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
310         patterns = testedTypeBase.getPatternConstraints();
311         assertEquals(1, patterns.size());
312
313         pattern = patterns.get(0);
314         assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
315                 pattern.getRegularExpression());
316
317         QName testedTypeBaseQName = testedTypeBase.getQName();
318         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
319         assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
320         assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
321     }
322
323     @Test
324     public void testIdentityref() {
325         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
326         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
327         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
328         IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
329         QName identity = baseType.getIdentity().getQName();
330         assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
331         assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
332         assertEquals("service-type", identity.getLocalName());
333
334         LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName("type");
335         assertNotNull(type);
336     }
337
338     @Test
339     public void testUnionWithExt() throws ReactorException {
340
341         final YangStatementSourceImpl yangFile1 = new YangStatementSourceImpl("/types/union-with-ext/extdef.yang",
342                 false);
343         final YangStatementSourceImpl yangFile2 = new YangStatementSourceImpl("/types/union-with-ext/unionbug.yang",
344                 false);
345         final YangStatementSourceImpl yangFile3 = new YangStatementSourceImpl("/ietf/ietf-inet-types@2010-09-24.yang",
346                 false);
347
348         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
349         addSources(reactor, yangFile1, yangFile2, yangFile3);
350
351         final EffectiveSchemaContext result = reactor.buildEffective();
352         assertNotNull(result);
353     }
354
355     @Test
356     public void testUnionWithBits() throws ReactorException {
357
358         final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
359                 "/types/union-with-bits/union-bits-model.yang", false);
360
361         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
362         addSources(reactor, yangFile);
363
364         final EffectiveSchemaContext result = reactor.buildEffective();
365         assertNotNull(result);
366     }
367
368     @Test
369     public void testUnionInList() {
370         final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
371                 "/types/union-in-list/unioninlisttest.yang", false);
372
373         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
374         addSources(reactor, yangFile);
375
376         try {
377             final EffectiveSchemaContext result = reactor.buildEffective();
378             fail("effective build should fail due to union in list; this is not allowed");
379         } catch (Exception e) {
380             assertEquals(IllegalArgumentException.class, e.getClass());
381             assertTrue(e.getMessage().startsWith("union is not a YANG statement or use of extension"));
382         }
383     }
384
385     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
386         for (YangStatementSourceImpl source : sources) {
387             reactor.addSource(source);
388         }
389     }
390 }