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.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17 import com.google.common.collect.Iterables;
18 import com.google.common.collect.Range;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.List;
23 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.common.Uint32;
29 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.Status;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
37 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
39 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
42 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
43 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
45 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
48 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
49 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
50 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
52 public class TypesResolutionTest {
53 private SchemaContext context;
56 public void init() throws Exception {
57 final StatementStreamSource yangFile = sourceForResource("/types/custom-types-test@2012-04-04.yang");
58 final StatementStreamSource yangFileDependency1 = sourceForResource("/ietf/iana-timezones@2012-07-09.yang");
59 final StatementStreamSource yangFileDependency2 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
60 final StatementStreamSource yangFileDependency3 = sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang");
61 context = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2, yangFileDependency3);
62 assertEquals(4, context.getModules().size());
66 public void testIPVersion() {
67 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
68 Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
69 assertEquals(14, typedefs.size());
71 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
72 assertTrue(type.getDescription().get().contains("This value represents the version of the IP protocol."));
73 assertTrue(type.getReference().get().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
75 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
76 List<EnumPair> values = enumType.getValues();
77 assertEquals(3, values.size());
79 EnumPair value0 = values.get(0);
80 assertEquals("unknown", value0.getName());
81 assertEquals(0, value0.getValue());
82 assertEquals(Optional.of("An unknown or unspecified version of the Internet protocol."),
83 value0.getDescription());
85 EnumPair value1 = values.get(1);
86 assertEquals("ipv4", value1.getName());
87 assertEquals(1, value1.getValue());
88 assertEquals(Optional.of("The IPv4 protocol as defined in RFC 791."), value1.getDescription());
90 EnumPair value2 = values.get(2);
91 assertEquals("ipv6", value2.getName());
92 assertEquals(2, value2.getValue());
93 assertEquals(Optional.of("The IPv6 protocol as defined in RFC 2460."), value2.getDescription());
97 public void testEnumeration() {
98 Module tested = TestUtils.findModule(context, "custom-types-test").get();
99 Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
101 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
102 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
103 List<EnumPair> values = enumType.getValues();
104 assertEquals(4, values.size());
106 EnumPair value0 = values.get(0);
107 assertEquals("unknown", value0.getName());
108 assertEquals(0, value0.getValue());
109 assertEquals(Optional.of("An unknown or unspecified version of the Internet protocol."),
110 value0.getDescription());
112 EnumPair value1 = values.get(1);
113 assertEquals("ipv4", value1.getName());
114 assertEquals(19, value1.getValue());
115 assertEquals(Optional.of("The IPv4 protocol as defined in RFC 791."), value1.getDescription());
117 EnumPair value2 = values.get(2);
118 assertEquals("ipv6", value2.getName());
119 assertEquals(7, value2.getValue());
120 assertEquals(Optional.of("The IPv6 protocol as defined in RFC 2460."), value2.getDescription());
122 EnumPair value3 = values.get(3);
123 assertEquals("default", value3.getName());
124 assertEquals(20, value3.getValue());
125 assertEquals(Optional.of("default ip"), value3.getDescription());
129 public void testIpAddress() {
130 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
131 Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
132 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
133 UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
134 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
136 StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
137 assertNotNull(ipv4.getBaseType());
138 String expectedPattern = "^(?:(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
139 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?)$";
140 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getJavaPatternString());
142 StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
143 assertNotNull(ipv6.getBaseType());
144 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
145 expectedPattern = "^(?:((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
146 + "((([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}"
147 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?)$";
148 assertEquals(expectedPattern, ipv6Patterns.get(0).getJavaPatternString());
150 expectedPattern = "^(?:(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
152 assertEquals(expectedPattern, ipv6Patterns.get(1).getJavaPatternString());
156 public void testDomainName() {
157 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
158 Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
159 StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
160 assertNotNull(type.getBaseType());
161 List<PatternConstraint> patterns = type.getPatternConstraints();
162 assertEquals(1, patterns.size());
163 String expectedPattern = "^(?:((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
164 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.)$";
165 assertEquals(expectedPattern, patterns.get(0).getJavaPatternString());
167 LengthConstraint lengths = type.getLengthConstraint().get();
168 assertEquals(1, lengths.getAllowedRanges().asRanges().size());
169 Range<Integer> length = lengths.getAllowedRanges().span();
170 assertEquals(Integer.valueOf(1), length.lowerEndpoint());
171 assertEquals(Integer.valueOf(253), length.upperEndpoint());
175 public void testInstanceIdentifier1() {
176 Module tested = TestUtils.findModule(context, "custom-types-test").get();
177 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
178 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
179 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
180 assertFalse(leafType.requireInstance());
181 assertEquals(1, leaf.getUnknownSchemaNodes().size());
185 public void testInstanceIdentifier2() {
186 Module tested = TestUtils.findModule(context, "custom-types-test").get();
187 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
188 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
189 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
190 assertFalse(leafType.requireInstance());
194 public void testIdentity() {
195 Module tested = TestUtils.findModule(context, "custom-types-test").get();
196 Collection<? extends IdentitySchemaNode> identities = tested.getIdentities();
197 assertEquals(5, identities.size());
198 IdentitySchemaNode cryptoAlg = null;
199 IdentitySchemaNode cryptoBase = null;
200 IdentitySchemaNode cryptoId = null;
201 for (IdentitySchemaNode id : identities) {
202 if (id.getQName().getLocalName().equals("crypto-alg")) {
204 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
206 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
210 assertNotNull(cryptoAlg);
211 IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(cryptoAlg.getBaseIdentities());
212 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
213 assertEquals(0, context.getDerivedIdentities(cryptoAlg).size());
214 assertEquals(0, baseIdentity.getBaseIdentities().size());
216 assertNotNull(cryptoBase);
217 assertTrue(cryptoBase.getBaseIdentities().isEmpty());
218 assertEquals(3, context.getDerivedIdentities(cryptoBase).size());
220 assertNotNull(cryptoId);
221 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
225 public void testBitsType1() {
226 Module tested = TestUtils.findModule(context, "custom-types-test").get();
227 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
228 QName.create(tested.getQNameModule(), "mybits"));
229 BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
230 Iterator<? extends Bit> bits = leafType.getBits().iterator();
232 Bit bit1 = bits.next();
233 assertEquals("disable-nagle", bit1.getName());
234 assertEquals(Uint32.ZERO, bit1.getPosition());
236 Bit bit2 = bits.next();
237 assertEquals("auto-sense-speed", bit2.getName());
238 assertEquals(Uint32.ONE, bit2.getPosition());
240 Bit bit3 = bits.next();
241 assertEquals("only-10-Mb", bit3.getName());
242 assertEquals(Uint32.TWO, bit3.getPosition());
244 assertFalse(bits.hasNext());
248 public void testBitsType2() {
249 Module tested = TestUtils.findModule(context, "custom-types-test").get();
250 Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
251 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
253 BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
254 Iterator<? extends Bit> bits = bitsType.getBits().iterator();
256 Bit bit0 = bits.next();
257 assertEquals("create", bit0.getName());
258 assertEquals(Uint32.ZERO, bit0.getPosition());
260 Bit bit1 = bits.next();
261 assertEquals("delete", bit1.getName());
262 assertEquals(Uint32.valueOf(365), bit1.getPosition());
264 Bit bit2 = bits.next();
265 assertEquals("read", bit2.getName());
266 assertEquals(Uint32.valueOf(500), bit2.getPosition());
268 Bit bit3 = bits.next();
269 assertEquals("update", bit3.getName());
270 assertEquals(Uint32.valueOf(501), bit3.getPosition());
272 Bit bit4 = bits.next();
273 assertEquals("exec", bit4.getName());
274 assertEquals(Uint32.valueOf(502), bit4.getPosition());
276 assertFalse(bits.hasNext());
280 public void testIanaTimezones() {
281 Module tested = TestUtils.findModule(context, "iana-timezones").get();
282 Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
283 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
285 String expectedDesc = "A timezone location as defined by the IANA timezone";
286 assertTrue(testedType.getDescription().get().contains(expectedDesc));
287 assertFalse(testedType.getReference().isPresent());
288 assertEquals(Status.CURRENT, testedType.getStatus());
290 QName testedTypeQName = testedType.getQName();
291 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
292 assertEquals(Revision.ofNullable("2012-07-09"), testedTypeQName.getRevision());
293 assertEquals("iana-timezone", testedTypeQName.getLocalName());
295 EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
296 List<EnumPair> values = enumType.getValues();
298 assertEquals(415, values.size());
300 EnumPair enum168 = values.get(168);
301 assertEquals("America/Danmarkshavn", enum168.getName());
302 assertEquals(168, enum168.getValue());
303 assertEquals(Optional.of("east coast, north of Scoresbysund"), enum168.getDescription());
305 EnumPair enum374 = values.get(374);
306 assertEquals("America/Indiana/Winamac", enum374.getName());
307 assertEquals(374, enum374.getValue());
308 assertEquals(Optional.of("Eastern Time - Indiana - Pulaski County"), enum374.getDescription());
312 public void testObjectId128() {
313 Module tested = TestUtils.findModule(context, "ietf-yang-types").get();
314 Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
315 StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
316 "object-identifier-128");
318 List<PatternConstraint> patterns = testedType.getPatternConstraints();
319 assertEquals(1, patterns.size());
320 PatternConstraint pattern = patterns.get(0);
321 assertEquals("^(?:\\d*(\\.\\d*){1,127})$", pattern.getJavaPatternString());
323 QName testedTypeQName = testedType.getQName();
324 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
325 assertEquals(Revision.ofNullable("2010-09-24"), testedTypeQName.getRevision());
326 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
328 StringTypeDefinition testedTypeBase = testedType.getBaseType();
329 patterns = testedTypeBase.getPatternConstraints();
330 assertEquals(1, patterns.size());
332 pattern = patterns.get(0);
333 assertEquals("^(?:(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*)$",
334 pattern.getJavaPatternString());
336 QName testedTypeBaseQName = testedTypeBase.getQName();
337 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
338 assertEquals(Revision.ofNullable("2010-09-24"), testedTypeBaseQName.getRevision());
339 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
343 public void testIdentityref() {
344 Module tested = TestUtils.findModule(context, "custom-types-test").get();
345 Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
346 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
347 IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
348 QName identity = baseType.getIdentities().iterator().next().getQName();
349 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
350 assertEquals(Revision.ofNullable("2012-04-16"), identity.getRevision());
351 assertEquals("service-type", identity.getLocalName());
353 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
358 public void testUnionWithExt() throws ReactorException {
359 final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
360 .addSource(sourceForResource("/types/union-with-ext/extdef.yang"))
361 .addSource(sourceForResource("/types/union-with-ext/unionbug.yang"))
362 .addSource(sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang"))
364 assertNotNull(result);
368 public void testUnionWithBits() throws ReactorException {
369 final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
370 .addSource(sourceForResource("/types/union-with-bits/union-bits-model.yang"))
372 assertNotNull(result);
376 public void testUnionInList() {
377 BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
378 .addSource(sourceForResource("/types/union-in-list/unioninlisttest.yang"));
381 final SchemaContext result = reactor.buildEffective();
382 fail("effective build should fail due to union in list; this is not allowed");
383 } catch (ReactorException e) {
384 assertEquals(SomeModifiersUnresolvedException.class, e.getClass());
385 assertTrue(e.getCause() instanceof SourceException);
386 assertTrue(e.getCause().getMessage().startsWith("union is not a YANG statement or use of extension"));