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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
15 import java.util.ArrayList;
16 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
23 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
24 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
25 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
26 import org.opendaylight.controller.sal.binding.model.api.Type;
27 import org.opendaylight.controller.yang.model.api.Module;
28 import org.opendaylight.controller.yang.model.api.SchemaContext;
29 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
30 import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;
32 public class GeneratedTypesTest {
34 private SchemaContext resolveSchemaContextFromFiles(
35 final String... yangFiles) {
36 final YangModelParser parser = new YangModelParserImpl();
38 final List<File> inputFiles = new ArrayList<File>();
39 for (int i = 0; i < yangFiles.length; ++i) {
40 inputFiles.add(new File(yangFiles[i]));
43 final Set<Module> modules = parser.parseYangModels(inputFiles);
44 return parser.resolveSchemaContext(modules);
48 public void testLeafEnumResolving() {
49 final String ietfInterfacesPath = getClass().getResource(
50 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
51 final String ifTypePath = getClass().getResource(
52 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
53 final String yangTypesPath = getClass().getResource(
54 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
56 final SchemaContext context = resolveSchemaContextFromFiles(
57 ietfInterfacesPath, ifTypePath, yangTypesPath);
58 assertTrue(context != null);
60 final BindingGenerator bindingGen = new BindingGeneratorImpl();
61 final List<Type> genTypes = bindingGen.generateTypes(context);
62 assertTrue(genTypes != null);
66 public void testTypedefEnumResolving() {
67 final String ianaIfTypePath = getClass().getResource(
68 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
70 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
71 assertTrue(context != null);
73 final BindingGenerator bindingGen = new BindingGeneratorImpl();
74 final List<Type> genTypes = bindingGen.generateTypes(context);
75 assertTrue(genTypes != null);
76 assertEquals(2, genTypes.size());
78 final Type type = genTypes.get(0);
79 assertTrue(type instanceof GeneratedType);
81 final GeneratedType genType = (GeneratedType) type;
82 assertEquals(1, genType.getEnumDefintions().size());
84 final Enumeration enumer = genType.getEnumDefintions().get(0);
85 assertEquals(272, enumer.getValues().size());
89 public void testMultipleModulesResolving() {
90 final String topologyPath = getClass().getResource(
91 "/abstract-topology.yang").getPath();
92 final String typesPath = getClass().getResource(
93 "/ietf-inet-types@2010-09-24.yang").getPath();
94 final SchemaContext context = resolveSchemaContextFromFiles(
95 topologyPath, typesPath);
96 assertTrue(context != null);
98 final BindingGenerator bindingGen = new BindingGeneratorImpl();
99 final List<Type> genTypes = bindingGen.generateTypes(context);
101 assertTrue(genTypes != null);
102 assertEquals(24, genTypes.size());
106 public void testLeafrefResolving() {
107 final String topologyPath = getClass().getResource(
108 "/leafref-test-models/abstract-topology@2013-02-08.yang")
110 final String interfacesPath = getClass().getResource(
111 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
113 // final String ifTypePath = getClass().getResource(
114 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
115 final String inetTypesPath = getClass().getResource(
116 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
118 final String yangTypesPath = getClass().getResource(
119 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
122 assertTrue(topologyPath != null);
123 assertTrue(interfacesPath != null);
124 // assertTrue(ifTypePath != null);
125 assertTrue(inetTypesPath != null);
126 assertTrue(yangTypesPath != null);
128 // final SchemaContext context = resolveSchemaContextFromFiles(
129 // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
131 final SchemaContext context = resolveSchemaContextFromFiles(
132 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
133 assertTrue(context != null);
134 assertEquals(4, context.getModules().size());
136 final BindingGenerator bindingGen = new BindingGeneratorImpl();
137 final List<Type> genTypes = bindingGen.generateTypes(context);
139 assertEquals(46, genTypes.size());
140 assertTrue(genTypes != null);
142 int resolvedLeafrefCount = 0;
143 for (final Type type : genTypes) {
144 if (type.getName().equals("InterfaceKey")
145 && type instanceof GeneratedTransferObject) {
146 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
147 final List<GeneratedProperty> properties = genTO
150 assertTrue(properties != null);
151 for (final GeneratedProperty property : properties) {
152 if (property.getName().equals("InterfaceId")) {
153 assertTrue(property.getReturnType() != null);
154 assertFalse(property.getReturnType().equals(
156 assertTrue(property.getReturnType().getName()
158 resolvedLeafrefCount++;
162 } else if (type.getName().equals("Interface")
163 && type instanceof GeneratedType) {
164 final GeneratedType genType = (GeneratedType) type;
165 final List<MethodSignature> methods = genType
166 .getMethodDefinitions();
168 assertTrue(methods != null);
169 for (final MethodSignature method : methods) {
170 if (method.getName().equals("getInterfaceKey")) {
171 assertTrue(method.getReturnType() != null);
172 assertFalse(method.getReturnType().equals(
174 assertTrue(method.getReturnType().getName()
175 .equals("InterfaceKey"));
176 resolvedLeafrefCount++;
177 } else if (method.getName().equals("getHigherLayerIf")) {
178 assertTrue(method.getReturnType() != null);
179 assertFalse(method.getReturnType().equals(
181 assertTrue(method.getReturnType().getName()
183 resolvedLeafrefCount++;
186 } else if (type.getName().equals("NetworkLink")
187 && type instanceof GeneratedType) {
188 final GeneratedType genType = (GeneratedType) type;
189 final List<MethodSignature> methods = genType
190 .getMethodDefinitions();
191 assertTrue(methods != null);
192 for (MethodSignature method : methods) {
193 if (method.getName().equals("getInterface")) {
194 assertTrue(method.getReturnType() != null);
195 assertFalse(method.getReturnType().equals(
197 assertTrue(method.getReturnType().getName()
199 resolvedLeafrefCount++;
202 } else if ((type.getName().equals("SourceNode") || type.getName()
203 .equals("DestinationNode"))
204 && type instanceof GeneratedType) {
205 final GeneratedType genType = (GeneratedType) type;
206 final List<MethodSignature> methods = genType
207 .getMethodDefinitions();
208 assertTrue(methods != null);
209 for (MethodSignature method : methods) {
210 if (method.getName().equals("getId")) {
211 assertTrue(method.getReturnType() != null);
212 assertFalse(method.getReturnType().equals(
214 assertTrue(method.getReturnType().getName()
216 resolvedLeafrefCount++;
219 } else if (type.getName().equals("Tunnel")
220 && type instanceof GeneratedType) {
221 final GeneratedType genType = (GeneratedType) type;
222 final List<MethodSignature> methods = genType
223 .getMethodDefinitions();
224 assertTrue(methods != null);
225 for (MethodSignature method : methods) {
226 if (method.getName().equals("getTunnelKey")) {
227 assertTrue(method.getReturnType() != null);
228 assertFalse(method.getReturnType().equals(
230 assertTrue(method.getReturnType().getName()
231 .equals("TunnelKey"));
232 resolvedLeafrefCount++;
235 } else if (type.getName().equals("TunnelKey")
236 && type instanceof GeneratedTransferObject) {
237 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
238 final List<GeneratedProperty> properties = genTO
241 assertTrue(properties != null);
242 for (final GeneratedProperty property : properties) {
243 if (property.getName().equals("TunnelId")) {
244 assertTrue(property.getReturnType() != null);
245 assertFalse(property.getReturnType().equals(
247 assertTrue(property.getReturnType().getName()
249 resolvedLeafrefCount++;
254 assertEquals(10, resolvedLeafrefCount);
258 public void testContainerResolving() {
259 final String filePath = getClass().getResource(
260 "/simple-container-demo.yang").getPath();
261 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
262 assertTrue(context != null);
264 final BindingGenerator bindingGen = new BindingGeneratorImpl();
265 final List<Type> genTypes = bindingGen.generateTypes(context);
267 assertTrue(genTypes != null);
268 assertEquals(3, genTypes.size());
270 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
271 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
273 assertEquals("SimpleContainer", simpleContainer.getName());
274 assertEquals("NestedContainer", nestedContainer.getName());
275 assertEquals(5, simpleContainer.getMethodDefinitions().size());
276 assertEquals(4, nestedContainer.getMethodDefinitions().size());
278 int methodsCount = 0;
279 for (final MethodSignature method : simpleContainer
280 .getMethodDefinitions()) {
281 if (method.getName().equals("getFoo")) {
282 method.getReturnType().getName().equals("Integer");
286 if (method.getName().equals("setFoo")) {
288 final MethodSignature.Parameter param = method.getParameters()
290 assertEquals("foo", param.getName());
291 assertEquals("Integer", param.getType().getName());
294 if (method.getName().equals("getBar")) {
295 method.getReturnType().getName().equals("String");
299 if (method.getName().equals("getNestedContainer")) {
300 method.getReturnType().getName().equals("NestedContainer");
304 assertEquals(4, methodsCount);
307 for (final MethodSignature method : nestedContainer
308 .getMethodDefinitions()) {
309 if (method.getName().equals("getFoo")) {
310 method.getReturnType().getName().equals("Short");
314 if (method.getName().equals("setFoo")) {
316 final MethodSignature.Parameter param = method.getParameters()
318 assertEquals("foo", param.getName());
319 assertEquals("Short", param.getType().getName());
322 if (method.getName().equals("getBar")) {
323 method.getReturnType().getName().equals("String");
327 if (method.getName().equals("setBar")) {
328 method.getReturnType().getName().equals("String");
332 assertEquals(4, methodsCount);
336 public void testLeafListResolving() {
337 final String filePath = getClass().getResource(
338 "/simple-leaf-list-demo.yang").getPath();
339 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
340 assertTrue(context != null);
342 final BindingGenerator bindingGen = new BindingGeneratorImpl();
343 final List<Type> genTypes = bindingGen.generateTypes(context);
345 assertTrue(genTypes != null);
346 assertEquals(3, genTypes.size());
348 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
349 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
351 assertEquals("SimpleContainer", simpleContainer.getName());
352 assertEquals("NestedContainer", nestedContainer.getName());
353 assertEquals(5, simpleContainer.getMethodDefinitions().size());
354 assertEquals(3, nestedContainer.getMethodDefinitions().size());
356 int methodsCount = 0;
357 for (final MethodSignature method : simpleContainer
358 .getMethodDefinitions()) {
359 if (method.getName().equals("getFoo")) {
360 method.getReturnType().getName().equals("List");
364 if (method.getName().equals("setFoo")) {
366 final MethodSignature.Parameter param = method.getParameters()
368 assertEquals("foo", param.getName());
369 assertEquals("List", param.getType().getName());
372 if (method.getName().equals("getBar")) {
373 method.getReturnType().getName().equals("String");
377 if (method.getName().equals("getNestedContainer")) {
378 method.getReturnType().getName().equals("NestedContainer");
382 assertEquals(4, methodsCount);
385 for (final MethodSignature method : nestedContainer
386 .getMethodDefinitions()) {
387 if (method.getName().equals("getFoo")) {
388 method.getReturnType().getName().equals("Short");
392 if (method.getName().equals("setFoo")) {
394 final MethodSignature.Parameter param = method.getParameters()
396 assertEquals("foo", param.getName());
397 assertEquals("Short", param.getType().getName());
400 if (method.getName().equals("getBar")) {
401 method.getReturnType().getName().equals("List");
405 assertEquals(3, methodsCount);
409 public void testListResolving() {
410 final String filePath = getClass()
411 .getResource("/simple-list-demo.yang").getPath();
412 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
413 assertTrue(context != null);
415 final BindingGenerator bindingGen = new BindingGeneratorImpl();
416 final List<Type> genTypes = bindingGen.generateTypes(context);
418 assertTrue(genTypes != null);
419 assertEquals(5, genTypes.size());
421 int genTypesCount = 0;
423 for (final Type type : genTypes) {
424 if (type instanceof GeneratedType && !(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 && !(type instanceof GeneratedTransferObject)) {
509 } else if (type instanceof GeneratedTransferObject) {
510 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
512 if (genTO.getName().equals("CompositeKeyListKey")) {
513 final List<GeneratedProperty> properties = genTO
515 int propertyCount = 0;
516 for (final GeneratedProperty prop : properties) {
517 if (prop.getName().equals("Key1")) {
519 } else if (prop.getName().equals("Key2")) {
523 assertEquals(2, propertyCount);
525 } else if (genTO.getName().equals("InnerListKey")) {
526 final List<GeneratedProperty> properties = genTO
528 assertEquals(1, properties.size());
534 assertEquals(5, genTypesCount);
535 assertEquals(2, genTOsCount);
539 public void testGeneratedTypes() {
540 final String filePath = getClass().getResource("/demo-topology.yang")
542 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
543 assertTrue(context != null);
545 final BindingGenerator bindingGen = new BindingGeneratorImpl();
546 final List<Type> genTypes = bindingGen.generateTypes(context);
548 assertTrue(genTypes != null);
549 assertEquals(14, genTypes.size());
551 int genTypesCount = 0;
553 for (final Type type : genTypes) {
554 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
556 } else if (type instanceof GeneratedTransferObject) {
561 assertEquals(11, genTypesCount);
562 assertEquals(3, genTOsCount);