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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
17 import java.math.BigInteger;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.Status;
30 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
32 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
33 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
34 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
35 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
37 import org.opendaylight.yangtools.yang.model.util.BitsType;
38 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
39 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
40 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
41 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
42 import org.opendaylight.yangtools.yang.model.util.UnionType;
43 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
45 public class TypesResolutionTest {
46 private Set<Module> testedModules;
49 public void init() throws Exception {
50 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
51 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
52 YangContextParser parser = new YangParserImpl();
53 testedModules = parser.parseFile(yangFile, dependenciesDir).getModules();
54 assertEquals(4, testedModules.size());
58 public void testIPVersion() {
59 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
60 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
61 assertEquals(14, typedefs.size());
63 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
64 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
65 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
67 EnumerationType enumType = (EnumerationType) type.getBaseType();
68 List<EnumPair> values = enumType.getValues();
69 assertEquals(3, values.size());
71 EnumPair value0 = values.get(0);
72 assertEquals("unknown", value0.getName());
73 assertEquals(0, (int) value0.getValue());
74 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
76 EnumPair value1 = values.get(1);
77 assertEquals("ipv4", value1.getName());
78 assertEquals(1, (int) value1.getValue());
79 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
81 EnumPair value2 = values.get(2);
82 assertEquals("ipv6", value2.getName());
83 assertEquals(2, (int) value2.getValue());
84 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
88 public void testEnumeration() {
89 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
90 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
92 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
93 EnumerationType enumType = (EnumerationType) type.getBaseType();
94 List<EnumPair> values = enumType.getValues();
95 assertEquals(4, values.size());
97 EnumPair value0 = values.get(0);
98 assertEquals("unknown", value0.getName());
99 assertEquals(0, (int) value0.getValue());
100 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
102 EnumPair value1 = values.get(1);
103 assertEquals("ipv4", value1.getName());
104 assertEquals(19, (int) value1.getValue());
105 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
107 EnumPair value2 = values.get(2);
108 assertEquals("ipv6", value2.getName());
109 assertEquals(7, (int) value2.getValue());
110 assertEquals("The IPv6 protocol as defined in RFC 2460.", 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])" + "(%[\\p{N}\\p{L}]+)?$";
130 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
132 ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
133 assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
134 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
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}))|" + "(((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])))" + "(%[\\p{N}\\p{L}]+)?$";
138 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
140 expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
142 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
146 public void testDomainName() {
147 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
148 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
149 ExtendedType type = (ExtendedType) TestUtils.findTypedef(typedefs, "domain-name");
150 assertTrue(type.getBaseType() instanceof StringTypeDefinition);
151 List<PatternConstraint> patterns = type.getPatternConstraints();
152 assertEquals(1, patterns.size());
153 String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
154 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
155 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
157 List<LengthConstraint> lengths = type.getLengthConstraints();
158 assertEquals(1, lengths.size());
159 LengthConstraint length = type.getLengthConstraints().get(0);
160 assertEquals(BigInteger.ONE, length.getMin());
161 assertEquals(BigInteger.valueOf(253), length.getMax());
165 public void testInstanceIdentifier1() {
166 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
167 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf1");
168 InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
169 assertFalse(leafType.requireInstance());
170 assertEquals(1, leaf.getUnknownSchemaNodes().size());
174 public void testInstanceIdentifier2() {
175 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
176 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf2");
177 InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
178 assertTrue(leafType.requireInstance());
182 public void testIdentity() {
183 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
184 Set<IdentitySchemaNode> identities = tested.getIdentities();
185 assertEquals(5, identities.size());
186 IdentitySchemaNode cryptoAlg = null;
187 IdentitySchemaNode cryptoBase = null;
188 IdentitySchemaNode cryptoId = null;
189 for (IdentitySchemaNode id : identities) {
190 if (id.getQName().getLocalName().equals("crypto-alg")) {
192 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
194 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
198 assertNotNull(cryptoAlg);
199 IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
200 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
201 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
202 assertNull(baseIdentity.getBaseIdentity());
204 assertNotNull(cryptoBase);
205 assertNull(cryptoBase.getBaseIdentity());
206 assertEquals(3, cryptoBase.getDerivedIdentities().size());
208 assertNotNull(cryptoId);
209 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
213 public void testBitsType1() {
214 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
215 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("mybits");
216 BitsType leafType = (BitsType) leaf.getType();
217 List<Bit> bits = leafType.getBits();
218 assertEquals(3, bits.size());
220 Bit bit1 = bits.get(0);
221 assertEquals("disable-nagle", bit1.getName());
222 assertEquals(0L, (long) bit1.getPosition());
224 Bit bit2 = bits.get(1);
225 assertEquals("auto-sense-speed", bit2.getName());
226 assertEquals(1L, (long) bit2.getPosition());
228 Bit bit3 = bits.get(2);
229 assertEquals("10-Mb-only", bit3.getName());
230 assertEquals(2L, (long) bit3.getPosition());
234 public void testBitsType2() {
235 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
236 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
237 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
239 BitsType bitsType = (BitsType) testedType.getBaseType();
240 List<Bit> bits = bitsType.getBits();
241 assertEquals(5, bits.size());
243 Bit bit0 = bits.get(0);
244 assertEquals(0L, (long) bit0.getPosition());
246 Bit bit1 = bits.get(1);
247 assertEquals(500L, (long) bit1.getPosition());
249 Bit bit2 = bits.get(2);
250 assertEquals(501L, (long) bit2.getPosition());
252 Bit bit3 = bits.get(3);
253 assertEquals(365L, (long) bit3.getPosition());
255 Bit bit4 = bits.get(4);
256 assertEquals(502L, (long) bit4.getPosition());
260 public void testIanaTimezones() {
261 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
262 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
263 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
265 String expectedDesc = "A timezone location as defined by the IANA timezone";
266 assertTrue(testedType.getDescription().contains(expectedDesc));
267 assertTrue(testedType.getReference().isEmpty());
268 assertEquals(Status.CURRENT, testedType.getStatus());
270 QName testedTypeQName = testedType.getQName();
271 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
272 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
273 assertEquals("iana-timezone", testedTypeQName.getLocalName());
275 EnumerationType enumType = (EnumerationType) testedType.getBaseType();
276 List<EnumPair> values = enumType.getValues();
277 assertEquals(415, values.size()); // 0-414
279 EnumPair enum168 = values.get(168);
280 assertEquals("America/Danmarkshavn", enum168.getName());
281 assertEquals(168, (int) enum168.getValue());
282 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
284 EnumPair enum374 = values.get(374);
285 assertEquals("America/Indiana/Winamac", enum374.getName());
286 assertEquals(374, (int) enum374.getValue());
287 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
291 public void testObjectId128() {
292 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
293 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
294 ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(typedefs, "object-identifier-128");
296 List<PatternConstraint> patterns = testedType.getPatternConstraints();
297 assertEquals(1, patterns.size());
298 PatternConstraint pattern = patterns.get(0);
299 assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
301 QName testedTypeQName = testedType.getQName();
302 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
303 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
304 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
306 ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
307 patterns = testedTypeBase.getPatternConstraints();
308 assertEquals(1, patterns.size());
310 pattern = patterns.get(0);
311 assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
312 pattern.getRegularExpression());
314 QName testedTypeBaseQName = testedTypeBase.getQName();
315 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
316 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
317 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
321 public void testIdentityref() {
322 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
323 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
324 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
325 IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
326 QName identity = baseType.getIdentity().getQName();
327 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
328 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
329 assertEquals("service-type", identity.getLocalName());
331 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName("type");
336 public void testUnionWithExt() throws Exception {
337 File extdef = new File(getClass().getResource("/types/union-with-ext/extdef.yang").toURI());
338 File unionbug = new File(getClass().getResource("/types/union-with-ext/unionbug.yang").toURI());
339 File inet = new File(getClass().getResource("/ietf/ietf-inet-types@2010-09-24.yang").toURI());
340 YangContextParser parser = new YangParserImpl();
341 parser.parseFiles(Arrays.asList(extdef, unionbug, inet));
345 public void testUnionWithBits() throws Exception {
346 File unionbits = new File(getClass().getResource("/types/union-with-bits/union-bits-model.yang").toURI());
347 YangContextParser parser = new YangParserImpl();
348 parser.parseFiles(Collections.singletonList(unionbits));
351 @Test(expected = YangParseException.class)
352 public void testUnionInList() throws Exception {
353 File unioninlist = new File(getClass().getResource("/types/union-in-list/unioninlisttest.yang").toURI());
354 YangContextParser parser = new YangParserImpl();
355 parser.parseFiles(Collections.singletonList(unioninlist));