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.model.parser.impl;
10 import static org.junit.Assert.*;
13 import java.util.List;
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;
35 public class TypesResolutionTest {
36 private Set<Module> testedModules;
40 testedModules = TestUtils.loadModules("src/test/resources/types");
44 public void testIPVersion() {
45 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
46 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
47 assertEquals(14, typedefs.size());
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"));
55 EnumerationType enumType = (EnumerationType) type.getBaseType();
56 List<EnumPair> values = enumType.getValues();
57 assertEquals(3, values.size());
59 EnumPair value0 = values.get(0);
60 assertEquals("unknown", value0.getName());
61 assertEquals(0, (int) value0.getValue());
63 "An unknown or unspecified version of the Internet protocol.",
64 value0.getDescription());
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());
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());
80 public void testEnumeration() {
81 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
82 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
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());
89 EnumPair value0 = values.get(0);
90 assertEquals("unknown", value0.getName());
91 assertEquals(0, (int) value0.getValue());
93 "An unknown or unspecified version of the Internet protocol.",
94 value0.getDescription());
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());
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());
108 EnumPair value3 = values.get(3);
109 assertEquals("default", value3.getName());
110 assertEquals(20, (int) value3.getValue());
111 assertEquals("default ip",
112 value3.getDescription());
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();
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());
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());
142 expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
143 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
144 assertEquals(expectedPattern, ipv6Patterns.get(1)
145 .getRegularExpression());
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]\\.?)"
159 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
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());
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());
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());
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")) {
197 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
198 assertEquals("crypto-base", baseIdentity.getQName()
200 assertNull(baseIdentity.getBaseIdentity());
203 assertNotNull(testedIdentity);
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());
216 Bit bit1 = bits.get(0);
217 assertEquals("disable-nagle", bit1.getName());
218 assertEquals(0L, (long) bit1.getPosition());
220 Bit bit2 = bits.get(1);
221 assertEquals("auto-sense-speed", bit2.getName());
222 assertEquals(1L, (long) bit2.getPosition());
224 Bit bit3 = bits.get(2);
225 assertEquals("10-Mb-only", bit3.getName());
226 assertEquals(2L, (long) bit3.getPosition());
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");
237 BitsType bitsType = (BitsType) testedType.getBaseType();
238 List<Bit> bits = bitsType.getBits();
239 assertEquals(5, bits.size());
241 Bit bit0 = bits.get(0);
242 assertEquals(0L, (long) bit0.getPosition());
244 Bit bit1 = bits.get(1);
245 assertEquals(500L, (long) bit1.getPosition());
247 Bit bit2 = bits.get(2);
248 assertEquals(501L, (long) bit2.getPosition());
250 Bit bit3 = bits.get(3);
251 assertEquals(365L, (long) bit3.getPosition());
253 Bit bit4 = bits.get(4);
254 assertEquals(502L, (long) bit4.getPosition());
259 public void testIanaTimezones() {
260 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
261 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
262 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
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());
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());
279 EnumerationType enumType = (EnumerationType) testedType.getBaseType();
280 List<EnumPair> values = enumType.getValues();
281 assertEquals(415, values.size()); // 0-414
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());
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());
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");
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());
311 ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
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);
319 "(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
320 pattern2.getRegularExpression());
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());
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,
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());