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.GeneratedProperty;
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
22 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
23 import org.opendaylight.controller.sal.binding.model.api.Type;
24 import org.opendaylight.controller.yang.model.api.Module;
25 import org.opendaylight.controller.yang.model.api.SchemaContext;
26 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
27 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
29 public class GeneratedTypesTest {
31 private SchemaContext resolveSchemaContextFromFiles(
32 final String... yangFiles) {
33 final YangModelParser parser = new YangParserImpl();
35 final List<File> inputFiles = new ArrayList<File>();
36 for (int i = 0; i < yangFiles.length; ++i) {
37 inputFiles.add(new File(yangFiles[i]));
40 final Set<Module> modules = parser.parseYangModels(inputFiles);
41 return parser.resolveSchemaContext(modules);
45 public void testMultipleModulesResolving() {
46 final String topologyPath = getClass().getResource(
47 "/abstract-topology.yang").getPath();
48 final String typesPath = getClass().getResource(
49 "/ietf-inet-types@2010-09-24.yang").getPath();
50 final SchemaContext context = resolveSchemaContextFromFiles(
51 topologyPath, typesPath);
52 assertTrue(context != null);
54 final BindingGenerator bindingGen = new BindingGeneratorImpl();
55 final List<Type> genTypes = bindingGen.generateTypes(context);
57 assertTrue(genTypes != null);
58 assertEquals(29, genTypes.size());
62 public void testLeafrefResolving() {
63 final String topologyPath = getClass().getResource(
64 "/leafref-test-models/abstract-topology@2013-02-08.yang")
66 final String interfacesPath = getClass().getResource(
67 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
69 // final String ifTypePath = getClass().getResource(
70 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
71 final String inetTypesPath = getClass().getResource(
72 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
74 final String yangTypesPath = getClass().getResource(
75 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
78 assertTrue(topologyPath != null);
79 assertTrue(interfacesPath != null);
80 // assertTrue(ifTypePath != null);
81 assertTrue(inetTypesPath != null);
82 assertTrue(yangTypesPath != null);
84 // final SchemaContext context = resolveSchemaContextFromFiles(
85 // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
87 final SchemaContext context = resolveSchemaContextFromFiles(
88 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
89 assertTrue(context != null);
90 assertEquals(4, context.getModules().size());
92 final BindingGenerator bindingGen = new BindingGeneratorImpl();
93 final List<Type> genTypes = bindingGen.generateTypes(context);
95 assertEquals(57, genTypes.size());
96 assertTrue(genTypes != null);
98 GeneratedTransferObject gtIfcKey = null;
99 GeneratedType gtIfc = null;
100 GeneratedType gtNetworkLink = null;
101 GeneratedType gtSource = null;
102 GeneratedType gtDest = null;
103 GeneratedType gtTunnel = null;
104 GeneratedTransferObject gtTunnelKey = null;
105 for (final Type type : genTypes) {
106 String name = type.getName();
107 if ("InterfaceKey".equals(name)) {
108 gtIfcKey = (GeneratedTransferObject) type;
109 } else if ("Interface".equals(name)) {
110 gtIfc = (GeneratedType) type;
111 } else if ("NetworkLink".equals(name)) {
112 gtNetworkLink = (GeneratedType) type;
113 } else if ("SourceNode".equals(name)) {
114 gtSource = (GeneratedType) type;
115 } else if ("DestinationNode".equals(name)) {
116 gtDest = (GeneratedType) type;
117 } else if ("Tunnel".equals(name)) {
118 gtTunnel = (GeneratedType) type;
119 } else if ("TunnelKey".equals(name)) {
120 gtTunnelKey = (GeneratedTransferObject) type;
124 assertNotNull(gtIfcKey);
125 assertNotNull(gtIfc);
126 assertNotNull(gtNetworkLink);
127 assertNotNull(gtSource);
128 assertNotNull(gtDest);
129 assertNotNull(gtTunnel);
130 assertNotNull(gtTunnelKey);
133 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
134 assertNotNull(gtIfcKeyProps);
135 GeneratedProperty ifcIdProp = null;
136 for (final GeneratedProperty property : gtIfcKeyProps) {
137 if (property.getName().equals("InterfaceId")) {
138 ifcIdProp = property;
141 assertNotNull(ifcIdProp);
142 Type ifcIdPropType = ifcIdProp.getReturnType();
143 assertNotNull(ifcIdPropType);
144 assertFalse(ifcIdPropType.equals("java.lang.Void"));
145 assertTrue(ifcIdPropType.getName().equals("String"));
148 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
149 assertNotNull(gtIfcMethods);
150 MethodSignature getIfcKey = null;
151 MethodSignature getHigherLayerIf = null;
152 for (final MethodSignature method : gtIfcMethods) {
153 if (method.getName().equals("getInterfaceKey")) {
155 } else if (method.getName().equals("getHigherLayerIf")) {
156 getHigherLayerIf = method;
159 assertNotNull(getIfcKey);
160 Type getIfcKeyType = getIfcKey.getReturnType();
161 assertNotNull(getIfcKeyType);
162 assertFalse(getIfcKeyType.equals("java.lang.Void"));
163 assertTrue(getIfcKeyType.getName().equals("InterfaceKey"));
165 assertNotNull(getHigherLayerIf);
166 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
167 assertNotNull(getHigherLayerIfType);
168 assertFalse(getHigherLayerIfType.equals("java.lang.Void"));
169 assertTrue(getHigherLayerIfType.getName().equals("List"));
172 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink
173 .getMethodDefinitions();
174 assertNotNull(gtNetworkLinkMethods);
175 MethodSignature getIfc = null;
176 for (MethodSignature method : gtNetworkLinkMethods) {
177 if (method.getName().equals("getInterface")) {
181 assertNotNull(getIfc);
182 Type getIfcType = getIfc.getReturnType();
183 assertNotNull(getIfcType);
184 assertFalse(getIfcType.equals("java.lang.Void"));
185 assertTrue(getIfcType.getName().equals("String"));
188 final List<MethodSignature> gtSourceMethods = gtSource
189 .getMethodDefinitions();
190 assertNotNull(gtSourceMethods);
191 MethodSignature getIdSource = null;
192 for (MethodSignature method : gtSourceMethods) {
193 if (method.getName().equals("getId")) {
194 getIdSource = method;
197 assertNotNull(getIdSource);
198 Type getIdType = getIdSource.getReturnType();
199 assertNotNull(getIdType);
200 assertFalse(getIdType.equals("java.lang.Void"));
201 assertTrue(getIdType.getName().equals("Uri"));
204 final List<MethodSignature> gtDestMethods = gtDest
205 .getMethodDefinitions();
206 assertNotNull(gtDestMethods);
207 MethodSignature getIdDest = null;
208 for (MethodSignature method : gtDestMethods) {
209 if (method.getName().equals("getId")) {
213 assertNotNull(getIdDest);
214 Type getIdDestType = getIdDest.getReturnType();
215 assertNotNull(getIdDestType);
216 assertFalse(getIdDestType.equals("java.lang.Void"));
217 assertTrue(getIdDestType.getName().equals("Uri"));
220 final List<MethodSignature> gtTunnelMethods = gtTunnel
221 .getMethodDefinitions();
222 assertNotNull(gtTunnelMethods);
223 MethodSignature getTunnelKey = null;
224 for (MethodSignature method : gtTunnelMethods) {
225 if (method.getName().equals("getTunnelKey")) {
226 getTunnelKey = method;
229 assertNotNull(getTunnelKey);
230 Type getTunnelKeyType = getTunnelKey.getReturnType();
231 assertNotNull(getTunnelKeyType);
232 assertFalse(getTunnelKeyType.equals("java.lang.Void"));
233 assertTrue(getTunnelKeyType.getName().equals("TunnelKey"));
236 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey
238 assertNotNull(gtTunnelKeyProps);
239 GeneratedProperty tunnelId = null;
240 for (final GeneratedProperty property : gtTunnelKeyProps) {
241 if (property.getName().equals("TunnelId")) {
245 assertNotNull(tunnelId);
246 Type tunnelIdType = tunnelId.getReturnType();
247 assertNotNull(tunnelIdType);
248 assertFalse(tunnelIdType.equals("java.lang.Void"));
249 assertTrue(tunnelIdType.getName().equals("Uri"));
253 public void testContainerResolving() {
254 final String filePath = getClass().getResource(
255 "/simple-container-demo.yang").getPath();
256 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
257 assertTrue(context != null);
259 final BindingGenerator bindingGen = new BindingGeneratorImpl();
260 final List<Type> genTypes = bindingGen.generateTypes(context);
262 assertTrue(genTypes != null);
263 assertEquals(4, genTypes.size());
265 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
266 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
268 assertEquals("SimpleContainer", simpleContainer.getName());
269 assertEquals("NestedContainer", nestedContainer.getName());
270 assertEquals(5, simpleContainer.getMethodDefinitions().size());
271 assertEquals(4, nestedContainer.getMethodDefinitions().size());
273 int methodsCount = 0;
274 for (final MethodSignature method : simpleContainer
275 .getMethodDefinitions()) {
276 if (method.getName().equals("getFoo")) {
277 method.getReturnType().getName().equals("Integer");
281 if (method.getName().equals("setFoo")) {
283 final MethodSignature.Parameter param = method.getParameters()
285 assertEquals("foo", param.getName());
286 assertEquals("Integer", param.getType().getName());
289 if (method.getName().equals("getBar")) {
290 method.getReturnType().getName().equals("String");
294 if (method.getName().equals("getNestedContainer")) {
295 method.getReturnType().getName().equals("NestedContainer");
299 assertEquals(4, methodsCount);
302 for (final MethodSignature method : nestedContainer
303 .getMethodDefinitions()) {
304 if (method.getName().equals("getFoo")) {
305 method.getReturnType().getName().equals("Short");
309 if (method.getName().equals("setFoo")) {
311 final MethodSignature.Parameter param = method.getParameters()
313 assertEquals("foo", param.getName());
314 assertEquals("Short", param.getType().getName());
317 if (method.getName().equals("getBar")) {
318 method.getReturnType().getName().equals("String");
322 if (method.getName().equals("setBar")) {
323 method.getReturnType().getName().equals("String");
327 assertEquals(4, methodsCount);
331 public void testLeafListResolving() {
332 final String filePath = getClass().getResource(
333 "/simple-leaf-list-demo.yang").getPath();
334 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
335 assertTrue(context != null);
337 final BindingGenerator bindingGen = new BindingGeneratorImpl();
338 final List<Type> genTypes = bindingGen.generateTypes(context);
340 assertTrue(genTypes != null);
341 assertEquals(4, genTypes.size());
343 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
344 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
346 assertEquals("SimpleContainer", simpleContainer.getName());
347 assertEquals("NestedContainer", nestedContainer.getName());
348 assertEquals(5, simpleContainer.getMethodDefinitions().size());
349 assertEquals(3, nestedContainer.getMethodDefinitions().size());
351 int methodsCount = 0;
352 for (final MethodSignature method : simpleContainer
353 .getMethodDefinitions()) {
354 if (method.getName().equals("getFoo")) {
355 method.getReturnType().getName().equals("List");
359 if (method.getName().equals("setFoo")) {
361 final MethodSignature.Parameter param = method.getParameters()
363 assertEquals("foo", param.getName());
364 assertEquals("List", param.getType().getName());
367 if (method.getName().equals("getBar")) {
368 method.getReturnType().getName().equals("String");
372 if (method.getName().equals("getNestedContainer")) {
373 method.getReturnType().getName().equals("NestedContainer");
377 assertEquals(4, methodsCount);
380 for (final MethodSignature method : nestedContainer
381 .getMethodDefinitions()) {
382 if (method.getName().equals("getFoo")) {
383 method.getReturnType().getName().equals("Short");
387 if (method.getName().equals("setFoo")) {
389 final MethodSignature.Parameter param = method.getParameters()
391 assertEquals("foo", param.getName());
392 assertEquals("Short", param.getType().getName());
395 if (method.getName().equals("getBar")) {
396 method.getReturnType().getName().equals("List");
400 assertEquals(3, methodsCount);
404 public void testListResolving() {
405 final String filePath = getClass()
406 .getResource("/simple-list-demo.yang").getPath();
407 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
408 assertTrue(context != null);
410 final BindingGenerator bindingGen = new BindingGeneratorImpl();
411 final List<Type> genTypes = bindingGen.generateTypes(context);
413 assertTrue(genTypes != null);
414 assertEquals(6, genTypes.size());
416 int genTypesCount = 0;
418 for (final Type type : genTypes) {
419 if (type instanceof GeneratedType
420 && !(type instanceof GeneratedTransferObject)) {
421 final GeneratedType genType = (GeneratedType) type;
422 if (genType.getName().equals("ListParentContainer")) {
423 assertEquals(2, genType.getMethodDefinitions().size());
425 } else if (genType.getName().equals("SimpleList")) {
426 assertEquals(8, genType.getMethodDefinitions().size());
427 final List<MethodSignature> methods = genType
428 .getMethodDefinitions();
429 int methodsCount = 0;
430 for (final MethodSignature method : methods) {
431 if (method.getName().equals("getSimpleListKey")) {
432 assertEquals("SimpleListKey", method
433 .getReturnType().getName());
435 } else if (method.getName().equals(
436 "getListChildContainer")) {
437 assertEquals("ListChildContainer", method
438 .getReturnType().getName());
440 } else if (method.getName().equals("getFoo")) {
442 } else if (method.getName().equals("setFoo")) {
444 } else if (method.getName().equals("getSimpleLeafList")) {
446 } else if (method.getName().equals("setSimpleLeafList")) {
448 } else if (method.getName().equals("getBar")) {
452 assertEquals(7, methodsCount);
454 } else if (genType.getName().equals("ListChildContainer")) {
455 assertEquals(2, genType.getMethodDefinitions().size());
458 } else if (type instanceof GeneratedTransferObject) {
460 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
461 final List<GeneratedProperty> properties = genTO
463 final List<GeneratedProperty> hashProps = genTO
464 .getHashCodeIdentifiers();
465 final List<GeneratedProperty> equalProps = genTO
466 .getEqualsIdentifiers();
468 assertEquals(1, properties.size());
469 assertEquals("ListKey", properties.get(0).getName());
470 assertEquals("Byte", properties.get(0).getReturnType()
472 assertEquals(true, properties.get(0).isReadOnly());
473 assertEquals(1, hashProps.size());
474 assertEquals("ListKey", hashProps.get(0).getName());
475 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
476 assertEquals(1, equalProps.size());
477 assertEquals("ListKey", equalProps.get(0).getName());
478 assertEquals("Byte", equalProps.get(0).getReturnType()
482 assertEquals(3, genTypesCount);
483 assertEquals(1, genTOsCount);
487 public void testListCompositeKeyResolving() {
488 final String filePath = getClass().getResource(
489 "/list-composite-key.yang").getPath();
490 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
492 assertTrue(context != null);
494 final BindingGenerator bindingGen = new BindingGeneratorImpl();
495 final List<Type> genTypes = bindingGen.generateTypes(context);
497 assertTrue(genTypes != null);
498 assertEquals(8, genTypes.size());
500 int genTypesCount = 0;
502 for (final Type type : genTypes) {
503 if (type instanceof GeneratedType
504 && !(type instanceof GeneratedTransferObject)) {
506 } else if (type instanceof GeneratedTransferObject) {
507 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
509 if (genTO.getName().equals("CompositeKeyListKey")) {
510 final List<GeneratedProperty> properties = genTO
512 int propertyCount = 0;
513 for (final GeneratedProperty prop : properties) {
514 if (prop.getName().equals("Key1")) {
516 } else if (prop.getName().equals("Key2")) {
520 assertEquals(2, propertyCount);
522 } else if (genTO.getName().equals("InnerListKey")) {
523 final List<GeneratedProperty> properties = genTO
525 assertEquals(1, properties.size());
531 assertEquals(6, genTypesCount);
532 assertEquals(2, genTOsCount);
536 public void testGeneratedTypes() {
537 final String filePath = getClass().getResource("/demo-topology.yang")
539 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
540 assertTrue(context != null);
542 final BindingGenerator bindingGen = new BindingGeneratorImpl();
543 final List<Type> genTypes = bindingGen.generateTypes(context);
545 assertTrue(genTypes != null);
546 assertEquals(15, genTypes.size());
548 int genTypesCount = 0;
550 for (final Type type : genTypes) {
551 if (type instanceof GeneratedType
552 && !(type instanceof GeneratedTransferObject)) {
554 } else if (type instanceof GeneratedTransferObject) {
559 assertEquals(12, genTypesCount);
560 assertEquals(3, genTOsCount);