Fixed bug in uses statement resolving.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / TypesResolutionTest.java
1 /*
2  * Copyright (c) 2013 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.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.io.FileNotFoundException;
13 import java.net.URI;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
26 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
27 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
28 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
29 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.util.BitsType;
31 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
32 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
33 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
34 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.model.util.UnionType;
36
37 public class TypesResolutionTest {
38     private Set<Module> testedModules;
39
40     @Before
41     public void init() throws FileNotFoundException {
42         testedModules = TestUtils.loadModules(getClass().getResource("/types").getPath());
43     }
44
45     @Test
46     public void testIPVersion() {
47         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
48         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
49         assertEquals(14, typedefs.size());
50
51         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
52         assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
53         assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
54
55         EnumerationType enumType = (EnumerationType) type.getBaseType();
56         List<EnumPair> values = enumType.getValues();
57         assertEquals(3, values.size());
58
59         EnumPair value0 = values.get(0);
60         assertEquals("unknown", value0.getName());
61         assertEquals(0, (int) value0.getValue());
62         assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
63
64         EnumPair value1 = values.get(1);
65         assertEquals("ipv4", value1.getName());
66         assertEquals(1, (int) value1.getValue());
67         assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
68
69         EnumPair value2 = values.get(2);
70         assertEquals("ipv6", value2.getName());
71         assertEquals(2, (int) value2.getValue());
72         assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
73     }
74
75     @Test
76     public void testEnumeration() {
77         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
78         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
79
80         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
81         EnumerationType enumType = (EnumerationType) type.getBaseType();
82         List<EnumPair> values = enumType.getValues();
83         assertEquals(4, values.size());
84
85         EnumPair value0 = values.get(0);
86         assertEquals("unknown", value0.getName());
87         assertEquals(0, (int) value0.getValue());
88         assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
89
90         EnumPair value1 = values.get(1);
91         assertEquals("ipv4", value1.getName());
92         assertEquals(19, (int) value1.getValue());
93         assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
94
95         EnumPair value2 = values.get(2);
96         assertEquals("ipv6", value2.getName());
97         assertEquals(7, (int) value2.getValue());
98         assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
99
100         EnumPair value3 = values.get(3);
101         assertEquals("default", value3.getName());
102         assertEquals(20, (int) value3.getValue());
103         assertEquals("default ip", value3.getDescription());
104     }
105
106     @Test
107     public void testIpAddress() {
108         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
109         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
110         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
111         UnionType baseType = (UnionType) type.getBaseType();
112         List<TypeDefinition<?>> unionTypes = baseType.getTypes();
113
114         ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
115         assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
116         String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
117                 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?";
118         assertEquals(expectedPattern, ipv4.getPatterns().get(0).getRegularExpression());
119
120         ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
121         assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
122         List<PatternConstraint> ipv6Patterns = ipv6.getPatterns();
123         expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
124                 + "((([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}"
125                 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?";
126         assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
127
128         expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
129                 + "(%.+)?";
130         assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
131     }
132
133     @Test
134     public void testDomainName() {
135         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
136         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
137         ExtendedType type = (ExtendedType) TestUtils.findTypedef(typedefs, "domain-name");
138         assertTrue(type.getBaseType() instanceof StringTypeDefinition);
139         List<PatternConstraint> patterns = type.getPatterns();
140         assertEquals(1, patterns.size());
141         String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
142                 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.";
143         assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
144
145         List<LengthConstraint> lengths = type.getLengths();
146         assertEquals(1, lengths.size());
147         LengthConstraint length = type.getLengths().get(0);
148         assertEquals(1L, length.getMin());
149         assertEquals(253L, length.getMax());
150     }
151
152     @Test
153     public void testInstanceIdentifier1() {
154         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
155         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf1");
156         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
157         assertFalse(leafType.requireInstance());
158     }
159
160     @Test
161     public void testInstanceIdentifier2() {
162         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
163         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf2");
164         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
165         assertTrue(leafType.requireInstance());
166     }
167
168     @Test
169     public void testIdentity() {
170         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
171         Set<IdentitySchemaNode> identities = tested.getIdentities();
172         IdentitySchemaNode testedIdentity = null;
173         for (IdentitySchemaNode id : identities) {
174             if (id.getQName().getLocalName().equals("crypto-alg")) {
175                 testedIdentity = id;
176                 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
177                 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
178                 assertNull(baseIdentity.getBaseIdentity());
179             }
180         }
181         assertNotNull(testedIdentity);
182     }
183
184     @Test
185     public void testBitsType1() {
186         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
187         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("mybits");
188         BitsType leafType = (BitsType) leaf.getType();
189         List<Bit> bits = leafType.getBits();
190         assertEquals(3, bits.size());
191
192         Bit bit1 = bits.get(0);
193         assertEquals("disable-nagle", bit1.getName());
194         assertEquals(0L, (long) bit1.getPosition());
195
196         Bit bit2 = bits.get(1);
197         assertEquals("auto-sense-speed", bit2.getName());
198         assertEquals(1L, (long) bit2.getPosition());
199
200         Bit bit3 = bits.get(2);
201         assertEquals("10-Mb-only", bit3.getName());
202         assertEquals(2L, (long) bit3.getPosition());
203     }
204
205     @Test
206     public void testBitsType2() {
207         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
208         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
209         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
210
211         BitsType bitsType = (BitsType) testedType.getBaseType();
212         List<Bit> bits = bitsType.getBits();
213         assertEquals(5, bits.size());
214
215         Bit bit0 = bits.get(0);
216         assertEquals(0L, (long) bit0.getPosition());
217
218         Bit bit1 = bits.get(1);
219         assertEquals(500L, (long) bit1.getPosition());
220
221         Bit bit2 = bits.get(2);
222         assertEquals(501L, (long) bit2.getPosition());
223
224         Bit bit3 = bits.get(3);
225         assertEquals(365L, (long) bit3.getPosition());
226
227         Bit bit4 = bits.get(4);
228         assertEquals(502L, (long) bit4.getPosition());
229     }
230
231     @Test
232     public void testIanaTimezones() {
233         Module tested = TestUtils.findModule(testedModules, "iana-timezones");
234         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
235         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
236
237         String expectedDesc = "A timezone location as defined by the IANA timezone";
238         assertTrue(testedType.getDescription().contains(expectedDesc));
239         assertNull(testedType.getReference());
240         assertEquals(Status.CURRENT, testedType.getStatus());
241
242         QName testedTypeQName = testedType.getQName();
243         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
244         assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
245         assertEquals("ianatz", testedTypeQName.getPrefix());
246         assertEquals("iana-timezone", testedTypeQName.getLocalName());
247
248         EnumerationType enumType = (EnumerationType) testedType.getBaseType();
249         List<EnumPair> values = enumType.getValues();
250         assertEquals(415, values.size()); // 0-414
251
252         EnumPair enum168 = values.get(168);
253         assertEquals("America/Danmarkshavn", enum168.getName());
254         assertEquals(168, (int) enum168.getValue());
255         assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
256
257         EnumPair enum374 = values.get(374);
258         assertEquals("America/Indiana/Winamac", enum374.getName());
259         assertEquals(374, (int) enum374.getValue());
260         assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
261     }
262
263     @Test
264     public void testObjectId128() {
265         Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
266         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
267         ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(typedefs, "object-identifier-128");
268
269         List<PatternConstraint> patterns = testedType.getPatterns();
270         assertEquals(1, patterns.size());
271         PatternConstraint pattern = patterns.get(0);
272         assertEquals("\\d*(\\.\\d*){1,127}", pattern.getRegularExpression());
273
274         QName testedTypeQName = testedType.getQName();
275         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
276         assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
277         assertEquals("yang", testedTypeQName.getPrefix());
278         assertEquals("object-identifier-128", testedTypeQName.getLocalName());
279
280         ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
281         patterns = testedTypeBase.getPatterns();
282         assertEquals(1, patterns.size());
283
284         pattern = patterns.get(0);
285         assertEquals("(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
286                 pattern.getRegularExpression());
287
288         QName testedTypeBaseQName = testedTypeBase.getQName();
289         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
290         assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
291         assertEquals("yang", testedTypeBaseQName.getPrefix());
292         assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
293     }
294
295     @Test
296     public void testIdentityref() {
297         Module tested = TestUtils.findModule(testedModules, "custom-types-test");
298         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
299         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
300         IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
301         QName identity = baseType.getIdentity();
302         assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
303         assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
304         assertEquals("iit", identity.getPrefix());
305         assertEquals("service-type", identity.getLocalName());
306
307         LeafSchemaNode type = (LeafSchemaNode)tested.getDataChildByName("type");
308         assertNotNull(type);
309     }
310
311 }