2 * Copyright (c) 2016 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.stmt;
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;
15 import static org.junit.Assert.fail;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
19 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.Status;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
33 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
36 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
37 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
41 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
42 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
43 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
44 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
47 public class TypesResolutionTest {
48 private Set<Module> testedModules;
51 public void init() throws Exception {
52 final StatementStreamSource yangFile = sourceForResource("/types/custom-types-test@2012-4-4.yang");
53 final StatementStreamSource yangFileDependency1 = sourceForResource("/ietf/iana-timezones@2012-07-09.yang");
54 final StatementStreamSource yangFileDependency2 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
55 final StatementStreamSource yangFileDependency3 = sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang");
57 testedModules = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2,
58 yangFileDependency3).getModules();
59 assertEquals(4, testedModules.size());
63 public void testIPVersion() {
64 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
65 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
66 assertEquals(14, typedefs.size());
68 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
69 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
70 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
72 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
73 List<EnumPair> values = enumType.getValues();
74 assertEquals(3, values.size());
76 EnumPair value0 = values.get(0);
77 assertEquals("unknown", value0.getName());
78 assertEquals(0, value0.getValue());
79 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
81 EnumPair value1 = values.get(1);
82 assertEquals("ipv4", value1.getName());
83 assertEquals(1, value1.getValue());
84 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
86 EnumPair value2 = values.get(2);
87 assertEquals("ipv6", value2.getName());
88 assertEquals(2, value2.getValue());
89 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
93 public void testEnumeration() {
94 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
95 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
97 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
98 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
99 List<EnumPair> values = enumType.getValues();
100 assertEquals(4, values.size());
102 EnumPair value0 = values.get(0);
103 assertEquals("unknown", value0.getName());
104 assertEquals(0, value0.getValue());
105 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
107 EnumPair value1 = values.get(1);
108 assertEquals("ipv4", value1.getName());
109 assertEquals(19, value1.getValue());
110 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
112 EnumPair value2 = values.get(2);
113 assertEquals("ipv6", value2.getName());
114 assertEquals(7, value2.getValue());
115 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
117 EnumPair value3 = values.get(3);
118 assertEquals("default", value3.getName());
119 assertEquals(20, value3.getValue());
120 assertEquals("default ip", value3.getDescription());
124 public void testIpAddress() {
125 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
126 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
127 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
128 UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
129 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
131 StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
132 assertNotNull(ipv4.getBaseType());
133 String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
134 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
135 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
137 StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
138 assertNotNull(ipv6.getBaseType());
139 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
140 expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
141 + "((([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}"
142 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
143 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
145 expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
147 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
151 public void testDomainName() {
152 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
153 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
154 StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
155 assertNotNull(type.getBaseType());
156 List<PatternConstraint> patterns = type.getPatternConstraints();
157 assertEquals(1, patterns.size());
158 String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
159 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
160 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
162 List<LengthConstraint> lengths = type.getLengthConstraints();
163 assertEquals(1, lengths.size());
164 LengthConstraint length = type.getLengthConstraints().get(0);
165 assertEquals(Integer.valueOf(1), length.getMin());
166 assertEquals(Integer.valueOf(253), length.getMax());
170 public void testInstanceIdentifier1() {
171 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
172 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
173 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
174 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
175 assertFalse(leafType.requireInstance());
176 assertEquals(1, leaf.getUnknownSchemaNodes().size());
180 public void testInstanceIdentifier2() {
181 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
182 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
183 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
184 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
185 assertFalse(leafType.requireInstance());
189 public void testIdentity() {
190 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
191 Set<IdentitySchemaNode> identities = tested.getIdentities();
192 assertEquals(5, identities.size());
193 IdentitySchemaNode cryptoAlg = null;
194 IdentitySchemaNode cryptoBase = null;
195 IdentitySchemaNode cryptoId = null;
196 for (IdentitySchemaNode id : identities) {
197 if (id.getQName().getLocalName().equals("crypto-alg")) {
199 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
201 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
205 assertNotNull(cryptoAlg);
206 IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
207 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
208 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
209 assertNull(baseIdentity.getBaseIdentity());
211 assertNotNull(cryptoBase);
212 assertNull(cryptoBase.getBaseIdentity());
213 assertEquals(3, cryptoBase.getDerivedIdentities().size());
215 assertNotNull(cryptoId);
216 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
220 public void testBitsType1() {
221 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
222 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
223 QName.create(tested.getQNameModule(), "mybits"));
224 BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
225 List<Bit> bits = leafType.getBits();
226 assertEquals(3, bits.size());
228 Bit bit1 = bits.get(0);
229 assertEquals("disable-nagle", bit1.getName());
230 assertEquals(0L, bit1.getPosition());
232 Bit bit2 = bits.get(1);
233 assertEquals("auto-sense-speed", bit2.getName());
234 assertEquals(1L, bit2.getPosition());
236 Bit bit3 = bits.get(2);
237 assertEquals("10-Mb-only", bit3.getName());
238 assertEquals(2L, bit3.getPosition());
242 public void testBitsType2() {
243 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
244 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
245 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
247 BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
248 List<Bit> bits = bitsType.getBits();
249 assertEquals(5, bits.size());
251 Bit bit0 = bits.get(0);
252 assertEquals("create", bit0.getName());
253 assertEquals(0L, bit0.getPosition());
255 Bit bit1 = bits.get(1);
256 assertEquals("delete", bit1.getName());
257 assertEquals(365L, bit1.getPosition());
259 Bit bit2 = bits.get(2);
260 assertEquals("read", bit2.getName());
261 assertEquals(500L, bit2.getPosition());
263 Bit bit3 = bits.get(3);
264 assertEquals("update", bit3.getName());
265 assertEquals(501L, bit3.getPosition());
267 Bit bit4 = bits.get(4);
268 assertEquals("exec", bit4.getName());
269 assertEquals(502L, bit4.getPosition());
273 public void testIanaTimezones() {
274 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
275 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
276 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
278 String expectedDesc = "A timezone location as defined by the IANA timezone";
279 assertTrue(testedType.getDescription().contains(expectedDesc));
280 assertNull(testedType.getReference());
281 assertEquals(Status.CURRENT, testedType.getStatus());
283 QName testedTypeQName = testedType.getQName();
284 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
285 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
286 assertEquals("iana-timezone", testedTypeQName.getLocalName());
288 EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
289 List<EnumPair> values = enumType.getValues();
290 assertEquals(415, values.size()); // 0-414
292 EnumPair enum168 = values.get(168);
293 assertEquals("America/Danmarkshavn", enum168.getName());
294 assertEquals(168, enum168.getValue());
295 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
297 EnumPair enum374 = values.get(374);
298 assertEquals("America/Indiana/Winamac", enum374.getName());
299 assertEquals(374, enum374.getValue());
300 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
304 public void testObjectId128() {
305 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
306 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
307 StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
308 "object-identifier-128");
310 List<PatternConstraint> patterns = testedType.getPatternConstraints();
311 assertEquals(1, patterns.size());
312 PatternConstraint pattern = patterns.get(0);
313 assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
315 QName testedTypeQName = testedType.getQName();
316 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
317 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
318 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
320 StringTypeDefinition testedTypeBase = testedType.getBaseType();
321 patterns = testedTypeBase.getPatternConstraints();
322 assertEquals(1, patterns.size());
324 pattern = patterns.get(0);
325 assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
326 pattern.getRegularExpression());
328 QName testedTypeBaseQName = testedTypeBase.getQName();
329 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
330 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
331 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
335 public void testIdentityref() {
336 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
337 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
338 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
339 IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
340 QName identity = baseType.getIdentity().getQName();
341 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
342 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
343 assertEquals("service-type", identity.getLocalName());
345 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
350 public void testUnionWithExt() throws ReactorException {
352 final StatementStreamSource yangFile1 = sourceForResource("/types/union-with-ext/extdef.yang");
353 final StatementStreamSource yangFile2 = sourceForResource("/types/union-with-ext/unionbug.yang");
354 final StatementStreamSource yangFile3 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
356 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
357 reactor.addSources(yangFile1, yangFile2, yangFile3);
359 final EffectiveSchemaContext result = reactor.buildEffective();
360 assertNotNull(result);
364 public void testUnionWithBits() throws ReactorException {
366 final StatementStreamSource yangFile = sourceForResource("/types/union-with-bits/union-bits-model.yang");
368 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
369 reactor.addSources(yangFile);
371 final EffectiveSchemaContext result = reactor.buildEffective();
372 assertNotNull(result);
376 public void testUnionInList() {
377 final StatementStreamSource yangFile = sourceForResource("/types/union-in-list/unioninlisttest.yang");
379 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
380 reactor.addSources(yangFile);
383 final EffectiveSchemaContext result = reactor.buildEffective();
384 fail("effective build should fail due to union in list; this is not allowed");
385 } catch (Exception e) {
386 assertEquals(SomeModifiersUnresolvedException.class, e.getClass());
387 assertTrue(e.getCause() instanceof SourceException);
388 assertTrue(e.getCause().getMessage().startsWith("union is not a YANG statement or use of extension"));