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.ArrayList;
16 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.Status;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
28 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
29 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
30 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
31 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
33 import org.opendaylight.yangtools.yang.model.util.BitsType;
34 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
35 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
36 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
37 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.model.util.UnionType;
40 public class TypesResolutionTest {
41 private Set<Module> testedModules;
44 public void init() throws FileNotFoundException {
45 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
46 File dependenciesDir = new File(getClass().getResource("/ietf").getPath());
47 YangModelParser parser = new YangParserImpl();
48 testedModules = parser.parseYangModels(yangFile, dependenciesDir);
49 assertEquals(4, testedModules.size());
53 public void testIPVersion() {
54 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
55 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
56 assertEquals(14, typedefs.size());
58 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
59 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
60 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
62 EnumerationType enumType = (EnumerationType) type.getBaseType();
63 List<EnumPair> values = enumType.getValues();
64 assertEquals(3, values.size());
66 EnumPair value0 = values.get(0);
67 assertEquals("unknown", value0.getName());
68 assertEquals(0, (int) value0.getValue());
69 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
71 EnumPair value1 = values.get(1);
72 assertEquals("ipv4", value1.getName());
73 assertEquals(1, (int) value1.getValue());
74 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
76 EnumPair value2 = values.get(2);
77 assertEquals("ipv6", value2.getName());
78 assertEquals(2, (int) value2.getValue());
79 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
83 public void testEnumeration() {
84 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
85 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
87 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
88 EnumerationType enumType = (EnumerationType) type.getBaseType();
89 List<EnumPair> values = enumType.getValues();
90 assertEquals(4, values.size());
92 EnumPair value0 = values.get(0);
93 assertEquals("unknown", value0.getName());
94 assertEquals(0, (int) value0.getValue());
95 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
97 EnumPair value1 = values.get(1);
98 assertEquals("ipv4", value1.getName());
99 assertEquals(19, (int) value1.getValue());
100 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
102 EnumPair value2 = values.get(2);
103 assertEquals("ipv6", value2.getName());
104 assertEquals(7, (int) value2.getValue());
105 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
107 EnumPair value3 = values.get(3);
108 assertEquals("default", value3.getName());
109 assertEquals(20, (int) value3.getValue());
110 assertEquals("default ip", value3.getDescription());
114 public void testIpAddress() {
115 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
116 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
117 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
118 UnionType baseType = (UnionType) type.getBaseType();
119 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
121 ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
122 assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
123 String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
124 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?";
125 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
127 ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
128 assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
129 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
130 expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
131 + "((([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}"
132 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?";
133 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
135 expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
137 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
141 public void testDomainName() {
142 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
143 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
144 ExtendedType type = (ExtendedType) TestUtils.findTypedef(typedefs, "domain-name");
145 assertTrue(type.getBaseType() instanceof StringTypeDefinition);
146 List<PatternConstraint> patterns = type.getPatternConstraints();
147 assertEquals(1, patterns.size());
148 String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
149 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.";
150 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
152 List<LengthConstraint> lengths = type.getLengthConstraints();
153 assertEquals(1, lengths.size());
154 LengthConstraint length = type.getLengthConstraints().get(0);
155 assertEquals(1L, length.getMin());
156 assertEquals(253L, length.getMax());
160 public void testInstanceIdentifier1() {
161 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
162 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("inst-id-leaf1");
163 InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
164 assertFalse(leafType.requireInstance());
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 IdentitySchemaNode testedIdentity = null;
180 for (IdentitySchemaNode id : identities) {
181 if (id.getQName().getLocalName().equals("crypto-alg")) {
183 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
184 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
185 assertNull(baseIdentity.getBaseIdentity());
188 assertNotNull(testedIdentity);
192 public void testBitsType1() {
193 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
194 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName("mybits");
195 BitsType leafType = (BitsType) leaf.getType();
196 List<Bit> bits = leafType.getBits();
197 assertEquals(3, bits.size());
199 Bit bit1 = bits.get(0);
200 assertEquals("disable-nagle", bit1.getName());
201 assertEquals(0L, (long) bit1.getPosition());
203 Bit bit2 = bits.get(1);
204 assertEquals("auto-sense-speed", bit2.getName());
205 assertEquals(1L, (long) bit2.getPosition());
207 Bit bit3 = bits.get(2);
208 assertEquals("10-Mb-only", bit3.getName());
209 assertEquals(2L, (long) bit3.getPosition());
213 public void testBitsType2() {
214 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
215 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
216 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
218 BitsType bitsType = (BitsType) testedType.getBaseType();
219 List<Bit> bits = bitsType.getBits();
220 assertEquals(5, bits.size());
222 Bit bit0 = bits.get(0);
223 assertEquals(0L, (long) bit0.getPosition());
225 Bit bit1 = bits.get(1);
226 assertEquals(500L, (long) bit1.getPosition());
228 Bit bit2 = bits.get(2);
229 assertEquals(501L, (long) bit2.getPosition());
231 Bit bit3 = bits.get(3);
232 assertEquals(365L, (long) bit3.getPosition());
234 Bit bit4 = bits.get(4);
235 assertEquals(502L, (long) bit4.getPosition());
239 public void testIanaTimezones() {
240 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
241 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
242 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
244 String expectedDesc = "A timezone location as defined by the IANA timezone";
245 assertTrue(testedType.getDescription().contains(expectedDesc));
246 assertNull(testedType.getReference());
247 assertEquals(Status.CURRENT, testedType.getStatus());
249 QName testedTypeQName = testedType.getQName();
250 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
251 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
252 assertEquals("ianatz", testedTypeQName.getPrefix());
253 assertEquals("iana-timezone", testedTypeQName.getLocalName());
255 EnumerationType enumType = (EnumerationType) testedType.getBaseType();
256 List<EnumPair> values = enumType.getValues();
257 assertEquals(415, values.size()); // 0-414
259 EnumPair enum168 = values.get(168);
260 assertEquals("America/Danmarkshavn", enum168.getName());
261 assertEquals(168, (int) enum168.getValue());
262 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
264 EnumPair enum374 = values.get(374);
265 assertEquals("America/Indiana/Winamac", enum374.getName());
266 assertEquals(374, (int) enum374.getValue());
267 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
271 public void testObjectId128() {
272 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
273 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
274 ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(typedefs, "object-identifier-128");
276 List<PatternConstraint> patterns = testedType.getPatternConstraints();
277 assertEquals(1, patterns.size());
278 PatternConstraint pattern = patterns.get(0);
279 assertEquals("\\d*(\\.\\d*){1,127}", pattern.getRegularExpression());
281 QName testedTypeQName = testedType.getQName();
282 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
283 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
284 assertEquals("yang", testedTypeQName.getPrefix());
285 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
287 ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
288 patterns = testedTypeBase.getPatternConstraints();
289 assertEquals(1, patterns.size());
291 pattern = patterns.get(0);
292 assertEquals("(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
293 pattern.getRegularExpression());
295 QName testedTypeBaseQName = testedTypeBase.getQName();
296 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
297 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
298 assertEquals("yang", testedTypeBaseQName.getPrefix());
299 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
303 public void testIdentityref() {
304 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
305 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
306 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
307 IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
308 QName identity = baseType.getIdentity();
309 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
310 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
311 assertEquals("iit", identity.getPrefix());
312 assertEquals("service-type", identity.getLocalName());
314 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName("type");