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 org.junit.Test;
11 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
12 import org.opendaylight.controller.sal.binding.model.api.*;
13 import org.opendaylight.controller.yang.model.api.Module;
14 import org.opendaylight.controller.yang.model.api.SchemaContext;
15 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
16 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
19 import java.util.ArrayList;
20 import java.util.List;
23 import static org.junit.Assert.*;
25 public class GeneratedTypesTest {
27 private SchemaContext resolveSchemaContextFromFiles(
28 final String... yangFiles) {
29 final YangModelParser parser = new YangParserImpl();
31 final List<File> inputFiles = new ArrayList<File>();
32 for (int i = 0; i < yangFiles.length; ++i) {
33 inputFiles.add(new File(yangFiles[i]));
36 final Set<Module> modules = parser.parseYangModels(inputFiles);
37 return parser.resolveSchemaContext(modules);
41 public void testLeafEnumResolving() {
42 final String ietfInterfacesPath = getClass().getResource(
43 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
44 final String ifTypePath = getClass().getResource(
45 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
46 final String yangTypesPath = getClass().getResource(
47 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
49 final SchemaContext context = resolveSchemaContextFromFiles(
50 ietfInterfacesPath, ifTypePath, yangTypesPath);
51 assertTrue(context != null);
53 final BindingGenerator bindingGen = new BindingGeneratorImpl();
54 final List<Type> genTypes = bindingGen.generateTypes(context);
55 assertTrue(genTypes != null);
59 public void testTypedefEnumResolving() {
60 final String ianaIfTypePath = getClass().getResource(
61 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
63 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
64 assertTrue(context != null);
65 final BindingGenerator bindingGen = new BindingGeneratorImpl();
66 final List<Type> genTypes = bindingGen.generateTypes(context);
67 assertTrue(genTypes != null);
68 assertEquals(2, genTypes.size());
70 final Type type = genTypes.get(1);
71 assertTrue(type instanceof GeneratedTransferObject);
73 final GeneratedTransferObject genTransObj = (GeneratedTransferObject) type;
74 final List<GeneratedProperty> properties = genTransObj.getProperties();
75 assertNotNull(properties);
76 assertEquals(1, properties.size());
78 GeneratedProperty property = properties.get(0);
79 assertNotNull(property);
80 assertNotNull(property.getReturnType());
82 assertTrue(property.getReturnType() instanceof Enumeration);
83 final Enumeration enumer = (Enumeration) property.getReturnType();
84 assertEquals(272, enumer.getValues().size());
88 public void testMultipleModulesResolving() {
89 final String topologyPath = getClass().getResource(
90 "/abstract-topology.yang").getPath();
91 final String typesPath = getClass().getResource(
92 "/ietf-inet-types@2010-09-24.yang").getPath();
93 final SchemaContext context = resolveSchemaContextFromFiles(
94 topologyPath, typesPath);
95 assertTrue(context != null);
97 final BindingGenerator bindingGen = new BindingGeneratorImpl();
98 final List<Type> genTypes = bindingGen.generateTypes(context);
100 assertTrue(genTypes != null);
101 assertEquals(24, genTypes.size());
105 public void testLeafrefResolving() {
106 final String topologyPath = getClass().getResource(
107 "/leafref-test-models/abstract-topology@2013-02-08.yang")
109 final String interfacesPath = getClass().getResource(
110 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
112 // final String ifTypePath = getClass().getResource(
113 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
114 final String inetTypesPath = getClass().getResource(
115 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
117 final String yangTypesPath = getClass().getResource(
118 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
121 assertTrue(topologyPath != null);
122 assertTrue(interfacesPath != null);
123 // assertTrue(ifTypePath != null);
124 assertTrue(inetTypesPath != null);
125 assertTrue(yangTypesPath != null);
127 // final SchemaContext context = resolveSchemaContextFromFiles(
128 // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
130 final SchemaContext context = resolveSchemaContextFromFiles(
131 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
132 assertTrue(context != null);
133 assertEquals(4, context.getModules().size());
135 final BindingGenerator bindingGen = new BindingGeneratorImpl();
136 final List<Type> genTypes = bindingGen.generateTypes(context);
138 assertEquals(50, genTypes.size());
139 assertTrue(genTypes != null);
141 int resolvedLeafrefCount = 0;
142 for (final Type type : genTypes) {
143 if (type.getName().equals("InterfaceKey")
144 && type instanceof GeneratedTransferObject) {
145 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
146 final List<GeneratedProperty> properties = genTO
149 assertTrue(properties != null);
150 for (final GeneratedProperty property : properties) {
151 if (property.getName().equals("InterfaceId")) {
152 assertTrue(property.getReturnType() != null);
153 assertFalse(property.getReturnType().equals(
155 assertTrue(property.getReturnType().getName()
157 resolvedLeafrefCount++;
161 } else if (type.getName().equals("Interface")
162 && type instanceof GeneratedType) {
163 final GeneratedType genType = (GeneratedType) type;
164 final List<MethodSignature> methods = genType
165 .getMethodDefinitions();
167 assertTrue(methods != null);
168 for (final MethodSignature method : methods) {
169 if (method.getName().equals("getInterfaceKey")) {
170 assertTrue(method.getReturnType() != null);
171 assertFalse(method.getReturnType().equals(
173 assertTrue(method.getReturnType().getName()
174 .equals("InterfaceKey"));
175 resolvedLeafrefCount++;
176 } else if (method.getName().equals("getHigherLayerIf")) {
177 assertTrue(method.getReturnType() != null);
178 assertFalse(method.getReturnType().equals(
180 assertTrue(method.getReturnType().getName()
182 resolvedLeafrefCount++;
185 } else if (type.getName().equals("NetworkLink")
186 && type instanceof GeneratedType) {
187 final GeneratedType genType = (GeneratedType) type;
188 final List<MethodSignature> methods = genType
189 .getMethodDefinitions();
190 assertTrue(methods != null);
191 for (MethodSignature method : methods) {
192 if (method.getName().equals("getInterface")) {
193 assertTrue(method.getReturnType() != null);
194 assertFalse(method.getReturnType().equals(
196 assertTrue(method.getReturnType().getName()
198 resolvedLeafrefCount++;
201 } else if ((type.getName().equals("SourceNode") || type.getName()
202 .equals("DestinationNode"))
203 && type instanceof GeneratedType) {
204 final GeneratedType genType = (GeneratedType) type;
205 final List<MethodSignature> methods = genType
206 .getMethodDefinitions();
207 assertTrue(methods != null);
208 for (MethodSignature method : methods) {
209 if (method.getName().equals("getId")) {
210 assertTrue(method.getReturnType() != null);
211 assertFalse(method.getReturnType().equals(
213 assertTrue(method.getReturnType().getName()
215 resolvedLeafrefCount++;
218 } else if (type.getName().equals("Tunnel")
219 && type instanceof GeneratedType) {
220 final GeneratedType genType = (GeneratedType) type;
221 final List<MethodSignature> methods = genType
222 .getMethodDefinitions();
223 assertTrue(methods != null);
224 for (MethodSignature method : methods) {
225 if (method.getName().equals("getTunnelKey")) {
226 assertTrue(method.getReturnType() != null);
227 assertFalse(method.getReturnType().equals(
229 assertTrue(method.getReturnType().getName()
230 .equals("TunnelKey"));
231 resolvedLeafrefCount++;
234 } else if (type.getName().equals("TunnelKey")
235 && type instanceof GeneratedTransferObject) {
236 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
237 final List<GeneratedProperty> properties = genTO
240 assertTrue(properties != null);
241 for (final GeneratedProperty property : properties) {
242 if (property.getName().equals("TunnelId")) {
243 assertTrue(property.getReturnType() != null);
244 assertFalse(property.getReturnType().equals(
246 assertTrue(property.getReturnType().getName()
248 resolvedLeafrefCount++;
253 assertEquals(10, resolvedLeafrefCount);
257 public void testContainerResolving() {
258 final String filePath = getClass().getResource(
259 "/simple-container-demo.yang").getPath();
260 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
261 assertTrue(context != null);
263 final BindingGenerator bindingGen = new BindingGeneratorImpl();
264 final List<Type> genTypes = bindingGen.generateTypes(context);
266 assertTrue(genTypes != null);
267 assertEquals(3, genTypes.size());
269 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
270 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
272 assertEquals("SimpleContainer", simpleContainer.getName());
273 assertEquals("NestedContainer", nestedContainer.getName());
274 assertEquals(5, simpleContainer.getMethodDefinitions().size());
275 assertEquals(4, nestedContainer.getMethodDefinitions().size());
277 int methodsCount = 0;
278 for (final MethodSignature method : simpleContainer
279 .getMethodDefinitions()) {
280 if (method.getName().equals("getFoo")) {
281 method.getReturnType().getName().equals("Integer");
285 if (method.getName().equals("setFoo")) {
287 final MethodSignature.Parameter param = method.getParameters()
289 assertEquals("foo", param.getName());
290 assertEquals("Integer", param.getType().getName());
293 if (method.getName().equals("getBar")) {
294 method.getReturnType().getName().equals("String");
298 if (method.getName().equals("getNestedContainer")) {
299 method.getReturnType().getName().equals("NestedContainer");
303 assertEquals(4, methodsCount);
306 for (final MethodSignature method : nestedContainer
307 .getMethodDefinitions()) {
308 if (method.getName().equals("getFoo")) {
309 method.getReturnType().getName().equals("Short");
313 if (method.getName().equals("setFoo")) {
315 final MethodSignature.Parameter param = method.getParameters()
317 assertEquals("foo", param.getName());
318 assertEquals("Short", param.getType().getName());
321 if (method.getName().equals("getBar")) {
322 method.getReturnType().getName().equals("String");
326 if (method.getName().equals("setBar")) {
327 method.getReturnType().getName().equals("String");
331 assertEquals(4, methodsCount);
335 public void testLeafListResolving() {
336 final String filePath = getClass().getResource(
337 "/simple-leaf-list-demo.yang").getPath();
338 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
339 assertTrue(context != null);
341 final BindingGenerator bindingGen = new BindingGeneratorImpl();
342 final List<Type> genTypes = bindingGen.generateTypes(context);
344 assertTrue(genTypes != null);
345 assertEquals(3, genTypes.size());
347 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
348 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
350 assertEquals("SimpleContainer", simpleContainer.getName());
351 assertEquals("NestedContainer", nestedContainer.getName());
352 assertEquals(5, simpleContainer.getMethodDefinitions().size());
353 assertEquals(3, nestedContainer.getMethodDefinitions().size());
355 int methodsCount = 0;
356 for (final MethodSignature method : simpleContainer
357 .getMethodDefinitions()) {
358 if (method.getName().equals("getFoo")) {
359 method.getReturnType().getName().equals("List");
363 if (method.getName().equals("setFoo")) {
365 final MethodSignature.Parameter param = method.getParameters()
367 assertEquals("foo", param.getName());
368 assertEquals("List", param.getType().getName());
371 if (method.getName().equals("getBar")) {
372 method.getReturnType().getName().equals("String");
376 if (method.getName().equals("getNestedContainer")) {
377 method.getReturnType().getName().equals("NestedContainer");
381 assertEquals(4, methodsCount);
384 for (final MethodSignature method : nestedContainer
385 .getMethodDefinitions()) {
386 if (method.getName().equals("getFoo")) {
387 method.getReturnType().getName().equals("Short");
391 if (method.getName().equals("setFoo")) {
393 final MethodSignature.Parameter param = method.getParameters()
395 assertEquals("foo", param.getName());
396 assertEquals("Short", param.getType().getName());
399 if (method.getName().equals("getBar")) {
400 method.getReturnType().getName().equals("List");
404 assertEquals(3, methodsCount);
408 public void testListResolving() {
409 final String filePath = getClass()
410 .getResource("/simple-list-demo.yang").getPath();
411 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
412 assertTrue(context != null);
414 final BindingGenerator bindingGen = new BindingGeneratorImpl();
415 final List<Type> genTypes = bindingGen.generateTypes(context);
417 assertTrue(genTypes != null);
418 assertEquals(5, genTypes.size());
420 int genTypesCount = 0;
422 for (final Type type : genTypes) {
423 if (type instanceof GeneratedType &&
424 !(type instanceof GeneratedTransferObject)) {
425 final GeneratedType genType = (GeneratedType) type;
426 if (genType.getName().equals("ListParentContainer")) {
427 assertEquals(2, genType.getMethodDefinitions().size());
429 } else if (genType.getName().equals("SimpleList")) {
430 assertEquals(8, genType.getMethodDefinitions().size());
431 final List<MethodSignature> methods = genType
432 .getMethodDefinitions();
433 int methodsCount = 0;
434 for (final MethodSignature method : methods) {
435 if (method.getName().equals("getSimpleListKey")) {
436 assertEquals("SimpleListKey", method
437 .getReturnType().getName());
439 } else if (method.getName().equals(
440 "getListChildContainer")) {
441 assertEquals("ListChildContainer", method
442 .getReturnType().getName());
444 } else if (method.getName().equals("getFoo")) {
446 } else if (method.getName().equals("setFoo")) {
448 } else if (method.getName().equals("getSimpleLeafList")) {
450 } else if (method.getName().equals("setSimpleLeafList")) {
452 } else if (method.getName().equals("getBar")) {
456 assertEquals(7, methodsCount);
458 } else if (genType.getName().equals("ListChildContainer")) {
459 assertEquals(2, genType.getMethodDefinitions().size());
462 } else if (type instanceof GeneratedTransferObject) {
464 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
465 final List<GeneratedProperty> properties = genTO
467 final List<GeneratedProperty> hashProps = genTO
468 .getHashCodeIdentifiers();
469 final List<GeneratedProperty> equalProps = genTO
470 .getEqualsIdentifiers();
472 assertEquals(1, properties.size());
473 assertEquals("ListKey", properties.get(0).getName());
474 assertEquals("Byte", properties.get(0).getReturnType()
476 assertEquals(true, properties.get(0).isReadOnly());
477 assertEquals(1, hashProps.size());
478 assertEquals("ListKey", hashProps.get(0).getName());
479 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
480 assertEquals(1, equalProps.size());
481 assertEquals("ListKey", equalProps.get(0).getName());
482 assertEquals("Byte", equalProps.get(0).getReturnType()
486 assertEquals(3, genTypesCount);
487 assertEquals(1, genTOsCount);
491 public void testListCompositeKeyResolving() {
492 final String filePath = getClass().getResource(
493 "/list-composite-key.yang").getPath();
494 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
496 assertTrue(context != null);
498 final BindingGenerator bindingGen = new BindingGeneratorImpl();
499 final List<Type> genTypes = bindingGen.generateTypes(context);
501 assertTrue(genTypes != null);
502 assertEquals(7, genTypes.size());
504 int genTypesCount = 0;
506 for (final Type type : genTypes) {
507 if (type instanceof GeneratedType &&
508 !(type instanceof GeneratedTransferObject)) {
510 } else if (type instanceof GeneratedTransferObject) {
511 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
513 if (genTO.getName().equals("CompositeKeyListKey")) {
514 final List<GeneratedProperty> properties = genTO
516 int propertyCount = 0;
517 for (final GeneratedProperty prop : properties) {
518 if (prop.getName().equals("Key1")) {
520 } else if (prop.getName().equals("Key2")) {
524 assertEquals(2, propertyCount);
526 } else if (genTO.getName().equals("InnerListKey")) {
527 final List<GeneratedProperty> properties = genTO
529 assertEquals(1, properties.size());
535 assertEquals(5, genTypesCount);
536 assertEquals(2, genTOsCount);
540 public void testGeneratedTypes() {
541 final String filePath = getClass().getResource("/demo-topology.yang")
543 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
544 assertTrue(context != null);
546 final BindingGenerator bindingGen = new BindingGeneratorImpl();
547 final List<Type> genTypes = bindingGen.generateTypes(context);
549 assertTrue(genTypes != null);
550 assertEquals(14, genTypes.size());
552 int genTypesCount = 0;
554 for (final Type type : genTypes) {
555 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
557 } else if (type instanceof GeneratedTransferObject) {
562 assertEquals(11, genTypesCount);
563 assertEquals(3, genTOsCount);