36c98c36e2a3928cded65ecdbad0ec11b850f2a4
[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.StringType;
35 import org.opendaylight.controller.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("src/test/resources/types");
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(
53                 "This value represents the version of the IP protocol."));
54         assertTrue(type.getReference().contains(
55                 "RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
56
57         EnumerationType enumType = (EnumerationType) type.getBaseType();
58         List<EnumPair> values = enumType.getValues();
59         assertEquals(3, values.size());
60
61         EnumPair value0 = values.get(0);
62         assertEquals("unknown", value0.getName());
63         assertEquals(0, (int) value0.getValue());
64         assertEquals(
65                 "An unknown or unspecified version of the Internet protocol.",
66                 value0.getDescription());
67
68         EnumPair value1 = values.get(1);
69         assertEquals("ipv4", value1.getName());
70         assertEquals(1, (int) value1.getValue());
71         assertEquals("The IPv4 protocol as defined in RFC 791.",
72                 value1.getDescription());
73
74         EnumPair value2 = values.get(2);
75         assertEquals("ipv6", value2.getName());
76         assertEquals(2, (int) value2.getValue());
77         assertEquals("The IPv6 protocol as defined in RFC 2460.",
78                 value2.getDescription());
79     }
80
81     @Test
82     public void testEnumeration() {
83         Module tested = TestUtils
84                 .findModule(testedModules, "custom-types-test");
85         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
86
87         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
88         EnumerationType enumType = (EnumerationType) type.getBaseType();
89         List<EnumPair> values = enumType.getValues();
90         assertEquals(4, values.size());
91
92         EnumPair value0 = values.get(0);
93         assertEquals("unknown", value0.getName());
94         assertEquals(0, (int) value0.getValue());
95         assertEquals(
96                 "An unknown or unspecified version of the Internet protocol.",
97                 value0.getDescription());
98
99         EnumPair value1 = values.get(1);
100         assertEquals("ipv4", value1.getName());
101         assertEquals(19, (int) value1.getValue());
102         assertEquals("The IPv4 protocol as defined in RFC 791.",
103                 value1.getDescription());
104
105         EnumPair value2 = values.get(2);
106         assertEquals("ipv6", value2.getName());
107         assertEquals(7, (int) value2.getValue());
108         assertEquals("The IPv6 protocol as defined in RFC 2460.",
109                 value2.getDescription());
110
111         EnumPair value3 = values.get(3);
112         assertEquals("default", value3.getName());
113         assertEquals(20, (int) value3.getValue());
114         assertEquals("default ip", value3.getDescription());
115     }
116
117     @Test
118     public void testIpAddress() {
119         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
120         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
121         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
122         UnionType baseType = (UnionType) type.getBaseType();
123         List<TypeDefinition<?>> unionTypes = baseType.getTypes();
124
125         ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
126         StringType ipv4Base = (StringType) ipv4.getBaseType();
127         String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
128                 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
129                 + "(%[\\p{N}\\p{L}]+)?";
130         assertEquals(expectedPattern, ipv4Base.getPatterns().get(0)
131                 .getRegularExpression());
132
133         ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
134         StringType ipv6Base = (StringType) ipv6.getBaseType();
135         List<PatternConstraint> ipv6Patterns = ipv6Base.getPatterns();
136         expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
137                 + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|"
138                 + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
139                 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))"
140                 + "(%[\\p{N}\\p{L}]+)?";
141         assertEquals(expectedPattern, ipv6Patterns.get(0)
142                 .getRegularExpression());
143
144         expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
145                 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
146         assertEquals(expectedPattern, ipv6Patterns.get(1)
147                 .getRegularExpression());
148     }
149
150     @Test
151     public void testDomainName() {
152         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
153         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
154         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "domain-name");
155         StringType baseType = (StringType) type.getBaseType();
156         List<PatternConstraint> patterns = baseType.getPatterns();
157         assertEquals(1, patterns.size());
158         String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
159                 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)"
160                 + "|\\.";
161         assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
162
163         List<LengthConstraint> lengths = baseType.getLengthStatements();
164         assertEquals(1, lengths.size());
165         LengthConstraint length = baseType.getLengthStatements().get(0);
166         assertEquals(1L, length.getMin());
167         assertEquals(253L, length.getMax());
168     }
169
170     @Test
171     public void testInstanceIdentifier1() {
172         Module tested = TestUtils
173                 .findModule(testedModules, "custom-types-test");
174         LeafSchemaNode leaf = (LeafSchemaNode) tested
175                 .getDataChildByName("inst-id-leaf1");
176         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
177         assertFalse(leafType.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 }