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