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