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.*;
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.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;
36 public class TypesResolutionTest {
37 private Set<Module> testedModules;
41 testedModules = TestUtils.loadModules("src/test/resources/types");
45 public void testIPVersion() {
46 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
47 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
48 assertEquals(14, typedefs.size());
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"));
56 EnumerationType enumType = (EnumerationType) type.getBaseType();
57 List<EnumPair> values = enumType.getValues();
58 assertEquals(3, values.size());
60 EnumPair value0 = values.get(0);
61 assertEquals("unknown", value0.getName());
62 assertEquals(0, (int) value0.getValue());
64 "An unknown or unspecified version of the Internet protocol.",
65 value0.getDescription());
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());
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());
81 public void testEnumeration() {
82 Module tested = TestUtils
83 .findModule(testedModules, "custom-types-test");
84 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
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());
91 EnumPair value0 = values.get(0);
92 assertEquals("unknown", value0.getName());
93 assertEquals(0, (int) value0.getValue());
95 "An unknown or unspecified version of the Internet protocol.",
96 value0.getDescription());
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());
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());
110 EnumPair value3 = values.get(3);
111 assertEquals("default", value3.getName());
112 assertEquals(20, (int) value3.getValue());
113 assertEquals("default ip", value3.getDescription());
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();
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());
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());
143 expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
144 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
145 assertEquals(expectedPattern, ipv6Patterns.get(1)
146 .getRegularExpression());
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]\\.?)"
160 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
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());
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());
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());
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")) {
198 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
199 assertEquals("crypto-base", baseIdentity.getQName()
201 assertNull(baseIdentity.getBaseIdentity());
204 assertNotNull(testedIdentity);
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());
217 Bit bit1 = bits.get(0);
218 assertEquals("disable-nagle", bit1.getName());
219 assertEquals(0L, (long) bit1.getPosition());
221 Bit bit2 = bits.get(1);
222 assertEquals("auto-sense-speed", bit2.getName());
223 assertEquals(1L, (long) bit2.getPosition());
225 Bit bit3 = bits.get(2);
226 assertEquals("10-Mb-only", bit3.getName());
227 assertEquals(2L, (long) bit3.getPosition());
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");
238 BitsType bitsType = (BitsType) testedType.getBaseType();
239 List<Bit> bits = bitsType.getBits();
240 assertEquals(5, bits.size());
242 Bit bit0 = bits.get(0);
243 assertEquals(0L, (long) bit0.getPosition());
245 Bit bit1 = bits.get(1);
246 assertEquals(500L, (long) bit1.getPosition());
248 Bit bit2 = bits.get(2);
249 assertEquals(501L, (long) bit2.getPosition());
251 Bit bit3 = bits.get(3);
252 assertEquals(365L, (long) bit3.getPosition());
254 Bit bit4 = bits.get(4);
255 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 String expectedDesc = "A timezone location as defined by the IANA timezone";
266 assertTrue(testedType.getDescription().contains(expectedDesc));
267 assertNull(testedType.getReference());
268 assertEquals(Status.CURRENT, testedType.getStatus());
270 QName testedTypeQName = testedType.getQName();
271 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"),
272 testedTypeQName.getNamespace());
273 assertEquals(TestUtils.createDate("2012-07-09"),
274 testedTypeQName.getRevision());
275 assertEquals("ianatz", testedTypeQName.getPrefix());
276 assertEquals("iana-timezone", testedTypeQName.getLocalName());
278 EnumerationType enumType = (EnumerationType) testedType.getBaseType();
279 List<EnumPair> values = enumType.getValues();
280 assertEquals(415, values.size()); // 0-414
282 EnumPair enum168 = values.get(168);
283 assertEquals("America/Danmarkshavn", enum168.getName());
284 assertEquals(168, (int) enum168.getValue());
285 assertEquals("east coast, north of Scoresbysund",
286 enum168.getDescription());
288 EnumPair enum374 = values.get(374);
289 assertEquals("America/Indiana/Winamac", enum374.getName());
290 assertEquals(374, (int) enum374.getValue());
291 assertEquals("Eastern Time - Indiana - Pulaski County",
292 enum374.getDescription());
296 public void testObjectId128() {
297 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
298 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
299 ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(
300 typedefs, "object-identifier-128");
302 QName testedTypeQName = testedType.getQName();
303 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
304 testedTypeQName.getNamespace());
305 assertEquals(TestUtils.createDate("2010-09-24"),
306 testedTypeQName.getRevision());
307 assertEquals("yang", testedTypeQName.getPrefix());
308 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
310 ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
312 List<PatternConstraint> patterns = testedTypeBase.getPatterns();
313 assertEquals(2, patterns.size());
314 PatternConstraint pattern1 = patterns.get(0);
315 assertEquals("\\d*(\\.\\d*){1,127}", pattern1.getRegularExpression());
316 PatternConstraint pattern2 = patterns.get(1);
318 "(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
319 pattern2.getRegularExpression());
321 QName testedTypeBaseQName = testedTypeBase.getQName();
322 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
323 testedTypeBaseQName.getNamespace());
324 assertEquals(TestUtils.createDate("2010-09-24"),
325 testedTypeBaseQName.getRevision());
326 assertEquals("yang", testedTypeBaseQName.getPrefix());
327 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
331 public void testIdentityref() {
332 Module tested = TestUtils
333 .findModule(testedModules, "custom-types-test");
334 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
335 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
337 IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
338 QName identity = baseType.getIdentity();
339 assertEquals(URI.create("urn:simple.container.demo"),
340 identity.getNamespace());
341 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
342 assertEquals("iit", identity.getPrefix());
343 assertEquals("service-type", identity.getLocalName());