2 * Copyright (c) 2013 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.controller.yang.model.parser.impl;
10 import static org.junit.Assert.*;
13 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
19 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.controller.yang.model.api.Module;
21 import org.opendaylight.controller.yang.model.api.TypeDefinition;
22 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;
23 import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair;
24 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
25 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
26 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
27 import org.opendaylight.controller.yang.model.util.BitsType;
28 import org.opendaylight.controller.yang.model.util.EnumerationType;
29 import org.opendaylight.controller.yang.model.util.ExtendedType;
30 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
31 import org.opendaylight.controller.yang.model.util.StringType;
32 import org.opendaylight.controller.yang.model.util.UnionType;
34 public class TypesResolutionTest {
36 private Set<Module> testedModules;
40 YangModelParser parser = new YangModelParserImpl();
41 File testDir = new File("src/test/resources/types");
42 String[] fileList = testDir.list();
43 String[] testFiles = new String[fileList.length];
44 for (int i = 0; i < fileList.length; i++) {
45 String fileName = fileList[i];
46 File file = new File(testDir, fileName);
47 testFiles[i] = file.getAbsolutePath();
49 testedModules = parser.parseYangModels(testFiles);
50 assertEquals(fileList.length, testedModules.size());
54 public void testIPVersion() {
55 Module tested = findModule(testedModules, "ietf-inet-types");
56 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
57 assertEquals(14, typedefs.size());
59 TypeDefinition<?> type = findTypedef(typedefs, "ip-version");
60 assertTrue(type.getDescription().contains(
61 "This value represents the version of the IP protocol."));
62 assertTrue(type.getReference().contains(
63 "RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
65 EnumerationType enumType = (EnumerationType) type.getBaseType();
66 List<EnumPair> values = enumType.getValues();
67 assertEquals(3, values.size());
69 EnumPair value0 = values.get(0);
70 assertEquals("unknown", value0.getName());
71 assertEquals(0, (int) value0.getValue());
73 "An unknown or unspecified version of the Internet protocol.",
74 value0.getDescription());
76 EnumPair value1 = values.get(1);
77 assertEquals("ipv4", value1.getName());
78 assertEquals(1, (int) value1.getValue());
79 assertEquals("The IPv4 protocol as defined in RFC 791.",
80 value1.getDescription());
82 EnumPair value2 = values.get(2);
83 assertEquals("ipv6", value2.getName());
84 assertEquals(2, (int) value2.getValue());
85 assertEquals("The IPv6 protocol as defined in RFC 2460.",
86 value2.getDescription());
90 public void testIpAddress() {
91 Module tested = findModule(testedModules, "ietf-inet-types");
92 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
93 TypeDefinition<?> type = findTypedef(typedefs, "ip-address");
94 UnionType baseType = (UnionType) type.getBaseType();
95 List<TypeDefinition<?>> unionTypes = baseType.getTypes();
97 ExtendedType ipv4 = (ExtendedType)unionTypes.get(0);
98 StringType ipv4Base = (StringType) ipv4.getBaseType();
99 String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
100 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
101 + "(%[\\p{N}\\p{L}]+)?";
102 assertEquals(expectedPattern, ipv4Base.getPatterns().get(0)
103 .getRegularExpression());
105 ExtendedType ipv6 = (ExtendedType)unionTypes.get(1);
106 StringType ipv6Base = (StringType) ipv6.getBaseType();
107 List<PatternConstraint> ipv6Patterns = ipv6Base.getPatterns();
108 expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
109 + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|"
110 + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
111 + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))"
112 + "(%[\\p{N}\\p{L}]+)?";
113 assertEquals(expectedPattern, ipv6Patterns.get(0)
114 .getRegularExpression());
116 expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
117 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
118 assertEquals(expectedPattern, ipv6Patterns.get(1)
119 .getRegularExpression());
123 public void testDomainName() {
124 Module tested = findModule(testedModules, "ietf-inet-types");
125 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
126 TypeDefinition<?> type = findTypedef(typedefs, "domain-name");
127 StringType baseType = (StringType) type.getBaseType();
128 List<PatternConstraint> patterns = baseType.getPatterns();
129 assertEquals(1, patterns.size());
130 String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
131 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)"
133 assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
135 List<LengthConstraint> lengths = baseType.getLengthStatements();
136 assertEquals(1, lengths.size());
137 LengthConstraint length = baseType.getLengthStatements().get(0);
138 assertEquals(1L, length.getMin());
139 assertEquals(253L, length.getMax());
143 public void testInstanceIdentifier1() {
144 Module tested = findModule(testedModules, "custom-types-test");
145 LeafSchemaNode leaf = (LeafSchemaNode) tested
146 .getDataChildByName("inst-id-leaf1");
147 InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
148 assertFalse(leafType.requireInstance());
152 public void testInstanceIdentifier2() {
153 Module tested = findModule(testedModules, "custom-types-test");
154 LeafSchemaNode leaf = (LeafSchemaNode) tested
155 .getDataChildByName("inst-id-leaf2");
156 InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
157 assertTrue(leafType.requireInstance());
161 public void testIdentity() {
162 Module tested = findModule(testedModules, "custom-types-test");
163 Set<IdentitySchemaNode> identities = tested.getIdentities();
164 IdentitySchemaNode testedIdentity = null;
165 for (IdentitySchemaNode id : identities) {
166 if (id.getQName().getLocalName().equals("crypto-alg")) {
168 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
169 assertEquals("crypto-base", baseIdentity.getQName()
171 assertNull(baseIdentity.getBaseIdentity());
174 assertNotNull(testedIdentity);
178 public void testBitsType1() {
179 Module tested = findModule(testedModules, "custom-types-test");
180 LeafSchemaNode leaf = (LeafSchemaNode) tested
181 .getDataChildByName("mybits");
182 BitsType leafType = (BitsType) leaf.getType();
183 List<Bit> bits = leafType.getBits();
184 assertEquals(3, bits.size());
186 Bit bit1 = bits.get(0);
187 assertEquals("disable-nagle", bit1.getName());
188 assertEquals(0L, (long) bit1.getPosition());
190 Bit bit2 = bits.get(1);
191 assertEquals("auto-sense-speed", bit2.getName());
192 assertEquals(1L, (long) bit2.getPosition());
194 Bit bit3 = bits.get(2);
195 assertEquals("10-Mb-only", bit3.getName());
196 assertEquals(2L, (long) bit3.getPosition());
200 public void testBitsType2() {
201 Module tested = findModule(testedModules, "custom-types-test");
202 Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
203 TypeDefinition<?> testedType = findTypedef(typedefs,
204 "access-operations-type");
206 BitsType bitsType = (BitsType) testedType.getBaseType();
207 List<Bit> bits = bitsType.getBits();
208 assertEquals(5, bits.size());
210 Bit bit0 = bits.get(0);
211 assertEquals(0L, (long) bit0.getPosition());
213 Bit bit1 = bits.get(1);
214 assertEquals(500L, (long) bit1.getPosition());
216 Bit bit2 = bits.get(2);
217 assertEquals(501L, (long) bit2.getPosition());
219 Bit bit3 = bits.get(3);
220 assertEquals(365L, (long) bit3.getPosition());
222 Bit bit4 = bits.get(4);
223 assertEquals(502L, (long) bit4.getPosition());
226 private Module findModule(Set<Module> modules, String name) {
227 Module result = null;
228 for (Module module : modules) {
229 if (module.getName().equals(name)) {
237 private TypeDefinition<?> findTypedef(Set<TypeDefinition<?>> typedefs,
239 TypeDefinition<?> result = null;
240 for (TypeDefinition<?> td : typedefs) {
241 if (td.getQName().getLocalName().equals(name)) {