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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.