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