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.List;
21 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.Revision;
27 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.Status;
32 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
35 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
37 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
40 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
41 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
43 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
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.BuildAction;
50 public class TypesResolutionTest {
51 private SchemaContext context;
54 public void init() throws Exception {
55 final StatementStreamSource yangFile = sourceForResource("/types/custom-types-test@2012-04-04.yang");
56 final StatementStreamSource yangFileDependency1 = sourceForResource("/ietf/iana-timezones@2012-07-09.yang");
57 final StatementStreamSource yangFileDependency2 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
58 final StatementStreamSource yangFileDependency3 = sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang");
59 context = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2, yangFileDependency3);
60 assertEquals(4, context.getModules().size());
64 public void testIPVersion() {
65 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
66 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
67 assertEquals(14, typedefs.size());
69 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
70 assertTrue(type.getDescription().get().contains("This value represents the version of the IP protocol."));
71 assertTrue(type.getReference().get().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
73 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
74 List<EnumPair> values = enumType.getValues();
75 assertEquals(3, values.size());
77 EnumPair value0 = values.get(0);
78 assertEquals("unknown", value0.getName());
79 assertEquals(0, value0.getValue());
80 assertEquals(Optional.of("An unknown or unspecified version of the Internet protocol."),
81 value0.getDescription());
83 EnumPair value1 = values.get(1);
84 assertEquals("ipv4", value1.getName());
85 assertEquals(1, value1.getValue());
86 assertEquals(Optional.of("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(Optional.of("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(Optional.of("An unknown or unspecified version of the Internet protocol."),
108 value0.getDescription());
110 EnumPair value1 = values.get(1);
111 assertEquals("ipv4", value1.getName());
112 assertEquals(19, value1.getValue());
113 assertEquals(Optional.of("The IPv4 protocol as defined in RFC 791."), value1.getDescription());
115 EnumPair value2 = values.get(2);
116 assertEquals("ipv6", value2.getName());
117 assertEquals(7, value2.getValue());
118 assertEquals(Optional.of("The IPv6 protocol as defined in RFC 2460."), value2.getDescription());
120 EnumPair value3 = values.get(3);
121 assertEquals("default", value3.getName());
122 assertEquals(20, value3.getValue());
123 assertEquals(Optional.of("default ip"), value3.getDescription());
127 public void testIpAddress() {
128 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
129 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
130 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
131 UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
132 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
134 StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
135 assertNotNull(ipv4.getBaseType());
136 String expectedPattern = "^(?:(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
137 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?)$";
138 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getJavaPatternString());
140 StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
141 assertNotNull(ipv6.getBaseType());
142 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
143 expectedPattern = "^(?:((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
144 + "((([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}"
145 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?)$";
146 assertEquals(expectedPattern, ipv6Patterns.get(0).getJavaPatternString());
148 expectedPattern = "^(?:(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
150 assertEquals(expectedPattern, ipv6Patterns.get(1).getJavaPatternString());
154 public void testDomainName() {
155 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
156 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
157 StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
158 assertNotNull(type.getBaseType());
159 List<PatternConstraint> patterns = type.getPatternConstraints();
160 assertEquals(1, patterns.size());
161 String expectedPattern = "^(?:((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
162 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.)$";
163 assertEquals(expectedPattern, patterns.get(0).getJavaPatternString());
165 LengthConstraint lengths = type.getLengthConstraint().get();
166 assertEquals(1, lengths.getAllowedRanges().asRanges().size());
167 Range<Integer> length = lengths.getAllowedRanges().span();
168 assertEquals(Integer.valueOf(1), length.lowerEndpoint());
169 assertEquals(Integer.valueOf(253), length.upperEndpoint());
173 public void testInstanceIdentifier1() {
174 Module tested = TestUtils.findModule(context, "custom-types-test").get();
175 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
176 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
177 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
178 assertFalse(leafType.requireInstance());
179 assertEquals(1, leaf.getUnknownSchemaNodes().size());
183 public void testInstanceIdentifier2() {
184 Module tested = TestUtils.findModule(context, "custom-types-test").get();
185 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
186 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
187 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
188 assertFalse(leafType.requireInstance());
192 public void testIdentity() {
193 Module tested = TestUtils.findModule(context, "custom-types-test").get();
194 Set<IdentitySchemaNode> identities = tested.getIdentities();
195 assertEquals(5, identities.size());
196 IdentitySchemaNode cryptoAlg = null;
197 IdentitySchemaNode cryptoBase = null;
198 IdentitySchemaNode cryptoId = null;
199 for (IdentitySchemaNode id : identities) {
200 if (id.getQName().getLocalName().equals("crypto-alg")) {
202 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
204 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
208 assertNotNull(cryptoAlg);
209 IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(cryptoAlg.getBaseIdentities());
210 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
211 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
212 assertTrue(baseIdentity.getBaseIdentities().isEmpty());
214 assertNotNull(cryptoBase);
215 assertTrue(cryptoBase.getBaseIdentities().isEmpty());
216 assertEquals(3, cryptoBase.getDerivedIdentities().size());
218 assertNotNull(cryptoId);
219 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
223 public void testBitsType1() {
224 Module tested = TestUtils.findModule(context, "custom-types-test").get();
225 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
226 QName.create(tested.getQNameModule(), "mybits"));
227 BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
228 List<Bit> bits = leafType.getBits();
229 assertEquals(3, bits.size());
231 Bit bit1 = bits.get(0);
232 assertEquals("disable-nagle", bit1.getName());
233 assertEquals(0L, bit1.getPosition());
235 Bit bit2 = bits.get(1);
236 assertEquals("auto-sense-speed", bit2.getName());
237 assertEquals(1L, bit2.getPosition());
239 Bit bit3 = bits.get(2);
240 assertEquals("10-Mb-only", bit3.getName());
241 assertEquals(2L, bit3.getPosition());
245 public void testBitsType2() {
246 Module tested = TestUtils.findModule(context, "custom-types-test").get();
247 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
248 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
250 BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
251 List<Bit> bits = bitsType.getBits();
252 assertEquals(5, bits.size());
254 Bit bit0 = bits.get(0);
255 assertEquals("create", bit0.getName());
256 assertEquals(0L, bit0.getPosition());
258 Bit bit1 = bits.get(1);
259 assertEquals("delete", bit1.getName());
260 assertEquals(365L, bit1.getPosition());
262 Bit bit2 = bits.get(2);
263 assertEquals("read", bit2.getName());
264 assertEquals(500L, bit2.getPosition());
266 Bit bit3 = bits.get(3);
267 assertEquals("update", bit3.getName());
268 assertEquals(501L, bit3.getPosition());
270 Bit bit4 = bits.get(4);
271 assertEquals("exec", bit4.getName());
272 assertEquals(502L, bit4.getPosition());
276 public void testIanaTimezones() {
277 Module tested = TestUtils.findModule(context, "iana-timezones").get();
278 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
279 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
281 String expectedDesc = "A timezone location as defined by the IANA timezone";
282 assertTrue(testedType.getDescription().get().contains(expectedDesc));
283 assertFalse(testedType.getReference().isPresent());
284 assertEquals(Status.CURRENT, testedType.getStatus());
286 QName testedTypeQName = testedType.getQName();
287 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
288 assertEquals(Revision.ofNullable("2012-07-09"), testedTypeQName.getRevision());
289 assertEquals("iana-timezone", testedTypeQName.getLocalName());
291 EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
292 List<EnumPair> values = enumType.getValues();
294 assertEquals(415, values.size());
296 EnumPair enum168 = values.get(168);
297 assertEquals("America/Danmarkshavn", enum168.getName());
298 assertEquals(168, enum168.getValue());
299 assertEquals(Optional.of("east coast, north of Scoresbysund"), enum168.getDescription());
301 EnumPair enum374 = values.get(374);
302 assertEquals("America/Indiana/Winamac", enum374.getName());
303 assertEquals(374, enum374.getValue());
304 assertEquals(Optional.of("Eastern Time - Indiana - Pulaski County"), enum374.getDescription());
308 public void testObjectId128() {
309 Module tested = TestUtils.findModule(context, "ietf-yang-types").get();
310 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
311 StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
312 "object-identifier-128");
314 List<PatternConstraint> patterns = testedType.getPatternConstraints();
315 assertEquals(1, patterns.size());
316 PatternConstraint pattern = patterns.get(0);
317 assertEquals("^(?:\\d*(\\.\\d*){1,127})$", pattern.getJavaPatternString());
319 QName testedTypeQName = testedType.getQName();
320 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
321 assertEquals(Revision.ofNullable("2010-09-24"), testedTypeQName.getRevision());
322 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
324 StringTypeDefinition testedTypeBase = testedType.getBaseType();
325 patterns = testedTypeBase.getPatternConstraints();
326 assertEquals(1, patterns.size());
328 pattern = patterns.get(0);
329 assertEquals("^(?:(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*)$",
330 pattern.getJavaPatternString());
332 QName testedTypeBaseQName = testedTypeBase.getQName();
333 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
334 assertEquals(Revision.ofNullable("2010-09-24"), testedTypeBaseQName.getRevision());
335 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
339 public void testIdentityref() {
340 Module tested = TestUtils.findModule(context, "custom-types-test").get();
341 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
342 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
343 IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
344 QName identity = baseType.getIdentities().iterator().next().getQName();
345 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
346 assertEquals(Revision.ofNullable("2012-04-16"), identity.getRevision());
347 assertEquals("service-type", identity.getLocalName());
349 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
354 public void testUnionWithExt() throws ReactorException {
355 final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
356 .addSource(sourceForResource("/types/union-with-ext/extdef.yang"))
357 .addSource(sourceForResource("/types/union-with-ext/unionbug.yang"))
358 .addSource(sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang"))
360 assertNotNull(result);
364 public void testUnionWithBits() throws ReactorException {
365 final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
366 .addSource(sourceForResource("/types/union-with-bits/union-bits-model.yang"))
368 assertNotNull(result);
372 public void testUnionInList() {
373 BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
374 .addSource(sourceForResource("/types/union-in-list/unioninlisttest.yang"));
377 final SchemaContext result = reactor.buildEffective();
378 fail("effective build should fail due to union in list; this is not allowed");
379 } catch (ReactorException e) {
380 assertEquals(SomeModifiersUnresolvedException.class, e.getClass());
381 assertTrue(e.getCause() instanceof SourceException);
382 assertTrue(e.getCause().getMessage().startsWith("union is not a YANG statement or use of extension"));