Remove deprecated Yin/YangStatementSourceImpl
[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 SchemaContext context;
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         context = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2, yangFileDependency3);
57         assertEquals(4, context.getModules().size());
58     }
59
60     @Test
61     public void testIPVersion() {
62         Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
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         EnumTypeDefinition enumType = (EnumTypeDefinition) 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, 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, 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, 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(context, "custom-types-test").get();
93         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
94
95         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
96         EnumTypeDefinition enumType = (EnumTypeDefinition) 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, 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, 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, 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, value3.getValue());
118         assertEquals("default ip", value3.getDescription());
119     }
120
121     @Test
122     public void testIpAddress() {
123         Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
124         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
125         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
126         UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
127         List<TypeDefinition<?>> unionTypes = baseType.getTypes();
128
129         StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
130         assertNotNull(ipv4.getBaseType());
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         StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
136         assertNotNull(ipv6.getBaseType());
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(context, "ietf-inet-types").get();
151         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
152         StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
153         assertNotNull(type.getBaseType());
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(Integer.valueOf(1), length.getMin());
164         assertEquals(Integer.valueOf(253), length.getMax());
165     }
166
167     @Test
168     public void testInstanceIdentifier1() {
169         Module tested = TestUtils.findModule(context, "custom-types-test").get();
170         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
171                 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
172         InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
173         assertFalse(leafType.requireInstance());
174         assertEquals(1, leaf.getUnknownSchemaNodes().size());
175     }
176
177     @Test
178     public void testInstanceIdentifier2() {
179         Module tested = TestUtils.findModule(context, "custom-types-test").get();
180         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
181                 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
182         InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
183         assertFalse(leafType.requireInstance());
184     }
185
186     @Test
187     public void testIdentity() {
188         Module tested = TestUtils.findModule(context, "custom-types-test").get();
189         Set<IdentitySchemaNode> identities = tested.getIdentities();
190         assertEquals(5, identities.size());
191         IdentitySchemaNode cryptoAlg = null;
192         IdentitySchemaNode cryptoBase = null;
193         IdentitySchemaNode cryptoId = null;
194         for (IdentitySchemaNode id : identities) {
195             if (id.getQName().getLocalName().equals("crypto-alg")) {
196                 cryptoAlg = id;
197             } else if ("crypto-base".equals(id.getQName().getLocalName())) {
198                 cryptoBase = id;
199             } else if ("crypto-id".equals(id.getQName().getLocalName())) {
200                 cryptoId = id;
201             }
202         }
203         assertNotNull(cryptoAlg);
204         IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
205         assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
206         assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
207         assertNull(baseIdentity.getBaseIdentity());
208
209         assertNotNull(cryptoBase);
210         assertNull(cryptoBase.getBaseIdentity());
211         assertEquals(3, cryptoBase.getDerivedIdentities().size());
212
213         assertNotNull(cryptoId);
214         assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
215     }
216
217     @Test
218     public void testBitsType1() {
219         Module tested = TestUtils.findModule(context, "custom-types-test").get();
220         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
221                 QName.create(tested.getQNameModule(), "mybits"));
222         BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
223         List<Bit> bits = leafType.getBits();
224         assertEquals(3, bits.size());
225
226         Bit bit1 = bits.get(0);
227         assertEquals("disable-nagle", bit1.getName());
228         assertEquals(0L, bit1.getPosition());
229
230         Bit bit2 = bits.get(1);
231         assertEquals("auto-sense-speed", bit2.getName());
232         assertEquals(1L, bit2.getPosition());
233
234         Bit bit3 = bits.get(2);
235         assertEquals("10-Mb-only", bit3.getName());
236         assertEquals(2L, bit3.getPosition());
237     }
238
239     @Test
240     public void testBitsType2() {
241         Module tested = TestUtils.findModule(context, "custom-types-test").get();
242         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
243         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
244
245         BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
246         List<Bit> bits = bitsType.getBits();
247         assertEquals(5, bits.size());
248
249         Bit bit0 = bits.get(0);
250         assertEquals("create", bit0.getName());
251         assertEquals(0L, bit0.getPosition());
252
253         Bit bit1 = bits.get(1);
254         assertEquals("delete", bit1.getName());
255         assertEquals(365L, bit1.getPosition());
256
257         Bit bit2 = bits.get(2);
258         assertEquals("read", bit2.getName());
259         assertEquals(500L, bit2.getPosition());
260
261         Bit bit3 = bits.get(3);
262         assertEquals("update", bit3.getName());
263         assertEquals(501L, bit3.getPosition());
264
265         Bit bit4 = bits.get(4);
266         assertEquals("exec", bit4.getName());
267         assertEquals(502L, bit4.getPosition());
268     }
269
270     @Test
271     public void testIanaTimezones() {
272         Module tested = TestUtils.findModule(context, "iana-timezones").get();
273         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
274         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
275
276         String expectedDesc = "A timezone location as defined by the IANA timezone";
277         assertTrue(testedType.getDescription().contains(expectedDesc));
278         assertNull(testedType.getReference());
279         assertEquals(Status.CURRENT, testedType.getStatus());
280
281         QName testedTypeQName = testedType.getQName();
282         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
283         assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
284         assertEquals("iana-timezone", testedTypeQName.getLocalName());
285
286         EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
287         List<EnumPair> values = enumType.getValues();
288         assertEquals(415, values.size()); // 0-414
289
290         EnumPair enum168 = values.get(168);
291         assertEquals("America/Danmarkshavn", enum168.getName());
292         assertEquals(168, enum168.getValue());
293         assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
294
295         EnumPair enum374 = values.get(374);
296         assertEquals("America/Indiana/Winamac", enum374.getName());
297         assertEquals(374, enum374.getValue());
298         assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
299     }
300
301     @Test
302     public void testObjectId128() {
303         Module tested = TestUtils.findModule(context, "ietf-yang-types").get();
304         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
305         StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
306                 "object-identifier-128");
307
308         List<PatternConstraint> patterns = testedType.getPatternConstraints();
309         assertEquals(1, patterns.size());
310         PatternConstraint pattern = patterns.get(0);
311         assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
312
313         QName testedTypeQName = testedType.getQName();
314         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
315         assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
316         assertEquals("object-identifier-128", testedTypeQName.getLocalName());
317
318         StringTypeDefinition testedTypeBase = testedType.getBaseType();
319         patterns = testedTypeBase.getPatternConstraints();
320         assertEquals(1, patterns.size());
321
322         pattern = patterns.get(0);
323         assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
324                 pattern.getRegularExpression());
325
326         QName testedTypeBaseQName = testedTypeBase.getQName();
327         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
328         assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
329         assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
330     }
331
332     @Test
333     public void testIdentityref() {
334         Module tested = TestUtils.findModule(context, "custom-types-test").get();
335         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
336         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
337         IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
338         QName identity = baseType.getIdentity().getQName();
339         assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
340         assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
341         assertEquals("service-type", identity.getLocalName());
342
343         LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
344         assertNotNull(type);
345     }
346
347     @Test
348     public void testUnionWithExt() throws ReactorException {
349
350         final StatementStreamSource yangFile1 = sourceForResource("/types/union-with-ext/extdef.yang");
351         final StatementStreamSource yangFile2 = sourceForResource("/types/union-with-ext/unionbug.yang");
352         final StatementStreamSource yangFile3 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
353
354         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
355         reactor.addSources(yangFile1, yangFile2, yangFile3);
356
357         final SchemaContext result = reactor.buildEffective();
358         assertNotNull(result);
359     }
360
361     @Test
362     public void testUnionWithBits() throws ReactorException {
363
364         final StatementStreamSource yangFile = sourceForResource("/types/union-with-bits/union-bits-model.yang");
365
366         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
367         reactor.addSources(yangFile);
368
369         final SchemaContext result = reactor.buildEffective();
370         assertNotNull(result);
371     }
372
373     @Test
374     public void testUnionInList() {
375         final StatementStreamSource yangFile = sourceForResource("/types/union-in-list/unioninlisttest.yang");
376
377         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
378         reactor.addSources(yangFile);
379
380         try {
381             final SchemaContext result = reactor.buildEffective();
382             fail("effective build should fail due to union in list; this is not allowed");
383         } catch (Exception e) {
384             assertEquals(SomeModifiersUnresolvedException.class, e.getClass());
385             assertTrue(e.getCause() instanceof SourceException);
386             assertTrue(e.getCause().getMessage().startsWith("union is not a YANG statement or use of extension"));
387         }
388     }
389 }