Update the API generation code and code generation sample
[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 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.binding.yang.types.test;\r
9 \r
10 import static org.junit.Assert.assertEquals;\r
11 import static org.junit.Assert.assertTrue;\r
12 \r
13 import java.util.List;\r
14 import java.util.Set;\r
15 \r
16 import org.junit.Test;\r
17 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;\r
18 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;\r
19 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;\r
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;\r
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;\r
22 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;\r
23 import org.opendaylight.controller.sal.binding.model.api.Type;\r
24 import org.opendaylight.controller.yang.model.api.Module;\r
25 import org.opendaylight.controller.yang.model.api.SchemaContext;\r
26 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;\r
27 import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;\r
28 \r
29 public class GeneratedTypesTest {\r
30 \r
31     private SchemaContext resolveSchemaContextFromFiles(\r
32             final String... yangFiles) {\r
33         final YangModelParser parser = new YangModelParserImpl();\r
34         final Set<Module> modules = parser.parseYangModels(yangFiles);\r
35 \r
36         return parser.resolveSchemaContext(modules);\r
37     }\r
38 \r
39     @Test\r
40     public void testMultipleModulesResolving() {\r
41         final String topologyPath = getClass().getResource(\r
42                 "/abstract-topology.yang").getPath();\r
43         final String typesPath = getClass().getResource(\r
44                 "/ietf-inet-types@2010-09-24.yang").getPath();\r
45         final SchemaContext context = resolveSchemaContextFromFiles(\r
46                 topologyPath, typesPath);\r
47         assertTrue(context != null);\r
48 \r
49         final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
50         final List<Type> genTypes = bindingGen.generateTypes(context);\r
51 \r
52         assertTrue(genTypes != null);\r
53         assertEquals(11, genTypes.size());\r
54     }\r
55     \r
56     @Test\r
57     public void testLeafrefResolving() {\r
58         final String topologyPath = getClass().getResource(\r
59                 "/leafref-test-models/abstract-topology@2013-02-08.yang")\r
60                 .getPath();\r
61         final String interfacesPath = getClass().getResource(\r
62                 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")\r
63                 .getPath();\r
64 //        final String ifTypePath = getClass().getResource(\r
65 //                "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();\r
66         final String inetTypesPath = getClass().getResource(\r
67                 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")\r
68                 .getPath();\r
69         final String yangTypesPath = getClass().getResource(\r
70                 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")\r
71                 .getPath();\r
72 \r
73         assertTrue(topologyPath != null);\r
74         assertTrue(interfacesPath != null);\r
75 //        assertTrue(ifTypePath != null);\r
76         assertTrue(inetTypesPath != null);\r
77         assertTrue(yangTypesPath != null);\r
78 \r
79 //        final SchemaContext context = resolveSchemaContextFromFiles(\r
80 //                topologyPath, interfacesPath, ifTypePath, inetTypesPath, yangTypesPath);\r
81         final SchemaContext context = resolveSchemaContextFromFiles(\r
82                 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);\r
83         assertTrue(context != null);\r
84         assertEquals(4, context.getModules().size());\r
85         \r
86         final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
87         final List<Type> genTypes = bindingGen.generateTypes(context);\r
88         \r
89         assertEquals(21, genTypes.size());\r
90         assertTrue(genTypes != null);\r
91         \r
92         for (final Type genType : genTypes) {\r
93             if (genType.getName().equals("Interface") && genType instanceof GeneratedType) {\r
94 //                System.out.println(((GeneratedType)genType).getMethodDefinitions().toString());\r
95             } else if (genType.getName().equals("NetworkLink") && genType instanceof GeneratedType) {\r
96 //                System.out.println(((GeneratedType)genType).getMethodDefinitions().toString());\r
97             } \r
98         }\r
99     }\r
100 \r
101     @Test\r
102     public void testContainerResolving() {\r
103         final String filePath = getClass().getResource(\r
104                 "/simple-container-demo.yang").getPath();\r
105         final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
106         assertTrue(context != null);\r
107 \r
108         final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
109         final List<Type> genTypes = bindingGen.generateTypes(context);\r
110 \r
111         assertTrue(genTypes != null);\r
112         assertEquals(2, genTypes.size());\r
113 \r
114         final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);\r
115         final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);\r
116 \r
117         assertEquals("SimpleContainer", simpleContainer.getName());\r
118         assertEquals("NestedContainer", nestedContainer.getName());\r
119         assertEquals(4, simpleContainer.getMethodDefinitions().size());\r
120         assertEquals(4, nestedContainer.getMethodDefinitions().size());\r
121 \r
122         int methodsCount = 0;\r
123         for (final MethodSignature method : simpleContainer\r
124                 .getMethodDefinitions()) {\r
125             if (method.getName().equals("getFoo")) {\r
126                 method.getReturnType().getName().equals("Integer");\r
127                 methodsCount++;\r
128             }\r
129 \r
130             if (method.getName().equals("setFoo")) {\r
131                 methodsCount++;\r
132                 final MethodSignature.Parameter param = method.getParameters()\r
133                         .get(0);\r
134                 assertEquals("foo", param.getName());\r
135                 assertEquals("Integer", param.getType().getName());\r
136             }\r
137 \r
138             if (method.getName().equals("getBar")) {\r
139                 method.getReturnType().getName().equals("String");\r
140                 methodsCount++;\r
141             }\r
142 \r
143             if (method.getName().equals("getNestedContainer")) {\r
144                 method.getReturnType().getName().equals("NestedContainer");\r
145                 methodsCount++;\r
146             }\r
147         }\r
148         assertEquals(4, methodsCount);\r
149 \r
150         methodsCount = 0;\r
151         for (final MethodSignature method : nestedContainer\r
152                 .getMethodDefinitions()) {\r
153             if (method.getName().equals("getFoo")) {\r
154                 method.getReturnType().getName().equals("Short");\r
155                 methodsCount++;\r
156             }\r
157 \r
158             if (method.getName().equals("setFoo")) {\r
159                 methodsCount++;\r
160                 final MethodSignature.Parameter param = method.getParameters()\r
161                         .get(0);\r
162                 assertEquals("foo", param.getName());\r
163                 assertEquals("Short", param.getType().getName());\r
164             }\r
165 \r
166             if (method.getName().equals("getBar")) {\r
167                 method.getReturnType().getName().equals("String");\r
168                 methodsCount++;\r
169             }\r
170 \r
171             if (method.getName().equals("setBar")) {\r
172                 method.getReturnType().getName().equals("String");\r
173                 methodsCount++;\r
174             }\r
175         }\r
176         assertEquals(4, methodsCount);\r
177     }\r
178 \r
179     @Test\r
180     public void testLeafListResolving() {\r
181         final String filePath = getClass().getResource(\r
182                 "/simple-leaf-list-demo.yang").getPath();\r
183         final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
184         assertTrue(context != null);\r
185 \r
186         final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
187         final List<Type> genTypes = bindingGen.generateTypes(context);\r
188 \r
189         assertTrue(genTypes != null);\r
190         assertEquals(2, genTypes.size());\r
191 \r
192         final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);\r
193         final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);\r
194 \r
195         assertEquals("SimpleContainer", simpleContainer.getName());\r
196         assertEquals("NestedContainer", nestedContainer.getName());\r
197         assertEquals(4, simpleContainer.getMethodDefinitions().size());\r
198         assertEquals(3, nestedContainer.getMethodDefinitions().size());\r
199 \r
200         int methodsCount = 0;\r
201         for (final MethodSignature method : simpleContainer\r
202                 .getMethodDefinitions()) {\r
203             if (method.getName().equals("getFoo")) {\r
204                 method.getReturnType().getName().equals("List");\r
205                 methodsCount++;\r
206             }\r
207 \r
208             if (method.getName().equals("setFoo")) {\r
209                 methodsCount++;\r
210                 final MethodSignature.Parameter param = method.getParameters()\r
211                         .get(0);\r
212                 assertEquals("foo", param.getName());\r
213                 assertEquals("List", param.getType().getName());\r
214             }\r
215 \r
216             if (method.getName().equals("getBar")) {\r
217                 method.getReturnType().getName().equals("String");\r
218                 methodsCount++;\r
219             }\r
220 \r
221             if (method.getName().equals("getNestedContainer")) {\r
222                 method.getReturnType().getName().equals("NestedContainer");\r
223                 methodsCount++;\r
224             }\r
225         }\r
226         assertEquals(4, methodsCount);\r
227 \r
228         methodsCount = 0;\r
229         for (final MethodSignature method : nestedContainer\r
230                 .getMethodDefinitions()) {\r
231             if (method.getName().equals("getFoo")) {\r
232                 method.getReturnType().getName().equals("Short");\r
233                 methodsCount++;\r
234             }\r
235 \r
236             if (method.getName().equals("setFoo")) {\r
237                 methodsCount++;\r
238                 final MethodSignature.Parameter param = method.getParameters()\r
239                         .get(0);\r
240                 assertEquals("foo", param.getName());\r
241                 assertEquals("Short", param.getType().getName());\r
242             }\r
243 \r
244             if (method.getName().equals("getBar")) {\r
245                 method.getReturnType().getName().equals("List");\r
246                 methodsCount++;\r
247             }\r
248         }\r
249         assertEquals(3, methodsCount);\r
250     }\r
251 \r
252     @Test\r
253     public void testListResolving() {\r
254         final String filePath = getClass()\r
255                 .getResource("/simple-list-demo.yang").getPath();\r
256         final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
257         assertTrue(context != null);\r
258 \r
259         final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
260         final List<Type> genTypes = bindingGen.generateTypes(context);\r
261 \r
262         assertTrue(genTypes != null);\r
263         assertEquals(4, genTypes.size());\r
264 \r
265         int genTypesCount = 0;\r
266         int genTOsCount = 0;\r
267         for (final Type type : genTypes) {\r
268             if (type instanceof GeneratedType) {\r
269                 final GeneratedType genType = (GeneratedType) type;\r
270                 if (genType.getName().equals("ListParentContainer")) {\r
271                     assertEquals(2, genType.getMethodDefinitions().size());\r
272                     genTypesCount++;\r
273                 } else if (genType.getName().equals("SimpleList")) {\r
274                     assertEquals(7, genType.getMethodDefinitions().size());\r
275                     final List<MethodSignature> methods = genType\r
276                             .getMethodDefinitions();\r
277                     int methodsCount = 0;\r
278                     for (final MethodSignature method : methods) {\r
279                         if (method.getName().equals("getSimpleListKey")) {\r
280                             assertEquals("SimpleListKey", method\r
281                                     .getReturnType().getName());\r
282                             methodsCount++;\r
283                         } else if (method.getName().equals(\r
284                                 "getListChildContainer")) {\r
285                             assertEquals("ListChildContainer", method\r
286                                     .getReturnType().getName());\r
287                             methodsCount++;\r
288                         } else if (method.getName().equals("getFoo")) {\r
289                             methodsCount++;\r
290                         } else if (method.getName().equals("setFoo")) {\r
291                             methodsCount++;\r
292                         } else if (method.getName().equals("getSimpleLeafList")) {\r
293                             methodsCount++;\r
294                         } else if (method.getName().equals("setSimpleLeafList")) {\r
295                             methodsCount++;\r
296                         } else if (method.getName().equals("getBar")) {\r
297                             methodsCount++;\r
298                         }\r
299                     }\r
300                     assertEquals(7, methodsCount);\r
301                     genTypesCount++;\r
302                 } else if (genType.getName().equals("ListChildContainer")) {\r
303                     assertEquals(2, genType.getMethodDefinitions().size());\r
304                     genTypesCount++;\r
305                 }\r
306             } else if (type instanceof GeneratedTransferObject) {\r
307                 genTOsCount++;\r
308                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;\r
309                 final List<GeneratedProperty> properties = genTO\r
310                         .getProperties();\r
311                 final List<GeneratedProperty> hashProps = genTO\r
312                         .getHashCodeIdentifiers();\r
313                 final List<GeneratedProperty> equalProps = genTO\r
314                         .getEqualsIdentifiers();\r
315 \r
316                 assertEquals(1, properties.size());\r
317                 assertEquals("ListKey", properties.get(0).getName());\r
318                 assertEquals("Byte", properties.get(0).getReturnType()\r
319                         .getName());\r
320                 assertEquals(true, properties.get(0).isReadOnly());\r
321                 assertEquals(1, hashProps.size());\r
322                 assertEquals("ListKey", hashProps.get(0).getName());\r
323                 assertEquals("Byte", hashProps.get(0).getReturnType().getName());\r
324                 assertEquals(1, equalProps.size());\r
325                 assertEquals("ListKey", equalProps.get(0).getName());\r
326                 assertEquals("Byte", equalProps.get(0).getReturnType()\r
327                         .getName());\r
328             }\r
329         }\r
330         assertEquals(3, genTypesCount);\r
331         assertEquals(1, genTOsCount);\r
332     }\r
333 \r
334     @Test\r
335     public void testListCompositeKeyResolving() {\r
336         final String filePath = getClass().getResource(\r
337                 "/list-composite-key.yang").getPath();\r
338         final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
339 \r
340         assertTrue(context != null);\r
341 \r
342         final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
343         final List<Type> genTypes = bindingGen.generateTypes(context);\r
344 \r
345         assertTrue(genTypes != null);\r
346         assertEquals(6, genTypes.size());\r
347 \r
348         int genTypesCount = 0;\r
349         int genTOsCount = 0;\r
350         for (final Type type : genTypes) {\r
351             if (type instanceof GeneratedType) {\r
352                 genTypesCount++;\r
353             } else if (type instanceof GeneratedTransferObject) {\r
354                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;\r
355 \r
356                 if (genTO.getName().equals("CompositeKeyListKey")) {\r
357                     final List<GeneratedProperty> properties = genTO\r
358                             .getProperties();\r
359                     int propertyCount = 0;\r
360                     for (final GeneratedProperty prop : properties) {\r
361                         if (prop.getName().equals("Key1")) {\r
362                             propertyCount++;\r
363                         } else if (prop.getName().equals("Key2")) {\r
364                             propertyCount++;\r
365                         }\r
366                     }\r
367                     assertEquals(2, propertyCount);\r
368                     genTOsCount++;\r
369                 } else if (genTO.getName().equals("InnerListKey")) {\r
370                     final List<GeneratedProperty> properties = genTO\r
371                             .getProperties();\r
372                     assertEquals(1, properties.size());\r
373                     genTOsCount++;\r
374                 }\r
375             }\r
376         }\r
377 \r
378         assertEquals(4, genTypesCount);\r
379         assertEquals(2, genTOsCount);\r
380     }\r
381 \r
382     @Test\r
383     public void testGeneratedTypes() {\r
384         final String filePath = getClass().getResource("/demo-topology.yang")\r
385                 .getPath();\r
386         final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
387         assertTrue(context != null);\r
388 \r
389         final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
390         final List<Type> genTypes = bindingGen.generateTypes(context);\r
391 \r
392         assertTrue(genTypes != null);\r
393         assertEquals(13, genTypes.size());\r
394 \r
395         int genTypesCount = 0;\r
396         int genTOsCount = 0;\r
397         for (final Type type : genTypes) {\r
398             if (type instanceof GeneratedType) {\r
399                 genTypesCount++;\r
400             } else if (type instanceof GeneratedTransferObject) {\r
401                 genTOsCount++;\r
402             }\r
403         }\r
404 \r
405         assertEquals(10, genTypesCount);\r
406         assertEquals(3, genTOsCount);\r
407     }\r
408 }\r

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.