When plugin receives error msg from the switch regarding a flow message sent to the...
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / test / java / org / opendaylight / controller / sal / binding / yang / types / test / GeneratedTypesTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.binding.yang.types.test;
9
10 import static org.junit.Assert.*;
11
12 import java.util.List;
13 import java.util.Set;
14
15 import org.junit.Ignore;
16 import org.junit.Test;
17 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
18 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
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.model.parser.impl.YangModelParserImpl;
29
30 public class GeneratedTypesTest {
31
32     private SchemaContext resolveSchemaContextFromFiles(
33             final String... yangFiles) {
34         final YangModelParser parser = new YangModelParserImpl();
35         final Set<Module> modules = parser.parseYangModels(yangFiles);
36
37         return parser.resolveSchemaContext(modules);
38     }
39
40     @Ignore
41     @Test
42     public void testLeafEnumResolving() {
43         final String ietfInterfacesPath = getClass().getResource(
44                 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
45         final String ifTypePath = getClass().getResource(
46                 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
47         final String yangTypesPath = getClass().getResource(
48                 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
49
50         final SchemaContext context = resolveSchemaContextFromFiles(
51                 ietfInterfacesPath, ifTypePath, yangTypesPath);
52         assertTrue(context != null);
53
54         final BindingGenerator bindingGen = new BindingGeneratorImpl();
55         final List<Type> genTypes = bindingGen.generateTypes(context);
56         assertTrue(genTypes != null);
57     }
58
59     @Test
60     public void testTypedefEnumResolving() {
61         final String ianaIfTypePath = getClass().getResource(
62                 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
63
64         final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
65         assertTrue(context != null);
66
67         final BindingGenerator bindingGen = new BindingGeneratorImpl();
68         final List<Type> genTypes = bindingGen.generateTypes(context);
69         assertTrue(genTypes != null);
70         assertEquals(1, genTypes.size());
71
72         final Type type = genTypes.get(0);
73         assertTrue(type instanceof GeneratedType);
74
75         final GeneratedType genType = (GeneratedType) type;
76         assertEquals(1, genType.getEnumDefintions().size());
77
78         final Enumeration enumer = genType.getEnumDefintions().get(0);
79         assertEquals(272, enumer.getValues().size());
80     }
81
82     @Test
83     public void testMultipleModulesResolving() {
84         final String topologyPath = getClass().getResource(
85                 "/abstract-topology.yang").getPath();
86         final String typesPath = getClass().getResource(
87                 "/ietf-inet-types@2010-09-24.yang").getPath();
88         final SchemaContext context = resolveSchemaContextFromFiles(
89                 topologyPath, typesPath);
90         assertTrue(context != null);
91
92         final BindingGenerator bindingGen = new BindingGeneratorImpl();
93         final List<Type> genTypes = bindingGen.generateTypes(context);
94
95         assertTrue(genTypes != null);
96         assertEquals(13, genTypes.size());
97     }
98
99     @Test
100     public void testLeafrefResolving() {
101         final String topologyPath = getClass().getResource(
102                 "/leafref-test-models/abstract-topology@2013-02-08.yang")
103                 .getPath();
104         final String interfacesPath = getClass().getResource(
105                 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
106                 .getPath();
107         // final String ifTypePath = getClass().getResource(
108         // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
109         final String inetTypesPath = getClass().getResource(
110                 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
111                 .getPath();
112         final String yangTypesPath = getClass().getResource(
113                 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
114                 .getPath();
115
116         assertTrue(topologyPath != null);
117         assertTrue(interfacesPath != null);
118         // assertTrue(ifTypePath != null);
119         assertTrue(inetTypesPath != null);
120         assertTrue(yangTypesPath != null);
121
122         // final SchemaContext context = resolveSchemaContextFromFiles(
123         // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
124         // yangTypesPath);
125         final SchemaContext context = resolveSchemaContextFromFiles(
126                 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
127         assertTrue(context != null);
128         assertEquals(4, context.getModules().size());
129
130         final BindingGenerator bindingGen = new BindingGeneratorImpl();
131         final List<Type> genTypes = bindingGen.generateTypes(context);
132
133         assertEquals(25, genTypes.size());
134         assertTrue(genTypes != null);
135
136         int resolvedLeafrefCount = 0;
137         for (final Type type : genTypes) {
138             if (type.getName().equals("InterfaceKey")
139                     && type instanceof GeneratedTransferObject) {
140                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
141                 final List<GeneratedProperty> properties = genTO
142                         .getProperties();
143
144                 assertTrue(properties != null);
145                 for (final GeneratedProperty property : properties) {
146                     if (property.getName().equals("InterfaceId")) {
147                         assertTrue(property.getReturnType() != null);
148                         assertFalse(property.getReturnType().equals(
149                                 "java.lang.Void"));
150                         assertTrue(property.getReturnType().getName()
151                                 .equals("String"));
152                         resolvedLeafrefCount++;
153                     }
154                 }
155
156             } else if (type.getName().equals("Interface")
157                     && type instanceof GeneratedType) {
158                 final GeneratedType genType = (GeneratedType) type;
159                 final List<MethodSignature> methods = genType
160                         .getMethodDefinitions();
161
162                 assertTrue(methods != null);
163                 for (final MethodSignature method : methods) {
164                     if (method.getName().equals("getInterfaceKey")) {
165                         assertTrue(method.getReturnType() != null);
166                         assertFalse(method.getReturnType().equals(
167                                 "java.lang.Void"));
168                         assertTrue(method.getReturnType().getName()
169                                 .equals("InterfaceKey"));
170                         resolvedLeafrefCount++;
171                     } else if (method.getName().equals("getHigherLayerIf")) {
172                         assertTrue(method.getReturnType() != null);
173                         assertFalse(method.getReturnType().equals(
174                                 "java.lang.Void"));
175                         assertTrue(method.getReturnType().getName()
176                                 .equals("List"));
177                         resolvedLeafrefCount++;
178                     }
179                 }
180             } else if (type.getName().equals("NetworkLink")
181                     && type instanceof GeneratedType) {
182                 final GeneratedType genType = (GeneratedType) type;
183                 final List<MethodSignature> methods = genType
184                         .getMethodDefinitions();
185                 assertTrue(methods != null);
186                 for (MethodSignature method : methods) {
187                     if (method.getName().equals("getInterface")) {
188                         assertTrue(method.getReturnType() != null);
189                         assertFalse(method.getReturnType().equals(
190                                 "java.lang.Void"));
191                         assertTrue(method.getReturnType().getName()
192                                 .equals("String"));
193                         resolvedLeafrefCount++;
194                     }
195                 }
196             } else if ((type.getName().equals("SourceNode") || type.getName()
197                     .equals("DestinationNode"))
198                     && type instanceof GeneratedType) {
199                 final GeneratedType genType = (GeneratedType) type;
200                 final List<MethodSignature> methods = genType
201                         .getMethodDefinitions();
202                 assertTrue(methods != null);
203                 for (MethodSignature method : methods) {
204                     if (method.getName().equals("getId")) {
205                         assertTrue(method.getReturnType() != null);
206                         assertFalse(method.getReturnType().equals(
207                                 "java.lang.Void"));
208                         assertTrue(method.getReturnType().getName()
209                                 .equals("String"));
210                         resolvedLeafrefCount++;
211                     }
212                 }
213             } else if (type.getName().equals("Tunnel")
214                     && type instanceof GeneratedType) {
215                 final GeneratedType genType = (GeneratedType) type;
216                 final List<MethodSignature> methods = genType
217                         .getMethodDefinitions();
218                 assertTrue(methods != null);
219                 for (MethodSignature method : methods) {
220                     if (method.getName().equals("getTunnelKey")) {
221                         assertTrue(method.getReturnType() != null);
222                         assertFalse(method.getReturnType().equals(
223                                 "java.lang.Void"));
224                         assertTrue(method.getReturnType().getName()
225                                 .equals("TunnelKey"));
226                         resolvedLeafrefCount++;
227                     }
228                 }
229             } else if (type.getName().equals("TunnelKey")
230                     && type instanceof GeneratedTransferObject) {
231                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
232                 final List<GeneratedProperty> properties = genTO
233                         .getProperties();
234
235                 assertTrue(properties != null);
236                 for (final GeneratedProperty property : properties) {
237                     if (property.getName().equals("TunnelId")) {
238                         assertTrue(property.getReturnType() != null);
239                         assertFalse(property.getReturnType().equals(
240                                 "java.lang.Void"));
241                         assertTrue(property.getReturnType().getName()
242                                 .equals("String"));
243                         resolvedLeafrefCount++;
244                     }
245                 }
246             }
247         }
248         assertEquals(10, resolvedLeafrefCount);
249     }
250
251     @Ignore
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(3, genTypes.size());
264
265         final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
266         final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
267
268         assertEquals("SimpleContainer", simpleContainer.getName());
269         assertEquals("NestedContainer", nestedContainer.getName());
270         assertEquals(4, 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     @Ignore
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(3, genTypes.size());
343
344         final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
345         final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
346
347         assertEquals("SimpleContainer", simpleContainer.getName());
348         assertEquals("NestedContainer", nestedContainer.getName());
349         assertEquals(4, 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     @Ignore
405     @Test
406     public void testListResolving() {
407         final String filePath = getClass()
408                 .getResource("/simple-list-demo.yang").getPath();
409         final SchemaContext context = resolveSchemaContextFromFiles(filePath);
410         assertTrue(context != null);
411
412         final BindingGenerator bindingGen = new BindingGeneratorImpl();
413         final List<Type> genTypes = bindingGen.generateTypes(context);
414
415         assertTrue(genTypes != null);
416         assertEquals(5, genTypes.size());
417
418         int genTypesCount = 0;
419         int genTOsCount = 0;
420         for (final Type type : genTypes) {
421             if (type instanceof GeneratedType) {
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(7, 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(7, genTypes.size());
500
501         int genTypesCount = 0;
502         int genTOsCount = 0;
503         for (final Type type : genTypes) {
504             if (type instanceof GeneratedType) {
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(5, 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(14, genTypes.size());
547
548         int genTypesCount = 0;
549         int genTOsCount = 0;
550         for (final Type type : genTypes) {
551             if (type instanceof GeneratedType) {
552                 genTypesCount++;
553             } else if (type instanceof GeneratedTransferObject) {
554                 genTOsCount++;
555             }
556         }
557
558         assertEquals(11, genTypesCount);
559         assertEquals(3, genTOsCount);
560     }
561 }