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