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.*;
13 import java.util.ArrayList;
14 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
19 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
22 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
23 import org.opendaylight.controller.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 public class GeneratedTypesTest {
31 private SchemaContext resolveSchemaContextFromFiles(final String... yangFiles) {
32 final YangModelParser parser = new YangParserImpl();
34 final List<File> inputFiles = new ArrayList<File>();
35 for (int i = 0; i < yangFiles.length; ++i) {
36 inputFiles.add(new File(yangFiles[i]));
39 final Set<Module> modules = parser.parseYangModels(inputFiles);
40 return parser.resolveSchemaContext(modules);
44 public void testMultipleModulesResolving() {
45 final String topologyPath = getClass().getResource("/abstract-topology.yang").getPath();
46 final String typesPath = getClass().getResource("/ietf-inet-types@2010-09-24.yang").getPath();
47 final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, typesPath);
48 assertNotNull(context);
50 final BindingGenerator bindingGen = new BindingGeneratorImpl();
51 final List<Type> genTypes = bindingGen.generateTypes(context);
53 assertNotNull(genTypes);
54 assertEquals(29, genTypes.size());
58 public void testLeafrefResolving() {
59 final String topologyPath = getClass().getResource("/leafref-test-models/abstract-topology@2013-02-08.yang")
61 final String interfacesPath = getClass().getResource("/leafref-test-models/ietf-interfaces@2012-11-15.yang")
63 // final String ifTypePath = getClass().getResource(
64 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
65 final String inetTypesPath = getClass().getResource("/leafref-test-models/ietf-inet-types@2010-09-24.yang")
67 final String yangTypesPath = getClass().getResource("/leafref-test-models/ietf-yang-types@2010-09-24.yang")
70 assertNotNull(topologyPath);
71 assertNotNull(interfacesPath);
72 // assertTrue(ifTypePath != null);
73 assertNotNull(inetTypesPath);
74 assertNotNull(yangTypesPath);
76 // final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, interfacesPath, ifTypePath,
77 // inetTypesPath, yangTypesPath);
78 final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, interfacesPath, inetTypesPath,
80 assertNotNull(context);
81 assertEquals(4, context.getModules().size());
83 final BindingGenerator bindingGen = new BindingGeneratorImpl();
84 final List<Type> genTypes = bindingGen.generateTypes(context);
86 assertEquals(57, genTypes.size());
87 assertNotNull(genTypes);
89 GeneratedTransferObject gtIfcKey = null;
90 GeneratedType gtIfc = null;
91 GeneratedType gtNetworkLink = null;
92 GeneratedType gtSource = null;
93 GeneratedType gtDest = null;
94 GeneratedType gtTunnel = null;
95 GeneratedTransferObject gtTunnelKey = null;
96 GeneratedType gtTopology = null;
97 for (final Type type : genTypes) {
98 String name = type.getName();
99 if ("InterfaceKey".equals(name)) {
100 gtIfcKey = (GeneratedTransferObject) type;
101 } else if ("Interface".equals(name)) {
102 gtIfc = (GeneratedType) type;
103 } else if ("NetworkLink".equals(name)) {
104 gtNetworkLink = (GeneratedType) type;
105 } else if ("SourceNode".equals(name)) {
106 gtSource = (GeneratedType) type;
107 } else if ("DestinationNode".equals(name)) {
108 gtDest = (GeneratedType) type;
109 } else if ("Tunnel".equals(name)) {
110 gtTunnel = (GeneratedType) type;
111 } else if ("TunnelKey".equals(name)) {
112 gtTunnelKey = (GeneratedTransferObject) type;
113 } else if ("Topology".equals(name)) {
114 gtTopology = (GeneratedType) type;
118 assertNotNull(gtIfcKey);
119 assertNotNull(gtIfc);
120 assertNotNull(gtNetworkLink);
121 assertNotNull(gtSource);
122 assertNotNull(gtDest);
123 assertNotNull(gtTunnel);
124 assertNotNull(gtTunnelKey);
125 assertNotNull(gtTopology);
128 final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
129 assertNotNull(gtTopoMethods);
130 MethodSignature condLeafref = null;
131 for (final MethodSignature method : gtTopoMethods) {
132 if (method.getName().equals("getCondLeafref")) {
133 condLeafref = method;
136 assertNotNull(condLeafref);
137 Type condLeafRT = condLeafref.getReturnType();
138 assertNotNull(condLeafRT);
139 assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
142 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
143 assertNotNull(gtIfcKeyProps);
144 GeneratedProperty ifcIdProp = null;
145 for (final GeneratedProperty property : gtIfcKeyProps) {
146 if (property.getName().equals("InterfaceId")) {
147 ifcIdProp = property;
150 assertNotNull(ifcIdProp);
151 Type ifcIdPropType = ifcIdProp.getReturnType();
152 assertNotNull(ifcIdPropType);
153 assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
156 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
157 assertNotNull(gtIfcMethods);
158 MethodSignature getIfcKey = null;
159 MethodSignature getHigherLayerIf = null;
160 for (final MethodSignature method : gtIfcMethods) {
161 if (method.getName().equals("getInterfaceKey")) {
163 } else if (method.getName().equals("getHigherLayerIf")) {
164 getHigherLayerIf = method;
167 assertNotNull(getIfcKey);
168 Type getIfcKeyType = getIfcKey.getReturnType();
169 assertNotNull(getIfcKeyType);
170 assertNotSame("java.lang.Void", getIfcKeyType);
171 assertEquals("InterfaceKey", getIfcKeyType.getName());
173 assertNotNull(getHigherLayerIf);
174 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
175 assertNotNull(getHigherLayerIfType);
176 assertNotSame("java.lang.Void", getHigherLayerIfType);
177 assertEquals("List", getHigherLayerIfType.getName());
180 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
181 assertNotNull(gtNetworkLinkMethods);
182 MethodSignature getIfc = null;
183 for (MethodSignature method : gtNetworkLinkMethods) {
184 if (method.getName().equals("getInterface")) {
188 assertNotNull(getIfc);
189 Type getIfcType = getIfc.getReturnType();
190 assertNotNull(getIfcType);
191 assertNotSame("java.lang.Void", getIfcType);
192 assertEquals("String", getIfcType.getName());
195 final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
196 assertNotNull(gtSourceMethods);
197 MethodSignature getIdSource = null;
198 for (MethodSignature method : gtSourceMethods) {
199 if (method.getName().equals("getId")) {
200 getIdSource = method;
203 assertNotNull(getIdSource);
204 Type getIdType = getIdSource.getReturnType();
205 assertNotNull(getIdType);
206 assertNotSame("java.lang.Void", getIdType);
207 assertEquals("Uri", getIdType.getName());
210 final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
211 assertNotNull(gtDestMethods);
212 MethodSignature getIdDest = null;
213 for (MethodSignature method : gtDestMethods) {
214 if (method.getName().equals("getId")) {
218 assertNotNull(getIdDest);
219 Type getIdDestType = getIdDest.getReturnType();
220 assertNotNull(getIdDestType);
221 assertNotSame("java.lang.Void", getIdDestType);
222 assertEquals("Uri", getIdDestType.getName());
225 final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
226 assertNotNull(gtTunnelMethods);
227 MethodSignature getTunnelKey = null;
228 for (MethodSignature method : gtTunnelMethods) {
229 if (method.getName().equals("getTunnelKey")) {
230 getTunnelKey = method;
233 assertNotNull(getTunnelKey);
234 Type getTunnelKeyType = getTunnelKey.getReturnType();
235 assertNotNull(getTunnelKeyType);
236 assertNotSame("java.lang.Void", getTunnelKeyType);
237 assertEquals("TunnelKey", getTunnelKeyType.getName());
240 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
241 assertNotNull(gtTunnelKeyProps);
242 GeneratedProperty tunnelId = null;
243 for (final GeneratedProperty property : gtTunnelKeyProps) {
244 if (property.getName().equals("TunnelId")) {
248 assertNotNull(tunnelId);
249 Type tunnelIdType = tunnelId.getReturnType();
250 assertNotNull(tunnelIdType);
251 assertNotSame("java.lang.Void", tunnelIdType);
252 assertEquals("Uri", tunnelIdType.getName());
256 public void testContainerResolving() {
257 final String filePath = getClass().getResource("/simple-container-demo.yang").getPath();
258 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
259 assert (context != null);
261 final BindingGenerator bindingGen = new BindingGeneratorImpl();
262 final List<Type> genTypes = bindingGen.generateTypes(context);
264 assertNotNull(genTypes);
265 assertEquals(4, genTypes.size());
267 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
268 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
270 assertEquals("SimpleContainer", simpleContainer.getName());
271 assertEquals("NestedContainer", nestedContainer.getName());
272 assertEquals(3, simpleContainer.getMethodDefinitions().size());
273 assertEquals(2, nestedContainer.getMethodDefinitions().size());
275 int getFooMethodCounter = 0;
276 int getBarMethodCounter = 0;
277 int getNestedContainerCounter = 0;
279 String getFooMethodReturnTypeName = "";
280 String getBarMethodReturnTypeName = "";
281 String getNestedContainerReturnTypeName = "";
282 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
283 if (method.getName().equals("getFoo")) {
284 getFooMethodCounter++;
285 getFooMethodReturnTypeName = method.getReturnType().getName();
288 if (method.getName().equals("getBar")) {
289 getBarMethodCounter++;
290 getBarMethodReturnTypeName = method.getReturnType().getName();
293 if (method.getName().equals("getNestedContainer")) {
294 getNestedContainerCounter++;
295 getNestedContainerReturnTypeName = method.getReturnType().getName();
299 assertEquals(1, getFooMethodCounter);
300 assertEquals("Integer", getFooMethodReturnTypeName);
302 assertEquals(1, getBarMethodCounter);
303 assertEquals("String", getBarMethodReturnTypeName);
305 assertEquals(1, getNestedContainerCounter);
306 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
308 getFooMethodCounter = 0;
309 getBarMethodCounter = 0;
311 getFooMethodReturnTypeName = "";
312 getBarMethodReturnTypeName = "";
314 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
316 if (method.getName().equals("getFoo")) {
317 getFooMethodCounter++;
318 getFooMethodReturnTypeName = method.getReturnType().getName();
321 if (method.getName().equals("getBar")) {
322 getBarMethodCounter++;
323 getBarMethodReturnTypeName = method.getReturnType().getName();
327 assertEquals(1, getFooMethodCounter);
328 assertEquals("Short", getFooMethodReturnTypeName);
330 assertEquals(1, getBarMethodCounter);
331 assertEquals("String", getBarMethodReturnTypeName);
335 public void testLeafListResolving() {
336 final String filePath = getClass().getResource("/simple-leaf-list-demo.yang").getPath();
337 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
338 assertNotNull(context);
340 final BindingGenerator bindingGen = new BindingGeneratorImpl();
341 final List<Type> genTypes = bindingGen.generateTypes(context);
343 assertNotNull(genTypes);
344 assertEquals(4, genTypes.size());
346 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
347 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
349 assertEquals("SimpleContainer", simpleContainer.getName());
350 assertEquals("NestedContainer", nestedContainer.getName());
351 assertEquals(3, simpleContainer.getMethodDefinitions().size());
352 assertEquals(2, nestedContainer.getMethodDefinitions().size());
354 int getFooMethodCounter = 0;
355 int getBarMethodCounter = 0;
356 int getNestedContainerCounter = 0;
358 String getFooMethodReturnTypeName = "";
359 String getBarMethodReturnTypeName = "";
360 String getNestedContainerReturnTypeName = "";
361 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
362 if (method.getName().equals("getFoo")) {
363 getFooMethodCounter++;
364 getFooMethodReturnTypeName = method.getReturnType().getName();
367 if (method.getName().equals("getBar")) {
368 getBarMethodCounter++;
369 getBarMethodReturnTypeName = method.getReturnType().getName();
372 if (method.getName().equals("getNestedContainer")) {
373 getNestedContainerCounter++;
374 getNestedContainerReturnTypeName = method.getReturnType().getName();
378 assertEquals(1, getFooMethodCounter);
379 assertEquals("List", getFooMethodReturnTypeName);
381 assertEquals(1, getBarMethodCounter);
382 assertEquals("String", getBarMethodReturnTypeName);
384 assertEquals(1, getNestedContainerCounter);
385 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
387 getFooMethodCounter = 0;
388 getBarMethodCounter = 0;
390 getFooMethodReturnTypeName = "";
391 getBarMethodReturnTypeName = "";
393 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
394 if (method.getName().equals("getFoo")) {
395 getFooMethodCounter++;
396 getFooMethodReturnTypeName = method.getReturnType().getName();
399 if (method.getName().equals("getBar")) {
400 getBarMethodCounter++;
401 getBarMethodReturnTypeName = method.getReturnType().getName();
405 assertEquals(1, getFooMethodCounter);
406 assertEquals("Short", getFooMethodReturnTypeName);
408 assertEquals(1, getBarMethodCounter);
409 assertEquals("List", getBarMethodReturnTypeName);
413 public void testListResolving() {
414 final String filePath = getClass().getResource("/simple-list-demo.yang").getPath();
415 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
416 assertNotNull(context);
418 final BindingGenerator bindingGen = new BindingGeneratorImpl();
419 final List<Type> genTypes = bindingGen.generateTypes(context);
421 assertNotNull(genTypes);
422 assertEquals(6, genTypes.size());
424 int listParentContainerMethodsCount = 0;
425 int simpleListMethodsCount = 0;
426 int listChildContainerMethodsCount = 0;
427 int listKeyClassCount = 0;
429 int getSimpleListKeyMethodCount = 0;
430 int getListChildContainerMethodCount = 0;
431 int getFooMethodCount = 0;
432 int setFooMethodCount = 0;
433 int getSimpleLeafListMethodCount = 0;
434 int setSimpleLeafListMethodCount = 0;
435 int getBarMethodCount = 0;
437 String getSimpleListKeyMethodReturnTypeName = "";
438 String getListChildContainerMethodReturnTypeName = "";
440 int listKeyClassPropertyCount = 0;
441 String listKeyClassPropertyName = "";
442 String listKeyClassPropertyTypeName = "";
443 boolean listKeyClassPropertyReadOnly = false;
445 int hashMethodParameterCount = 0;
446 String hashMethodParameterName = "";
447 String hashMethodParameterReturnTypeName = "";
449 int equalMethodParameterCount = 0;
450 String equalMethodParameterName = "";
451 String equalMethodParameterReturnTypeName = "";
453 for (final Type type : genTypes) {
454 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
455 final GeneratedType genType = (GeneratedType) type;
456 if (genType.getName().equals("ListParentContainer")) {
457 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
458 } else if (genType.getName().equals("SimpleList")) {
459 simpleListMethodsCount = genType.getMethodDefinitions().size();
460 final List<MethodSignature> methods = genType.getMethodDefinitions();
461 for (final MethodSignature method : methods) {
462 if (method.getName().equals("getSimpleListKey")) {
463 getSimpleListKeyMethodCount++;
464 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
465 } else if (method.getName().equals("getListChildContainer")) {
466 getListChildContainerMethodCount++;
467 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
468 } else if (method.getName().equals("getFoo")) {
470 } else if (method.getName().equals("setFoo")) {
472 } else if (method.getName().equals("getSimpleLeafList")) {
473 getSimpleLeafListMethodCount++;
474 } else if (method.getName().equals("setSimpleLeafList")) {
475 setSimpleLeafListMethodCount++;
476 } else if (method.getName().equals("getBar")) {
480 } else if (genType.getName().equals("ListChildContainer")) {
481 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
483 } else if (type instanceof GeneratedTransferObject) {
484 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
485 final List<GeneratedProperty> properties = genTO.getProperties();
486 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
487 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
490 listKeyClassPropertyCount = properties.size();
491 listKeyClassPropertyName = properties.get(0).getName();
492 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
493 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
495 hashMethodParameterCount = hashProps.size();
496 hashMethodParameterName = hashProps.get(0).getName();
497 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
499 equalMethodParameterCount = equalProps.size();
500 equalMethodParameterName = equalProps.get(0).getName();
501 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
506 assertEquals(1, listParentContainerMethodsCount);
507 assertEquals(1, listChildContainerMethodsCount);
508 assertEquals(1, getSimpleListKeyMethodCount);
509 assertEquals(1, listKeyClassCount);
511 assertEquals(1, listKeyClassPropertyCount);
512 assertEquals("ListKey", listKeyClassPropertyName);
513 assertEquals("Byte", listKeyClassPropertyTypeName);
514 assertEquals(true, listKeyClassPropertyReadOnly);
515 assertEquals(1, hashMethodParameterCount);
516 assertEquals("ListKey", hashMethodParameterName);
517 assertEquals("Byte", hashMethodParameterReturnTypeName);
518 assertEquals(1, equalMethodParameterCount);
519 assertEquals("ListKey", equalMethodParameterName);
520 assertEquals("Byte", equalMethodParameterReturnTypeName);
522 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
524 assertEquals(1, getListChildContainerMethodCount);
525 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
526 assertEquals(1, getFooMethodCount);
527 assertEquals(0, setFooMethodCount);
528 assertEquals(1, getSimpleLeafListMethodCount);
529 assertEquals(0, setSimpleLeafListMethodCount);
530 assertEquals(1, getBarMethodCount);
532 assertEquals(5, simpleListMethodsCount);
536 public void testListCompositeKeyResolving() {
537 final String filePath = getClass().getResource("/list-composite-key.yang").getPath();
538 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
540 assertNotNull(context);
542 final BindingGenerator bindingGen = new BindingGeneratorImpl();
543 final List<Type> genTypes = bindingGen.generateTypes(context);
545 assertNotNull(genTypes);
546 assertEquals(8, genTypes.size());
548 int genTypesCount = 0;
551 int compositeKeyListKeyPropertyCount = 0;
552 int compositeKeyListKeyCount = 0;
553 int innerListKeyPropertyCount = 0;
555 for (final Type type : genTypes) {
556 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
558 } else if (type instanceof GeneratedTransferObject) {
559 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
561 if (genTO.getName().equals("CompositeKeyListKey")) {
562 compositeKeyListKeyCount++;
563 final List<GeneratedProperty> properties = genTO.getProperties();
564 for (final GeneratedProperty prop : properties) {
565 if (prop.getName().equals("Key1")) {
566 compositeKeyListKeyPropertyCount++;
567 } else if (prop.getName().equals("Key2")) {
568 compositeKeyListKeyPropertyCount++;
572 } else if (genTO.getName().equals("InnerListKey")) {
573 final List<GeneratedProperty> properties = genTO.getProperties();
574 innerListKeyPropertyCount = properties.size();
579 assertEquals(1, compositeKeyListKeyCount);
580 assertEquals(2, compositeKeyListKeyPropertyCount);
582 assertEquals(1, innerListKeyPropertyCount);
584 assertEquals(6, genTypesCount);
585 assertEquals(2, genTOsCount);
589 public void testGeneratedTypes() {
590 final String filePath = getClass().getResource("/demo-topology.yang").getPath();
591 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
592 assertNotNull(context);
594 final BindingGenerator bindingGen = new BindingGeneratorImpl();
595 final List<Type> genTypes = bindingGen.generateTypes(context);
597 assertNotNull(genTypes);
598 assertEquals(15, genTypes.size());
600 int genTypesCount = 0;
602 for (final Type type : genTypes) {
603 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
605 } else if (type instanceof GeneratedTransferObject) {
610 assertEquals(12, genTypesCount);
611 assertEquals(3, genTOsCount);