Added generation of Transfer Objects from Type Definitions.
[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.Status;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;
25 import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair;
26 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
27 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
28 import org.opendaylight.controller.yang.model.util.BitsType;
29 import org.opendaylight.controller.yang.model.util.EnumerationType;
30 import org.opendaylight.controller.yang.model.util.ExtendedType;
31 import org.opendaylight.controller.yang.model.util.IdentityrefType;
32 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
33 import org.opendaylight.controller.yang.model.util.StringType;
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() {
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.findModule(testedModules, "custom-types-test");
83         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
84
85         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
86         EnumerationType enumType = (EnumerationType) type.getBaseType();
87         List<EnumPair> values = enumType.getValues();
88         assertEquals(4, values.size());
89
90         EnumPair value0 = values.get(0);
91         assertEquals("unknown", value0.getName());
92         assertEquals(0, (int) value0.getValue());
93         assertEquals(
94                 "An unknown or unspecified version of the Internet protocol.",
95                 value0.getDescription());
96
97         EnumPair value1 = values.get(1);
98         assertEquals("ipv4", value1.getName());
99         assertEquals(19, (int) value1.getValue());
100         assertEquals("The IPv4 protocol as defined in RFC 791.",
101                 value1.getDescription());
102
103         EnumPair value2 = values.get(2);
104         assertEquals("ipv6", value2.getName());
105         assertEquals(7, (int) value2.getValue());
106         assertEquals("The IPv6 protocol as defined in RFC 2460.",
107                 value2.getDescription());
108
109         EnumPair value3 = values.get(3);
110         assertEquals("default", value3.getName());
111         assertEquals(20, (int) value3.getValue());
112         assertEquals("default ip",
113                 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         StringType ipv4Base = (StringType) 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         StringType ipv6Base = (StringType) 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         StringType baseType = (StringType) 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.getLengthStatements();
163         assertEquals(1, lengths.size());
164         LengthConstraint length = baseType.getLengthStatements().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         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
176         assertFalse(leafType.requireInstance());
177     }
178
179     @Test
180     public void testInstanceIdentifier2() {
181         Module tested = TestUtils
182                 .findModule(testedModules, "custom-types-test");
183         LeafSchemaNode leaf = (LeafSchemaNode) tested
184                 .getDataChildByName("inst-id-leaf2");
185         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
186         assertTrue(leafType.requireInstance());
187     }
188
189     @Test
190     public void testIdentity() {
191         Module tested = TestUtils
192                 .findModule(testedModules, "custom-types-test");
193         Set<IdentitySchemaNode> identities = tested.getIdentities();
194         IdentitySchemaNode testedIdentity = null;
195         for (IdentitySchemaNode id : identities) {
196             if (id.getQName().getLocalName().equals("crypto-alg")) {
197                 testedIdentity = id;
198                 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
199                 assertEquals("crypto-base", baseIdentity.getQName()
200                         .getLocalName());
201                 assertNull(baseIdentity.getBaseIdentity());
202             }
203         }
204         assertNotNull(testedIdentity);
205     }
206
207     @Test
208     public void testBitsType1() {
209         Module tested = TestUtils
210                 .findModule(testedModules, "custom-types-test");
211         LeafSchemaNode leaf = (LeafSchemaNode) tested
212                 .getDataChildByName("mybits");
213         BitsType leafType = (BitsType) leaf.getType();
214         List<Bit> bits = leafType.getBits();
215         assertEquals(3, bits.size());
216
217         Bit bit1 = bits.get(0);
218         assertEquals("disable-nagle", bit1.getName());
219         assertEquals(0L, (long) bit1.getPosition());
220
221         Bit bit2 = bits.get(1);
222         assertEquals("auto-sense-speed", bit2.getName());
223         assertEquals(1L, (long) bit2.getPosition());
224
225         Bit bit3 = bits.get(2);
226         assertEquals("10-Mb-only", bit3.getName());
227         assertEquals(2L, (long) bit3.getPosition());
228     }
229
230     @Test
231     public void testBitsType2() {
232         Module tested = TestUtils
233                 .findModule(testedModules, "custom-types-test");
234         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
235         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
236                 "access-operations-type");
237
238         BitsType bitsType = (BitsType) testedType.getBaseType();
239         List<Bit> bits = bitsType.getBits();
240         assertEquals(5, bits.size());
241
242         Bit bit0 = bits.get(0);
243         assertEquals(0L, (long) bit0.getPosition());
244
245         Bit bit1 = bits.get(1);
246         assertEquals(500L, (long) bit1.getPosition());
247
248         Bit bit2 = bits.get(2);
249         assertEquals(501L, (long) bit2.getPosition());
250
251         Bit bit3 = bits.get(3);
252         assertEquals(365L, (long) bit3.getPosition());
253
254         Bit bit4 = bits.get(4);
255         assertEquals(502L, (long) bit4.getPosition());
256     }
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         // FIXME: Refactor sources not to be timezone specific.
267         String expectedDesc = "A timezone location as defined by the IANA timezone\n       database (http://www.iana.org/time-zones)";
268         assertEquals(expectedDesc, testedType.getDescription());
269         assertNull(testedType.getReference());
270         assertEquals(Status.CURRENT, testedType.getStatus());
271         */
272         QName testedTypeQName = testedType.getQName();
273         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"),
274                 testedTypeQName.getNamespace());
275         assertEquals(TestUtils.createDate("2012-07-09"),
276                 testedTypeQName.getRevision());
277         assertEquals("ianatz", testedTypeQName.getPrefix());
278         assertEquals("iana-timezone", testedTypeQName.getLocalName());
279
280         EnumerationType enumType = (EnumerationType) testedType.getBaseType();
281         List<EnumPair> values = enumType.getValues();
282         assertEquals(415, values.size()); // 0-414
283
284         EnumPair enum168 = values.get(168);
285         assertEquals("America/Danmarkshavn", enum168.getName());
286         assertEquals(168, (int) enum168.getValue());
287         assertEquals("east coast, north of Scoresbysund",
288                 enum168.getDescription());
289
290         EnumPair enum374 = values.get(374);
291         assertEquals("America/Indiana/Winamac", enum374.getName());
292         assertEquals(374, (int) enum374.getValue());
293         assertEquals("Eastern Time - Indiana - Pulaski County",
294                 enum374.getDescription());
295     }
296
297     @Test
298     public void testObjectId128() {
299         Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
300         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
301         ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(
302                 typedefs, "object-identifier-128");
303
304         QName testedTypeQName = testedType.getQName();
305         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
306                 testedTypeQName.getNamespace());
307         assertEquals(TestUtils.createDate("2010-09-24"),
308                 testedTypeQName.getRevision());
309         assertEquals("yang", testedTypeQName.getPrefix());
310         assertEquals("object-identifier-128", testedTypeQName.getLocalName());
311
312         ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
313
314         List<PatternConstraint> patterns = testedTypeBase.getPatterns();
315         assertEquals(2, patterns.size());
316         PatternConstraint pattern1 = patterns.get(0);
317         assertEquals("\\d*(\\.\\d*){1,127}", pattern1.getRegularExpression());
318         PatternConstraint pattern2 = patterns.get(1);
319         assertEquals(
320                 "(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
321                 pattern2.getRegularExpression());
322
323         QName testedTypeBaseQName = testedTypeBase.getQName();
324         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
325                 testedTypeBaseQName.getNamespace());
326         assertEquals(TestUtils.createDate("2010-09-24"),
327                 testedTypeBaseQName.getRevision());
328         assertEquals("yang", testedTypeBaseQName.getPrefix());
329         assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
330     }
331
332     @Test
333     public void testIdentityref() {
334         Module tested = TestUtils
335                 .findModule(testedModules, "custom-types-test");
336         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
337         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
338                 "service-type-ref");
339         IdentityrefType baseType = (IdentityrefType)testedType.getBaseType();
340         QName identity = baseType.getIdentity();
341         assertEquals(URI.create("urn:simple.container.demo"), 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 }