2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.generator.impl;
10 import static org.junit.Assert.*;
11 import static org.junit.Assert.assertEquals;
14 import java.util.ArrayList;
15 import java.util.List;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
25 import org.opendaylight.controller.sal.binding.model.api.Type;
26 import org.opendaylight.controller.yang.model.api.Module;
27 import org.opendaylight.controller.yang.model.api.SchemaContext;
28 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
29 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
31 public class GeneratedTypesTest {
33 private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
34 final YangModelParser parser = new YangParserImpl();
36 final List<File> inputFiles = new ArrayList<File>();
37 for (int i = 0; i < yangFiles.length; ++i) {
38 inputFiles.add(new File(yangFiles[i]));
41 final Set<Module> modules = parser.parseYangModels(inputFiles);
42 return parser.resolveSchemaContext(modules);
46 public void testMultipleModulesResolving() {
47 final String topologyPath = getClass().getResource("/abstract-topology.yang").getPath();
48 final String typesPath = getClass().getResource("/ietf-inet-types@2010-09-24.yang").getPath();
49 final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, typesPath);
50 assertNotNull(context);
52 final BindingGenerator bindingGen = new BindingGeneratorImpl();
53 final List<Type> genTypes = bindingGen.generateTypes(context);
55 assertNotNull(genTypes);
56 assertEquals(29, genTypes.size());
60 public void testLeafrefResolving() {
61 final String topologyPath = getClass().getResource("/leafref-test-models/abstract-topology@2013-02-08.yang")
63 final String interfacesPath = getClass().getResource("/leafref-test-models/ietf-interfaces@2012-11-15.yang")
65 // final String ifTypePath = getClass().getResource(
66 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
67 final String inetTypesPath = getClass().getResource("/leafref-test-models/ietf-inet-types@2010-09-24.yang")
69 final String yangTypesPath = getClass().getResource("/leafref-test-models/ietf-yang-types@2010-09-24.yang")
72 assertNotNull(topologyPath);
73 assertNotNull(interfacesPath);
74 // assertTrue(ifTypePath != null);
75 assertNotNull(inetTypesPath);
76 assertNotNull(yangTypesPath);
78 // final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, interfacesPath, ifTypePath,
79 // inetTypesPath, yangTypesPath);
80 final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, interfacesPath, inetTypesPath,
82 assertNotNull(context);
83 assertEquals(4, context.getModules().size());
85 final BindingGenerator bindingGen = new BindingGeneratorImpl();
86 final List<Type> genTypes = bindingGen.generateTypes(context);
88 assertEquals(57, genTypes.size());
89 assertNotNull(genTypes);
91 GeneratedTransferObject gtIfcKey = null;
92 GeneratedType gtIfc = null;
93 GeneratedType gtNetworkLink = null;
94 GeneratedType gtSource = null;
95 GeneratedType gtDest = null;
96 GeneratedType gtTunnel = null;
97 GeneratedTransferObject gtTunnelKey = null;
98 GeneratedType gtTopology = null;
99 for (final Type type : genTypes) {
100 String name = type.getName();
101 if ("InterfaceKey".equals(name)) {
102 gtIfcKey = (GeneratedTransferObject) type;
103 } else if ("Interface".equals(name)) {
104 gtIfc = (GeneratedType) type;
105 } else if ("NetworkLink".equals(name)) {
106 gtNetworkLink = (GeneratedType) type;
107 } else if ("SourceNode".equals(name)) {
108 gtSource = (GeneratedType) type;
109 } else if ("DestinationNode".equals(name)) {
110 gtDest = (GeneratedType) type;
111 } else if ("Tunnel".equals(name)) {
112 gtTunnel = (GeneratedType) type;
113 } else if ("TunnelKey".equals(name)) {
114 gtTunnelKey = (GeneratedTransferObject) type;
115 } else if ("Topology".equals(name)) {
116 gtTopology = (GeneratedType) type;
120 assertNotNull(gtIfcKey);
121 assertNotNull(gtIfc);
122 assertNotNull(gtNetworkLink);
123 assertNotNull(gtSource);
124 assertNotNull(gtDest);
125 assertNotNull(gtTunnel);
126 assertNotNull(gtTunnelKey);
127 assertNotNull(gtTopology);
130 final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
131 assertNotNull(gtTopoMethods);
132 MethodSignature condLeafref = null;
133 for (final MethodSignature method : gtTopoMethods) {
134 if (method.getName().equals("getCondLeafref")) {
135 condLeafref = method;
138 assertNotNull(condLeafref);
139 Type condLeafRT = condLeafref.getReturnType();
140 assertNotNull(condLeafRT);
141 assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
144 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
145 assertNotNull(gtIfcKeyProps);
146 GeneratedProperty ifcIdProp = null;
147 for (final GeneratedProperty property : gtIfcKeyProps) {
148 if (property.getName().equals("InterfaceId")) {
149 ifcIdProp = property;
152 assertNotNull(ifcIdProp);
153 Type ifcIdPropType = ifcIdProp.getReturnType();
154 assertNotNull(ifcIdPropType);
155 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
158 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
159 assertNotNull(gtIfcMethods);
160 MethodSignature getIfcKey = null;
161 MethodSignature getHigherLayerIf = null;
162 for (final MethodSignature method : gtIfcMethods) {
163 if (method.getName().equals("getInterfaceKey")) {
165 } else if (method.getName().equals("getHigherLayerIf")) {
166 getHigherLayerIf = method;
169 assertNotNull(getIfcKey);
170 Type getIfcKeyType = getIfcKey.getReturnType();
171 assertNotNull(getIfcKeyType);
172 assertNotSame("java.lang.Void", getIfcKeyType);
173 assertEquals("InterfaceKey", getIfcKeyType.getName());
175 assertNotNull(getHigherLayerIf);
176 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
177 assertNotNull(getHigherLayerIfType);
178 assertNotSame("java.lang.Void", getHigherLayerIfType);
179 assertEquals("List", getHigherLayerIfType.getName());
182 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
183 assertNotNull(gtNetworkLinkMethods);
184 MethodSignature getIfc = null;
185 for (MethodSignature method : gtNetworkLinkMethods) {
186 if (method.getName().equals("getInterface")) {
190 assertNotNull(getIfc);
191 Type getIfcType = getIfc.getReturnType();
192 assertNotNull(getIfcType);
193 assertNotSame("java.lang.Void", getIfcType);
194 assertEquals("String", getIfcType.getName());
197 final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
198 assertNotNull(gtSourceMethods);
199 MethodSignature getIdSource = null;
200 for (MethodSignature method : gtSourceMethods) {
201 if (method.getName().equals("getId")) {
202 getIdSource = method;
205 assertNotNull(getIdSource);
206 Type getIdType = getIdSource.getReturnType();
207 assertNotNull(getIdType);
208 assertNotSame("java.lang.Void", getIdType);
209 assertEquals("Uri", getIdType.getName());
212 final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
213 assertNotNull(gtDestMethods);
214 MethodSignature getIdDest = null;
215 for (MethodSignature method : gtDestMethods) {
216 if (method.getName().equals("getId")) {
220 assertNotNull(getIdDest);
221 Type getIdDestType = getIdDest.getReturnType();
222 assertNotNull(getIdDestType);
223 assertNotSame("java.lang.Void", getIdDestType);
224 assertEquals("Uri", getIdDestType.getName());
227 final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
228 assertNotNull(gtTunnelMethods);
229 MethodSignature getTunnelKey = null;
230 for (MethodSignature method : gtTunnelMethods) {
231 if (method.getName().equals("getTunnelKey")) {
232 getTunnelKey = method;
235 assertNotNull(getTunnelKey);
236 Type getTunnelKeyType = getTunnelKey.getReturnType();
237 assertNotNull(getTunnelKeyType);
238 assertNotSame("java.lang.Void", getTunnelKeyType);
239 assertEquals("TunnelKey", getTunnelKeyType.getName());
242 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
243 assertNotNull(gtTunnelKeyProps);
244 GeneratedProperty tunnelId = null;
245 for (final GeneratedProperty property : gtTunnelKeyProps) {
246 if (property.getName().equals("TunnelId")) {
250 assertNotNull(tunnelId);
251 Type tunnelIdType = tunnelId.getReturnType();
252 assertNotNull(tunnelIdType);
253 assertNotSame("java.lang.Void", tunnelIdType);
254 assertEquals("Uri", tunnelIdType.getName());
258 public void testContainerResolving() {
259 final String filePath = getClass().getResource("/simple-container-demo.yang").getPath();
260 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
261 assert (context != null);
263 final BindingGenerator bindingGen = new BindingGeneratorImpl();
264 final List<Type> genTypes = bindingGen.generateTypes(context);
266 assertNotNull(genTypes);
267 assertEquals(4, genTypes.size());
269 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
270 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
272 assertEquals("SimpleContainer", simpleContainer.getName());
273 assertEquals("NestedContainer", nestedContainer.getName());
274 assertEquals(3, simpleContainer.getMethodDefinitions().size());
275 assertEquals(2, nestedContainer.getMethodDefinitions().size());
277 int getFooMethodCounter = 0;
278 int getBarMethodCounter = 0;
279 int getNestedContainerCounter = 0;
281 String getFooMethodReturnTypeName = "";
282 String getBarMethodReturnTypeName = "";
283 String getNestedContainerReturnTypeName = "";
284 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
285 if (method.getName().equals("getFoo")) {
286 getFooMethodCounter++;
287 getFooMethodReturnTypeName = method.getReturnType().getName();
290 if (method.getName().equals("getBar")) {
291 getBarMethodCounter++;
292 getBarMethodReturnTypeName = method.getReturnType().getName();
295 if (method.getName().equals("getNestedContainer")) {
296 getNestedContainerCounter++;
297 getNestedContainerReturnTypeName = method.getReturnType().getName();
301 assertEquals(1, getFooMethodCounter);
302 assertEquals("Integer", getFooMethodReturnTypeName);
304 assertEquals(1, getBarMethodCounter);
305 assertEquals("String", getBarMethodReturnTypeName);
307 assertEquals(1, getNestedContainerCounter);
308 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
310 getFooMethodCounter = 0;
311 getBarMethodCounter = 0;
313 getFooMethodReturnTypeName = "";
314 getBarMethodReturnTypeName = "";
316 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
318 if (method.getName().equals("getFoo")) {
319 getFooMethodCounter++;
320 getFooMethodReturnTypeName = method.getReturnType().getName();
323 if (method.getName().equals("getBar")) {
324 getBarMethodCounter++;
325 getBarMethodReturnTypeName = method.getReturnType().getName();
329 assertEquals(1, getFooMethodCounter);
330 assertEquals("Short", getFooMethodReturnTypeName);
332 assertEquals(1, getBarMethodCounter);
333 assertEquals("String", getBarMethodReturnTypeName);
337 public void testLeafListResolving() {
338 final String filePath = getClass().getResource("/simple-leaf-list-demo.yang").getPath();
339 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
340 assertNotNull(context);
342 final BindingGenerator bindingGen = new BindingGeneratorImpl();
343 final List<Type> genTypes = bindingGen.generateTypes(context);
345 assertNotNull(genTypes);
346 assertEquals(4, genTypes.size());
348 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
349 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
351 assertEquals("SimpleContainer", simpleContainer.getName());
352 assertEquals("NestedContainer", nestedContainer.getName());
353 assertEquals(3, simpleContainer.getMethodDefinitions().size());
354 assertEquals(2, nestedContainer.getMethodDefinitions().size());
356 int getFooMethodCounter = 0;
357 int getBarMethodCounter = 0;
358 int getNestedContainerCounter = 0;
360 String getFooMethodReturnTypeName = "";
361 String getBarMethodReturnTypeName = "";
362 String getNestedContainerReturnTypeName = "";
363 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
364 if (method.getName().equals("getFoo")) {
365 getFooMethodCounter++;
366 getFooMethodReturnTypeName = method.getReturnType().getName();
369 if (method.getName().equals("getBar")) {
370 getBarMethodCounter++;
371 getBarMethodReturnTypeName = method.getReturnType().getName();
374 if (method.getName().equals("getNestedContainer")) {
375 getNestedContainerCounter++;
376 getNestedContainerReturnTypeName = method.getReturnType().getName();
380 assertEquals(1, getFooMethodCounter);
381 assertEquals("List", getFooMethodReturnTypeName);
383 assertEquals(1, getBarMethodCounter);
384 assertEquals("String", getBarMethodReturnTypeName);
386 assertEquals(1, getNestedContainerCounter);
387 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
389 getFooMethodCounter = 0;
390 getBarMethodCounter = 0;
392 getFooMethodReturnTypeName = "";
393 getBarMethodReturnTypeName = "";
395 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
396 if (method.getName().equals("getFoo")) {
397 getFooMethodCounter++;
398 getFooMethodReturnTypeName = method.getReturnType().getName();
401 if (method.getName().equals("getBar")) {
402 getBarMethodCounter++;
403 getBarMethodReturnTypeName = method.getReturnType().getName();
407 assertEquals(1, getFooMethodCounter);
408 assertEquals("Short", getFooMethodReturnTypeName);
410 assertEquals(1, getBarMethodCounter);
411 assertEquals("List", getBarMethodReturnTypeName);
415 public void testListResolving() {
416 final String filePath = getClass().getResource("/simple-list-demo.yang").getPath();
417 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
418 assertNotNull(context);
420 final BindingGenerator bindingGen = new BindingGeneratorImpl();
421 final List<Type> genTypes = bindingGen.generateTypes(context);
423 assertNotNull(genTypes);
424 assertEquals(6, genTypes.size());
426 int listParentContainerMethodsCount = 0;
427 int simpleListMethodsCount = 0;
428 int listChildContainerMethodsCount = 0;
429 int listKeyClassCount = 0;
431 int getSimpleListKeyMethodCount = 0;
432 int getListChildContainerMethodCount = 0;
433 int getFooMethodCount = 0;
434 int setFooMethodCount = 0;
435 int getSimpleLeafListMethodCount = 0;
436 int setSimpleLeafListMethodCount = 0;
437 int getBarMethodCount = 0;
439 String getSimpleListKeyMethodReturnTypeName = "";
440 String getListChildContainerMethodReturnTypeName = "";
442 int listKeyClassPropertyCount = 0;
443 String listKeyClassPropertyName = "";
444 String listKeyClassPropertyTypeName = "";
445 boolean listKeyClassPropertyReadOnly = false;
447 int hashMethodParameterCount = 0;
448 String hashMethodParameterName = "";
449 String hashMethodParameterReturnTypeName = "";
451 int equalMethodParameterCount = 0;
452 String equalMethodParameterName = "";
453 String equalMethodParameterReturnTypeName = "";
455 for (final Type type : genTypes) {
456 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
457 final GeneratedType genType = (GeneratedType) type;
458 if (genType.getName().equals("ListParentContainer")) {
459 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
460 } else if (genType.getName().equals("SimpleList")) {
461 simpleListMethodsCount = genType.getMethodDefinitions().size();
462 final List<MethodSignature> methods = genType.getMethodDefinitions();
463 for (final MethodSignature method : methods) {
464 if (method.getName().equals("getSimpleListKey")) {
465 getSimpleListKeyMethodCount++;
466 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
467 } else if (method.getName().equals("getListChildContainer")) {
468 getListChildContainerMethodCount++;
469 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
470 } else if (method.getName().equals("getFoo")) {
472 } else if (method.getName().equals("setFoo")) {
474 } else if (method.getName().equals("getSimpleLeafList")) {
475 getSimpleLeafListMethodCount++;
476 } else if (method.getName().equals("setSimpleLeafList")) {
477 setSimpleLeafListMethodCount++;
478 } else if (method.getName().equals("getBar")) {
482 } else if (genType.getName().equals("ListChildContainer")) {
483 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
485 } else if (type instanceof GeneratedTransferObject) {
486 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
487 final List<GeneratedProperty> properties = genTO.getProperties();
488 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
489 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
492 listKeyClassPropertyCount = properties.size();
493 listKeyClassPropertyName = properties.get(0).getName();
494 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
495 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
497 hashMethodParameterCount = hashProps.size();
498 hashMethodParameterName = hashProps.get(0).getName();
499 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
501 equalMethodParameterCount = equalProps.size();
502 equalMethodParameterName = equalProps.get(0).getName();
503 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
508 assertEquals(1, listParentContainerMethodsCount);
509 assertEquals(1, listChildContainerMethodsCount);
510 assertEquals(1, getSimpleListKeyMethodCount);
511 assertEquals(1, listKeyClassCount);
513 assertEquals(1, listKeyClassPropertyCount);
514 assertEquals("ListKey", listKeyClassPropertyName);
515 assertEquals("Byte", listKeyClassPropertyTypeName);
516 assertEquals(true, listKeyClassPropertyReadOnly);
517 assertEquals(1, hashMethodParameterCount);
518 assertEquals("ListKey", hashMethodParameterName);
519 assertEquals("Byte", hashMethodParameterReturnTypeName);
520 assertEquals(1, equalMethodParameterCount);
521 assertEquals("ListKey", equalMethodParameterName);
522 assertEquals("Byte", equalMethodParameterReturnTypeName);
524 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
526 assertEquals(1, getListChildContainerMethodCount);
527 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
528 assertEquals(1, getFooMethodCount);
529 assertEquals(0, setFooMethodCount);
530 assertEquals(1, getSimpleLeafListMethodCount);
531 assertEquals(0, setSimpleLeafListMethodCount);
532 assertEquals(1, getBarMethodCount);
534 assertEquals(5, simpleListMethodsCount);
538 public void testListCompositeKeyResolving() {
539 final String filePath = getClass().getResource("/list-composite-key.yang").getPath();
540 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
542 assertNotNull(context);
544 final BindingGenerator bindingGen = new BindingGeneratorImpl();
545 final List<Type> genTypes = bindingGen.generateTypes(context);
547 assertNotNull(genTypes);
548 assertEquals(8, genTypes.size());
550 int genTypesCount = 0;
553 int compositeKeyListKeyPropertyCount = 0;
554 int compositeKeyListKeyCount = 0;
555 int innerListKeyPropertyCount = 0;
557 for (final Type type : genTypes) {
558 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
560 } else if (type instanceof GeneratedTransferObject) {
561 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
563 if (genTO.getName().equals("CompositeKeyListKey")) {
564 compositeKeyListKeyCount++;
565 final List<GeneratedProperty> properties = genTO.getProperties();
566 for (final GeneratedProperty prop : properties) {
567 if (prop.getName().equals("Key1")) {
568 compositeKeyListKeyPropertyCount++;
569 } else if (prop.getName().equals("Key2")) {
570 compositeKeyListKeyPropertyCount++;
574 } else if (genTO.getName().equals("InnerListKey")) {
575 final List<GeneratedProperty> properties = genTO.getProperties();
576 innerListKeyPropertyCount = properties.size();
581 assertEquals(1, compositeKeyListKeyCount);
582 assertEquals(2, compositeKeyListKeyPropertyCount);
584 assertEquals(1, innerListKeyPropertyCount);
586 assertEquals(6, genTypesCount);
587 assertEquals(2, genTOsCount);
591 public void testGeneratedTypes() {
592 final String filePath = getClass().getResource("/demo-topology.yang").getPath();
593 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
594 assertNotNull(context);
596 final BindingGenerator bindingGen = new BindingGeneratorImpl();
597 final List<Type> genTypes = bindingGen.generateTypes(context);
599 assertNotNull(genTypes);
600 assertEquals(15, genTypes.size());
602 int genTypesCount = 0;
604 for (final Type type : genTypes) {
605 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
607 } else if (type instanceof GeneratedTransferObject) {
612 assertEquals(12, genTypesCount);
613 assertEquals(3, genTOsCount);