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;
17 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.Status;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
29 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
31 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
34 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
35 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
41 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
42 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
44 public class TypesResolutionTest {
45 private Set<Module> testedModules;
48 public void init() throws Exception {
49 final StatementStreamSource yangFile =
50 new YangStatementSourceImpl("/types/custom-types-test@2012-4-4.yang", false);
51 final StatementStreamSource yangFileDependency1 =
52 new YangStatementSourceImpl ("/ietf/iana-timezones@2012-07-09.yang", false);
53 final StatementStreamSource yangFileDependency2 =
54 new YangStatementSourceImpl ("/ietf/ietf-inet-types@2010-09-24.yang", false);
55 final StatementStreamSource yangFileDependency3 =
56 new YangStatementSourceImpl ("/ietf/ietf-yang-types@2010-09-24.yang", false);
58 testedModules = TestUtils.parseYangSources(yangFile, yangFileDependency1, yangFileDependency2,
59 yangFileDependency3).getModules();
60 assertEquals(4, testedModules.size());
64 public void testIPVersion() {
65 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
66 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
67 assertEquals(14, typedefs.size());
69 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
70 assertTrue(type.getDescription().contains("This value represents the version of the IP protocol."));
71 assertTrue(type.getReference().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, (int) value0.getValue());
80 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
82 EnumPair value1 = values.get(1);
83 assertEquals("ipv4", value1.getName());
84 assertEquals(1, (int) value1.getValue());
85 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
87 EnumPair value2 = values.get(2);
88 assertEquals("ipv6", value2.getName());
89 assertEquals(2, (int) value2.getValue());
90 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
94 public void testEnumeration() {
95 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
96 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
98 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
99 EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
100 List<EnumPair> values = enumType.getValues();
101 assertEquals(4, values.size());
103 EnumPair value0 = values.get(0);
104 assertEquals("unknown", value0.getName());
105 assertEquals(0, (int) value0.getValue());
106 assertEquals("An unknown or unspecified version of the Internet protocol.", value0.getDescription());
108 EnumPair value1 = values.get(1);
109 assertEquals("ipv4", value1.getName());
110 assertEquals(19, (int) value1.getValue());
111 assertEquals("The IPv4 protocol as defined in RFC 791.", value1.getDescription());
113 EnumPair value2 = values.get(2);
114 assertEquals("ipv6", value2.getName());
115 assertEquals(7, (int) value2.getValue());
116 assertEquals("The IPv6 protocol as defined in RFC 2460.", value2.getDescription());
118 EnumPair value3 = values.get(3);
119 assertEquals("default", value3.getName());
120 assertEquals(20, (int) value3.getValue());
121 assertEquals("default ip", value3.getDescription());
125 public void testIpAddress() {
126 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
127 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
128 TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
129 UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
130 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
132 StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
133 assertNotNull(ipv4.getBaseType());
134 String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
135 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
136 assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression());
138 StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
139 assertNotNull(ipv6.getBaseType());
140 List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
141 expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
142 + "((([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}"
143 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
144 assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression());
146 expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
148 assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression());
152 public void testDomainName() {
153 Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
154 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
155 StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
156 assertNotNull(type.getBaseType());
157 List<PatternConstraint> patterns = type.getPatternConstraints();
158 assertEquals(1, patterns.size());
159 String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
160 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
161 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
163 List<LengthConstraint> lengths = type.getLengthConstraints();
164 assertEquals(1, lengths.size());
165 LengthConstraint length = type.getLengthConstraints().get(0);
166 assertEquals(Integer.valueOf(1), length.getMin());
167 assertEquals(Integer.valueOf(253), length.getMax());
171 public void testInstanceIdentifier1() {
172 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
173 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
174 QName.create(tested.getQNameModule(), "inst-id-leaf1"));
175 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
176 assertFalse(leafType.requireInstance());
177 assertEquals(1, leaf.getUnknownSchemaNodes().size());
181 public void testInstanceIdentifier2() {
182 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
183 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
184 QName.create(tested.getQNameModule(), "inst-id-leaf2"));
185 InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
186 assertFalse(leafType.requireInstance());
190 public void testIdentity() {
191 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
192 Set<IdentitySchemaNode> identities = tested.getIdentities();
193 assertEquals(5, identities.size());
194 IdentitySchemaNode cryptoAlg = null;
195 IdentitySchemaNode cryptoBase = null;
196 IdentitySchemaNode cryptoId = null;
197 for (IdentitySchemaNode id : identities) {
198 if (id.getQName().getLocalName().equals("crypto-alg")) {
200 } else if ("crypto-base".equals(id.getQName().getLocalName())) {
202 } else if ("crypto-id".equals(id.getQName().getLocalName())) {
206 assertNotNull(cryptoAlg);
207 IdentitySchemaNode baseIdentity = cryptoAlg.getBaseIdentity();
208 assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
209 assertTrue(cryptoAlg.getDerivedIdentities().isEmpty());
210 assertNull(baseIdentity.getBaseIdentity());
212 assertNotNull(cryptoBase);
213 assertNull(cryptoBase.getBaseIdentity());
214 assertEquals(3, cryptoBase.getDerivedIdentities().size());
216 assertNotNull(cryptoId);
217 assertEquals(1, cryptoId.getUnknownSchemaNodes().size());
221 public void testBitsType1() {
222 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
223 LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
224 QName.create(tested.getQNameModule(), "mybits"));
225 BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
226 List<Bit> bits = leafType.getBits();
227 assertEquals(3, bits.size());
229 Bit bit1 = bits.get(0);
230 assertEquals("disable-nagle", bit1.getName());
231 assertEquals(0L, (long) bit1.getPosition());
233 Bit bit2 = bits.get(1);
234 assertEquals("auto-sense-speed", bit2.getName());
235 assertEquals(1L, (long) bit2.getPosition());
237 Bit bit3 = bits.get(2);
238 assertEquals("10-Mb-only", bit3.getName());
239 assertEquals(2L, (long) bit3.getPosition());
243 public void testBitsType2() {
244 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
245 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
246 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
248 BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
249 List<Bit> bits = bitsType.getBits();
250 assertEquals(5, bits.size());
252 Bit bit0 = bits.get(0);
253 assertEquals(0L, (long) bit0.getPosition());
255 Bit bit1 = bits.get(1);
256 assertEquals(500L, (long) bit1.getPosition());
258 Bit bit2 = bits.get(2);
259 assertEquals(501L, (long) bit2.getPosition());
261 Bit bit3 = bits.get(3);
262 assertEquals(502L, (long) bit3.getPosition());
264 Bit bit4 = bits.get(4);
265 assertEquals(365L, (long) bit4.getPosition());
269 public void testIanaTimezones() {
270 Module tested = TestUtils.findModule(testedModules, "iana-timezones");
271 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
272 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
274 String expectedDesc = "A timezone location as defined by the IANA timezone";
275 assertTrue(testedType.getDescription().contains(expectedDesc));
276 assertNull(testedType.getReference());
277 assertEquals(Status.CURRENT, testedType.getStatus());
279 QName testedTypeQName = testedType.getQName();
280 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
281 assertEquals(TestUtils.createDate("2012-07-09"), testedTypeQName.getRevision());
282 assertEquals("iana-timezone", testedTypeQName.getLocalName());
284 EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
285 List<EnumPair> values = enumType.getValues();
286 assertEquals(415, values.size()); // 0-414
288 EnumPair enum168 = values.get(168);
289 assertEquals("America/Danmarkshavn", enum168.getName());
290 assertEquals(168, (int) enum168.getValue());
291 assertEquals("east coast, north of Scoresbysund", enum168.getDescription());
293 EnumPair enum374 = values.get(374);
294 assertEquals("America/Indiana/Winamac", enum374.getName());
295 assertEquals(374, (int) enum374.getValue());
296 assertEquals("Eastern Time - Indiana - Pulaski County", enum374.getDescription());
300 public void testObjectId128() {
301 Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
302 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
303 StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
304 "object-identifier-128");
306 List<PatternConstraint> patterns = testedType.getPatternConstraints();
307 assertEquals(1, patterns.size());
308 PatternConstraint pattern = patterns.get(0);
309 assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression());
311 QName testedTypeQName = testedType.getQName();
312 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
313 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeQName.getRevision());
314 assertEquals("object-identifier-128", testedTypeQName.getLocalName());
316 StringTypeDefinition testedTypeBase = testedType.getBaseType();
317 patterns = testedTypeBase.getPatternConstraints();
318 assertEquals(1, patterns.size());
320 pattern = patterns.get(0);
321 assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
322 pattern.getRegularExpression());
324 QName testedTypeBaseQName = testedTypeBase.getQName();
325 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeBaseQName.getNamespace());
326 assertEquals(TestUtils.createDate("2010-09-24"), testedTypeBaseQName.getRevision());
327 assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
331 public void testIdentityref() {
332 Module tested = TestUtils.findModule(testedModules, "custom-types-test");
333 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
334 TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
335 IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
336 QName identity = baseType.getIdentity().getQName();
337 assertEquals(URI.create("urn:custom.types.demo"), identity.getNamespace());
338 assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
339 assertEquals("service-type", identity.getLocalName());
341 LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
346 public void testUnionWithExt() throws ReactorException {
348 final YangStatementSourceImpl yangFile1 = new YangStatementSourceImpl("/types/union-with-ext/extdef.yang",
350 final YangStatementSourceImpl yangFile2 = new YangStatementSourceImpl("/types/union-with-ext/unionbug.yang",
352 final YangStatementSourceImpl yangFile3 = new YangStatementSourceImpl("/ietf/ietf-inet-types@2010-09-24.yang",
355 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
356 addSources(reactor, yangFile1, yangFile2, yangFile3);
358 final EffectiveSchemaContext result = reactor.buildEffective();
359 assertNotNull(result);
363 public void testUnionWithBits() throws ReactorException {
365 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
366 "/types/union-with-bits/union-bits-model.yang", false);
368 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
369 addSources(reactor, yangFile);
371 final EffectiveSchemaContext result = reactor.buildEffective();
372 assertNotNull(result);
376 public void testUnionInList() {
377 final YangStatementSourceImpl yangFile = new YangStatementSourceImpl(
378 "/types/union-in-list/unioninlisttest.yang", false);
380 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
381 addSources(reactor, yangFile);
384 final EffectiveSchemaContext result = reactor.buildEffective();
385 fail("effective build should fail due to union in list; this is not allowed");
386 } catch (Exception e) {
387 assertEquals(IllegalArgumentException.class, e.getClass());
388 assertTrue(e.getMessage().startsWith("union is not a YANG statement or use of extension"));
392 private static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
393 for (YangStatementSourceImpl source : sources) {
394 reactor.addSource(source);