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