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