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.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.Status;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
32 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
34 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
37 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
38 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
42 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
43 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
47 public class TypesResolutionTest {
48 private SchemaContext context;
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");
56 context = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2, yangFileDependency3);
57 assertEquals(4, context.getModules().size());
61 public void testIPVersion() {
62 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
63 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
64 assertEquals(14, typedefs.size());
66 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
67 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
68 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
70 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
71 List<EnumPair> values = enumType.getValues();
72 assertEquals(3, values.size());
74 EnumPair value0 = values.get(0);
75 assertEquals("unknown", value0.getName());
76 assertEquals(0, value0.getValue());
77 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
79 EnumPair value1 = values.get(1);
80 assertEquals("ipv4", value1.getName());
81 assertEquals(1, value1.getValue());
82 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
84 EnumPair value2 = values.get(2);
85 assertEquals("ipv6", value2.getName());
86 assertEquals(2, value2.getValue());
87 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
91 public void testEnumeration() {
92 Module tested = TestUtils.findModule(context, "custom-types-test").get();
93 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
95 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
96 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
97 List<EnumPair> values = enumType.getValues();
98 assertEquals(4, values.size());
100 EnumPair value0 = values.get(0);
101 assertEquals("unknown", value0.getName());
102 assertEquals(0, value0.getValue());
103 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
105 EnumPair value1 = values.get(1);
106 assertEquals("ipv4", value1.getName());
107 assertEquals(19, value1.getValue());
108 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
110 EnumPair value2 = values.get(2);
111 assertEquals("ipv6", value2.getName());
112 assertEquals(7, value2.getValue());
113 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
115 EnumPair value3 = values.get(3);
116 assertEquals("default", value3.getName());
117 assertEquals(20, value3.getValue());
118 assertEquals("default ip", value3.getDescription());
122 public void testIpAddress() {
123 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
124 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
125 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
126 UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
127 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
129 StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
130 assertNotNull(ipv4.getBaseType());
131 String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
132 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
133 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
135 StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
136 assertNotNull(ipv6.getBaseType());
137 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
138 expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
139 + "((([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}"
140 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
141 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
143 expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
145 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
149 public void testDomainName() {
150 Module tested = TestUtils.findModule(context, "ietf-inet-types").get();
151 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
152 StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
153 assertNotNull(type.getBaseType());
154 List<PatternConstraint> patterns = type.getPatternConstraints();
155 assertEquals(1, patterns.size());
156 String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
157 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
158 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
160 List<LengthConstraint> lengths = type.getLengthConstraints();
161 assertEquals(1, lengths.size());
162 LengthConstraint length = type.getLengthConstraints().get(0);
163 assertEquals(Integer.valueOf(1), length.getMin());
164 assertEquals(Integer.valueOf(253), length.getMax());
168 public void testInstanceIdentifier1() {
169 Module tested = TestUtils.findModule(context, "custom-types-test").get();
170 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
171 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
172 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
173 assertFalse(leafType.requireInstance());
174 assertEquals(1, leaf.getUnknownSchemaNodes().size());
178 public void testInstanceIdentifier2() {
179 Module tested = TestUtils.findModule(context, "custom-types-test").get();
180 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
181 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
182 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
183 assertFalse(leafType.requireInstance());
187 public void testIdentity() {
188 Module tested = TestUtils.findModule(context, "custom-types-test").get();
189 Set<IdentitySchemaNode> identities = tested.getIdentities();
190 assertEquals(5, identities.size());
191 IdentitySchemaNode cryptoAlg = null;
192 IdentitySchemaNode cryptoBase = null;
193 IdentitySchemaNode cryptoId = null;
194 for (IdentitySchemaNode id : identities) {
195 if (id.getQName().getLocalName().equals("crypto-alg")) {
197 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
199 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
203 assertNotNull(cryptoAlg);
204 IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
205 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
206 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
207 assertNull(baseIdentity.getBaseIdentity());
209 assertNotNull(cryptoBase);
210 assertNull(cryptoBase.getBaseIdentity());
211 assertEquals(3, cryptoBase.getDerivedIdentities().size());
213 assertNotNull(cryptoId);
214 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
218 public void testBitsType1() {
219 Module tested = TestUtils.findModule(context, "custom-types-test").get();
220 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
221 QName.create(tested.getQNameModule(), "mybits"));
222 BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
223 List<Bit> bits = leafType.getBits();
224 assertEquals(3, bits.size());
226 Bit bit1 = bits.get(0);
227 assertEquals("disable-nagle", bit1.getName());
228 assertEquals(0L, bit1.getPosition());
230 Bit bit2 = bits.get(1);
231 assertEquals("auto-sense-speed", bit2.getName());
232 assertEquals(1L, bit2.getPosition());
234 Bit bit3 = bits.get(2);
235 assertEquals("10-Mb-only", bit3.getName());
236 assertEquals(2L, bit3.getPosition());
240 public void testBitsType2() {
241 Module tested = TestUtils.findModule(context, "custom-types-test").get();
242 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
243 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
245 BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
246 List<Bit> bits = bitsType.getBits();
247 assertEquals(5, bits.size());
249 Bit bit0 = bits.get(0);
250 assertEquals("create", bit0.getName());
251 assertEquals(0L, bit0.getPosition());
253 Bit bit1 = bits.get(1);
254 assertEquals("delete", bit1.getName());
255 assertEquals(365L, bit1.getPosition());
257 Bit bit2 = bits.get(2);
258 assertEquals("read", bit2.getName());
259 assertEquals(500L, bit2.getPosition());
261 Bit bit3 = bits.get(3);
262 assertEquals("update", bit3.getName());
263 assertEquals(501L, bit3.getPosition());
265 Bit bit4 = bits.get(4);
266 assertEquals("exec", bit4.getName());
267 assertEquals(502L, bit4.getPosition());
271 public void testIanaTimezones() {
272 Module tested = TestUtils.findModule(context, "iana-timezones").get();
273 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
274 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
276 String expectedDesc = "A timezone location as defined by the IANA timezone";
277 assertTrue(testedType.getDescription().contains(expectedDesc));
278 assertNull(testedType.getReference());
279 assertEquals(Status.CURRENT, testedType.getStatus());
281 QName testedTypeQName = testedType.getQName();
282 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
283 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
284 assertEquals("iana-timezone", testedTypeQName.getLocalName());
286 EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
287 List<EnumPair> values = enumType.getValues();
288 assertEquals(415, values.size()); // 0-414
290 EnumPair enum168 = values.get(168);
291 assertEquals("America/Danmarkshavn", enum168.getName());
292 assertEquals(168, enum168.getValue());
293 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
295 EnumPair enum374 = values.get(374);
296 assertEquals("America/Indiana/Winamac", enum374.getName());
297 assertEquals(374, enum374.getValue());
298 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
302 public void testObjectId128() {
303 Module tested = TestUtils.findModule(context, "ietf-yang-types").get();
304 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
305 StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
306 "object-identifier-128");
308 List<PatternConstraint> patterns = testedType.getPatternConstraints();
309 assertEquals(1, patterns.size());
310 PatternConstraint pattern = patterns.get(0);
311 assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
313 QName testedTypeQName = testedType.getQName();
314 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
315 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
316 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
318 StringTypeDefinition testedTypeBase = testedType.getBaseType();
319 patterns = testedTypeBase.getPatternConstraints();
320 assertEquals(1, patterns.size());
322 pattern = patterns.get(0);
323 assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
324 pattern.getRegularExpression());
326 QName testedTypeBaseQName = testedTypeBase.getQName();
327 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
328 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
329 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
333 public void testIdentityref() {
334 Module tested = TestUtils.findModule(context, "custom-types-test").get();
335 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
336 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
337 IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
338 QName identity = baseType.getIdentity().getQName();
339 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
340 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
341 assertEquals("service-type", identity.getLocalName());
343 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
348 public void testUnionWithExt() throws ReactorException {
350 final StatementStreamSource yangFile1 = sourceForResource("/types/union-with-ext/extdef.yang");
351 final StatementStreamSource yangFile2 = sourceForResource("/types/union-with-ext/unionbug.yang");
352 final StatementStreamSource yangFile3 = sourceForResource("/ietf/ietf-inet-types@2010-09-24.yang");
354 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
355 reactor.addSources(yangFile1, yangFile2, yangFile3);
357 final SchemaContext result = reactor.buildEffective();
358 assertNotNull(result);
362 public void testUnionWithBits() throws ReactorException {
364 final StatementStreamSource yangFile = sourceForResource("/types/union-with-bits/union-bits-model.yang");
366 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
367 reactor.addSources(yangFile);
369 final SchemaContext result = reactor.buildEffective();
370 assertNotNull(result);
374 public void testUnionInList() {
375 final StatementStreamSource yangFile = sourceForResource("/types/union-in-list/unioninlisttest.yang");
377 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
378 reactor.addSources(yangFile);
381 final SchemaContext result = reactor.buildEffective();
382 fail("effective build should fail due to union in list; this is not allowed");
383 } catch (Exception 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"));