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