Fixed a bug to block the creation of a static host on an ISL port, removed the code...
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / 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.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.io.FileNotFoundException;
13 import java.net.URI;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.yang.common.QName;
20 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
21 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.controller.yang.model.api.Module;
23 import org.opendaylight.controller.yang.model.api.Status;
24 import org.opendaylight.controller.yang.model.api.TypeDefinition;
25 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;
26 import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition.EnumPair;
27 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
28 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
29 import org.opendaylight.controller.yang.model.api.type.StringTypeDefinition;
30 import org.opendaylight.controller.yang.model.util.BitsType;
31 import org.opendaylight.controller.yang.model.util.EnumerationType;
32 import org.opendaylight.controller.yang.model.util.ExtendedType;
33 import org.opendaylight.controller.yang.model.util.IdentityrefType;
34 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
35 import org.opendaylight.controller.yang.model.util.UnionType;
36
37 public class TypesResolutionTest {
38     private Set<Module> testedModules;
39
40     @Before
41     public void init() throws FileNotFoundException {
42         testedModules = TestUtils.loadModules(getClass().getResource
43                 ("/types").getPath());
44     }
45
46     @Test
47     public void testIPVersion() {
48         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
49         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
50         assertEquals(14, typedefs.size());
51
52         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
53         assertTrue(type.getDescription().contains(
54                 "This value represents the version of the IP protocol."));
55         assertTrue(type.getReference().contains(
56                 "RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
57
58         EnumerationType enumType = (EnumerationType) type.getBaseType();
59         List<EnumPair> values = enumType.getValues();
60         assertEquals(3, values.size());
61
62         EnumPair value0 = values.get(0);
63         assertEquals("unknown", value0.getName());
64         assertEquals(0, (int) value0.getValue());
65         assertEquals(
66                 "An unknown or unspecified version of the Internet protocol.",
67                 value0.getDescription());
68
69         EnumPair value1 = values.get(1);
70         assertEquals("ipv4", value1.getName());
71         assertEquals(1, (int) value1.getValue());
72         assertEquals("The IPv4 protocol as defined in RFC 791.",
73                 value1.getDescription());
74
75         EnumPair value2 = values.get(2);
76         assertEquals("ipv6", value2.getName());
77         assertEquals(2, (int) value2.getValue());
78         assertEquals("The IPv6 protocol as defined in RFC 2460.",
79                 value2.getDescription());
80     }
81
82     @Test
83     public void testEnumeration() {
84         Module tested = TestUtils
85                 .findModule(testedModules, "custom-types-test");
86         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
87
88         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
89         EnumerationType enumType = (EnumerationType) type.getBaseType();
90         List<EnumPair> values = enumType.getValues();
91         assertEquals(4, values.size());
92
93         EnumPair value0 = values.get(0);
94         assertEquals("unknown", value0.getName());
95         assertEquals(0, (int) value0.getValue());
96         assertEquals(
97                 "An unknown or unspecified version of the Internet protocol.",
98                 value0.getDescription());
99
100         EnumPair value1 = values.get(1);
101         assertEquals("ipv4", value1.getName());
102         assertEquals(19, (int) value1.getValue());
103         assertEquals("The IPv4 protocol as defined in RFC 791.",
104                 value1.getDescription());
105
106         EnumPair value2 = values.get(2);
107         assertEquals("ipv6", value2.getName());
108         assertEquals(7, (int) value2.getValue());
109         assertEquals("The IPv6 protocol as defined in RFC 2460.",
110                 value2.getDescription());
111
112         EnumPair value3 = values.get(3);
113         assertEquals("default", value3.getName());
114         assertEquals(20, (int) value3.getValue());
115         assertEquals("default ip", value3.getDescription());
116     }
117
118     @Test
119     public void testIpAddress() {
120         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
121         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
122         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
123         UnionType baseType = (UnionType) type.getBaseType();
124         List<TypeDefinition<?>> unionTypes = baseType.getTypes();
125
126         ExtendedType ipv4 = (ExtendedType) unionTypes.get(0);
127         assertTrue(ipv4.getBaseType() instanceof StringTypeDefinition);
128         String expectedPattern = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
129                 + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
130                 + "(%[\\p{N}\\p{L}]+)?";
131         assertEquals(expectedPattern, ipv4.getPatterns().get(0)
132                 .getRegularExpression());
133
134         TypeDefinition<?> ipv4Address = TestUtils.findTypedef(typedefs, "ipv4-address");
135         assertEquals(ipv4Address, ipv4);
136
137         ExtendedType ipv6 = (ExtendedType) unionTypes.get(1);
138         assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition);
139         List<PatternConstraint> ipv6Patterns = ipv6.getPatterns();
140         expectedPattern = "((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
141                 + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|"
142                 + "(((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])))"
144                 + "(%[\\p{N}\\p{L}]+)?";
145         assertEquals(expectedPattern, ipv6Patterns.get(0)
146                 .getRegularExpression());
147
148         TypeDefinition<?> ipv6Address = TestUtils.findTypedef(typedefs, "ipv6-address");
149         assertEquals(ipv6Address, ipv6);
150
151         expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|"
152                 + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?";
153         assertEquals(expectedPattern, ipv6Patterns.get(1)
154                 .getRegularExpression());
155     }
156
157     @Test
158     public void testDomainName() {
159         Module tested = TestUtils.findModule(testedModules, "ietf-inet-types");
160         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
161         ExtendedType type = (ExtendedType)TestUtils.findTypedef(typedefs, "domain-name");
162         assertTrue(type.getBaseType() instanceof StringTypeDefinition);
163         List<PatternConstraint> patterns = type.getPatterns();
164         assertEquals(1, patterns.size());
165         String expectedPattern = "((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
166                 + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)"
167                 + "|\\.";
168         assertEquals(expectedPattern, patterns.get(0).getRegularExpression());
169
170         List<LengthConstraint> lengths = type.getLengths();
171         assertEquals(1, lengths.size());
172         LengthConstraint length = type.getLengths().get(0);
173         assertEquals(1L, length.getMin());
174         assertEquals(253L, length.getMax());
175     }
176
177     @Test
178     public void testInstanceIdentifier1() {
179         Module tested = TestUtils
180                 .findModule(testedModules, "custom-types-test");
181         LeafSchemaNode leaf = (LeafSchemaNode) tested
182                 .getDataChildByName("inst-id-leaf1");
183         ExtendedType leafType = (ExtendedType) leaf.getType();
184         InstanceIdentifier leafTypeBase = (InstanceIdentifier) leafType
185                 .getBaseType();
186         assertFalse(leafTypeBase.requireInstance());
187     }
188
189     @Test
190     public void testInstanceIdentifier2() {
191         Module tested = TestUtils
192                 .findModule(testedModules, "custom-types-test");
193         LeafSchemaNode leaf = (LeafSchemaNode) tested
194                 .getDataChildByName("inst-id-leaf2");
195         InstanceIdentifier leafType = (InstanceIdentifier) leaf.getType();
196         assertTrue(leafType.requireInstance());
197     }
198
199     @Test
200     public void testIdentity() {
201         Module tested = TestUtils
202                 .findModule(testedModules, "custom-types-test");
203         Set<IdentitySchemaNode> identities = tested.getIdentities();
204         IdentitySchemaNode testedIdentity = null;
205         for (IdentitySchemaNode id : identities) {
206             if (id.getQName().getLocalName().equals("crypto-alg")) {
207                 testedIdentity = id;
208                 IdentitySchemaNode baseIdentity = id.getBaseIdentity();
209                 assertEquals("crypto-base", baseIdentity.getQName()
210                         .getLocalName());
211                 assertNull(baseIdentity.getBaseIdentity());
212             }
213         }
214         assertNotNull(testedIdentity);
215     }
216
217     @Test
218     public void testBitsType1() {
219         Module tested = TestUtils
220                 .findModule(testedModules, "custom-types-test");
221         LeafSchemaNode leaf = (LeafSchemaNode) tested
222                 .getDataChildByName("mybits");
223         BitsType leafType = (BitsType) leaf.getType();
224         List<Bit> bits = leafType.getBits();
225         assertEquals(3, bits.size());
226
227         Bit bit1 = bits.get(0);
228         assertEquals("disable-nagle", bit1.getName());
229         assertEquals(0L, (long) bit1.getPosition());
230
231         Bit bit2 = bits.get(1);
232         assertEquals("auto-sense-speed", bit2.getName());
233         assertEquals(1L, (long) bit2.getPosition());
234
235         Bit bit3 = bits.get(2);
236         assertEquals("10-Mb-only", bit3.getName());
237         assertEquals(2L, (long) bit3.getPosition());
238     }
239
240     @Test
241     public void testBitsType2() {
242         Module tested = TestUtils
243                 .findModule(testedModules, "custom-types-test");
244         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
245         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
246                 "access-operations-type");
247
248         BitsType bitsType = (BitsType) testedType.getBaseType();
249         List<Bit> bits = bitsType.getBits();
250         assertEquals(5, bits.size());
251
252         Bit bit0 = bits.get(0);
253         assertEquals(0L, (long) bit0.getPosition());
254
255         Bit bit1 = bits.get(1);
256         assertEquals(500L, (long) bit1.getPosition());
257
258         Bit bit2 = bits.get(2);
259         assertEquals(501L, (long) bit2.getPosition());
260
261         Bit bit3 = bits.get(3);
262         assertEquals(365L, (long) bit3.getPosition());
263
264         Bit bit4 = bits.get(4);
265         assertEquals(502L, (long) bit4.getPosition());
266     }
267
268     @Test
269     public void testIanaTimezones() {
270         Module tested = TestUtils.findModule(testedModules, "iana-timezones");
271         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
272         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
273                 "iana-timezone");
274
275         String expectedDesc = "A timezone location as defined by the IANA timezone";
276         assertTrue(testedType.getDescription().contains(expectedDesc));
277         assertNull(testedType.getReference());
278         assertEquals(Status.CURRENT, testedType.getStatus());
279
280         QName testedTypeQName = testedType.getQName();
281         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:iana-timezones"),
282                 testedTypeQName.getNamespace());
283         assertEquals(TestUtils.createDate("2012-07-09"),
284                 testedTypeQName.getRevision());
285         assertEquals("ianatz", testedTypeQName.getPrefix());
286         assertEquals("iana-timezone", testedTypeQName.getLocalName());
287
288         EnumerationType enumType = (EnumerationType) testedType.getBaseType();
289         List<EnumPair> values = enumType.getValues();
290         assertEquals(415, values.size()); // 0-414
291
292         EnumPair enum168 = values.get(168);
293         assertEquals("America/Danmarkshavn", enum168.getName());
294         assertEquals(168, (int) enum168.getValue());
295         assertEquals("east coast, north of Scoresbysund",
296                 enum168.getDescription());
297
298         EnumPair enum374 = values.get(374);
299         assertEquals("America/Indiana/Winamac", enum374.getName());
300         assertEquals(374, (int) enum374.getValue());
301         assertEquals("Eastern Time - Indiana - Pulaski County",
302                 enum374.getDescription());
303     }
304
305     @Test
306     public void testObjectId128() {
307         Module tested = TestUtils.findModule(testedModules, "ietf-yang-types");
308         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
309         ExtendedType testedType = (ExtendedType) TestUtils.findTypedef(
310                 typedefs, "object-identifier-128");
311
312         List<PatternConstraint> patterns = testedType.getPatterns();
313         assertEquals(1, patterns.size());
314         PatternConstraint pattern = patterns.get(0);
315         assertEquals("\\d*(\\.\\d*){1,127}", pattern.getRegularExpression());
316
317         QName testedTypeQName = testedType.getQName();
318         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
319                 testedTypeQName.getNamespace());
320         assertEquals(TestUtils.createDate("2010-09-24"),
321                 testedTypeQName.getRevision());
322         assertEquals("yang", testedTypeQName.getPrefix());
323         assertEquals("object-identifier-128", testedTypeQName.getLocalName());
324
325         ExtendedType testedTypeBase = (ExtendedType) testedType.getBaseType();
326         patterns = testedTypeBase.getPatterns();
327         assertEquals(1, patterns.size());
328
329         pattern = patterns.get(0);
330         assertEquals(
331                 "(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*",
332                 pattern.getRegularExpression());
333
334         QName testedTypeBaseQName = testedTypeBase.getQName();
335         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
336                 testedTypeBaseQName.getNamespace());
337         assertEquals(TestUtils.createDate("2010-09-24"),
338                 testedTypeBaseQName.getRevision());
339         assertEquals("yang", testedTypeBaseQName.getPrefix());
340         assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
341     }
342
343     @Test
344     public void testIdentityref() {
345         Module tested = TestUtils
346                 .findModule(testedModules, "custom-types-test");
347         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
348         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs,
349                 "service-type-ref");
350         IdentityrefType baseType = (IdentityrefType) testedType.getBaseType();
351         QName identity = baseType.getIdentity();
352         assertEquals(URI.create("urn:simple.container.demo"),
353                 identity.getNamespace());
354         assertEquals(TestUtils.createDate("2012-04-16"), identity.getRevision());
355         assertEquals("iit", identity.getPrefix());
356         assertEquals("service-type", identity.getLocalName());
357     }
358
359 }