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;
20 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.Status;
30 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
33 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
35 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
38 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
39 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
43 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
44 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
45 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
48 public class TypesResolutionTest {
49 private SchemaContext context;
52 public void init() throws Exception {
53 final StatementStreamSource yangFile = sourceForResource("/types/custom-types-test@2012-4-4.yang");
54 final StatementStreamSource yangFileDependency1 = sourceForResource("/ietf/iana-timezones@2012-07-09.yang");
55 final StatementStreamSource yangFileDependency2 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
56 final StatementStreamSource yangFileDependency3 = sourceForResource("/ietf/ietf-yang-types@2010-09-24.yang");
57 context = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2, yangFileDependency3);
58 assertEquals(4, context.getModules().size());
62 public void testIPVersion() {
63 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
64 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
65 assertEquals(14, typedefs.size());
67 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
68 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
69 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
71 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
72 List<EnumPair> values = enumType.getValues();
73 assertEquals(3, values.size());
75 EnumPair value0 = values.get(0);
76 assertEquals("unknown", value0.getName());
77 assertEquals(0, value0.getValue());
78 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
80 EnumPair value1 = values.get(1);
81 assertEquals("ipv4", value1.getName());
82 assertEquals(1, value1.getValue());
83 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
85 EnumPair value2 = values.get(2);
86 assertEquals("ipv6", value2.getName());
87 assertEquals(2, value2.getValue());
88 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
92 public void testEnumeration() {
93 Module tested = TestUtils.findModule(context, "custom-types-test").get();
94 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
96 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
97 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
98 List<EnumPair> values = enumType.getValues();
99 assertEquals(4, values.size());
101 EnumPair value0 = values.get(0);
102 assertEquals("unknown", value0.getName());
103 assertEquals(0, value0.getValue());
104 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
106 EnumPair value1 = values.get(1);
107 assertEquals("ipv4", value1.getName());
108 assertEquals(19, value1.getValue());
109 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
111 EnumPair value2 = values.get(2);
112 assertEquals("ipv6", value2.getName());
113 assertEquals(7, value2.getValue());
114 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
116 EnumPair value3 = values.get(3);
117 assertEquals("default", value3.getName());
118 assertEquals(20, value3.getValue());
119 assertEquals("default ip", value3.getDescription());
123 public void testIpAddress() {
124 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
125 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
126 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
127 UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
128 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
130 StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
131 assertNotNull(ipv4.getBaseType());
132 String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
133 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
134 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
136 StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
137 assertNotNull(ipv6.getBaseType());
138 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
139 expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
140 + "((([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}"
141 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
142 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
144 expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
146 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
150 public void testDomainName() {
151 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
152 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
153 StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
154 assertNotNull(type.getBaseType());
155 List<PatternConstraint> patterns = type.getPatternConstraints();
156 assertEquals(1, patterns.size());
157 String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
158 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
159 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
161 List<LengthConstraint> lengths = type.getLengthConstraints();
162 assertEquals(1, lengths.size());
163 LengthConstraint length = type.getLengthConstraints().get(0);
164 assertEquals(Integer.valueOf(1), length.getMin());
165 assertEquals(Integer.valueOf(253), length.getMax());
169 public void testInstanceIdentifier1() {
170 Module tested = TestUtils.findModule(context, "custom-types-test").get();
171 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
172 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
173 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
174 assertFalse(leafType.requireInstance());
175 assertEquals(1, leaf.getUnknownSchemaNodes().size());
179 public void testInstanceIdentifier2() {
180 Module tested = TestUtils.findModule(context, "custom-types-test").get();
181 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
182 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
183 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
184 assertFalse(leafType.requireInstance());
188 public void testIdentity() {
189 Module tested = TestUtils.findModule(context, "custom-types-test").get();
190 Set<IdentitySchemaNode> identities = tested.getIdentities();
191 assertEquals(5, identities.size());
192 IdentitySchemaNode cryptoAlg = null;
193 IdentitySchemaNode cryptoBase = null;
194 IdentitySchemaNode cryptoId = null;
195 for (IdentitySchemaNode id : identities) {
196 if (id.getQName().getLocalName().equals("crypto-alg")) {
198 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
200 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
204 assertNotNull(cryptoAlg);
205 IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(cryptoAlg.getBaseIdentities());
206 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
207 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
208 assertTrue(baseIdentity.getBaseIdentities().isEmpty());
210 assertNotNull(cryptoBase);
211 assertTrue(cryptoBase.getBaseIdentities().isEmpty());
212 assertEquals(3, cryptoBase.getDerivedIdentities().size());
214 assertNotNull(cryptoId);
215 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
219 public void testBitsType1() {
220 Module tested = TestUtils.findModule(context, "custom-types-test").get();
221 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
222 QName.create(tested.getQNameModule(), "mybits"));
223 BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
224 List<Bit> bits = leafType.getBits();
225 assertEquals(3, bits.size());
227 Bit bit1 = bits.get(0);
228 assertEquals("disable-nagle", bit1.getName());
229 assertEquals(0L, bit1.getPosition());
231 Bit bit2 = bits.get(1);
232 assertEquals("auto-sense-speed", bit2.getName());
233 assertEquals(1L, bit2.getPosition());
235 Bit bit3 = bits.get(2);
236 assertEquals("10-Mb-only", bit3.getName());
237 assertEquals(2L, bit3.getPosition());
241 public void testBitsType2() {
242 Module tested = TestUtils.findModule(context, "custom-types-test").get();
243 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
244 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
246 BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
247 List<Bit> bits = bitsType.getBits();
248 assertEquals(5, bits.size());
250 Bit bit0 = bits.get(0);
251 assertEquals("create", bit0.getName());
252 assertEquals(0L, bit0.getPosition());
254 Bit bit1 = bits.get(1);
255 assertEquals("delete", bit1.getName());
256 assertEquals(365L, bit1.getPosition());
258 Bit bit2 = bits.get(2);
259 assertEquals("read", bit2.getName());
260 assertEquals(500L, bit2.getPosition());
262 Bit bit3 = bits.get(3);
263 assertEquals("update", bit3.getName());
264 assertEquals(501L, bit3.getPosition());
266 Bit bit4 = bits.get(4);
267 assertEquals("exec", bit4.getName());
268 assertEquals(502L, bit4.getPosition());
272 public void testIanaTimezones() {
273 Module tested = TestUtils.findModule(context, "iana-timezones").get();
274 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
275 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
277 String expectedDesc = "A timezone location as defined by the IANA timezone";
278 assertTrue(testedType.getDescription().contains(expectedDesc));
279 assertNull(testedType.getReference());
280 assertEquals(Status.CURRENT, testedType.getStatus());
282 QName testedTypeQName = testedType.getQName();
283 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
284 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
285 assertEquals("iana-timezone", testedTypeQName.getLocalName());
287 EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
288 List<EnumPair> values = enumType.getValues();
289 assertEquals(415, values.size()); // 0-414
291 EnumPair enum168 = values.get(168);
292 assertEquals("America/Danmarkshavn", enum168.getName());
293 assertEquals(168, enum168.getValue());
294 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
296 EnumPair enum374 = values.get(374);
297 assertEquals("America/Indiana/Winamac", enum374.getName());
298 assertEquals(374, enum374.getValue());
299 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
303 public void testObjectId128() {
304 Module tested = TestUtils.findModule(context, "ietf-yang-types").get();
305 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
306 StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
307 "object-identifier-128");
309 List<PatternConstraint> patterns = testedType.getPatternConstraints();
310 assertEquals(1, patterns.size());
311 PatternConstraint pattern = patterns.get(0);
312 assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
314 QName testedTypeQName = testedType.getQName();
315 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
316 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
317 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
319 StringTypeDefinition testedTypeBase = testedType.getBaseType();
320 patterns = testedTypeBase.getPatternConstraints();
321 assertEquals(1, patterns.size());
323 pattern = patterns.get(0);
324 assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
325 pattern.getRegularExpression());
327 QName testedTypeBaseQName = testedTypeBase.getQName();
328 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
329 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
330 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
334 public void testIdentityref() {
335 Module tested = TestUtils.findModule(context, "custom-types-test").get();
336 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
337 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
338 IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
339 QName identity = baseType.getIdentity().getQName();
340 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
341 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
342 assertEquals("service-type", identity.getLocalName());
344 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
349 public void testUnionWithExt() throws ReactorException {
351 final StatementStreamSource yangFile1 = sourceForResource("/types/union-with-ext/extdef.yang");
352 final StatementStreamSource yangFile2 = sourceForResource("/types/union-with-ext/unionbug.yang");
353 final StatementStreamSource yangFile3 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
355 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
356 reactor.addSources(yangFile1, yangFile2, yangFile3);
358 final SchemaContext result = reactor.buildEffective();
359 assertNotNull(result);
363 public void testUnionWithBits() throws ReactorException {
365 final StatementStreamSource yangFile = sourceForResource("/types/union-with-bits/union-bits-model.yang");
367 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
368 reactor.addSources(yangFile);
370 final SchemaContext result = reactor.buildEffective();
371 assertNotNull(result);
375 public void testUnionInList() {
376 final StatementStreamSource yangFile = sourceForResource("/types/union-in-list/unioninlisttest.yang");
378 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
379 reactor.addSources(yangFile);
382 final SchemaContext result = reactor.buildEffective();
383 fail("effective build should fail due to union in list; this is not allowed");
384 } catch (Exception e) {
385 assertEquals(SomeModifiersUnresolvedException.class, e.getClass());
386 assertTrue(e.getCause() instanceof SourceException);
387 assertTrue(e.getCause().getMessage().startsWith("union is not a YANG statement or use of extension"));