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.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.parser.impl.YangParserImpl;
30 public class GeneratedTypesTest {
32 private SchemaContext resolveSchemaContextFromFiles(
33 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 testLeafEnumResolving() {
47 final String ietfInterfacesPath = getClass().getResource(
48 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
49 final String ifTypePath = getClass().getResource(
50 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
51 final String yangTypesPath = getClass().getResource(
52 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
54 final SchemaContext context = resolveSchemaContextFromFiles(
55 ietfInterfacesPath, ifTypePath, yangTypesPath);
56 assertTrue(context != null);
58 final BindingGenerator bindingGen = new BindingGeneratorImpl();
59 final List<Type> genTypes = bindingGen.generateTypes(context);
60 assertTrue(genTypes != null);
64 public void testTypedefEnumResolving() {
65 final String ianaIfTypePath = getClass().getResource(
66 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
68 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
69 assertTrue(context != null);
71 final BindingGenerator bindingGen = new BindingGeneratorImpl();
72 final List<Type> genTypes = bindingGen.generateTypes(context);
73 assertTrue(genTypes != null);
74 assertEquals(2, genTypes.size());
76 final Type type = genTypes.get(0);
77 assertTrue(type instanceof GeneratedType);
79 final GeneratedType genType = (GeneratedType) type;
80 assertEquals(1, genType.getEnumDefintions().size());
82 final Enumeration enumer = genType.getEnumDefintions().get(0);
83 assertEquals(272, enumer.getValues().size());
87 public void testMultipleModulesResolving() {
88 final String topologyPath = getClass().getResource(
89 "/abstract-topology.yang").getPath();
90 final String typesPath = getClass().getResource(
91 "/ietf-inet-types@2010-09-24.yang").getPath();
92 final SchemaContext context = resolveSchemaContextFromFiles(
93 topologyPath, typesPath);
94 assertTrue(context != null);
96 final BindingGenerator bindingGen = new BindingGeneratorImpl();
97 final List<Type> genTypes = bindingGen.generateTypes(context);
99 assertTrue(genTypes != null);
100 assertEquals(24, genTypes.size());
104 public void testLeafrefResolving() {
105 final String topologyPath = getClass().getResource(
106 "/leafref-test-models/abstract-topology@2013-02-08.yang")
108 final String interfacesPath = getClass().getResource(
109 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
111 // final String ifTypePath = getClass().getResource(
112 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
113 final String inetTypesPath = getClass().getResource(
114 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
116 final String yangTypesPath = getClass().getResource(
117 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
120 assertTrue(topologyPath != null);
121 assertTrue(interfacesPath != null);
122 // assertTrue(ifTypePath != null);
123 assertTrue(inetTypesPath != null);
124 assertTrue(yangTypesPath != null);
126 // final SchemaContext context = resolveSchemaContextFromFiles(
127 // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
129 final SchemaContext context = resolveSchemaContextFromFiles(
130 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
131 assertTrue(context != null);
132 assertEquals(4, context.getModules().size());
134 final BindingGenerator bindingGen = new BindingGeneratorImpl();
135 final List<Type> genTypes = bindingGen.generateTypes(context);
137 assertEquals(46, genTypes.size());
138 assertTrue(genTypes != null);
140 int resolvedLeafrefCount = 0;
141 for (final Type type : genTypes) {
142 if (type.getName().equals("InterfaceKey")
143 && type instanceof GeneratedTransferObject) {
144 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
145 final List<GeneratedProperty> properties = genTO
148 assertTrue(properties != null);
149 for (final GeneratedProperty property : properties) {
150 if (property.getName().equals("InterfaceId")) {
151 assertTrue(property.getReturnType() != null);
152 assertFalse(property.getReturnType().equals(
154 assertTrue(property.getReturnType().getName()
156 resolvedLeafrefCount++;
160 } else if (type.getName().equals("Interface")
161 && type instanceof GeneratedType) {
162 final GeneratedType genType = (GeneratedType) type;
163 final List<MethodSignature> methods = genType
164 .getMethodDefinitions();
166 assertTrue(methods != null);
167 for (final MethodSignature method : methods) {
168 if (method.getName().equals("getInterfaceKey")) {
169 assertTrue(method.getReturnType() != null);
170 assertFalse(method.getReturnType().equals(
172 assertTrue(method.getReturnType().getName()
173 .equals("InterfaceKey"));
174 resolvedLeafrefCount++;
175 } else if (method.getName().equals("getHigherLayerIf")) {
176 assertTrue(method.getReturnType() != null);
177 assertFalse(method.getReturnType().equals(
179 assertTrue(method.getReturnType().getName()
181 resolvedLeafrefCount++;
184 } else if (type.getName().equals("NetworkLink")
185 && type instanceof GeneratedType) {
186 final GeneratedType genType = (GeneratedType) type;
187 final List<MethodSignature> methods = genType
188 .getMethodDefinitions();
189 assertTrue(methods != null);
190 for (MethodSignature method : methods) {
191 if (method.getName().equals("getInterface")) {
192 assertTrue(method.getReturnType() != null);
193 assertFalse(method.getReturnType().equals(
195 assertTrue(method.getReturnType().getName()
197 resolvedLeafrefCount++;
200 } else if ((type.getName().equals("SourceNode") || type.getName()
201 .equals("DestinationNode"))
202 && type instanceof GeneratedType) {
203 final GeneratedType genType = (GeneratedType) type;
204 final List<MethodSignature> methods = genType
205 .getMethodDefinitions();
206 assertTrue(methods != null);
207 for (MethodSignature method : methods) {
208 if (method.getName().equals("getId")) {
209 assertTrue(method.getReturnType() != null);
210 assertFalse(method.getReturnType().equals(
212 assertTrue(method.getReturnType().getName()
214 resolvedLeafrefCount++;
217 } else if (type.getName().equals("Tunnel")
218 && type instanceof GeneratedType) {
219 final GeneratedType genType = (GeneratedType) type;
220 final List<MethodSignature> methods = genType
221 .getMethodDefinitions();
222 assertTrue(methods != null);
223 for (MethodSignature method : methods) {
224 if (method.getName().equals("getTunnelKey")) {
225 assertTrue(method.getReturnType() != null);
226 assertFalse(method.getReturnType().equals(
228 assertTrue(method.getReturnType().getName()
229 .equals("TunnelKey"));
230 resolvedLeafrefCount++;
233 } else if (type.getName().equals("TunnelKey")
234 && type instanceof GeneratedTransferObject) {
235 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
236 final List<GeneratedProperty> properties = genTO
239 assertTrue(properties != null);
240 for (final GeneratedProperty property : properties) {
241 if (property.getName().equals("TunnelId")) {
242 assertTrue(property.getReturnType() != null);
243 assertFalse(property.getReturnType().equals(
245 assertTrue(property.getReturnType().getName()
247 resolvedLeafrefCount++;
252 assertEquals(10, resolvedLeafrefCount);
256 public void testContainerResolving() {
257 final String filePath = getClass().getResource(
258 "/simple-container-demo.yang").getPath();
259 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
260 assertTrue(context != null);
262 final BindingGenerator bindingGen = new BindingGeneratorImpl();
263 final List<Type> genTypes = bindingGen.generateTypes(context);
265 assertTrue(genTypes != null);
266 assertEquals(3, genTypes.size());
268 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
269 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
271 assertEquals("SimpleContainer", simpleContainer.getName());
272 assertEquals("NestedContainer", nestedContainer.getName());
273 assertEquals(5, simpleContainer.getMethodDefinitions().size());
274 assertEquals(4, nestedContainer.getMethodDefinitions().size());
276 int methodsCount = 0;
277 for (final MethodSignature method : simpleContainer
278 .getMethodDefinitions()) {
279 if (method.getName().equals("getFoo")) {
280 method.getReturnType().getName().equals("Integer");
284 if (method.getName().equals("setFoo")) {
286 final MethodSignature.Parameter param = method.getParameters()
288 assertEquals("foo", param.getName());
289 assertEquals("Integer", param.getType().getName());
292 if (method.getName().equals("getBar")) {
293 method.getReturnType().getName().equals("String");
297 if (method.getName().equals("getNestedContainer")) {
298 method.getReturnType().getName().equals("NestedContainer");
302 assertEquals(4, methodsCount);
305 for (final MethodSignature method : nestedContainer
306 .getMethodDefinitions()) {
307 if (method.getName().equals("getFoo")) {
308 method.getReturnType().getName().equals("Short");
312 if (method.getName().equals("setFoo")) {
314 final MethodSignature.Parameter param = method.getParameters()
316 assertEquals("foo", param.getName());
317 assertEquals("Short", param.getType().getName());
320 if (method.getName().equals("getBar")) {
321 method.getReturnType().getName().equals("String");
325 if (method.getName().equals("setBar")) {
326 method.getReturnType().getName().equals("String");
330 assertEquals(4, methodsCount);
334 public void testLeafListResolving() {
335 final String filePath = getClass().getResource(
336 "/simple-leaf-list-demo.yang").getPath();
337 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
338 assertTrue(context != null);
340 final BindingGenerator bindingGen = new BindingGeneratorImpl();
341 final List<Type> genTypes = bindingGen.generateTypes(context);
343 assertTrue(genTypes != null);
344 assertEquals(3, genTypes.size());
346 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
347 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
349 assertEquals("SimpleContainer", simpleContainer.getName());
350 assertEquals("NestedContainer", nestedContainer.getName());
351 assertEquals(5, simpleContainer.getMethodDefinitions().size());
352 assertEquals(3, nestedContainer.getMethodDefinitions().size());
354 int methodsCount = 0;
355 for (final MethodSignature method : simpleContainer
356 .getMethodDefinitions()) {
357 if (method.getName().equals("getFoo")) {
358 method.getReturnType().getName().equals("List");
362 if (method.getName().equals("setFoo")) {
364 final MethodSignature.Parameter param = method.getParameters()
366 assertEquals("foo", param.getName());
367 assertEquals("List", param.getType().getName());
370 if (method.getName().equals("getBar")) {
371 method.getReturnType().getName().equals("String");
375 if (method.getName().equals("getNestedContainer")) {
376 method.getReturnType().getName().equals("NestedContainer");
380 assertEquals(4, methodsCount);
383 for (final MethodSignature method : nestedContainer
384 .getMethodDefinitions()) {
385 if (method.getName().equals("getFoo")) {
386 method.getReturnType().getName().equals("Short");
390 if (method.getName().equals("setFoo")) {
392 final MethodSignature.Parameter param = method.getParameters()
394 assertEquals("foo", param.getName());
395 assertEquals("Short", param.getType().getName());
398 if (method.getName().equals("getBar")) {
399 method.getReturnType().getName().equals("List");
403 assertEquals(3, methodsCount);
407 public void testListResolving() {
408 final String filePath = getClass()
409 .getResource("/simple-list-demo.yang").getPath();
410 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
411 assertTrue(context != null);
413 final BindingGenerator bindingGen = new BindingGeneratorImpl();
414 final List<Type> genTypes = bindingGen.generateTypes(context);
416 assertTrue(genTypes != null);
417 assertEquals(5, genTypes.size());
419 int genTypesCount = 0;
421 for (final Type type : genTypes) {
422 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
423 final GeneratedType genType = (GeneratedType) type;
424 if (genType.getName().equals("ListParentContainer")) {
425 assertEquals(2, genType.getMethodDefinitions().size());
427 } else if (genType.getName().equals("SimpleList")) {
428 assertEquals(8, genType.getMethodDefinitions().size());
429 final List<MethodSignature> methods = genType
430 .getMethodDefinitions();
431 int methodsCount = 0;
432 for (final MethodSignature method : methods) {
433 if (method.getName().equals("getSimpleListKey")) {
434 assertEquals("SimpleListKey", method
435 .getReturnType().getName());
437 } else if (method.getName().equals(
438 "getListChildContainer")) {
439 assertEquals("ListChildContainer", method
440 .getReturnType().getName());
442 } else if (method.getName().equals("getFoo")) {
444 } else if (method.getName().equals("setFoo")) {
446 } else if (method.getName().equals("getSimpleLeafList")) {
448 } else if (method.getName().equals("setSimpleLeafList")) {
450 } else if (method.getName().equals("getBar")) {
454 assertEquals(7, methodsCount);
456 } else if (genType.getName().equals("ListChildContainer")) {
457 assertEquals(2, genType.getMethodDefinitions().size());
460 } else if (type instanceof GeneratedTransferObject) {
462 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
463 final List<GeneratedProperty> properties = genTO
465 final List<GeneratedProperty> hashProps = genTO
466 .getHashCodeIdentifiers();
467 final List<GeneratedProperty> equalProps = genTO
468 .getEqualsIdentifiers();
470 assertEquals(1, properties.size());
471 assertEquals("ListKey", properties.get(0).getName());
472 assertEquals("Byte", properties.get(0).getReturnType()
474 assertEquals(true, properties.get(0).isReadOnly());
475 assertEquals(1, hashProps.size());
476 assertEquals("ListKey", hashProps.get(0).getName());
477 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
478 assertEquals(1, equalProps.size());
479 assertEquals("ListKey", equalProps.get(0).getName());
480 assertEquals("Byte", equalProps.get(0).getReturnType()
484 assertEquals(3, genTypesCount);
485 assertEquals(1, genTOsCount);
489 public void testListCompositeKeyResolving() {
490 final String filePath = getClass().getResource(
491 "/list-composite-key.yang").getPath();
492 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
494 assertTrue(context != null);
496 final BindingGenerator bindingGen = new BindingGeneratorImpl();
497 final List<Type> genTypes = bindingGen.generateTypes(context);
499 assertTrue(genTypes != null);
500 assertEquals(7, genTypes.size());
502 int genTypesCount = 0;
504 for (final Type type : genTypes) {
505 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
507 } else if (type instanceof GeneratedTransferObject) {
508 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
510 if (genTO.getName().equals("CompositeKeyListKey")) {
511 final List<GeneratedProperty> properties = genTO
513 int propertyCount = 0;
514 for (final GeneratedProperty prop : properties) {
515 if (prop.getName().equals("Key1")) {
517 } else if (prop.getName().equals("Key2")) {
521 assertEquals(2, propertyCount);
523 } else if (genTO.getName().equals("InnerListKey")) {
524 final List<GeneratedProperty> properties = genTO
526 assertEquals(1, properties.size());
532 assertEquals(5, genTypesCount);
533 assertEquals(2, genTOsCount);
537 public void testGeneratedTypes() {
538 final String filePath = getClass().getResource("/demo-topology.yang")
540 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
541 assertTrue(context != null);
543 final BindingGenerator bindingGen = new BindingGeneratorImpl();
544 final List<Type> genTypes = bindingGen.generateTypes(context);
546 assertTrue(genTypes != null);
547 assertEquals(14, genTypes.size());
549 int genTypesCount = 0;
551 for (final Type type : genTypes) {
552 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
554 } else if (type instanceof GeneratedTransferObject) {
559 assertEquals(11, genTypesCount);
560 assertEquals(3, genTOsCount);