Refactored YANG types resolving.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / model / parser / impl / TypesResolutionTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.yang.model.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.io.File;
13 import java.util.List;
14 import java.util.Set;
15
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;
33
34 public class TypesResolutionTest {
35
36     private Set<Module> testedModules;
37
38     @Before
39     public void init() {
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();
48         }
49         testedModules = parser.parseYangModels(testFiles);
50         assertEquals(fileList.length, testedModules.size());
51     }
52
53     @Test
54     public void testIPVersion() {
55         Module tested = findModule(testedModules, "ietf-inet-types");
56         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
57         assertEquals(14, typedefs.size());
58
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"));
64
65         EnumerationType enumType = (EnumerationType) type.getBaseType();
66         List<EnumPair> values = enumType.getValues();
67         assertEquals(3, values.size());
68
69         EnumPair value0 = values.get(0);
70         assertEquals("unknown", value0.getName());
71         assertEquals(0, (int) value0.getValue());
72         assertEquals(
73                 "An unknown or unspecified version of the Internet protocol.",
74                 value0.getDescription());
75
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());
81
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());
87     }
88
89     @Test
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();
96
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());
104
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());
115
116         expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
117                 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
118         assertEquals(expectedPattern, ipv6Patterns.get(1)
119                 .getRegularExpression());
120     }
121
122     @Test
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]\\.?)"
132                 + "|\\.";
133         assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
134
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());
140     }
141
142     @Test
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());
149     }
150
151     @Test
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());
158     }
159
160     @Test
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")) {
167                 testedIdentity = id;
168                 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
169                 assertEquals("crypto-base", baseIdentity.getQName()
170                         .getLocalName());
171                 assertNull(baseIdentity.getBaseIdentity());
172             }
173         }
174         assertNotNull(testedIdentity);
175     }
176
177     @Test
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());
185
186         Bit bit1 = bits.get(0);
187         assertEquals("disable-nagle", bit1.getName());
188         assertEquals(0L, (long) bit1.getPosition());
189
190         Bit bit2 = bits.get(1);
191         assertEquals("auto-sense-speed", bit2.getName());
192         assertEquals(1L, (long) bit2.getPosition());
193
194         Bit bit3 = bits.get(2);
195         assertEquals("10-Mb-only", bit3.getName());
196         assertEquals(2L, (long) bit3.getPosition());
197     }
198
199     @Test
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");
205
206         BitsType bitsType = (BitsType) testedType.getBaseType();
207         List<Bit> bits = bitsType.getBits();
208         assertEquals(5, bits.size());
209
210         Bit bit0 = bits.get(0);
211         assertEquals(0L, (long) bit0.getPosition());
212
213         Bit bit1 = bits.get(1);
214         assertEquals(500L, (long) bit1.getPosition());
215
216         Bit bit2 = bits.get(2);
217         assertEquals(501L, (long) bit2.getPosition());
218
219         Bit bit3 = bits.get(3);
220         assertEquals(365L, (long) bit3.getPosition());
221
222         Bit bit4 = bits.get(4);
223         assertEquals(502L, (long) bit4.getPosition());
224     }
225
226     private Module findModule(Set<Module> modules, String name) {
227         Module result = null;
228         for (Module module : modules) {
229             if (module.getName().equals(name)) {
230                 result = module;
231                 break;
232             }
233         }
234         return result;
235     }
236
237     private TypeDefinition<?> findTypedef(Set<TypeDefinition<?>> typedefs,
238             String name) {
239         TypeDefinition<?> result = null;
240         for (TypeDefinition<?> td : typedefs) {
241             if (td.getQName().getLocalName().equals(name)) {
242                 result = td;
243                 break;
244             }
245         }
246         return result;
247     }
248
249 }