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 testMultipleModulesResolving() {
47 final String topologyPath = getClass().getResource(
48 "/abstract-topology.yang").getPath();
49 final String typesPath = getClass().getResource(
50 "/ietf-inet-types@2010-09-24.yang").getPath();
51 final SchemaContext context = resolveSchemaContextFromFiles(
52 topologyPath, typesPath);
53 assertTrue(context != null);
55 final BindingGenerator bindingGen = new BindingGeneratorImpl();
56 final List<Type> genTypes = bindingGen.generateTypes(context);
58 assertTrue(genTypes != null);
59 assertEquals(29, genTypes.size());
63 public void testLeafrefResolving() {
64 final String topologyPath = getClass().getResource(
65 "/leafref-test-models/abstract-topology@2013-02-08.yang")
67 final String interfacesPath = getClass().getResource(
68 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
70 // final String ifTypePath = getClass().getResource(
71 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
72 final String inetTypesPath = getClass().getResource(
73 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
75 final String yangTypesPath = getClass().getResource(
76 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
79 assertTrue(topologyPath != null);
80 assertTrue(interfacesPath != null);
81 // assertTrue(ifTypePath != null);
82 assertTrue(inetTypesPath != null);
83 assertTrue(yangTypesPath != null);
85 // final SchemaContext context = resolveSchemaContextFromFiles(
86 // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
88 final SchemaContext context = resolveSchemaContextFromFiles(
89 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
90 assertTrue(context != null);
91 assertEquals(4, context.getModules().size());
93 final BindingGenerator bindingGen = new BindingGeneratorImpl();
94 final List<Type> genTypes = bindingGen.generateTypes(context);
96 assertEquals(57, genTypes.size());
97 assertTrue(genTypes != null);
99 GeneratedTransferObject gtIfcKey = null;
100 GeneratedType gtIfc = null;
101 GeneratedType gtNetworkLink = null;
102 GeneratedType gtSource = null;
103 GeneratedType gtDest = null;
104 GeneratedType gtTunnel = null;
105 GeneratedTransferObject gtTunnelKey = null;
106 for (final Type type : genTypes) {
107 String name = type.getName();
108 if ("InterfaceKey".equals(name)) {
109 gtIfcKey = (GeneratedTransferObject) type;
110 } else if ("Interface".equals(name)) {
111 gtIfc = (GeneratedType) type;
112 } else if ("NetworkLink".equals(name)) {
113 gtNetworkLink = (GeneratedType) type;
114 } else if ("SourceNode".equals(name)) {
115 gtSource = (GeneratedType) type;
116 } else if ("DestinationNode".equals(name)) {
117 gtDest = (GeneratedType) type;
118 } else if ("Tunnel".equals(name)) {
119 gtTunnel = (GeneratedType) type;
120 } else if ("TunnelKey".equals(name)) {
121 gtTunnelKey = (GeneratedTransferObject) type;
125 assertNotNull(gtIfcKey);
126 assertNotNull(gtIfc);
127 assertNotNull(gtNetworkLink);
128 assertNotNull(gtSource);
129 assertNotNull(gtDest);
130 assertNotNull(gtTunnel);
131 assertNotNull(gtTunnelKey);
134 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
135 assertNotNull(gtIfcKeyProps);
136 GeneratedProperty ifcIdProp = null;
137 for (final GeneratedProperty property : gtIfcKeyProps) {
138 if (property.getName().equals("InterfaceId")) {
139 ifcIdProp = property;
142 assertNotNull(ifcIdProp);
143 Type ifcIdPropType = ifcIdProp.getReturnType();
144 assertNotNull(ifcIdPropType);
145 assertFalse(ifcIdPropType.equals("java.lang.Void"));
146 assertTrue(ifcIdPropType.getName().equals("String"));
149 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
150 assertNotNull(gtIfcMethods);
151 MethodSignature getIfcKey = null;
152 MethodSignature getHigherLayerIf = null;
153 for (final MethodSignature method : gtIfcMethods) {
154 if (method.getName().equals("getInterfaceKey")) {
156 } else if (method.getName().equals("getHigherLayerIf")) {
157 getHigherLayerIf = method;
160 assertNotNull(getIfcKey);
161 Type getIfcKeyType = getIfcKey.getReturnType();
162 assertNotNull(getIfcKeyType);
163 assertFalse(getIfcKeyType.equals("java.lang.Void"));
164 assertTrue(getIfcKeyType.getName().equals("InterfaceKey"));
166 assertNotNull(getHigherLayerIf);
167 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
168 assertNotNull(getHigherLayerIfType);
169 assertFalse(getHigherLayerIfType.equals("java.lang.Void"));
170 assertTrue(getHigherLayerIfType.getName().equals("List"));
173 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink
174 .getMethodDefinitions();
175 assertNotNull(gtNetworkLinkMethods);
176 MethodSignature getIfc = null;
177 for (MethodSignature method : gtNetworkLinkMethods) {
178 if (method.getName().equals("getInterface")) {
182 assertNotNull(getIfc);
183 Type getIfcType = getIfc.getReturnType();
184 assertNotNull(getIfcType);
185 assertFalse(getIfcType.equals("java.lang.Void"));
186 assertTrue(getIfcType.getName().equals("String"));
189 final List<MethodSignature> gtSourceMethods = gtSource
190 .getMethodDefinitions();
191 assertNotNull(gtSourceMethods);
192 MethodSignature getIdSource = null;
193 for (MethodSignature method : gtSourceMethods) {
194 if (method.getName().equals("getId")) {
195 getIdSource = method;
198 assertNotNull(getIdSource);
199 Type getIdType = getIdSource.getReturnType();
200 assertNotNull(getIdType);
201 assertFalse(getIdType.equals("java.lang.Void"));
202 assertTrue(getIdType.getName().equals("Uri"));
205 final List<MethodSignature> gtDestMethods = gtDest
206 .getMethodDefinitions();
207 assertNotNull(gtDestMethods);
208 MethodSignature getIdDest = null;
209 for (MethodSignature method : gtDestMethods) {
210 if (method.getName().equals("getId")) {
214 assertNotNull(getIdDest);
215 Type getIdDestType = getIdDest.getReturnType();
216 assertNotNull(getIdDestType);
217 assertFalse(getIdDestType.equals("java.lang.Void"));
218 assertTrue(getIdDestType.getName().equals("Uri"));
221 final List<MethodSignature> gtTunnelMethods = gtTunnel
222 .getMethodDefinitions();
223 assertNotNull(gtTunnelMethods);
224 MethodSignature getTunnelKey = null;
225 for (MethodSignature method : gtTunnelMethods) {
226 if (method.getName().equals("getTunnelKey")) {
227 getTunnelKey = method;
230 assertNotNull(getTunnelKey);
231 Type getTunnelKeyType = getTunnelKey.getReturnType();
232 assertNotNull(getTunnelKeyType);
233 assertFalse(getTunnelKeyType.equals("java.lang.Void"));
234 assertTrue(getTunnelKeyType.getName().equals("TunnelKey"));
237 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey
239 assertNotNull(gtTunnelKeyProps);
240 GeneratedProperty tunnelId = null;
241 for (final GeneratedProperty property : gtTunnelKeyProps) {
242 if (property.getName().equals("TunnelId")) {
246 assertNotNull(tunnelId);
247 Type tunnelIdType = tunnelId.getReturnType();
248 assertNotNull(tunnelIdType);
249 assertFalse(tunnelIdType.equals("java.lang.Void"));
250 assertTrue(tunnelIdType.getName().equals("Uri"));
254 public void testContainerResolving() {
255 final String filePath = getClass().getResource(
256 "/simple-container-demo.yang").getPath();
257 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
258 assertTrue(context != null);
260 final BindingGenerator bindingGen = new BindingGeneratorImpl();
261 final List<Type> genTypes = bindingGen.generateTypes(context);
263 assertTrue(genTypes != null);
264 assertEquals(4, genTypes.size());
266 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
267 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
269 assertEquals("SimpleContainer", simpleContainer.getName());
270 assertEquals("NestedContainer", nestedContainer.getName());
271 assertEquals(5, simpleContainer.getMethodDefinitions().size());
272 assertEquals(4, nestedContainer.getMethodDefinitions().size());
274 int methodsCount = 0;
275 for (final MethodSignature method : simpleContainer
276 .getMethodDefinitions()) {
277 if (method.getName().equals("getFoo")) {
278 method.getReturnType().getName().equals("Integer");
282 if (method.getName().equals("setFoo")) {
284 final MethodSignature.Parameter param = method.getParameters()
286 assertEquals("foo", param.getName());
287 assertEquals("Integer", param.getType().getName());
290 if (method.getName().equals("getBar")) {
291 method.getReturnType().getName().equals("String");
295 if (method.getName().equals("getNestedContainer")) {
296 method.getReturnType().getName().equals("NestedContainer");
300 assertEquals(4, methodsCount);
303 for (final MethodSignature method : nestedContainer
304 .getMethodDefinitions()) {
305 if (method.getName().equals("getFoo")) {
306 method.getReturnType().getName().equals("Short");
310 if (method.getName().equals("setFoo")) {
312 final MethodSignature.Parameter param = method.getParameters()
314 assertEquals("foo", param.getName());
315 assertEquals("Short", param.getType().getName());
318 if (method.getName().equals("getBar")) {
319 method.getReturnType().getName().equals("String");
323 if (method.getName().equals("setBar")) {
324 method.getReturnType().getName().equals("String");
328 assertEquals(4, methodsCount);
332 public void testLeafListResolving() {
333 final String filePath = getClass().getResource(
334 "/simple-leaf-list-demo.yang").getPath();
335 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
336 assertTrue(context != null);
338 final BindingGenerator bindingGen = new BindingGeneratorImpl();
339 final List<Type> genTypes = bindingGen.generateTypes(context);
341 assertTrue(genTypes != null);
342 assertEquals(4, genTypes.size());
344 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
345 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
347 assertEquals("SimpleContainer", simpleContainer.getName());
348 assertEquals("NestedContainer", nestedContainer.getName());
349 assertEquals(5, simpleContainer.getMethodDefinitions().size());
350 assertEquals(3, nestedContainer.getMethodDefinitions().size());
352 int methodsCount = 0;
353 for (final MethodSignature method : simpleContainer
354 .getMethodDefinitions()) {
355 if (method.getName().equals("getFoo")) {
356 method.getReturnType().getName().equals("List");
360 if (method.getName().equals("setFoo")) {
362 final MethodSignature.Parameter param = method.getParameters()
364 assertEquals("foo", param.getName());
365 assertEquals("List", param.getType().getName());
368 if (method.getName().equals("getBar")) {
369 method.getReturnType().getName().equals("String");
373 if (method.getName().equals("getNestedContainer")) {
374 method.getReturnType().getName().equals("NestedContainer");
378 assertEquals(4, methodsCount);
381 for (final MethodSignature method : nestedContainer
382 .getMethodDefinitions()) {
383 if (method.getName().equals("getFoo")) {
384 method.getReturnType().getName().equals("Short");
388 if (method.getName().equals("setFoo")) {
390 final MethodSignature.Parameter param = method.getParameters()
392 assertEquals("foo", param.getName());
393 assertEquals("Short", param.getType().getName());
396 if (method.getName().equals("getBar")) {
397 method.getReturnType().getName().equals("List");
401 assertEquals(3, methodsCount);
405 public void testListResolving() {
406 final String filePath = getClass()
407 .getResource("/simple-list-demo.yang").getPath();
408 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
409 assertTrue(context != null);
411 final BindingGenerator bindingGen = new BindingGeneratorImpl();
412 final List<Type> genTypes = bindingGen.generateTypes(context);
414 assertTrue(genTypes != null);
415 assertEquals(6, genTypes.size());
417 int genTypesCount = 0;
419 for (final Type type : genTypes) {
420 if (type instanceof GeneratedType
421 && !(type instanceof GeneratedTransferObject)) {
422 final GeneratedType genType = (GeneratedType) type;
423 if (genType.getName().equals("ListParentContainer")) {
424 assertEquals(2, genType.getMethodDefinitions().size());
426 } else if (genType.getName().equals("SimpleList")) {
427 assertEquals(8, genType.getMethodDefinitions().size());
428 final List<MethodSignature> methods = genType
429 .getMethodDefinitions();
430 int methodsCount = 0;
431 for (final MethodSignature method : methods) {
432 if (method.getName().equals("getSimpleListKey")) {
433 assertEquals("SimpleListKey", method
434 .getReturnType().getName());
436 } else if (method.getName().equals(
437 "getListChildContainer")) {
438 assertEquals("ListChildContainer", method
439 .getReturnType().getName());
441 } else if (method.getName().equals("getFoo")) {
443 } else if (method.getName().equals("setFoo")) {
445 } else if (method.getName().equals("getSimpleLeafList")) {
447 } else if (method.getName().equals("setSimpleLeafList")) {
449 } else if (method.getName().equals("getBar")) {
453 assertEquals(7, methodsCount);
455 } else if (genType.getName().equals("ListChildContainer")) {
456 assertEquals(2, genType.getMethodDefinitions().size());
459 } else if (type instanceof GeneratedTransferObject) {
461 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
462 final List<GeneratedProperty> properties = genTO
464 final List<GeneratedProperty> hashProps = genTO
465 .getHashCodeIdentifiers();
466 final List<GeneratedProperty> equalProps = genTO
467 .getEqualsIdentifiers();
469 assertEquals(1, properties.size());
470 assertEquals("ListKey", properties.get(0).getName());
471 assertEquals("Byte", properties.get(0).getReturnType()
473 assertEquals(true, properties.get(0).isReadOnly());
474 assertEquals(1, hashProps.size());
475 assertEquals("ListKey", hashProps.get(0).getName());
476 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
477 assertEquals(1, equalProps.size());
478 assertEquals("ListKey", equalProps.get(0).getName());
479 assertEquals("Byte", equalProps.get(0).getReturnType()
483 assertEquals(3, genTypesCount);
484 assertEquals(1, genTOsCount);
488 public void testListCompositeKeyResolving() {
489 final String filePath = getClass().getResource(
490 "/list-composite-key.yang").getPath();
491 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
493 assertTrue(context != null);
495 final BindingGenerator bindingGen = new BindingGeneratorImpl();
496 final List<Type> genTypes = bindingGen.generateTypes(context);
498 assertTrue(genTypes != null);
499 assertEquals(8, genTypes.size());
501 int genTypesCount = 0;
503 for (final Type type : genTypes) {
504 if (type instanceof GeneratedType
505 && !(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(6, 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(15, genTypes.size());
549 int genTypesCount = 0;
551 for (final Type type : genTypes) {
552 if (type instanceof GeneratedType
553 && !(type instanceof GeneratedTransferObject)) {
555 } else if (type instanceof GeneratedTransferObject) {
560 assertEquals(12, genTypesCount);
561 assertEquals(3, genTOsCount);