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