Merge "Fixed validation bug of YANG import statement"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / test / java / org / opendaylight / controller / sal / binding / yang / types / test / GeneratedTypesTest.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.sal.binding.yang.types.test;
9
10 import static org.junit.Assert.*;
11
12 import java.util.List;
13 import java.util.Set;
14
15 import org.junit.Ignore;
16 import org.junit.Test;
17 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
18 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
19 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
24 import org.opendaylight.controller.sal.binding.model.api.Type;
25 import org.opendaylight.controller.yang.model.api.Module;
26 import org.opendaylight.controller.yang.model.api.SchemaContext;
27 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
28 import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;
29
30 public class GeneratedTypesTest {
31
32     private SchemaContext resolveSchemaContextFromFiles(
33             final String... yangFiles) {
34         final YangModelParser parser = new YangModelParserImpl();
35         final Set<Module> modules = parser.parseYangModels(yangFiles);
36
37         return parser.resolveSchemaContext(modules);
38     }
39
40     @Test
41     public void testLeafEnumResolving() {
42         final String ietfInterfacesPath = getClass().getResource(
43                 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
44         final String ifTypePath = getClass().getResource(
45                 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
46         final String yangTypesPath = getClass().getResource(
47                 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
48
49         final SchemaContext context = resolveSchemaContextFromFiles(
50                 ietfInterfacesPath, ifTypePath, yangTypesPath);
51         assertTrue(context != null);
52         
53         final BindingGenerator bindingGen = new BindingGeneratorImpl();
54         final List<Type> genTypes = bindingGen.generateTypes(context);
55         assertTrue(genTypes != null);
56     }
57
58     @Test
59     public void testTypedefEnumResolving() {
60         final String ianaIfTypePath = getClass().getResource(
61                 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
62
63         final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
64         assertTrue(context != null);
65
66         final BindingGenerator bindingGen = new BindingGeneratorImpl();
67         final List<Type> genTypes = bindingGen.generateTypes(context);
68         assertTrue(genTypes != null);
69         assertEquals(1, genTypes.size());
70
71         final Type type = genTypes.get(0);
72         assertTrue(type instanceof GeneratedType);
73
74         final GeneratedType genType = (GeneratedType) type;
75         assertEquals(1, genType.getEnumDefintions().size());
76
77         final Enumeration enumer = genType.getEnumDefintions().get(0);
78         assertEquals(272, enumer.getValues().size());
79     }
80
81     @Test
82     public void testMultipleModulesResolving() {
83         final String topologyPath = getClass().getResource(
84                 "/abstract-topology.yang").getPath();
85         final String typesPath = getClass().getResource(
86                 "/ietf-inet-types@2010-09-24.yang").getPath();
87         final SchemaContext context = resolveSchemaContextFromFiles(
88                 topologyPath, typesPath);
89         assertTrue(context != null);
90
91         final BindingGenerator bindingGen = new BindingGeneratorImpl();
92         final List<Type> genTypes = bindingGen.generateTypes(context);
93
94         assertTrue(genTypes != null);
95         assertEquals(13, genTypes.size());
96     }
97
98     @Test
99     public void testLeafrefResolving() {
100         final String topologyPath = getClass().getResource(
101                 "/leafref-test-models/abstract-topology@2013-02-08.yang")
102                 .getPath();
103         final String interfacesPath = getClass().getResource(
104                 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
105                 .getPath();
106         // final String ifTypePath = getClass().getResource(
107         // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
108         final String inetTypesPath = getClass().getResource(
109                 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
110                 .getPath();
111         final String yangTypesPath = getClass().getResource(
112                 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
113                 .getPath();
114
115         assertTrue(topologyPath != null);
116         assertTrue(interfacesPath != null);
117         // assertTrue(ifTypePath != null);
118         assertTrue(inetTypesPath != null);
119         assertTrue(yangTypesPath != null);
120
121         // final SchemaContext context = resolveSchemaContextFromFiles(
122         // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
123         // yangTypesPath);
124         final SchemaContext context = resolveSchemaContextFromFiles(
125                 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
126         assertTrue(context != null);
127         assertEquals(4, context.getModules().size());
128
129         final BindingGenerator bindingGen = new BindingGeneratorImpl();
130         final List<Type> genTypes = bindingGen.generateTypes(context);
131
132         assertEquals(25, genTypes.size());
133         assertTrue(genTypes != null);
134
135         int resolvedLeafrefCount = 0;
136         for (final Type type : genTypes) {
137             if (type.getName().equals("InterfaceKey")
138                     && type instanceof GeneratedTransferObject) {
139                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
140                 final List<GeneratedProperty> properties = genTO
141                         .getProperties();
142
143                 assertTrue(properties != null);
144                 for (final GeneratedProperty property : properties) {
145                     if (property.getName().equals("InterfaceId")) {
146                         assertTrue(property.getReturnType() != null);
147                         assertFalse(property.getReturnType().equals(
148                                 "java.lang.Void"));
149                         assertTrue(property.getReturnType().getName()
150                                 .equals("String"));
151                         resolvedLeafrefCount++;
152                     }
153                 }
154
155             } else if (type.getName().equals("Interface")
156                     && type instanceof GeneratedType) {
157                 final GeneratedType genType = (GeneratedType) type;
158                 final List<MethodSignature> methods = genType
159                         .getMethodDefinitions();
160
161                 assertTrue(methods != null);
162                 for (final MethodSignature method : methods) {
163                     if (method.getName().equals("getInterfaceKey")) {
164                         assertTrue(method.getReturnType() != null);
165                         assertFalse(method.getReturnType().equals(
166                                 "java.lang.Void"));
167                         assertTrue(method.getReturnType().getName()
168                                 .equals("InterfaceKey"));
169                         resolvedLeafrefCount++;
170                     } else if (method.getName().equals("getHigherLayerIf")) {
171                         assertTrue(method.getReturnType() != null);
172                         assertFalse(method.getReturnType().equals(
173                                 "java.lang.Void"));
174                         assertTrue(method.getReturnType().getName()
175                                 .equals("List"));
176                         resolvedLeafrefCount++;
177                     }
178                 }
179             } else if (type.getName().equals("NetworkLink")
180                     && type instanceof GeneratedType) {
181                 final GeneratedType genType = (GeneratedType) type;
182                 final List<MethodSignature> methods = genType
183                         .getMethodDefinitions();
184                 assertTrue(methods != null);
185                 for (MethodSignature method : methods) {
186                     if (method.getName().equals("getInterface")) {
187                         assertTrue(method.getReturnType() != null);
188                         assertFalse(method.getReturnType().equals(
189                                 "java.lang.Void"));
190                         assertTrue(method.getReturnType().getName()
191                                 .equals("String"));
192                         resolvedLeafrefCount++;
193                     }
194                 }
195             } else if ((type.getName().equals("SourceNode") || type.getName()
196                     .equals("DestinationNode"))
197                     && type instanceof GeneratedType) {
198                 final GeneratedType genType = (GeneratedType) type;
199                 final List<MethodSignature> methods = genType
200                         .getMethodDefinitions();
201                 assertTrue(methods != null);
202                 for (MethodSignature method : methods) {
203                     if (method.getName().equals("getId")) {
204                         assertTrue(method.getReturnType() != null);
205                         assertFalse(method.getReturnType().equals(
206                                 "java.lang.Void"));
207                         assertTrue(method.getReturnType().getName()
208                                 .equals("String"));
209                         resolvedLeafrefCount++;
210                     }
211                 }
212             } else if (type.getName().equals("Tunnel")
213                     && type instanceof GeneratedType) {
214                 final GeneratedType genType = (GeneratedType) type;
215                 final List<MethodSignature> methods = genType
216                         .getMethodDefinitions();
217                 assertTrue(methods != null);
218                 for (MethodSignature method : methods) {
219                     if (method.getName().equals("getTunnelKey")) {
220                         assertTrue(method.getReturnType() != null);
221                         assertFalse(method.getReturnType().equals(
222                                 "java.lang.Void"));
223                         assertTrue(method.getReturnType().getName()
224                                 .equals("TunnelKey"));
225                         resolvedLeafrefCount++;
226                     }
227                 }
228             } else if (type.getName().equals("TunnelKey")
229                     && type instanceof GeneratedTransferObject) {
230                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
231                 final List<GeneratedProperty> properties = genTO
232                         .getProperties();
233
234                 assertTrue(properties != null);
235                 for (final GeneratedProperty property : properties) {
236                     if (property.getName().equals("TunnelId")) {
237                         assertTrue(property.getReturnType() != null);
238                         assertFalse(property.getReturnType().equals(
239                                 "java.lang.Void"));
240                         assertTrue(property.getReturnType().getName()
241                                 .equals("String"));
242                         resolvedLeafrefCount++;
243                     }
244                 }
245             }
246         }
247         assertEquals(10, resolvedLeafrefCount);
248     }
249
250     @Ignore
251     @Test
252     public void testContainerResolving() {
253         final String filePath = getClass().getResource(
254                 "/simple-container-demo.yang").getPath();
255         final SchemaContext context = resolveSchemaContextFromFiles(filePath);
256         assertTrue(context != null);
257
258         final BindingGenerator bindingGen = new BindingGeneratorImpl();
259         final List<Type> genTypes = bindingGen.generateTypes(context);
260
261         assertTrue(genTypes != null);
262         assertEquals(3, genTypes.size());
263
264         final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
265         final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
266
267         assertEquals("SimpleContainer", simpleContainer.getName());
268         assertEquals("NestedContainer", nestedContainer.getName());
269         assertEquals(4, simpleContainer.getMethodDefinitions().size());
270         assertEquals(4, nestedContainer.getMethodDefinitions().size());
271
272         int methodsCount = 0;
273         for (final MethodSignature method : simpleContainer
274                 .getMethodDefinitions()) {
275             if (method.getName().equals("getFoo")) {
276                 method.getReturnType().getName().equals("Integer");
277                 methodsCount++;
278             }
279
280             if (method.getName().equals("setFoo")) {
281                 methodsCount++;
282                 final MethodSignature.Parameter param = method.getParameters()
283                         .get(0);
284                 assertEquals("foo", param.getName());
285                 assertEquals("Integer", param.getType().getName());
286             }
287
288             if (method.getName().equals("getBar")) {
289                 method.getReturnType().getName().equals("String");
290                 methodsCount++;
291             }
292
293             if (method.getName().equals("getNestedContainer")) {
294                 method.getReturnType().getName().equals("NestedContainer");
295                 methodsCount++;
296             }
297         }
298         assertEquals(4, methodsCount);
299
300         methodsCount = 0;
301         for (final MethodSignature method : nestedContainer
302                 .getMethodDefinitions()) {
303             if (method.getName().equals("getFoo")) {
304                 method.getReturnType().getName().equals("Short");
305                 methodsCount++;
306             }
307
308             if (method.getName().equals("setFoo")) {
309                 methodsCount++;
310                 final MethodSignature.Parameter param = method.getParameters()
311                         .get(0);
312                 assertEquals("foo", param.getName());
313                 assertEquals("Short", param.getType().getName());
314             }
315
316             if (method.getName().equals("getBar")) {
317                 method.getReturnType().getName().equals("String");
318                 methodsCount++;
319             }
320
321             if (method.getName().equals("setBar")) {
322                 method.getReturnType().getName().equals("String");
323                 methodsCount++;
324             }
325         }
326         assertEquals(4, methodsCount);
327     }
328
329     @Ignore
330     @Test
331     public void testLeafListResolving() {
332         final String filePath = getClass().getResource(
333                 "/simple-leaf-list-demo.yang").getPath();
334         final SchemaContext context = resolveSchemaContextFromFiles(filePath);
335         assertTrue(context != null);
336
337         final BindingGenerator bindingGen = new BindingGeneratorImpl();
338         final List<Type> genTypes = bindingGen.generateTypes(context);
339
340         assertTrue(genTypes != null);
341         assertEquals(3, genTypes.size());
342
343         final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
344         final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
345
346         assertEquals("SimpleContainer", simpleContainer.getName());
347         assertEquals("NestedContainer", nestedContainer.getName());
348         assertEquals(4, simpleContainer.getMethodDefinitions().size());
349         assertEquals(3, nestedContainer.getMethodDefinitions().size());
350
351         int methodsCount = 0;
352         for (final MethodSignature method : simpleContainer
353                 .getMethodDefinitions()) {
354             if (method.getName().equals("getFoo")) {
355                 method.getReturnType().getName().equals("List");
356                 methodsCount++;
357             }
358
359             if (method.getName().equals("setFoo")) {
360                 methodsCount++;
361                 final MethodSignature.Parameter param = method.getParameters()
362                         .get(0);
363                 assertEquals("foo", param.getName());
364                 assertEquals("List", param.getType().getName());
365             }
366
367             if (method.getName().equals("getBar")) {
368                 method.getReturnType().getName().equals("String");
369                 methodsCount++;
370             }
371
372             if (method.getName().equals("getNestedContainer")) {
373                 method.getReturnType().getName().equals("NestedContainer");
374                 methodsCount++;
375             }
376         }
377         assertEquals(4, methodsCount);
378
379         methodsCount = 0;
380         for (final MethodSignature method : nestedContainer
381                 .getMethodDefinitions()) {
382             if (method.getName().equals("getFoo")) {
383                 method.getReturnType().getName().equals("Short");
384                 methodsCount++;
385             }
386
387             if (method.getName().equals("setFoo")) {
388                 methodsCount++;
389                 final MethodSignature.Parameter param = method.getParameters()
390                         .get(0);
391                 assertEquals("foo", param.getName());
392                 assertEquals("Short", param.getType().getName());
393             }
394
395             if (method.getName().equals("getBar")) {
396                 method.getReturnType().getName().equals("List");
397                 methodsCount++;
398             }
399         }
400         assertEquals(3, methodsCount);
401     }
402
403     @Ignore
404     @Test
405     public void testListResolving() {
406         final String filePath = getClass()
407                 .getResource("/simple-list-demo.yang").getPath();
408         final SchemaContext context = resolveSchemaContextFromFiles(filePath);
409         assertTrue(context != null);
410
411         final BindingGenerator bindingGen = new BindingGeneratorImpl();
412         final List<Type> genTypes = bindingGen.generateTypes(context);
413
414         assertTrue(genTypes != null);
415         assertEquals(5, genTypes.size());
416
417         int genTypesCount = 0;
418         int genTOsCount = 0;
419         for (final Type type : genTypes) {
420             if (type instanceof GeneratedType) {
421                 final GeneratedType genType = (GeneratedType) type;
422                 if (genType.getName().equals("ListParentContainer")) {
423                     assertEquals(2, genType.getMethodDefinitions().size());
424                     genTypesCount++;
425                 } else if (genType.getName().equals("SimpleList")) {
426                     assertEquals(7, genType.getMethodDefinitions().size());
427                     final List<MethodSignature> methods = genType
428                             .getMethodDefinitions();
429                     int methodsCount = 0;
430                     for (final MethodSignature method : methods) {
431                         if (method.getName().equals("getSimpleListKey")) {
432                             assertEquals("SimpleListKey", method
433                                     .getReturnType().getName());
434                             methodsCount++;
435                         } else if (method.getName().equals(
436                                 "getListChildContainer")) {
437                             assertEquals("ListChildContainer", method
438                                     .getReturnType().getName());
439                             methodsCount++;
440                         } else if (method.getName().equals("getFoo")) {
441                             methodsCount++;
442                         } else if (method.getName().equals("setFoo")) {
443                             methodsCount++;
444                         } else if (method.getName().equals("getSimpleLeafList")) {
445                             methodsCount++;
446                         } else if (method.getName().equals("setSimpleLeafList")) {
447                             methodsCount++;
448                         } else if (method.getName().equals("getBar")) {
449                             methodsCount++;
450                         }
451                     }
452                     assertEquals(7, methodsCount);
453                     genTypesCount++;
454                 } else if (genType.getName().equals("ListChildContainer")) {
455                     assertEquals(2, genType.getMethodDefinitions().size());
456                     genTypesCount++;
457                 }
458             } else if (type instanceof GeneratedTransferObject) {
459                 genTOsCount++;
460                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
461                 final List<GeneratedProperty> properties = genTO
462                         .getProperties();
463                 final List<GeneratedProperty> hashProps = genTO
464                         .getHashCodeIdentifiers();
465                 final List<GeneratedProperty> equalProps = genTO
466                         .getEqualsIdentifiers();
467
468                 assertEquals(1, properties.size());
469                 assertEquals("ListKey", properties.get(0).getName());
470                 assertEquals("Byte", properties.get(0).getReturnType()
471                         .getName());
472                 assertEquals(true, properties.get(0).isReadOnly());
473                 assertEquals(1, hashProps.size());
474                 assertEquals("ListKey", hashProps.get(0).getName());
475                 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
476                 assertEquals(1, equalProps.size());
477                 assertEquals("ListKey", equalProps.get(0).getName());
478                 assertEquals("Byte", equalProps.get(0).getReturnType()
479                         .getName());
480             }
481         }
482         assertEquals(3, genTypesCount);
483         assertEquals(1, genTOsCount);
484     }
485
486     @Test
487     public void testListCompositeKeyResolving() {
488         final String filePath = getClass().getResource(
489                 "/list-composite-key.yang").getPath();
490         final SchemaContext context = resolveSchemaContextFromFiles(filePath);
491
492         assertTrue(context != null);
493
494         final BindingGenerator bindingGen = new BindingGeneratorImpl();
495         final List<Type> genTypes = bindingGen.generateTypes(context);
496
497         assertTrue(genTypes != null);
498         assertEquals(7, genTypes.size());
499
500         int genTypesCount = 0;
501         int genTOsCount = 0;
502         for (final Type type : genTypes) {
503             if (type instanceof GeneratedType) {
504                 genTypesCount++;
505             } else if (type instanceof GeneratedTransferObject) {
506                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
507
508                 if (genTO.getName().equals("CompositeKeyListKey")) {
509                     final List<GeneratedProperty> properties = genTO
510                             .getProperties();
511                     int propertyCount = 0;
512                     for (final GeneratedProperty prop : properties) {
513                         if (prop.getName().equals("Key1")) {
514                             propertyCount++;
515                         } else if (prop.getName().equals("Key2")) {
516                             propertyCount++;
517                         }
518                     }
519                     assertEquals(2, propertyCount);
520                     genTOsCount++;
521                 } else if (genTO.getName().equals("InnerListKey")) {
522                     final List<GeneratedProperty> properties = genTO
523                             .getProperties();
524                     assertEquals(1, properties.size());
525                     genTOsCount++;
526                 }
527             }
528         }
529
530         assertEquals(5, genTypesCount);
531         assertEquals(2, genTOsCount);
532     }
533
534     @Test
535     public void testGeneratedTypes() {
536         final String filePath = getClass().getResource("/demo-topology.yang")
537                 .getPath();
538         final SchemaContext context = resolveSchemaContextFromFiles(filePath);
539         assertTrue(context != null);
540
541         final BindingGenerator bindingGen = new BindingGeneratorImpl();
542         final List<Type> genTypes = bindingGen.generateTypes(context);
543
544         assertTrue(genTypes != null);
545         assertEquals(14, genTypes.size());
546
547         int genTypesCount = 0;
548         int genTOsCount = 0;
549         for (final Type type : genTypes) {
550             if (type instanceof GeneratedType) {
551                 genTypesCount++;
552             } else if (type instanceof GeneratedTransferObject) {
553                 genTOsCount++;
554             }
555         }
556
557         assertEquals(11, genTypesCount);
558         assertEquals(3, genTOsCount);
559     }
560 }