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;
18 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.Status;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
30 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
32 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
35 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
36 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
40 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
41 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
42 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
43 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
44 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
47 public class TypesResolutionTest {
48 private Set<Module> testedModules;
51 public void init() throws Exception {
52 final StatementStreamSource yangFile =
53 new YangStatementSourceImpl("/types/custom-types-test@2012-4-4.yang", false);
54 final StatementStreamSource yangFileDependency1 =
55 new YangStatementSourceImpl ("/ietf/iana-timezones@2012-07-09.yang", false);
56 final StatementStreamSource yangFileDependency2 =
57 new YangStatementSourceImpl ("/ietf/ietf-inet-types@2010-09-24.yang", false);
58 final StatementStreamSource yangFileDependency3 =
59 new YangStatementSourceImpl ("/ietf/ietf-yang-types@2010-09-24.yang", false);
61 testedModules = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2,
62 yangFileDependency3).getModules();
63 assertEquals(4, testedModules.size());
67 public void testIPVersion() {
68 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
69 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
70 assertEquals(14, typedefs.size());
72 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
73 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
74 assertTrue(type.getReference().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
76 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
77 List<EnumPair> values = enumType.getValues();
78 assertEquals(3, values.size());
80 EnumPair value0 = values.get(0);
81 assertEquals("unknown", value0.getName());
82 assertEquals(0, value0.getValue());
83 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
85 EnumPair value1 = values.get(1);
86 assertEquals("ipv4", value1.getName());
87 assertEquals(1, value1.getValue());
88 assertEquals("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("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
97 public void testEnumeration() {
98 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
99 Set<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("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
111 EnumPair value1 = values.get(1);
112 assertEquals("ipv4", value1.getName());
113 assertEquals(19, value1.getValue());
114 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
116 EnumPair value2 = values.get(2);
117 assertEquals("ipv6", value2.getName());
118 assertEquals(7, value2.getValue());
119 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
121 EnumPair value3 = values.get(3);
122 assertEquals("default", value3.getName());
123 assertEquals(20, value3.getValue());
124 assertEquals("default ip", value3.getDescription());
128 public void testIpAddress() {
129 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
130 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
131 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
132 UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
133 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
135 StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
136 assertNotNull(ipv4.getBaseType());
137 String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
138 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
139 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
141 StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
142 assertNotNull(ipv6.getBaseType());
143 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
144 expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
145 + "((([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}"
146 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
147 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
149 expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
151 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
155 public void testDomainName() {
156 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
157 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
158 StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
159 assertNotNull(type.getBaseType());
160 List<PatternConstraint> patterns = type.getPatternConstraints();
161 assertEquals(1, patterns.size());
162 String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
163 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
164 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
166 List<LengthConstraint> lengths = type.getLengthConstraints();
167 assertEquals(1, lengths.size());
168 LengthConstraint length = type.getLengthConstraints().get(0);
169 assertEquals(Integer.valueOf(1), length.getMin());
170 assertEquals(Integer.valueOf(253), length.getMax());
174 public void testInstanceIdentifier1() {
175 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
176 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
177 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
178 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
179 assertFalse(leafType.requireInstance());
180 assertEquals(1, leaf.getUnknownSchemaNodes().size());
184 public void testInstanceIdentifier2() {
185 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
186 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
187 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
188 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
189 assertFalse(leafType.requireInstance());
193 public void testIdentity() {
194 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
195 Set<IdentitySchemaNode> identities = tested.getIdentities();
196 assertEquals(5, identities.size());
197 IdentitySchemaNode cryptoAlg = null;
198 IdentitySchemaNode cryptoBase = null;
199 IdentitySchemaNode cryptoId = null;
200 for (IdentitySchemaNode id : identities) {
201 if (id.getQName().getLocalName().equals("crypto-alg")) {
203 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
205 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
209 assertNotNull(cryptoAlg);
210 IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
211 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
212 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
213 assertNull(baseIdentity.getBaseIdentity());
215 assertNotNull(cryptoBase);
216 assertNull(cryptoBase.getBaseIdentity());
217 assertEquals(3, cryptoBase.getDerivedIdentities().size());
219 assertNotNull(cryptoId);
220 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
224 public void testBitsType1() {
225 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
226 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
227 QName.create(tested.getQNameModule(), "mybits"));
228 BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
229 List<Bit> bits = leafType.getBits();
230 assertEquals(3, bits.size());
232 Bit bit1 = bits.get(0);
233 assertEquals("disable-nagle", bit1.getName());
234 assertEquals(0L, bit1.getPosition());
236 Bit bit2 = bits.get(1);
237 assertEquals("auto-sense-speed", bit2.getName());
238 assertEquals(1L, bit2.getPosition());
240 Bit bit3 = bits.get(2);
241 assertEquals("10-Mb-only", bit3.getName());
242 assertEquals(2L, bit3.getPosition());
246 public void testBitsType2() {
247 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
248 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
249 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
251 BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
252 List<Bit> bits = bitsType.getBits();
253 assertEquals(5, bits.size());
255 Bit bit0 = bits.get(0);
256 assertEquals("create", bit0.getName());
257 assertEquals(0L, bit0.getPosition());
259 Bit bit1 = bits.get(1);
260 assertEquals("delete", bit1.getName());
261 assertEquals(365L, bit1.getPosition());
263 Bit bit2 = bits.get(2);
264 assertEquals("read", bit2.getName());
265 assertEquals(500L, bit2.getPosition());
267 Bit bit3 = bits.get(3);
268 assertEquals("update", bit3.getName());
269 assertEquals(501L, bit3.getPosition());
271 Bit bit4 = bits.get(4);
272 assertEquals("exec", bit4.getName());
273 assertEquals(502L, bit4.getPosition());
277 public void testIanaTimezones() {
278 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
279 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
280 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
282 String expectedDesc = "A timezone location as defined by the IANA timezone";
283 assertTrue(testedType.getDescription().contains(expectedDesc));
284 assertNull(testedType.getReference());
285 assertEquals(Status.CURRENT, testedType.getStatus());
287 QName testedTypeQName = testedType.getQName();
288 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
289 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
290 assertEquals("iana-timezone", testedTypeQName.getLocalName());
292 EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
293 List<EnumPair> values = enumType.getValues();
294 assertEquals(415, values.size()); // 0-414
296 EnumPair enum168 = values.get(168);
297 assertEquals("America/Danmarkshavn", enum168.getName());
298 assertEquals(168, enum168.getValue());
299 assertEquals("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("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
308 public void testObjectId128() {
309 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
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.getRegularExpression());
319 QName testedTypeQName = testedType.getQName();
320 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
321 assertEquals(TestUtils.createDate("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.getRegularExpression());
332 QName testedTypeBaseQName = testedTypeBase.getQName();
333 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
334 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
335 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
339 public void testIdentityref() {
340 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
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.getIdentity().getQName();
345 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
346 assertEquals(TestUtils.createDate("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 {
356 final YangStatementSourceImpl yangFile1 = new YangStatementSourceImpl("/types/union-with-ext/extdef.yang",
358 final YangStatementSourceImpl yangFile2 = new YangStatementSourceImpl("/types/union-with-ext/unionbug.yang",
360 final YangStatementSourceImpl yangFile3 = new YangStatementSourceImpl("/ietf/ietf-inet-types@2010-09-24.yang",
363 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
364 addSources(reactor, yangFile1, yangFile2, yangFile3);
366 final EffectiveSchemaContext result = reactor.buildEffective();
367 assertNotNull(result);
371 public void testUnionWithBits() throws ReactorException {
373 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
374 "/types/union-with-bits/union-bits-model.yang", false);
376 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
377 addSources(reactor, yangFile);
379 final EffectiveSchemaContext result = reactor.buildEffective();
380 assertNotNull(result);
384 public void testUnionInList() {
385 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
386 "/types/union-in-list/unioninlisttest.yang", false);
388 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
389 addSources(reactor, yangFile);
392 final EffectiveSchemaContext result = reactor.buildEffective();
393 fail("effective build should fail due to union in list; this is not allowed");
394 } catch (Exception e) {
395 assertEquals(SomeModifiersUnresolvedException.class, e.getClass());
396 assertTrue(e.getCause() instanceof SourceException);
397 assertTrue(e.getCause().getMessage().startsWith("union is not a YANG statement or use of extension"));
401 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
402 for (YangStatementSourceImpl source : sources) {
403 reactor.addSource(source);