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