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.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.model.parser.impl.YangModelParserImpl;
30 public class GeneratedTypesTest {
32 private SchemaContext resolveSchemaContextFromFiles(
33 final String... yangFiles) {
34 final YangModelParser parser = new YangModelParserImpl();
35 final Set<Module> modules = parser.parseYangModels(yangFiles);
37 return parser.resolveSchemaContext(modules);
41 public void testMultipleModulesResolving() {
42 final String topologyPath = getClass().getResource(
43 "/abstract-topology.yang").getPath();
44 final String typesPath = getClass().getResource(
45 "/ietf-inet-types@2010-09-24.yang").getPath();
46 final SchemaContext context = resolveSchemaContextFromFiles(
47 topologyPath, typesPath);
48 assertTrue(context != null);
50 final BindingGenerator bindingGen = new BindingGeneratorImpl();
51 final List<Type> genTypes = bindingGen.generateTypes(context);
53 assertTrue(genTypes != null);
54 assertEquals(11, genTypes.size());
58 public void testLeafrefResolving() {
59 final String topologyPath = getClass().getResource(
60 "/leafref-test-models/abstract-topology@2013-02-08.yang")
62 final String interfacesPath = getClass().getResource(
63 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
65 // final String ifTypePath = getClass().getResource(
66 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
67 final String inetTypesPath = getClass().getResource(
68 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
70 final String yangTypesPath = getClass().getResource(
71 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
74 assertTrue(topologyPath != null);
75 assertTrue(interfacesPath != null);
76 // assertTrue(ifTypePath != null);
77 assertTrue(inetTypesPath != null);
78 assertTrue(yangTypesPath != null);
80 // final SchemaContext context = resolveSchemaContextFromFiles(
81 // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
83 final SchemaContext context = resolveSchemaContextFromFiles(
84 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
85 assertTrue(context != null);
86 assertEquals(4, context.getModules().size());
88 final BindingGenerator bindingGen = new BindingGeneratorImpl();
89 final List<Type> genTypes = bindingGen.generateTypes(context);
91 assertEquals(21, genTypes.size());
92 assertTrue(genTypes != null);
94 int resolvedLeafrefCount = 0;
95 for (final Type type : genTypes) {
96 if (type.getName().equals("InterfaceKey")
97 && type instanceof GeneratedTransferObject) {
98 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
99 final List<GeneratedProperty> properties = genTO
102 assertTrue(properties != null);
103 for (final GeneratedProperty property : properties) {
104 if (property.getName().equals("InterfaceId")) {
105 assertTrue(property.getReturnType() != null);
106 assertFalse(property.getReturnType().equals(
108 assertTrue(property.getReturnType().getName()
110 resolvedLeafrefCount++;
114 } else if (type.getName().equals("Interface")
115 && type instanceof GeneratedType) {
116 final GeneratedType genType = (GeneratedType) type;
117 final List<MethodSignature> methods = genType
118 .getMethodDefinitions();
120 assertTrue(methods != null);
121 for (final MethodSignature method : methods) {
122 if (method.getName().equals("getInterfaceKey")) {
123 assertTrue(method.getReturnType() != null);
124 assertFalse(method.getReturnType().equals(
126 assertTrue(method.getReturnType().getName()
127 .equals("InterfaceKey"));
128 resolvedLeafrefCount++;
129 } else if (method.getName().equals("getHigherLayerIf")) {
130 assertTrue(method.getReturnType() != null);
131 assertFalse(method.getReturnType().equals(
133 assertTrue(method.getReturnType().getName()
135 resolvedLeafrefCount++;
138 } else if (type.getName().equals("NetworkLink")
139 && type instanceof GeneratedType) {
140 final GeneratedType genType = (GeneratedType) type;
141 final List<MethodSignature> methods = genType
142 .getMethodDefinitions();
143 assertTrue(methods != null);
144 for (MethodSignature method : methods) {
145 if (method.getName().equals("getInterface")) {
146 assertTrue(method.getReturnType() != null);
147 assertFalse(method.getReturnType().equals(
149 assertTrue(method.getReturnType().getName()
151 resolvedLeafrefCount++;
154 } else if ((type.getName().equals("SourceNode") || type.getName()
155 .equals("DestinationNode"))
156 && type instanceof GeneratedType) {
157 final GeneratedType genType = (GeneratedType) type;
158 final List<MethodSignature> methods = genType
159 .getMethodDefinitions();
160 assertTrue(methods != null);
161 for (MethodSignature method : methods) {
162 if (method.getName().equals("getId")) {
163 assertTrue(method.getReturnType() != null);
164 assertFalse(method.getReturnType().equals(
166 assertTrue(method.getReturnType().getName()
168 resolvedLeafrefCount++;
171 } else if (type.getName().equals("Tunnel")
172 && type instanceof GeneratedType) {
173 final GeneratedType genType = (GeneratedType) type;
174 final List<MethodSignature> methods = genType
175 .getMethodDefinitions();
176 assertTrue(methods != null);
177 for (MethodSignature method : methods) {
178 if (method.getName().equals("getTunnelKey")) {
179 assertTrue(method.getReturnType() != null);
180 assertFalse(method.getReturnType().equals(
182 assertTrue(method.getReturnType().getName()
183 .equals("TunnelKey"));
184 resolvedLeafrefCount++;
187 } else if (type.getName().equals("TunnelKey")
188 && type instanceof GeneratedTransferObject) {
189 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
190 final List<GeneratedProperty> properties = genTO
193 assertTrue(properties != null);
194 for (final GeneratedProperty property : properties) {
195 if (property.getName().equals("TunnelId")) {
196 assertTrue(property.getReturnType() != null);
197 assertFalse(property.getReturnType().equals(
199 assertTrue(property.getReturnType().getName()
201 resolvedLeafrefCount++;
206 assertEquals(10, resolvedLeafrefCount);
210 public void testContainerResolving() {
211 final String filePath = getClass().getResource(
212 "/simple-container-demo.yang").getPath();
213 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
214 assertTrue(context != null);
216 final BindingGenerator bindingGen = new BindingGeneratorImpl();
217 final List<Type> genTypes = bindingGen.generateTypes(context);
219 assertTrue(genTypes != null);
220 assertEquals(2, genTypes.size());
222 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
223 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
225 assertEquals("SimpleContainer", simpleContainer.getName());
226 assertEquals("NestedContainer", nestedContainer.getName());
227 assertEquals(4, simpleContainer.getMethodDefinitions().size());
228 assertEquals(4, nestedContainer.getMethodDefinitions().size());
230 int methodsCount = 0;
231 for (final MethodSignature method : simpleContainer
232 .getMethodDefinitions()) {
233 if (method.getName().equals("getFoo")) {
234 method.getReturnType().getName().equals("Integer");
238 if (method.getName().equals("setFoo")) {
240 final MethodSignature.Parameter param = method.getParameters()
242 assertEquals("foo", param.getName());
243 assertEquals("Integer", param.getType().getName());
246 if (method.getName().equals("getBar")) {
247 method.getReturnType().getName().equals("String");
251 if (method.getName().equals("getNestedContainer")) {
252 method.getReturnType().getName().equals("NestedContainer");
256 assertEquals(4, methodsCount);
259 for (final MethodSignature method : nestedContainer
260 .getMethodDefinitions()) {
261 if (method.getName().equals("getFoo")) {
262 method.getReturnType().getName().equals("Short");
266 if (method.getName().equals("setFoo")) {
268 final MethodSignature.Parameter param = method.getParameters()
270 assertEquals("foo", param.getName());
271 assertEquals("Short", param.getType().getName());
274 if (method.getName().equals("getBar")) {
275 method.getReturnType().getName().equals("String");
279 if (method.getName().equals("setBar")) {
280 method.getReturnType().getName().equals("String");
284 assertEquals(4, methodsCount);
288 public void testLeafListResolving() {
289 final String filePath = getClass().getResource(
290 "/simple-leaf-list-demo.yang").getPath();
291 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
292 assertTrue(context != null);
294 final BindingGenerator bindingGen = new BindingGeneratorImpl();
295 final List<Type> genTypes = bindingGen.generateTypes(context);
297 assertTrue(genTypes != null);
298 assertEquals(2, genTypes.size());
300 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
301 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
303 assertEquals("SimpleContainer", simpleContainer.getName());
304 assertEquals("NestedContainer", nestedContainer.getName());
305 assertEquals(4, simpleContainer.getMethodDefinitions().size());
306 assertEquals(3, nestedContainer.getMethodDefinitions().size());
308 int methodsCount = 0;
309 for (final MethodSignature method : simpleContainer
310 .getMethodDefinitions()) {
311 if (method.getName().equals("getFoo")) {
312 method.getReturnType().getName().equals("List");
316 if (method.getName().equals("setFoo")) {
318 final MethodSignature.Parameter param = method.getParameters()
320 assertEquals("foo", param.getName());
321 assertEquals("List", param.getType().getName());
324 if (method.getName().equals("getBar")) {
325 method.getReturnType().getName().equals("String");
329 if (method.getName().equals("getNestedContainer")) {
330 method.getReturnType().getName().equals("NestedContainer");
334 assertEquals(4, methodsCount);
337 for (final MethodSignature method : nestedContainer
338 .getMethodDefinitions()) {
339 if (method.getName().equals("getFoo")) {
340 method.getReturnType().getName().equals("Short");
344 if (method.getName().equals("setFoo")) {
346 final MethodSignature.Parameter param = method.getParameters()
348 assertEquals("foo", param.getName());
349 assertEquals("Short", param.getType().getName());
352 if (method.getName().equals("getBar")) {
353 method.getReturnType().getName().equals("List");
357 assertEquals(3, methodsCount);
361 public void testListResolving() {
362 final String filePath = getClass()
363 .getResource("/simple-list-demo.yang").getPath();
364 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
365 assertTrue(context != null);
367 final BindingGenerator bindingGen = new BindingGeneratorImpl();
368 final List<Type> genTypes = bindingGen.generateTypes(context);
370 assertTrue(genTypes != null);
371 assertEquals(4, genTypes.size());
373 int genTypesCount = 0;
375 for (final Type type : genTypes) {
376 if (type instanceof GeneratedType) {
377 final GeneratedType genType = (GeneratedType) type;
378 if (genType.getName().equals("ListParentContainer")) {
379 assertEquals(2, genType.getMethodDefinitions().size());
381 } else if (genType.getName().equals("SimpleList")) {
382 assertEquals(7, genType.getMethodDefinitions().size());
383 final List<MethodSignature> methods = genType
384 .getMethodDefinitions();
385 int methodsCount = 0;
386 for (final MethodSignature method : methods) {
387 if (method.getName().equals("getSimpleListKey")) {
388 assertEquals("SimpleListKey", method
389 .getReturnType().getName());
391 } else if (method.getName().equals(
392 "getListChildContainer")) {
393 assertEquals("ListChildContainer", method
394 .getReturnType().getName());
396 } else if (method.getName().equals("getFoo")) {
398 } else if (method.getName().equals("setFoo")) {
400 } else if (method.getName().equals("getSimpleLeafList")) {
402 } else if (method.getName().equals("setSimpleLeafList")) {
404 } else if (method.getName().equals("getBar")) {
408 assertEquals(7, methodsCount);
410 } else if (genType.getName().equals("ListChildContainer")) {
411 assertEquals(2, genType.getMethodDefinitions().size());
414 } else if (type instanceof GeneratedTransferObject) {
416 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
417 final List<GeneratedProperty> properties = genTO
419 final List<GeneratedProperty> hashProps = genTO
420 .getHashCodeIdentifiers();
421 final List<GeneratedProperty> equalProps = genTO
422 .getEqualsIdentifiers();
424 assertEquals(1, properties.size());
425 assertEquals("ListKey", properties.get(0).getName());
426 assertEquals("Byte", properties.get(0).getReturnType()
428 assertEquals(true, properties.get(0).isReadOnly());
429 assertEquals(1, hashProps.size());
430 assertEquals("ListKey", hashProps.get(0).getName());
431 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
432 assertEquals(1, equalProps.size());
433 assertEquals("ListKey", equalProps.get(0).getName());
434 assertEquals("Byte", equalProps.get(0).getReturnType()
438 assertEquals(3, genTypesCount);
439 assertEquals(1, genTOsCount);
443 public void testListCompositeKeyResolving() {
444 final String filePath = getClass().getResource(
445 "/list-composite-key.yang").getPath();
446 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
448 assertTrue(context != null);
450 final BindingGenerator bindingGen = new BindingGeneratorImpl();
451 final List<Type> genTypes = bindingGen.generateTypes(context);
453 assertTrue(genTypes != null);
454 assertEquals(6, genTypes.size());
456 int genTypesCount = 0;
458 for (final Type type : genTypes) {
459 if (type instanceof GeneratedType) {
461 } else if (type instanceof GeneratedTransferObject) {
462 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
464 if (genTO.getName().equals("CompositeKeyListKey")) {
465 final List<GeneratedProperty> properties = genTO
467 int propertyCount = 0;
468 for (final GeneratedProperty prop : properties) {
469 if (prop.getName().equals("Key1")) {
471 } else if (prop.getName().equals("Key2")) {
475 assertEquals(2, propertyCount);
477 } else if (genTO.getName().equals("InnerListKey")) {
478 final List<GeneratedProperty> properties = genTO
480 assertEquals(1, properties.size());
486 assertEquals(4, genTypesCount);
487 assertEquals(2, genTOsCount);
491 public void testGeneratedTypes() {
492 final String filePath = getClass().getResource("/demo-topology.yang")
494 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
495 assertTrue(context != null);
497 final BindingGenerator bindingGen = new BindingGeneratorImpl();
498 final List<Type> genTypes = bindingGen.generateTypes(context);
500 assertTrue(genTypes != null);
501 assertEquals(13, genTypes.size());
503 int genTypesCount = 0;
505 for (final Type type : genTypes) {
506 if (type instanceof GeneratedType) {
508 } else if (type instanceof GeneratedTransferObject) {
513 assertEquals(10, genTypesCount);
514 assertEquals(3, genTOsCount);