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;
18 import com.google.common.collect.Iterables;
19 import com.google.common.collect.Range;
21 import java.util.List;
22 import java.util.Optional;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.Status;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
36 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
38 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
41 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
42 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
46 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
47 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
48 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
51 public class TypesResolutionTest {
52 private SchemaContext context;
55 public void init() throws Exception {
56 final StatementStreamSource yangFile = sourceForResource("/types/custom-types-test@2012-04-04.yang");
57 final StatementStreamSource yangFileDependency1 = sourceForResource("/ietf/iana-timezones@2012-07-09.yang");
58 final StatementStreamSource yangFileDependency2 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
59 final StatementStreamSource yangFileDependency3 = sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang");
60 context = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2, yangFileDependency3);
61 assertEquals(4, context.getModules().size());
65 public void testIPVersion() {
66 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
67 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
68 assertEquals(14, typedefs.size());
70 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
71 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
72 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
74 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
75 List<EnumPair> values = enumType.getValues();
76 assertEquals(3, values.size());
78 EnumPair value0 = values.get(0);
79 assertEquals("unknown", value0.getName());
80 assertEquals(0, value0.getValue());
81 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
83 EnumPair value1 = values.get(1);
84 assertEquals("ipv4", value1.getName());
85 assertEquals(1, value1.getValue());
86 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
88 EnumPair value2 = values.get(2);
89 assertEquals("ipv6", value2.getName());
90 assertEquals(2, value2.getValue());
91 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
95 public void testEnumeration() {
96 Module tested = TestUtils.findModule(context, "custom-types-test").get();
97 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
99 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
100 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
101 List<EnumPair> values = enumType.getValues();
102 assertEquals(4, values.size());
104 EnumPair value0 = values.get(0);
105 assertEquals("unknown", value0.getName());
106 assertEquals(0, value0.getValue());
107 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
109 EnumPair value1 = values.get(1);
110 assertEquals("ipv4", value1.getName());
111 assertEquals(19, value1.getValue());
112 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
114 EnumPair value2 = values.get(2);
115 assertEquals("ipv6", value2.getName());
116 assertEquals(7, value2.getValue());
117 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
119 EnumPair value3 = values.get(3);
120 assertEquals("default", value3.getName());
121 assertEquals(20, value3.getValue());
122 assertEquals("default ip", value3.getDescription());
126 public void testIpAddress() {
127 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
128 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
129 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
130 UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
131 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
133 StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
134 assertNotNull(ipv4.getBaseType());
135 String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
136 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
137 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
139 StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
140 assertNotNull(ipv6.getBaseType());
141 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
142 expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
143 + "((([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}"
144 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
145 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
147 expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
149 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
153 public void testDomainName() {
154 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
155 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
156 StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
157 assertNotNull(type.getBaseType());
158 List<PatternConstraint> patterns = type.getPatternConstraints();
159 assertEquals(1, patterns.size());
160 String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
161 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
162 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
164 LengthConstraint lengths = type.getLengthConstraint().get();
165 assertEquals(1, lengths.getAllowedRanges().asRanges().size());
166 Range<Integer> length = lengths.getAllowedRanges().span();
167 assertEquals(Integer.valueOf(1), length.lowerEndpoint());
168 assertEquals(Integer.valueOf(253), length.upperEndpoint());
172 public void testInstanceIdentifier1() {
173 Module tested = TestUtils.findModule(context, "custom-types-test").get();
174 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
175 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
176 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
177 assertFalse(leafType.requireInstance());
178 assertEquals(1, leaf.getUnknownSchemaNodes().size());
182 public void testInstanceIdentifier2() {
183 Module tested = TestUtils.findModule(context, "custom-types-test").get();
184 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
185 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
186 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
187 assertFalse(leafType.requireInstance());
191 public void testIdentity() {
192 Module tested = TestUtils.findModule(context, "custom-types-test").get();
193 Set<IdentitySchemaNode> identities = tested.getIdentities();
194 assertEquals(5, identities.size());
195 IdentitySchemaNode cryptoAlg = null;
196 IdentitySchemaNode cryptoBase = null;
197 IdentitySchemaNode cryptoId = null;
198 for (IdentitySchemaNode id : identities) {
199 if (id.getQName().getLocalName().equals("crypto-alg")) {
201 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
203 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
207 assertNotNull(cryptoAlg);
208 IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(cryptoAlg.getBaseIdentities());
209 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
210 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
211 assertTrue(baseIdentity.getBaseIdentities().isEmpty());
213 assertNotNull(cryptoBase);
214 assertTrue(cryptoBase.getBaseIdentities().isEmpty());
215 assertEquals(3, cryptoBase.getDerivedIdentities().size());
217 assertNotNull(cryptoId);
218 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
222 public void testBitsType1() {
223 Module tested = TestUtils.findModule(context, "custom-types-test").get();
224 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
225 QName.create(tested.getQNameModule(), "mybits"));
226 BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
227 List<Bit> bits = leafType.getBits();
228 assertEquals(3, bits.size());
230 Bit bit1 = bits.get(0);
231 assertEquals("disable-nagle", bit1.getName());
232 assertEquals(0L, bit1.getPosition());
234 Bit bit2 = bits.get(1);
235 assertEquals("auto-sense-speed", bit2.getName());
236 assertEquals(1L, bit2.getPosition());
238 Bit bit3 = bits.get(2);
239 assertEquals("10-Mb-only", bit3.getName());
240 assertEquals(2L, bit3.getPosition());
244 public void testBitsType2() {
245 Module tested = TestUtils.findModule(context, "custom-types-test").get();
246 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
247 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
249 BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
250 List<Bit> bits = bitsType.getBits();
251 assertEquals(5, bits.size());
253 Bit bit0 = bits.get(0);
254 assertEquals("create", bit0.getName());
255 assertEquals(0L, bit0.getPosition());
257 Bit bit1 = bits.get(1);
258 assertEquals("delete", bit1.getName());
259 assertEquals(365L, bit1.getPosition());
261 Bit bit2 = bits.get(2);
262 assertEquals("read", bit2.getName());
263 assertEquals(500L, bit2.getPosition());
265 Bit bit3 = bits.get(3);
266 assertEquals("update", bit3.getName());
267 assertEquals(501L, bit3.getPosition());
269 Bit bit4 = bits.get(4);
270 assertEquals("exec", bit4.getName());
271 assertEquals(502L, bit4.getPosition());
275 public void testIanaTimezones() {
276 Module tested = TestUtils.findModule(context, "iana-timezones").get();
277 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
278 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
280 String expectedDesc = "A timezone location as defined by the IANA timezone";
281 assertTrue(testedType.getDescription().contains(expectedDesc));
282 assertNull(testedType.getReference());
283 assertEquals(Status.CURRENT, testedType.getStatus());
285 QName testedTypeQName = testedType.getQName();
286 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
287 assertEquals(Optional.of(Revision.valueOf("2012-07-09")), testedTypeQName.getRevision());
288 assertEquals("iana-timezone", testedTypeQName.getLocalName());
290 EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
291 List<EnumPair> values = enumType.getValues();
292 assertEquals(415, values.size()); // 0-414
294 EnumPair enum168 = values.get(168);
295 assertEquals("America/Danmarkshavn", enum168.getName());
296 assertEquals(168, enum168.getValue());
297 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
299 EnumPair enum374 = values.get(374);
300 assertEquals("America/Indiana/Winamac", enum374.getName());
301 assertEquals(374, enum374.getValue());
302 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
306 public void testObjectId128() {
307 Module tested = TestUtils.findModule(context, "ietf-yang-types").get();
308 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
309 StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
310 "object-identifier-128");
312 List<PatternConstraint> patterns = testedType.getPatternConstraints();
313 assertEquals(1, patterns.size());
314 PatternConstraint pattern = patterns.get(0);
315 assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
317 QName testedTypeQName = testedType.getQName();
318 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
319 assertEquals(Optional.of(Revision.valueOf("2010-09-24")), testedTypeQName.getRevision());
320 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
322 StringTypeDefinition testedTypeBase = testedType.getBaseType();
323 patterns = testedTypeBase.getPatternConstraints();
324 assertEquals(1, patterns.size());
326 pattern = patterns.get(0);
327 assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
328 pattern.getRegularExpression());
330 QName testedTypeBaseQName = testedTypeBase.getQName();
331 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
332 assertEquals(Optional.of(Revision.valueOf("2010-09-24")), testedTypeBaseQName.getRevision());
333 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
337 public void testIdentityref() {
338 Module tested = TestUtils.findModule(context, "custom-types-test").get();
339 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
340 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
341 IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
342 QName identity = baseType.getIdentity().getQName();
343 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
344 assertEquals(Optional.of(Revision.valueOf("2012-04-16")), identity.getRevision());
345 assertEquals("service-type", identity.getLocalName());
347 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
352 public void testUnionWithExt() throws ReactorException {
354 final StatementStreamSource yangFile1 = sourceForResource("/types/union-with-ext/extdef.yang");
355 final StatementStreamSource yangFile2 = sourceForResource("/types/union-with-ext/unionbug.yang");
356 final StatementStreamSource yangFile3 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
358 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
359 reactor.addSources(yangFile1, yangFile2, yangFile3);
361 final SchemaContext result = reactor.buildEffective();
362 assertNotNull(result);
366 public void testUnionWithBits() throws ReactorException {
368 final StatementStreamSource yangFile = sourceForResource("/types/union-with-bits/union-bits-model.yang");
370 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
371 reactor.addSources(yangFile);
373 final SchemaContext result = reactor.buildEffective();
374 assertNotNull(result);
378 public void testUnionInList() {
379 final StatementStreamSource yangFile = sourceForResource("/types/union-in-list/unioninlisttest.yang");
381 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
382 reactor.addSources(yangFile);
385 final SchemaContext result = reactor.buildEffective();
386 fail("effective build should fail due to union in list; this is not allowed");
387 } catch (Exception e) {
388 assertEquals(SomeModifiersUnresolvedException.class, e.getClass());
389 assertTrue(e.getCause() instanceof SourceException);
390 assertTrue(e.getCause().getMessage().startsWith("union is not a YANG statement or use of extension"));