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("/types").getPath());
46 public void testIPVersion() {
47 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
48 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
49 assertEquals(14, typedefs.size());
51 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
52 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
53 assertTrue(type.getReference().contains("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());
62 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
64 EnumPair value1 = values.get(1);
65 assertEquals("ipv4", value1.getName());
66 assertEquals(1, (int) value1.getValue());
67 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
69 EnumPair value2 = values.get(2);
70 assertEquals("ipv6", value2.getName());
71 assertEquals(2, (int) value2.getValue());
72 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
76 public void testEnumeration() {
77 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
78 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
80 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
81 EnumerationType enumType = (EnumerationType) type.getBaseType();
82 List<EnumPair> values = enumType.getValues();
83 assertEquals(4, values.size());
85 EnumPair value0 = values.get(0);
86 assertEquals("unknown", value0.getName());
87 assertEquals(0, (int) value0.getValue());
88 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
90 EnumPair value1 = values.get(1);
91 assertEquals("ipv4", value1.getName());
92 assertEquals(19, (int) value1.getValue());
93 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
95 EnumPair value2 = values.get(2);
96 assertEquals("ipv6", value2.getName());
97 assertEquals(7, (int) value2.getValue());
98 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
100 EnumPair value3 = values.get(3);
101 assertEquals("default", value3.getName());
102 assertEquals(20, (int) value3.getValue());
103 assertEquals("default ip", value3.getDescription());
107 public void testIpAddress() {
108 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
109 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
110 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
111 UnionType baseType = (UnionType) type.getBaseType();
112 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
114 ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
115 assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
116 String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
117 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?";
118 assertEquals(expectedPattern, ipv4.getPatterns().get(0).getRegularExpression());
120 TypeDefinition<?> ipv4Address = TestUtils.findTypedef(typedefs, "ipv4-address");
121 assertEquals(ipv4Address, ipv4);
123 ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
124 assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
125 List<PatternConstraint> ipv6Patterns = ipv6.getPatterns();
126 expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
127 + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|" + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
128 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?";
129 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
131 TypeDefinition<?> ipv6Address = TestUtils.findTypedef(typedefs, "ipv6-address");
132 assertEquals(ipv6Address, ipv6);
134 expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
136 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
140 public void testDomainName() {
141 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
142 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
143 ExtendedType type = (ExtendedType) TestUtils.findTypedef(typedefs, "domain-name");
144 assertTrue(type.getBaseType() instanceof StringTypeDefinition);
145 List<PatternConstraint> patterns = type.getPatterns();
146 assertEquals(1, patterns.size());
147 String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
148 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.";
149 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
151 List<LengthConstraint> lengths = type.getLengths();
152 assertEquals(1, lengths.size());
153 LengthConstraint length = type.getLengths().get(0);
154 assertEquals(1L, length.getMin());
155 assertEquals(253L, length.getMax());
159 public void testInstanceIdentifier1() {
160 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
161 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf1");
162 InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
163 assertFalse(leafType.requireInstance());
167 public void testInstanceIdentifier2() {
168 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
169 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf2");
170 InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
171 assertTrue(leafType.requireInstance());
175 public void testIdentity() {
176 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
177 Set<IdentitySchemaNode> identities = tested.getIdentities();
178 IdentitySchemaNode testedIdentity = null;
179 for (IdentitySchemaNode id : identities) {
180 if (id.getQName().getLocalName().equals("crypto-alg")) {
182 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
183 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
184 assertNull(baseIdentity.getBaseIdentity());
187 assertNotNull(testedIdentity);
191 public void testBitsType1() {
192 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
193 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("mybits");
194 BitsType leafType = (BitsType) leaf.getType();
195 List<Bit> bits = leafType.getBits();
196 assertEquals(3, bits.size());
198 Bit bit1 = bits.get(0);
199 assertEquals("disable-nagle", bit1.getName());
200 assertEquals(0L, (long) bit1.getPosition());
202 Bit bit2 = bits.get(1);
203 assertEquals("auto-sense-speed", bit2.getName());
204 assertEquals(1L, (long) bit2.getPosition());
206 Bit bit3 = bits.get(2);
207 assertEquals("10-Mb-only", bit3.getName());
208 assertEquals(2L, (long) bit3.getPosition());
212 public void testBitsType2() {
213 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
214 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
215 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
217 BitsType bitsType = (BitsType) testedType.getBaseType();
218 List<Bit> bits = bitsType.getBits();
219 assertEquals(5, bits.size());
221 Bit bit0 = bits.get(0);
222 assertEquals(0L, (long) bit0.getPosition());
224 Bit bit1 = bits.get(1);
225 assertEquals(500L, (long) bit1.getPosition());
227 Bit bit2 = bits.get(2);
228 assertEquals(501L, (long) bit2.getPosition());
230 Bit bit3 = bits.get(3);
231 assertEquals(365L, (long) bit3.getPosition());
233 Bit bit4 = bits.get(4);
234 assertEquals(502L, (long) bit4.getPosition());
238 public void testIanaTimezones() {
239 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
240 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
241 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
243 String expectedDesc = "A timezone location as defined by the IANA timezone";
244 assertTrue(testedType.getDescription().contains(expectedDesc));
245 assertNull(testedType.getReference());
246 assertEquals(Status.CURRENT, testedType.getStatus());
248 QName testedTypeQName = testedType.getQName();
249 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
250 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
251 assertEquals("ianatz", testedTypeQName.getPrefix());
252 assertEquals("iana-timezone", testedTypeQName.getLocalName());
254 EnumerationType enumType = (EnumerationType) testedType.getBaseType();
255 List<EnumPair> values = enumType.getValues();
256 assertEquals(415, values.size()); // 0-414
258 EnumPair enum168 = values.get(168);
259 assertEquals("America/Danmarkshavn", enum168.getName());
260 assertEquals(168, (int) enum168.getValue());
261 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
263 EnumPair enum374 = values.get(374);
264 assertEquals("America/Indiana/Winamac", enum374.getName());
265 assertEquals(374, (int) enum374.getValue());
266 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
270 public void testObjectId128() {
271 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
272 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
273 ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(typedefs, "object-identifier-128");
275 List<PatternConstraint> patterns = testedType.getPatterns();
276 assertEquals(1, patterns.size());
277 PatternConstraint pattern = patterns.get(0);
278 assertEquals("\\d*(\\.\\d*){1,127}", pattern.getRegularExpression());
280 QName testedTypeQName = testedType.getQName();
281 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
282 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
283 assertEquals("yang", testedTypeQName.getPrefix());
284 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
286 ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
287 patterns = testedTypeBase.getPatterns();
288 assertEquals(1, patterns.size());
290 pattern = patterns.get(0);
291 assertEquals("(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
292 pattern.getRegularExpression());
294 QName testedTypeBaseQName = testedTypeBase.getQName();
295 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
296 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
297 assertEquals("yang", testedTypeBaseQName.getPrefix());
298 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
302 public void testIdentityref() {
303 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
304 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
305 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
306 IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
307 QName identity = baseType.getIdentity();
308 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
309 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
310 assertEquals("iit", identity.getPrefix());
311 assertEquals("service-type", identity.getLocalName());
313 LeafSchemaNode type = (LeafSchemaNode)tested.getDataChildByName("type");
315 TypeDefinition<?> leafType = type.getType();
316 assertEquals(testedType, leafType);