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