2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.FileNotFoundException;
14 import java.util.List;
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.api.type.StringTypeDefinition;
30 import org.opendaylight.controller.yang.model.util.BitsType;
31 import org.opendaylight.controller.yang.model.util.EnumerationType;
32 import org.opendaylight.controller.yang.model.util.ExtendedType;
33 import org.opendaylight.controller.yang.model.util.IdentityrefType;
34 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
35 import org.opendaylight.controller.yang.model.util.UnionType;
37 public class TypesResolutionTest {
38 private Set<Module> testedModules;
41 public void init() throws FileNotFoundException {
42 testedModules = TestUtils.loadModules(getClass().getResource
43 ("/types").getPath());
47 public void testIPVersion() {
48 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
49 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
50 assertEquals(14, typedefs.size());
52 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
53 assertTrue(type.getDescription().contains(
54 "This value represents the version of the IP protocol."));
55 assertTrue(type.getReference().contains(
56 "RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
58 EnumerationType enumType = (EnumerationType) type.getBaseType();
59 List<EnumPair> values = enumType.getValues();
60 assertEquals(3, values.size());
62 EnumPair value0 = values.get(0);
63 assertEquals("unknown", value0.getName());
64 assertEquals(0, (int) value0.getValue());
66 "An unknown or unspecified version of the Internet protocol.",
67 value0.getDescription());
69 EnumPair value1 = values.get(1);
70 assertEquals("ipv4", value1.getName());
71 assertEquals(1, (int) value1.getValue());
72 assertEquals("The IPv4 protocol as defined in RFC 791.",
73 value1.getDescription());
75 EnumPair value2 = values.get(2);
76 assertEquals("ipv6", value2.getName());
77 assertEquals(2, (int) value2.getValue());
78 assertEquals("The IPv6 protocol as defined in RFC 2460.",
79 value2.getDescription());
83 public void testEnumeration() {
84 Module tested = TestUtils
85 .findModule(testedModules, "custom-types-test");
86 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
88 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
89 EnumerationType enumType = (EnumerationType) type.getBaseType();
90 List<EnumPair> values = enumType.getValues();
91 assertEquals(4, values.size());
93 EnumPair value0 = values.get(0);
94 assertEquals("unknown", value0.getName());
95 assertEquals(0, (int) value0.getValue());
97 "An unknown or unspecified version of the Internet protocol.",
98 value0.getDescription());
100 EnumPair value1 = values.get(1);
101 assertEquals("ipv4", value1.getName());
102 assertEquals(19, (int) value1.getValue());
103 assertEquals("The IPv4 protocol as defined in RFC 791.",
104 value1.getDescription());
106 EnumPair value2 = values.get(2);
107 assertEquals("ipv6", value2.getName());
108 assertEquals(7, (int) value2.getValue());
109 assertEquals("The IPv6 protocol as defined in RFC 2460.",
110 value2.getDescription());
112 EnumPair value3 = values.get(3);
113 assertEquals("default", value3.getName());
114 assertEquals(20, (int) value3.getValue());
115 assertEquals("default ip", value3.getDescription());
119 public void testIpAddress() {
120 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
121 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
122 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
123 UnionType baseType = (UnionType) type.getBaseType();
124 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
126 ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
127 assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
128 String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
129 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
130 + "(%[\\p{N}\\p{L}]+)?";
131 assertEquals(expectedPattern, ipv4.getPatterns().get(0)
132 .getRegularExpression());
134 TypeDefinition<?> ipv4Address = TestUtils.findTypedef(typedefs, "ipv4-address");
135 assertEquals(ipv4Address, ipv4);
137 ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
138 assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
139 List<PatternConstraint> ipv6Patterns = ipv6.getPatterns();
140 expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
141 + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|"
142 + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
143 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))"
144 + "(%[\\p{N}\\p{L}]+)?";
145 assertEquals(expectedPattern, ipv6Patterns.get(0)
146 .getRegularExpression());
148 TypeDefinition<?> ipv6Address = TestUtils.findTypedef(typedefs, "ipv6-address");
149 assertEquals(ipv6Address, ipv6);
151 expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
152 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
153 assertEquals(expectedPattern, ipv6Patterns.get(1)
154 .getRegularExpression());
158 public void testDomainName() {
159 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
160 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
161 ExtendedType type = (ExtendedType)TestUtils.findTypedef(typedefs, "domain-name");
162 assertTrue(type.getBaseType() instanceof StringTypeDefinition);
163 List<PatternConstraint> patterns = type.getPatterns();
164 assertEquals(1, patterns.size());
165 String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
166 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)"
168 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
170 List<LengthConstraint> lengths = type.getLengths();
171 assertEquals(1, lengths.size());
172 LengthConstraint length = type.getLengths().get(0);
173 assertEquals(1L, length.getMin());
174 assertEquals(253L, length.getMax());
178 public void testInstanceIdentifier1() {
179 Module tested = TestUtils
180 .findModule(testedModules, "custom-types-test");
181 LeafSchemaNode leaf = (LeafSchemaNode) tested
182 .getDataChildByName("inst-id-leaf1");
183 ExtendedType leafType = (ExtendedType) leaf.getType();
184 InstanceIdentifier leafTypeBase = (InstanceIdentifier) leafType
186 assertFalse(leafTypeBase.requireInstance());
190 public void testInstanceIdentifier2() {
191 Module tested = TestUtils
192 .findModule(testedModules, "custom-types-test");
193 LeafSchemaNode leaf = (LeafSchemaNode) tested
194 .getDataChildByName("inst-id-leaf2");
195 InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
196 assertTrue(leafType.requireInstance());
200 public void testIdentity() {
201 Module tested = TestUtils
202 .findModule(testedModules, "custom-types-test");
203 Set<IdentitySchemaNode> identities = tested.getIdentities();
204 IdentitySchemaNode testedIdentity = null;
205 for (IdentitySchemaNode id : identities) {
206 if (id.getQName().getLocalName().equals("crypto-alg")) {
208 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
209 assertEquals("crypto-base", baseIdentity.getQName()
211 assertNull(baseIdentity.getBaseIdentity());
214 assertNotNull(testedIdentity);
218 public void testBitsType1() {
219 Module tested = TestUtils
220 .findModule(testedModules, "custom-types-test");
221 LeafSchemaNode leaf = (LeafSchemaNode) tested
222 .getDataChildByName("mybits");
223 BitsType leafType = (BitsType) leaf.getType();
224 List<Bit> bits = leafType.getBits();
225 assertEquals(3, bits.size());
227 Bit bit1 = bits.get(0);
228 assertEquals("disable-nagle", bit1.getName());
229 assertEquals(0L, (long) bit1.getPosition());
231 Bit bit2 = bits.get(1);
232 assertEquals("auto-sense-speed", bit2.getName());
233 assertEquals(1L, (long) bit2.getPosition());
235 Bit bit3 = bits.get(2);
236 assertEquals("10-Mb-only", bit3.getName());
237 assertEquals(2L, (long) bit3.getPosition());
241 public void testBitsType2() {
242 Module tested = TestUtils
243 .findModule(testedModules, "custom-types-test");
244 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
245 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
246 "access-operations-type");
248 BitsType bitsType = (BitsType) testedType.getBaseType();
249 List<Bit> bits = bitsType.getBits();
250 assertEquals(5, bits.size());
252 Bit bit0 = bits.get(0);
253 assertEquals(0L, (long) bit0.getPosition());
255 Bit bit1 = bits.get(1);
256 assertEquals(500L, (long) bit1.getPosition());
258 Bit bit2 = bits.get(2);
259 assertEquals(501L, (long) bit2.getPosition());
261 Bit bit3 = bits.get(3);
262 assertEquals(365L, (long) bit3.getPosition());
264 Bit bit4 = bits.get(4);
265 assertEquals(502L, (long) bit4.getPosition());
269 public void testIanaTimezones() {
270 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
271 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
272 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
275 String expectedDesc = "A timezone location as defined by the IANA timezone";
276 assertTrue(testedType.getDescription().contains(expectedDesc));
277 assertNull(testedType.getReference());
278 assertEquals(Status.CURRENT, testedType.getStatus());
280 QName testedTypeQName = testedType.getQName();
281 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"),
282 testedTypeQName.getNamespace());
283 assertEquals(TestUtils.createDate("2012-07-09"),
284 testedTypeQName.getRevision());
285 assertEquals("ianatz", testedTypeQName.getPrefix());
286 assertEquals("iana-timezone", testedTypeQName.getLocalName());
288 EnumerationType enumType = (EnumerationType) testedType.getBaseType();
289 List<EnumPair> values = enumType.getValues();
290 assertEquals(415, values.size()); // 0-414
292 EnumPair enum168 = values.get(168);
293 assertEquals("America/Danmarkshavn", enum168.getName());
294 assertEquals(168, (int) enum168.getValue());
295 assertEquals("east coast, north of Scoresbysund",
296 enum168.getDescription());
298 EnumPair enum374 = values.get(374);
299 assertEquals("America/Indiana/Winamac", enum374.getName());
300 assertEquals(374, (int) enum374.getValue());
301 assertEquals("Eastern Time - Indiana - Pulaski County",
302 enum374.getDescription());
306 public void testObjectId128() {
307 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
308 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
309 ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(
310 typedefs, "object-identifier-128");
312 List<PatternConstraint> patterns = testedType.getPatterns();
313 assertEquals(1, patterns.size());
314 PatternConstraint pattern = patterns.get(0);
315 assertEquals("\\d*(\\.\\d*){1,127}", pattern.getRegularExpression());
317 QName testedTypeQName = testedType.getQName();
318 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
319 testedTypeQName.getNamespace());
320 assertEquals(TestUtils.createDate("2010-09-24"),
321 testedTypeQName.getRevision());
322 assertEquals("yang", testedTypeQName.getPrefix());
323 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
325 ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
326 patterns = testedTypeBase.getPatterns();
327 assertEquals(1, patterns.size());
329 pattern = patterns.get(0);
331 "(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
332 pattern.getRegularExpression());
334 QName testedTypeBaseQName = testedTypeBase.getQName();
335 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
336 testedTypeBaseQName.getNamespace());
337 assertEquals(TestUtils.createDate("2010-09-24"),
338 testedTypeBaseQName.getRevision());
339 assertEquals("yang", testedTypeBaseQName.getPrefix());
340 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
344 public void testIdentityref() {
345 Module tested = TestUtils
346 .findModule(testedModules, "custom-types-test");
347 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
348 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
350 IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
351 QName identity = baseType.getIdentity();
352 assertEquals(URI.create("urn:simple.container.demo"),
353 identity.getNamespace());
354 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
355 assertEquals("iit", identity.getPrefix());
356 assertEquals("service-type", identity.getLocalName());