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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertFalse;
15 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
16 import static org.junit.jupiter.api.Assertions.assertNotNull;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
19 import com.google.common.collect.Iterables;
20 import java.util.Optional;
21 import org.junit.jupiter.api.BeforeAll;
22 import org.junit.jupiter.api.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.Uint32;
26 import org.opendaylight.yangtools.yang.common.XMLNamespace;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
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.Status;
31 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
32 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
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.StringTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
40 class TypesResolutionTest extends AbstractYangTest {
41 private static EffectiveModelContext CONTEXT;
44 static void beforeClass() {
45 CONTEXT = assertEffectiveModel(
46 "/types/custom-types-test@2012-04-04.yang",
47 "/ietf/iana-timezones@2012-07-09.yang",
48 "/ietf/ietf-inet-types@2010-09-24.yang",
49 "/ietf/ietf-yang-types@2010-09-24.yang");
50 assertEquals(4, CONTEXT.getModules().size());
54 void testIPVersion() {
55 var tested = CONTEXT.findModules("ietf-inet-types").iterator().next();
56 var typedefs = tested.getTypeDefinitions();
57 assertEquals(14, typedefs.size());
59 var type = TestUtils.findTypedef(typedefs, "ip-version");
60 assertThat(type.getDescription().orElseThrow(),
61 containsString("This value represents the version of the IP protocol."));
62 assertThat(type.getReference().orElseThrow(),
63 containsString("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
65 var enumType = assertInstanceOf(EnumTypeDefinition.class, type.getBaseType());
66 var values = enumType.getValues();
67 assertEquals(3, values.size());
69 EnumPair value0 = values.get(0);
70 assertEquals("unknown", value0.getName());
71 assertEquals(0, value0.getValue());
72 assertEquals(Optional.of("An unknown or unspecified version of the Internet protocol."),
73 value0.getDescription());
75 EnumPair value1 = values.get(1);
76 assertEquals("ipv4", value1.getName());
77 assertEquals(1, value1.getValue());
78 assertEquals(Optional.of("The IPv4 protocol as defined in RFC 791."), value1.getDescription());
80 EnumPair value2 = values.get(2);
81 assertEquals("ipv6", value2.getName());
82 assertEquals(2, value2.getValue());
83 assertEquals(Optional.of("The IPv6 protocol as defined in RFC 2460."), value2.getDescription());
87 void testEnumeration() {
88 var tested = CONTEXT.findModules("custom-types-test").iterator().next();
89 var typedefs = tested.getTypeDefinitions();
91 var type = TestUtils.findTypedef(typedefs, "ip-version");
92 var enumType = assertInstanceOf(EnumTypeDefinition.class, type.getBaseType());
93 var values = enumType.getValues();
94 assertEquals(4, values.size());
96 var value0 = values.get(0);
97 assertEquals("unknown", value0.getName());
98 assertEquals(0, value0.getValue());
99 assertEquals(Optional.of("An unknown or unspecified version of the Internet protocol."),
100 value0.getDescription());
102 var value1 = values.get(1);
103 assertEquals("ipv4", value1.getName());
104 assertEquals(19, value1.getValue());
105 assertEquals(Optional.of("The IPv4 protocol as defined in RFC 791."), value1.getDescription());
107 var value2 = values.get(2);
108 assertEquals("ipv6", value2.getName());
109 assertEquals(7, value2.getValue());
110 assertEquals(Optional.of("The IPv6 protocol as defined in RFC 2460."), value2.getDescription());
112 var value3 = values.get(3);
113 assertEquals("default", value3.getName());
114 assertEquals(20, value3.getValue());
115 assertEquals(Optional.of("default ip"), value3.getDescription());
119 void testIpAddress() {
120 var tested = CONTEXT.findModules("ietf-inet-types").iterator().next();
121 var typedefs = tested.getTypeDefinitions();
122 var type = TestUtils.findTypedef(typedefs, "ip-address");
123 var baseType = assertInstanceOf(UnionTypeDefinition.class, type.getBaseType());
124 var unionTypes = baseType.getTypes();
126 var ipv4 = assertInstanceOf(StringTypeDefinition.class, unionTypes.get(0));
127 assertNotNull(ipv4.getBaseType());
129 ^(?:(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}\
130 ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\
131 (%[\\p{N}\\p{L}]+)?)$""", ipv4.getPatternConstraints().get(0).getJavaPatternString());
133 var ipv6 = assertInstanceOf(StringTypeDefinition.class, unionTypes.get(1));
134 assertNotNull(ipv6.getBaseType());
135 var ipv6Patterns = ipv6.getPatternConstraints();
137 ^(?:((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}\
138 ((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|\
139 (((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])))\
141 (%[\\p{N}\\p{L}]+)?)$""", ipv6Patterns.get(0).getJavaPatternString());
143 ^(?:(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|\
144 ((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)\
145 (%.+)?)$""", ipv6Patterns.get(1).getJavaPatternString());
149 void testDomainName() {
150 var tested = CONTEXT.findModules("ietf-inet-types").iterator().next();
151 var typedefs = tested.getTypeDefinitions();
152 var type = assertInstanceOf(StringTypeDefinition.class, TestUtils.findTypedef(typedefs, "domain-name"));
153 assertNotNull(type.getBaseType());
154 var patterns = type.getPatternConstraints();
155 assertEquals(1, patterns.size());
157 ^(?:((([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 |\\.)$""", patterns.get(0).getJavaPatternString());
161 var lengths = type.getLengthConstraint().orElseThrow();
162 assertEquals(1, lengths.getAllowedRanges().asRanges().size());
163 var length = lengths.getAllowedRanges().span();
164 assertEquals(Integer.valueOf(1), length.lowerEndpoint());
165 assertEquals(Integer.valueOf(253), length.upperEndpoint());
169 void testInstanceIdentifier1() {
170 var tested = CONTEXT.findModules("custom-types-test").iterator().next();
171 var leaf = assertInstanceOf(LeafSchemaNode.class,
172 tested.getDataChildByName(QName.create(tested.getQNameModule(), "inst-id-leaf1")));
173 var leafType = assertInstanceOf(InstanceIdentifierTypeDefinition.class, leaf.getType());
174 assertFalse(leafType.requireInstance());
176 leaf.asEffectiveStatement().getDeclared().declaredSubstatements(UnrecognizedStatement.class).size());
180 void testInstanceIdentifier2() {
181 var tested = CONTEXT.findModules("custom-types-test").iterator().next();
182 var leaf = assertInstanceOf(LeafSchemaNode.class,
183 tested.getDataChildByName(QName.create(tested.getQNameModule(), "inst-id-leaf2")));
184 var leafType = assertInstanceOf(InstanceIdentifierTypeDefinition.class, leaf.getType());
185 assertFalse(leafType.requireInstance());
189 void testIdentity() {
190 var tested = CONTEXT.findModules("custom-types-test").iterator().next();
191 var identities = tested.getIdentities();
192 assertEquals(5, identities.size());
193 IdentitySchemaNode cryptoAlg = null;
194 IdentitySchemaNode cryptoBase = null;
195 IdentitySchemaNode cryptoId = null;
196 for (var id : identities) {
197 if (id.getQName().getLocalName().equals("crypto-alg")) {
199 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
201 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
205 assertNotNull(cryptoAlg);
206 var baseIdentity = Iterables.getOnlyElement(cryptoAlg.getBaseIdentities());
207 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
208 assertEquals(0, CONTEXT.getDerivedIdentities(cryptoAlg).size());
209 assertEquals(0, baseIdentity.getBaseIdentities().size());
211 assertNotNull(cryptoBase);
212 assertTrue(cryptoBase.getBaseIdentities().isEmpty());
213 assertEquals(3, CONTEXT.getDerivedIdentities(cryptoBase).size());
215 assertNotNull(cryptoId);
217 cryptoId.asEffectiveStatement().getDeclared().declaredSubstatements(UnrecognizedStatement.class).size());
221 void testBitsType1() {
222 var tested = CONTEXT.findModules("custom-types-test").iterator().next();
223 var leaf = assertInstanceOf(LeafSchemaNode.class,
224 tested.getDataChildByName(QName.create(tested.getQNameModule(), "mybits")));
225 var leafType = assertInstanceOf(BitsTypeDefinition.class, leaf.getType());
226 var bits = leafType.getBits().iterator();
228 var bit1 = bits.next();
229 assertEquals("disable-nagle", bit1.getName());
230 assertEquals(Uint32.ZERO, bit1.getPosition());
232 var bit2 = bits.next();
233 assertEquals("auto-sense-speed", bit2.getName());
234 assertEquals(Uint32.ONE, bit2.getPosition());
236 var bit3 = bits.next();
237 assertEquals("only-10-Mb", bit3.getName());
238 assertEquals(Uint32.TWO, bit3.getPosition());
240 assertFalse(bits.hasNext());
244 void testBitsType2() {
245 var tested = CONTEXT.findModules("custom-types-test").iterator().next();
246 var typedefs = tested.getTypeDefinitions();
247 var testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
249 var bitsType = assertInstanceOf(BitsTypeDefinition.class, testedType.getBaseType());
250 var bits = bitsType.getBits().iterator();
252 var bit0 = bits.next();
253 assertEquals("create", bit0.getName());
254 assertEquals(Uint32.ZERO, bit0.getPosition());
256 var bit1 = bits.next();
257 assertEquals("delete", bit1.getName());
258 assertEquals(Uint32.valueOf(365), bit1.getPosition());
260 var bit2 = bits.next();
261 assertEquals("read", bit2.getName());
262 assertEquals(Uint32.valueOf(500), bit2.getPosition());
264 var bit3 = bits.next();
265 assertEquals("update", bit3.getName());
266 assertEquals(Uint32.valueOf(501), bit3.getPosition());
268 var bit4 = bits.next();
269 assertEquals("exec", bit4.getName());
270 assertEquals(Uint32.valueOf(502), bit4.getPosition());
272 assertFalse(bits.hasNext());
276 void testIanaTimezones() {
277 var tested = CONTEXT.findModules("iana-timezones").iterator().next();
278 var typedefs = tested.getTypeDefinitions();
279 var testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
281 assertThat(testedType.getDescription().orElseThrow(),
282 containsString("A timezone location as defined by the IANA timezone"));
283 assertFalse(testedType.getReference().isPresent());
284 assertEquals(Status.CURRENT, testedType.getStatus());
286 QName testedTypeQName = testedType.getQName();
287 assertEquals(XMLNamespace.of("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 var enumType = assertInstanceOf(EnumTypeDefinition.class, testedType.getBaseType());
292 var values = enumType.getValues();
294 assertEquals(415, values.size());
296 var 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 var 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 void testObjectId128() {
309 var tested = CONTEXT.findModules("ietf-yang-types").iterator().next();
310 var typedefs = tested.getTypeDefinitions();
311 var testedType = assertInstanceOf(StringTypeDefinition.class,
312 TestUtils.findTypedef(typedefs, "object-identifier-128"));
314 var patterns = testedType.getPatternConstraints();
315 assertEquals(1, patterns.size());
316 var pattern = patterns.get(0);
317 assertEquals("^(?:\\d*(\\.\\d*){1,127})$", pattern.getJavaPatternString());
319 QName testedTypeQName = testedType.getQName();
320 assertEquals(XMLNamespace.of("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 var 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(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
334 testedTypeBaseQName.getNamespace());
335 assertEquals(Revision.ofNullable("2010-09-24"), testedTypeBaseQName.getRevision());
336 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
340 void testIdentityref() {
341 var tested = CONTEXT.findModules("custom-types-test").iterator().next();
342 var typedefs = tested.getTypeDefinitions();
343 var testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
344 var baseType = assertInstanceOf(IdentityrefTypeDefinition.class, testedType.getBaseType());
345 QName identity = baseType.getIdentities().iterator().next().getQName();
346 assertEquals(XMLNamespace.of("urn:custom.types.demo"), identity.getNamespace());
347 assertEquals(Revision.ofNullable("2012-04-16"), identity.getRevision());
348 assertEquals("service-type", identity.getLocalName());
350 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
355 void testUnionWithExt() {
356 assertEffectiveModel(
357 "/types/union-with-ext/extdef.yang",
358 "/types/union-with-ext/unionbug.yang",
359 "/ietf/ietf-inet-types@2010-09-24.yang");
363 void testUnionWithBits() {
364 assertEffectiveModel("/types/union-with-bits/union-bits-model.yang");
368 void testUnionInList() {
369 assertSourceException(startsWith("union is not a YANG statement or use of extension"),
370 "/types/union-in-list/unioninlisttest.yang");