461cc4b5c28522d675851bb417563f2baaa91830
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / 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.controller.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.controller.yang.common.QName;
20 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
21 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.controller.yang.model.api.Module;
23 import org.opendaylight.controller.yang.model.api.Status;
24 import org.opendaylight.controller.yang.model.api.TypeDefinition;
25 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;
26 import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair;
27 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
28 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
29 import org.opendaylight.controller.yang.model.util.BitsType;
30 import org.opendaylight.controller.yang.model.util.EnumerationType;
31 import org.opendaylight.controller.yang.model.util.ExtendedType;
32 import org.opendaylight.controller.yang.model.util.IdentityrefType;
33 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
34 import org.opendaylight.controller.yang.model.util.UnionType;
35
36 public class TypesResolutionTest {
37     private Set<Module> testedModules;
38
39     @Before
40     public void init() throws FileNotFoundException {
41         testedModules = TestUtils.loadModules("src/test/resources/types");
42     }
43
44     @Test
45     public void testIPVersion() {
46         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
47         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
48         assertEquals(14, typedefs.size());
49
50         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
51         assertTrue(type.getDescription().contains(
52                 "This value represents the version of the IP protocol."));
53         assertTrue(type.getReference().contains(
54                 "RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
55
56         EnumerationType enumType = (EnumerationType) type.getBaseType();
57         List<EnumPair> values = enumType.getValues();
58         assertEquals(3, values.size());
59
60         EnumPair value0 = values.get(0);
61         assertEquals("unknown", value0.getName());
62         assertEquals(0, (int) value0.getValue());
63         assertEquals(
64                 "An unknown or unspecified version of the Internet protocol.",
65                 value0.getDescription());
66
67         EnumPair value1 = values.get(1);
68         assertEquals("ipv4", value1.getName());
69         assertEquals(1, (int) value1.getValue());
70         assertEquals("The IPv4 protocol as defined in RFC 791.",
71                 value1.getDescription());
72
73         EnumPair value2 = values.get(2);
74         assertEquals("ipv6", value2.getName());
75         assertEquals(2, (int) value2.getValue());
76         assertEquals("The IPv6 protocol as defined in RFC 2460.",
77                 value2.getDescription());
78     }
79
80     @Test
81     public void testEnumeration() {
82         Module tested = TestUtils
83                 .findModule(testedModules, "custom-types-test");
84         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
85
86         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
87         EnumerationType enumType = (EnumerationType) type.getBaseType();
88         List<EnumPair> values = enumType.getValues();
89         assertEquals(4, values.size());
90
91         EnumPair value0 = values.get(0);
92         assertEquals("unknown", value0.getName());
93         assertEquals(0, (int) value0.getValue());
94         assertEquals(
95                 "An unknown or unspecified version of the Internet protocol.",
96                 value0.getDescription());
97
98         EnumPair value1 = values.get(1);
99         assertEquals("ipv4", value1.getName());
100         assertEquals(19, (int) value1.getValue());
101         assertEquals("The IPv4 protocol as defined in RFC 791.",
102                 value1.getDescription());
103
104         EnumPair value2 = values.get(2);
105         assertEquals("ipv6", value2.getName());
106         assertEquals(7, (int) value2.getValue());
107         assertEquals("The IPv6 protocol as defined in RFC 2460.",
108                 value2.getDescription());
109
110         EnumPair value3 = values.get(3);
111         assertEquals("default", value3.getName());
112         assertEquals(20, (int) value3.getValue());
113         assertEquals("default ip", value3.getDescription());
114     }
115
116     @Test
117     public void testIpAddress() {
118         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
119         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
120         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
121         UnionType baseType = (UnionType) type.getBaseType();
122         List<TypeDefinition<?>> unionTypes = baseType.getTypes();
123
124         ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
125         ExtendedType ipv4Base = (ExtendedType) ipv4.getBaseType();
126         String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
127                 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
128                 + "(%[\\p{N}\\p{L}]+)?";
129         assertEquals(expectedPattern, ipv4Base.getPatterns().get(0)
130                 .getRegularExpression());
131
132         ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
133         ExtendedType ipv6Base = (ExtendedType) ipv6.getBaseType();
134         List<PatternConstraint> ipv6Patterns = ipv6Base.getPatterns();
135         expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
136                 + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|"
137                 + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
138                 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))"
139                 + "(%[\\p{N}\\p{L}]+)?";
140         assertEquals(expectedPattern, ipv6Patterns.get(0)
141                 .getRegularExpression());
142
143         expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
144                 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
145         assertEquals(expectedPattern, ipv6Patterns.get(1)
146                 .getRegularExpression());
147     }
148
149     @Test
150     public void testDomainName() {
151         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
152         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
153         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "domain-name");
154         ExtendedType baseType = (ExtendedType) type.getBaseType();
155         List<PatternConstraint> patterns = baseType.getPatterns();
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                 + "|\\.";
160         assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
161
162         List<LengthConstraint> lengths = baseType.getLengths();
163         assertEquals(1, lengths.size());
164         LengthConstraint length = baseType.getLengths().get(0);
165         assertEquals(1L, length.getMin());
166         assertEquals(253L, length.getMax());
167     }
168
169     @Test
170     public void testInstanceIdentifier1() {
171         Module tested = TestUtils
172                 .findModule(testedModules, "custom-types-test");
173         LeafSchemaNode leaf = (LeafSchemaNode) tested
174                 .getDataChildByName("inst-id-leaf1");
175         ExtendedType leafType = (ExtendedType) leaf.getType();
176         InstanceIdentifier leafTypeBase = (InstanceIdentifier)leafType.getBaseType();
177         assertFalse(leafTypeBase.requireInstance());
178     }
179
180     @Test
181     public void testInstanceIdentifier2() {
182         Module tested = TestUtils
183                 .findModule(testedModules, "custom-types-test");
184         LeafSchemaNode leaf = (LeafSchemaNode) tested
185                 .getDataChildByName("inst-id-leaf2");
186         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
187         assertTrue(leafType.requireInstance());
188     }
189
190     @Test
191     public void testIdentity() {
192         Module tested = TestUtils
193                 .findModule(testedModules, "custom-types-test");
194         Set<IdentitySchemaNode> identities = tested.getIdentities();
195         IdentitySchemaNode testedIdentity = null;
196         for (IdentitySchemaNode id : identities) {
197             if (id.getQName().getLocalName().equals("crypto-alg")) {
198                 testedIdentity = id;
199                 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
200                 assertEquals("crypto-base", baseIdentity.getQName()
201                         .getLocalName());
202                 assertNull(baseIdentity.getBaseIdentity());
203             }
204         }
205         assertNotNull(testedIdentity);
206     }
207
208     @Test
209     public void testBitsType1() {
210         Module tested = TestUtils
211                 .findModule(testedModules, "custom-types-test");
212         LeafSchemaNode leaf = (LeafSchemaNode) tested
213                 .getDataChildByName("mybits");
214         BitsType leafType = (BitsType) leaf.getType();
215         List<Bit> bits = leafType.getBits();
216         assertEquals(3, bits.size());
217
218         Bit bit1 = bits.get(0);
219         assertEquals("disable-nagle", bit1.getName());
220         assertEquals(0L, (long) bit1.getPosition());
221
222         Bit bit2 = bits.get(1);
223         assertEquals("auto-sense-speed", bit2.getName());
224         assertEquals(1L, (long) bit2.getPosition());
225
226         Bit bit3 = bits.get(2);
227         assertEquals("10-Mb-only", bit3.getName());
228         assertEquals(2L, (long) bit3.getPosition());
229     }
230
231     @Test
232     public void testBitsType2() {
233         Module tested = TestUtils
234                 .findModule(testedModules, "custom-types-test");
235         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
236         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
237                 "access-operations-type");
238
239         BitsType bitsType = (BitsType) testedType.getBaseType();
240         List<Bit> bits = bitsType.getBits();
241         assertEquals(5, bits.size());
242
243         Bit bit0 = bits.get(0);
244         assertEquals(0L, (long) bit0.getPosition());
245
246         Bit bit1 = bits.get(1);
247         assertEquals(500L, (long) bit1.getPosition());
248
249         Bit bit2 = bits.get(2);
250         assertEquals(501L, (long) bit2.getPosition());
251
252         Bit bit3 = bits.get(3);
253         assertEquals(365L, (long) bit3.getPosition());
254
255         Bit bit4 = bits.get(4);
256         assertEquals(502L, (long) bit4.getPosition());
257     }
258
259     @Test
260     public void testIanaTimezones() {
261         Module tested = TestUtils.findModule(testedModules, "iana-timezones");
262         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
263         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
264                 "iana-timezone");
265
266         String expectedDesc = "A timezone location as defined by the IANA timezone";
267         assertTrue(testedType.getDescription().contains(expectedDesc));
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"),
341                 identity.getNamespace());
342         assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
343         assertEquals("iit", identity.getPrefix());
344         assertEquals("service-type", identity.getLocalName());
345     }
346
347 }