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.yangtools.yang.parser.impl;
10 import static org.junit.Assert.*;
13 import java.io.FileNotFoundException;
15 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.Status;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
27 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
28 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
29 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
30 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
32 import org.opendaylight.yangtools.yang.model.util.BitsType;
33 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
34 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
35 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
36 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.model.util.UnionType;
39 public class TypesResolutionTest {
40 private Set<Module> testedModules;
43 public void init() throws FileNotFoundException {
44 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
45 File dependenciesDir = new File(getClass().getResource("/ietf").getPath());
46 YangModelParser parser = new YangParserImpl();
47 testedModules = parser.parseYangModels(yangFile, dependenciesDir);
48 assertEquals(4, testedModules.size());
52 public void testIPVersion() {
53 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
54 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
55 assertEquals(14, typedefs.size());
57 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
58 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
59 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
61 EnumerationType enumType = (EnumerationType) type.getBaseType();
62 List<EnumPair> values = enumType.getValues();
63 assertEquals(3, values.size());
65 EnumPair value0 = values.get(0);
66 assertEquals("unknown", value0.getName());
67 assertEquals(0, (int) value0.getValue());
68 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
70 EnumPair value1 = values.get(1);
71 assertEquals("ipv4", value1.getName());
72 assertEquals(1, (int) value1.getValue());
73 assertEquals("The IPv4 protocol as defined in RFC 791.", 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.", value2.getDescription());
82 public void testEnumeration() {
83 Module tested = TestUtils.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());
94 assertEquals("An unknown or unspecified version of the Internet protocol.", 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.", value1.getDescription());
101 EnumPair value2 = values.get(2);
102 assertEquals("ipv6", value2.getName());
103 assertEquals(7, (int) value2.getValue());
104 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
106 EnumPair value3 = values.get(3);
107 assertEquals("default", value3.getName());
108 assertEquals(20, (int) value3.getValue());
109 assertEquals("default ip", value3.getDescription());
113 public void testIpAddress() {
114 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
115 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
116 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
117 UnionType baseType = (UnionType) type.getBaseType();
118 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
120 ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
121 assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
122 String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
123 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?";
124 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
126 ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
127 assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
128 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
129 expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
130 + "((([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}"
131 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?";
132 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
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.getPatternConstraints();
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.getLengthConstraints();
152 assertEquals(1, lengths.size());
153 LengthConstraint length = type.getLengthConstraints().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());
164 assertEquals(1, leaf.getUnknownSchemaNodes().size());
168 public void testInstanceIdentifier2() {
169 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
170 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf2");
171 InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
172 assertTrue(leafType.requireInstance());
176 public void testIdentity() {
177 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
178 Set<IdentitySchemaNode> identities = tested.getIdentities();
179 assertEquals(5, identities.size());
180 IdentitySchemaNode cryptoAlg = null;
181 IdentitySchemaNode cryptoBase = null;
182 IdentitySchemaNode cryptoId = null;
183 for (IdentitySchemaNode id : identities) {
184 if (id.getQName().getLocalName().equals("crypto-alg")) {
186 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
188 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
192 assertNotNull(cryptoAlg);
193 IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
194 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
195 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
196 assertNull(baseIdentity.getBaseIdentity());
198 assertNotNull(cryptoBase);
199 assertNull(cryptoBase.getBaseIdentity());
200 assertEquals(3, cryptoBase.getDerivedIdentities().size());
202 assertNotNull(cryptoId);
203 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
207 public void testBitsType1() {
208 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
209 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("mybits");
210 BitsType leafType = (BitsType) leaf.getType();
211 List<Bit> bits = leafType.getBits();
212 assertEquals(3, bits.size());
214 Bit bit1 = bits.get(0);
215 assertEquals("disable-nagle", bit1.getName());
216 assertEquals(0L, (long) bit1.getPosition());
218 Bit bit2 = bits.get(1);
219 assertEquals("auto-sense-speed", bit2.getName());
220 assertEquals(1L, (long) bit2.getPosition());
222 Bit bit3 = bits.get(2);
223 assertEquals("10-Mb-only", bit3.getName());
224 assertEquals(2L, (long) bit3.getPosition());
228 public void testBitsType2() {
229 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
230 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
231 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
233 BitsType bitsType = (BitsType) testedType.getBaseType();
234 List<Bit> bits = bitsType.getBits();
235 assertEquals(5, bits.size());
237 Bit bit0 = bits.get(0);
238 assertEquals(0L, (long) bit0.getPosition());
240 Bit bit1 = bits.get(1);
241 assertEquals(500L, (long) bit1.getPosition());
243 Bit bit2 = bits.get(2);
244 assertEquals(501L, (long) bit2.getPosition());
246 Bit bit3 = bits.get(3);
247 assertEquals(365L, (long) bit3.getPosition());
249 Bit bit4 = bits.get(4);
250 assertEquals(502L, (long) bit4.getPosition());
254 public void testIanaTimezones() {
255 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
256 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
257 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
259 String expectedDesc = "A timezone location as defined by the IANA timezone";
260 assertTrue(testedType.getDescription().contains(expectedDesc));
261 assertNull(testedType.getReference());
262 assertEquals(Status.CURRENT, testedType.getStatus());
264 QName testedTypeQName = testedType.getQName();
265 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
266 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
267 assertEquals("ianatz", testedTypeQName.getPrefix());
268 assertEquals("iana-timezone", testedTypeQName.getLocalName());
270 EnumerationType enumType = (EnumerationType) testedType.getBaseType();
271 List<EnumPair> values = enumType.getValues();
272 assertEquals(415, values.size()); // 0-414
274 EnumPair enum168 = values.get(168);
275 assertEquals("America/Danmarkshavn", enum168.getName());
276 assertEquals(168, (int) enum168.getValue());
277 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
279 EnumPair enum374 = values.get(374);
280 assertEquals("America/Indiana/Winamac", enum374.getName());
281 assertEquals(374, (int) enum374.getValue());
282 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
286 public void testObjectId128() {
287 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
288 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
289 ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(typedefs, "object-identifier-128");
291 List<PatternConstraint> patterns = testedType.getPatternConstraints();
292 assertEquals(1, patterns.size());
293 PatternConstraint pattern = patterns.get(0);
294 assertEquals("\\d*(\\.\\d*){1,127}", pattern.getRegularExpression());
296 QName testedTypeQName = testedType.getQName();
297 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
298 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
299 assertEquals("yang", testedTypeQName.getPrefix());
300 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
302 ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
303 patterns = testedTypeBase.getPatternConstraints();
304 assertEquals(1, patterns.size());
306 pattern = patterns.get(0);
307 assertEquals("(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
308 pattern.getRegularExpression());
310 QName testedTypeBaseQName = testedTypeBase.getQName();
311 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
312 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
313 assertEquals("yang", testedTypeBaseQName.getPrefix());
314 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
318 public void testIdentityref() {
319 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
320 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
321 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
322 IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
323 QName identity = baseType.getIdentity().getQName();
324 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
325 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
326 assertEquals("iit", identity.getPrefix());
327 assertEquals("service-type", identity.getLocalName());
329 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName("type");