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