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