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.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.Status;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
32 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
33 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
34 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
36 import org.opendaylight.yangtools.yang.model.util.BitsType;
37 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
38 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
39 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
40 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
41 import org.opendaylight.yangtools.yang.model.util.UnionType;
42 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
44 public class TypesResolutionTest {
45 private Set<Module> testedModules;
48 public void init() throws Exception {
49 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
50 File dependenciesDir = new File(getClass().getResource("/ietf").toURI());
51 YangContextParser parser = new YangParserImpl();
52 testedModules = parser.parseFile(yangFile, dependenciesDir).getModules();
53 assertEquals(4, testedModules.size());
57 public void testIPVersion() {
58 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
59 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
60 assertEquals(14, typedefs.size());
62 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
63 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
64 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
66 EnumerationType enumType = (EnumerationType) type.getBaseType();
67 List<EnumPair> values = enumType.getValues();
68 assertEquals(3, values.size());
70 EnumPair value0 = values.get(0);
71 assertEquals("unknown", value0.getName());
72 assertEquals(0, (int) value0.getValue());
73 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
75 EnumPair value1 = values.get(1);
76 assertEquals("ipv4", value1.getName());
77 assertEquals(1, (int) value1.getValue());
78 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
80 EnumPair value2 = values.get(2);
81 assertEquals("ipv6", value2.getName());
82 assertEquals(2, (int) value2.getValue());
83 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
87 public void testEnumeration() {
88 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
89 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
91 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
92 EnumerationType enumType = (EnumerationType) type.getBaseType();
93 List<EnumPair> values = enumType.getValues();
94 assertEquals(4, values.size());
96 EnumPair value0 = values.get(0);
97 assertEquals("unknown", value0.getName());
98 assertEquals(0, (int) value0.getValue());
99 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
101 EnumPair value1 = values.get(1);
102 assertEquals("ipv4", value1.getName());
103 assertEquals(19, (int) value1.getValue());
104 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
106 EnumPair value2 = values.get(2);
107 assertEquals("ipv6", value2.getName());
108 assertEquals(7, (int) value2.getValue());
109 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
111 EnumPair value3 = values.get(3);
112 assertEquals("default", value3.getName());
113 assertEquals(20, (int) value3.getValue());
114 assertEquals("default ip", value3.getDescription());
118 public void testIpAddress() {
119 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
120 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
121 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
122 UnionType baseType = (UnionType) type.getBaseType();
123 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
125 ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
126 assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
127 String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
128 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
129 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
131 ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
132 assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
133 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
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}))|" + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
136 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
137 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
139 expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
141 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
145 public void testDomainName() {
146 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
147 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
148 ExtendedType type = (ExtendedType) TestUtils.findTypedef(typedefs, "domain-name");
149 assertTrue(type.getBaseType() instanceof StringTypeDefinition);
150 List<PatternConstraint> patterns = type.getPatternConstraints();
151 assertEquals(1, patterns.size());
152 String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
153 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
154 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
156 List<LengthConstraint> lengths = type.getLengthConstraints();
157 assertEquals(1, lengths.size());
158 LengthConstraint length = type.getLengthConstraints().get(0);
159 assertEquals(BigInteger.ONE, length.getMin());
160 assertEquals(BigInteger.valueOf(253), length.getMax());
164 public void testInstanceIdentifier1() {
165 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
166 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf1");
167 InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
168 assertFalse(leafType.requireInstance());
169 assertEquals(1, leaf.getUnknownSchemaNodes().size());
173 public void testInstanceIdentifier2() {
174 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
175 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf2");
176 InstanceIdentifierType leafType = (InstanceIdentifierType) leaf.getType();
177 assertTrue(leafType.requireInstance());
181 public void testIdentity() {
182 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
183 Set<IdentitySchemaNode> identities = tested.getIdentities();
184 assertEquals(5, identities.size());
185 IdentitySchemaNode cryptoAlg = null;
186 IdentitySchemaNode cryptoBase = null;
187 IdentitySchemaNode cryptoId = null;
188 for (IdentitySchemaNode id : identities) {
189 if (id.getQName().getLocalName().equals("crypto-alg")) {
191 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
193 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
197 assertNotNull(cryptoAlg);
198 IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
199 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
200 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
201 assertNull(baseIdentity.getBaseIdentity());
203 assertNotNull(cryptoBase);
204 assertNull(cryptoBase.getBaseIdentity());
205 assertEquals(3, cryptoBase.getDerivedIdentities().size());
207 assertNotNull(cryptoId);
208 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
212 public void testBitsType1() {
213 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
214 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("mybits");
215 BitsType leafType = (BitsType) leaf.getType();
216 List<Bit> bits = leafType.getBits();
217 assertEquals(3, bits.size());
219 Bit bit1 = bits.get(0);
220 assertEquals("disable-nagle", bit1.getName());
221 assertEquals(0L, (long) bit1.getPosition());
223 Bit bit2 = bits.get(1);
224 assertEquals("auto-sense-speed", bit2.getName());
225 assertEquals(1L, (long) bit2.getPosition());
227 Bit bit3 = bits.get(2);
228 assertEquals("10-Mb-only", bit3.getName());
229 assertEquals(2L, (long) bit3.getPosition());
233 public void testBitsType2() {
234 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
235 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
236 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "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, "iana-timezone");
264 String expectedDesc = "A timezone location as defined by the IANA timezone";
265 assertTrue(testedType.getDescription().contains(expectedDesc));
266 assertTrue(testedType.getReference().isEmpty());
267 assertEquals(Status.CURRENT, testedType.getStatus());
269 QName testedTypeQName = testedType.getQName();
270 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
271 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
272 assertEquals("iana-timezone", testedTypeQName.getLocalName());
274 EnumerationType enumType = (EnumerationType) testedType.getBaseType();
275 List<EnumPair> values = enumType.getValues();
276 assertEquals(415, values.size()); // 0-414
278 EnumPair enum168 = values.get(168);
279 assertEquals("America/Danmarkshavn", enum168.getName());
280 assertEquals(168, (int) enum168.getValue());
281 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
283 EnumPair enum374 = values.get(374);
284 assertEquals("America/Indiana/Winamac", enum374.getName());
285 assertEquals(374, (int) enum374.getValue());
286 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
290 public void testObjectId128() {
291 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
292 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
293 ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(typedefs, "object-identifier-128");
295 List<PatternConstraint> patterns = testedType.getPatternConstraints();
296 assertEquals(1, patterns.size());
297 PatternConstraint pattern = patterns.get(0);
298 assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
300 QName testedTypeQName = testedType.getQName();
301 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
302 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
303 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
305 ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
306 patterns = testedTypeBase.getPatternConstraints();
307 assertEquals(1, patterns.size());
309 pattern = patterns.get(0);
310 assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
311 pattern.getRegularExpression());
313 QName testedTypeBaseQName = testedTypeBase.getQName();
314 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
315 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
316 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
320 public void testIdentityref() {
321 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
322 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
323 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
324 IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
325 QName identity = baseType.getIdentity().getQName();
326 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
327 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
328 assertEquals("service-type", identity.getLocalName());
330 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName("type");
335 public void testUnionWithExt() throws Exception {
336 File extdef = new File(getClass().getResource("/types/union-with-ext/extdef.yang").toURI());
337 File unionbug = new File(getClass().getResource("/types/union-with-ext/unionbug.yang").toURI());
338 File inet = new File(getClass().getResource("/ietf/ietf-inet-types@2010-09-24.yang").toURI());
339 YangContextParser parser = new YangParserImpl();
340 parser.parseFiles(Arrays.asList(extdef, unionbug, inet));
344 public void testUnionWithBits() throws Exception {
345 File unionbits = new File(getClass().getResource("/types/union-with-bits/union-bits-model.yang").toURI());
346 YangContextParser parser = new YangParserImpl();
347 parser.parseFiles(Arrays.asList(unionbits));
350 @Test(expected = YangParseException.class)
351 public void testUnionInList() throws Exception {
352 File unioninlist = new File(getClass().getResource("/types/union-in-list/unioninlisttest.yang").toURI());
353 YangContextParser parser = new YangParserImpl();
354 parser.parseFiles(Arrays.asList(unioninlist));