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.yang.types.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
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.generator.impl.BindingGeneratorImpl;
20 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
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.model.parser.impl.YangModelParserImpl;
31 public class GeneratedTypesTest {
33 private SchemaContext resolveSchemaContextFromFiles(
34 final String... yangFiles) {
35 final YangModelParser parser = new YangModelParserImpl();
36 final Set<Module> modules = parser.parseYangModels(yangFiles);
38 return parser.resolveSchemaContext(modules);
42 public void testLeafEnumResolving() {
43 final String ietfInterfacesPath = getClass().getResource(
44 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
45 final String ifTypePath = getClass().getResource(
46 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
47 final String yangTypesPath = getClass().getResource(
48 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
50 final SchemaContext context = resolveSchemaContextFromFiles(
51 ietfInterfacesPath, ifTypePath, yangTypesPath);
52 assertTrue(context != null);
54 final BindingGenerator bindingGen = new BindingGeneratorImpl();
55 final List<Type> genTypes = bindingGen.generateTypes(context);
56 assertTrue(genTypes != null);
60 public void testTypedefEnumResolving() {
61 final String ianaIfTypePath = getClass().getResource(
62 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
64 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
65 assertTrue(context != null);
67 final BindingGenerator bindingGen = new BindingGeneratorImpl();
68 final List<Type> genTypes = bindingGen.generateTypes(context);
69 assertTrue(genTypes != null);
70 assertEquals(1, genTypes.size());
72 final Type type = genTypes.get(0);
73 assertTrue(type instanceof GeneratedType);
75 final GeneratedType genType = (GeneratedType) type;
76 assertEquals(1, genType.getEnumDefintions().size());
78 final Enumeration enumer = genType.getEnumDefintions().get(0);
79 assertEquals(272, enumer.getValues().size());
83 public void testMultipleModulesResolving() {
84 final String topologyPath = getClass().getResource(
85 "/abstract-topology.yang").getPath();
86 final String typesPath = getClass().getResource(
87 "/ietf-inet-types@2010-09-24.yang").getPath();
88 final SchemaContext context = resolveSchemaContextFromFiles(
89 topologyPath, typesPath);
90 assertTrue(context != null);
92 final BindingGenerator bindingGen = new BindingGeneratorImpl();
93 final List<Type> genTypes = bindingGen.generateTypes(context);
95 assertTrue(genTypes != null);
96 assertEquals(13, genTypes.size());
100 public void testLeafrefResolving() {
101 final String topologyPath = getClass().getResource(
102 "/leafref-test-models/abstract-topology@2013-02-08.yang")
104 final String interfacesPath = getClass().getResource(
105 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
107 // final String ifTypePath = getClass().getResource(
108 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
109 final String inetTypesPath = getClass().getResource(
110 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
112 final String yangTypesPath = getClass().getResource(
113 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
116 assertTrue(topologyPath != null);
117 assertTrue(interfacesPath != null);
118 // assertTrue(ifTypePath != null);
119 assertTrue(inetTypesPath != null);
120 assertTrue(yangTypesPath != null);
122 // final SchemaContext context = resolveSchemaContextFromFiles(
123 // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
125 final SchemaContext context = resolveSchemaContextFromFiles(
126 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
127 assertTrue(context != null);
128 assertEquals(4, context.getModules().size());
130 final BindingGenerator bindingGen = new BindingGeneratorImpl();
131 final List<Type> genTypes = bindingGen.generateTypes(context);
133 assertEquals(25, genTypes.size());
134 assertTrue(genTypes != null);
136 int resolvedLeafrefCount = 0;
137 for (final Type type : genTypes) {
138 if (type.getName().equals("InterfaceKey")
139 && type instanceof GeneratedTransferObject) {
140 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
141 final List<GeneratedProperty> properties = genTO
144 assertTrue(properties != null);
145 for (final GeneratedProperty property : properties) {
146 if (property.getName().equals("InterfaceId")) {
147 assertTrue(property.getReturnType() != null);
148 assertFalse(property.getReturnType().equals(
150 assertTrue(property.getReturnType().getName()
152 resolvedLeafrefCount++;
156 } else if (type.getName().equals("Interface")
157 && type instanceof GeneratedType) {
158 final GeneratedType genType = (GeneratedType) type;
159 final List<MethodSignature> methods = genType
160 .getMethodDefinitions();
162 assertTrue(methods != null);
163 for (final MethodSignature method : methods) {
164 if (method.getName().equals("getInterfaceKey")) {
165 assertTrue(method.getReturnType() != null);
166 assertFalse(method.getReturnType().equals(
168 assertTrue(method.getReturnType().getName()
169 .equals("InterfaceKey"));
170 resolvedLeafrefCount++;
171 } else if (method.getName().equals("getHigherLayerIf")) {
172 assertTrue(method.getReturnType() != null);
173 assertFalse(method.getReturnType().equals(
175 assertTrue(method.getReturnType().getName()
177 resolvedLeafrefCount++;
180 } else if (type.getName().equals("NetworkLink")
181 && type instanceof GeneratedType) {
182 final GeneratedType genType = (GeneratedType) type;
183 final List<MethodSignature> methods = genType
184 .getMethodDefinitions();
185 assertTrue(methods != null);
186 for (MethodSignature method : methods) {
187 if (method.getName().equals("getInterface")) {
188 assertTrue(method.getReturnType() != null);
189 assertFalse(method.getReturnType().equals(
191 assertTrue(method.getReturnType().getName()
193 resolvedLeafrefCount++;
196 } else if ((type.getName().equals("SourceNode") || type.getName()
197 .equals("DestinationNode"))
198 && type instanceof GeneratedType) {
199 final GeneratedType genType = (GeneratedType) type;
200 final List<MethodSignature> methods = genType
201 .getMethodDefinitions();
202 assertTrue(methods != null);
203 for (MethodSignature method : methods) {
204 if (method.getName().equals("getId")) {
205 assertTrue(method.getReturnType() != null);
206 assertFalse(method.getReturnType().equals(
208 assertTrue(method.getReturnType().getName()
210 resolvedLeafrefCount++;
213 } else if (type.getName().equals("Tunnel")
214 && type instanceof GeneratedType) {
215 final GeneratedType genType = (GeneratedType) type;
216 final List<MethodSignature> methods = genType
217 .getMethodDefinitions();
218 assertTrue(methods != null);
219 for (MethodSignature method : methods) {
220 if (method.getName().equals("getTunnelKey")) {
221 assertTrue(method.getReturnType() != null);
222 assertFalse(method.getReturnType().equals(
224 assertTrue(method.getReturnType().getName()
225 .equals("TunnelKey"));
226 resolvedLeafrefCount++;
229 } else if (type.getName().equals("TunnelKey")
230 && type instanceof GeneratedTransferObject) {
231 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
232 final List<GeneratedProperty> properties = genTO
235 assertTrue(properties != null);
236 for (final GeneratedProperty property : properties) {
237 if (property.getName().equals("TunnelId")) {
238 assertTrue(property.getReturnType() != null);
239 assertFalse(property.getReturnType().equals(
241 assertTrue(property.getReturnType().getName()
243 resolvedLeafrefCount++;
248 assertEquals(10, resolvedLeafrefCount);
252 public void testContainerResolving() {
253 final String filePath = getClass().getResource(
254 "/simple-container-demo.yang").getPath();
255 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
256 assertTrue(context != null);
258 final BindingGenerator bindingGen = new BindingGeneratorImpl();
259 final List<Type> genTypes = bindingGen.generateTypes(context);
261 assertTrue(genTypes != null);
262 assertEquals(3, genTypes.size());
264 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
265 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
267 assertEquals("SimpleContainer", simpleContainer.getName());
268 assertEquals("NestedContainer", nestedContainer.getName());
269 assertEquals(4, simpleContainer.getMethodDefinitions().size());
270 assertEquals(4, nestedContainer.getMethodDefinitions().size());
272 int methodsCount = 0;
273 for (final MethodSignature method : simpleContainer
274 .getMethodDefinitions()) {
275 if (method.getName().equals("getFoo")) {
276 method.getReturnType().getName().equals("Integer");
280 if (method.getName().equals("setFoo")) {
282 final MethodSignature.Parameter param = method.getParameters()
284 assertEquals("foo", param.getName());
285 assertEquals("Integer", param.getType().getName());
288 if (method.getName().equals("getBar")) {
289 method.getReturnType().getName().equals("String");
293 if (method.getName().equals("getNestedContainer")) {
294 method.getReturnType().getName().equals("NestedContainer");
298 assertEquals(4, methodsCount);
301 for (final MethodSignature method : nestedContainer
302 .getMethodDefinitions()) {
303 if (method.getName().equals("getFoo")) {
304 method.getReturnType().getName().equals("Short");
308 if (method.getName().equals("setFoo")) {
310 final MethodSignature.Parameter param = method.getParameters()
312 assertEquals("foo", param.getName());
313 assertEquals("Short", param.getType().getName());
316 if (method.getName().equals("getBar")) {
317 method.getReturnType().getName().equals("String");
321 if (method.getName().equals("setBar")) {
322 method.getReturnType().getName().equals("String");
326 assertEquals(4, methodsCount);
330 public void testLeafListResolving() {
331 final String filePath = getClass().getResource(
332 "/simple-leaf-list-demo.yang").getPath();
333 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
334 assertTrue(context != null);
336 final BindingGenerator bindingGen = new BindingGeneratorImpl();
337 final List<Type> genTypes = bindingGen.generateTypes(context);
339 assertTrue(genTypes != null);
340 assertEquals(3, genTypes.size());
342 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
343 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
345 assertEquals("SimpleContainer", simpleContainer.getName());
346 assertEquals("NestedContainer", nestedContainer.getName());
347 assertEquals(4, simpleContainer.getMethodDefinitions().size());
348 assertEquals(3, nestedContainer.getMethodDefinitions().size());
350 int methodsCount = 0;
351 for (final MethodSignature method : simpleContainer
352 .getMethodDefinitions()) {
353 if (method.getName().equals("getFoo")) {
354 method.getReturnType().getName().equals("List");
358 if (method.getName().equals("setFoo")) {
360 final MethodSignature.Parameter param = method.getParameters()
362 assertEquals("foo", param.getName());
363 assertEquals("List", param.getType().getName());
366 if (method.getName().equals("getBar")) {
367 method.getReturnType().getName().equals("String");
371 if (method.getName().equals("getNestedContainer")) {
372 method.getReturnType().getName().equals("NestedContainer");
376 assertEquals(4, methodsCount);
379 for (final MethodSignature method : nestedContainer
380 .getMethodDefinitions()) {
381 if (method.getName().equals("getFoo")) {
382 method.getReturnType().getName().equals("Short");
386 if (method.getName().equals("setFoo")) {
388 final MethodSignature.Parameter param = method.getParameters()
390 assertEquals("foo", param.getName());
391 assertEquals("Short", param.getType().getName());
394 if (method.getName().equals("getBar")) {
395 method.getReturnType().getName().equals("List");
399 assertEquals(3, methodsCount);
403 public void testListResolving() {
404 final String filePath = getClass()
405 .getResource("/simple-list-demo.yang").getPath();
406 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
407 assertTrue(context != null);
409 final BindingGenerator bindingGen = new BindingGeneratorImpl();
410 final List<Type> genTypes = bindingGen.generateTypes(context);
412 assertTrue(genTypes != null);
413 assertEquals(5, genTypes.size());
415 int genTypesCount = 0;
417 for (final Type type : genTypes) {
418 if (type instanceof GeneratedType) {
419 final GeneratedType genType = (GeneratedType) type;
420 if (genType.getName().equals("ListParentContainer")) {
421 assertEquals(2, genType.getMethodDefinitions().size());
423 } else if (genType.getName().equals("SimpleList")) {
424 assertEquals(7, genType.getMethodDefinitions().size());
425 final List<MethodSignature> methods = genType
426 .getMethodDefinitions();
427 int methodsCount = 0;
428 for (final MethodSignature method : methods) {
429 if (method.getName().equals("getSimpleListKey")) {
430 assertEquals("SimpleListKey", method
431 .getReturnType().getName());
433 } else if (method.getName().equals(
434 "getListChildContainer")) {
435 assertEquals("ListChildContainer", method
436 .getReturnType().getName());
438 } else if (method.getName().equals("getFoo")) {
440 } else if (method.getName().equals("setFoo")) {
442 } else if (method.getName().equals("getSimpleLeafList")) {
444 } else if (method.getName().equals("setSimpleLeafList")) {
446 } else if (method.getName().equals("getBar")) {
450 assertEquals(7, methodsCount);
452 } else if (genType.getName().equals("ListChildContainer")) {
453 assertEquals(2, genType.getMethodDefinitions().size());
456 } else if (type instanceof GeneratedTransferObject) {
458 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
459 final List<GeneratedProperty> properties = genTO
461 final List<GeneratedProperty> hashProps = genTO
462 .getHashCodeIdentifiers();
463 final List<GeneratedProperty> equalProps = genTO
464 .getEqualsIdentifiers();
466 assertEquals(1, properties.size());
467 assertEquals("ListKey", properties.get(0).getName());
468 assertEquals("Byte", properties.get(0).getReturnType()
470 assertEquals(true, properties.get(0).isReadOnly());
471 assertEquals(1, hashProps.size());
472 assertEquals("ListKey", hashProps.get(0).getName());
473 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
474 assertEquals(1, equalProps.size());
475 assertEquals("ListKey", equalProps.get(0).getName());
476 assertEquals("Byte", equalProps.get(0).getReturnType()
480 assertEquals(3, genTypesCount);
481 assertEquals(1, genTOsCount);
485 public void testListCompositeKeyResolving() {
486 final String filePath = getClass().getResource(
487 "/list-composite-key.yang").getPath();
488 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
490 assertTrue(context != null);
492 final BindingGenerator bindingGen = new BindingGeneratorImpl();
493 final List<Type> genTypes = bindingGen.generateTypes(context);
495 assertTrue(genTypes != null);
496 assertEquals(7, genTypes.size());
498 int genTypesCount = 0;
500 for (final Type type : genTypes) {
501 if (type instanceof GeneratedType) {
503 } else if (type instanceof GeneratedTransferObject) {
504 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
506 if (genTO.getName().equals("CompositeKeyListKey")) {
507 final List<GeneratedProperty> properties = genTO
509 int propertyCount = 0;
510 for (final GeneratedProperty prop : properties) {
511 if (prop.getName().equals("Key1")) {
513 } else if (prop.getName().equals("Key2")) {
517 assertEquals(2, propertyCount);
519 } else if (genTO.getName().equals("InnerListKey")) {
520 final List<GeneratedProperty> properties = genTO
522 assertEquals(1, properties.size());
528 assertEquals(5, genTypesCount);
529 assertEquals(2, genTOsCount);
533 public void testGeneratedTypes() {
534 final String filePath = getClass().getResource("/demo-topology.yang")
536 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
537 assertTrue(context != null);
539 final BindingGenerator bindingGen = new BindingGeneratorImpl();
540 final List<Type> genTypes = bindingGen.generateTypes(context);
542 assertTrue(genTypes != null);
543 assertEquals(14, genTypes.size());
545 int genTypesCount = 0;
547 for (final Type type : genTypes) {
548 if (type instanceof GeneratedType) {
550 } else if (type instanceof GeneratedTransferObject) {
555 assertEquals(11, genTypesCount);
556 assertEquals(3, genTOsCount);