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 testLeafEnumResolving() {
47 final String ietfInterfacesPath = getClass().getResource(
48 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
49 final String ifTypePath = getClass().getResource(
50 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
51 final String yangTypesPath = getClass().getResource(
52 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
54 final SchemaContext context = resolveSchemaContextFromFiles(
55 ietfInterfacesPath, ifTypePath, yangTypesPath);
56 assertTrue(context != null);
58 final BindingGenerator bindingGen = new BindingGeneratorImpl();
59 final List<Type> genTypes = bindingGen.generateTypes(context);
60 assertTrue(genTypes != null);
64 public void testTypedefEnumResolving() {
65 final String ianaIfTypePath = getClass().getResource(
66 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
68 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
69 assertTrue(context != null);
70 final BindingGenerator bindingGen = new BindingGeneratorImpl();
71 final List<Type> genTypes = bindingGen.generateTypes(context);
72 assertTrue(genTypes != null);
73 assertEquals(2, genTypes.size());
75 final Type type = genTypes.get(1);
76 assertTrue(type instanceof GeneratedTransferObject);
78 final GeneratedTransferObject genTransObj = (GeneratedTransferObject) type;
79 final List<GeneratedProperty> properties = genTransObj.getProperties();
80 assertNotNull(properties);
81 assertEquals(1, properties.size());
83 GeneratedProperty property = properties.get(0);
84 assertNotNull(property);
85 assertNotNull(property.getReturnType());
87 assertTrue(property.getReturnType() instanceof Enumeration);
88 final Enumeration enumer = (Enumeration) property.getReturnType();
89 assertEquals(272, enumer.getValues().size());
93 public void testMultipleModulesResolving() {
94 final String topologyPath = getClass().getResource(
95 "/abstract-topology.yang").getPath();
96 final String typesPath = getClass().getResource(
97 "/ietf-inet-types@2010-09-24.yang").getPath();
98 final SchemaContext context = resolveSchemaContextFromFiles(
99 topologyPath, typesPath);
100 assertTrue(context != null);
102 final BindingGenerator bindingGen = new BindingGeneratorImpl();
103 final List<Type> genTypes = bindingGen.generateTypes(context);
105 assertTrue(genTypes != null);
106 assertEquals(24, genTypes.size());
110 public void testLeafrefResolving() {
111 final String topologyPath = getClass().getResource(
112 "/leafref-test-models/abstract-topology@2013-02-08.yang")
114 final String interfacesPath = getClass().getResource(
115 "/leafref-test-models/ietf-interfaces@2012-11-15.yang")
117 // final String ifTypePath = getClass().getResource(
118 // "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
119 final String inetTypesPath = getClass().getResource(
120 "/leafref-test-models/ietf-inet-types@2010-09-24.yang")
122 final String yangTypesPath = getClass().getResource(
123 "/leafref-test-models/ietf-yang-types@2010-09-24.yang")
126 assertTrue(topologyPath != null);
127 assertTrue(interfacesPath != null);
128 // assertTrue(ifTypePath != null);
129 assertTrue(inetTypesPath != null);
130 assertTrue(yangTypesPath != null);
132 // final SchemaContext context = resolveSchemaContextFromFiles(
133 // topologyPath, interfacesPath, ifTypePath, inetTypesPath,
135 final SchemaContext context = resolveSchemaContextFromFiles(
136 topologyPath, interfacesPath, inetTypesPath, yangTypesPath);
137 assertTrue(context != null);
138 assertEquals(4, context.getModules().size());
140 final BindingGenerator bindingGen = new BindingGeneratorImpl();
141 final List<Type> genTypes = bindingGen.generateTypes(context);
143 assertEquals(50, genTypes.size());
144 assertTrue(genTypes != null);
146 GeneratedTransferObject gtIfcKey = null;
147 GeneratedType gtIfc = null;
148 GeneratedType gtNetworkLink = null;
149 GeneratedType gtSource = null;
150 GeneratedType gtDest = null;
151 GeneratedType gtTunnel = null;
152 GeneratedTransferObject gtTunnelKey = null;
153 for (final Type type : genTypes) {
154 String name = type.getName();
155 if ("InterfaceKey".equals(name)) {
156 gtIfcKey = (GeneratedTransferObject) type;
157 } else if ("Interface".equals(name)) {
158 gtIfc = (GeneratedType) type;
159 } else if ("NetworkLink".equals(name)) {
160 gtNetworkLink = (GeneratedType) type;
161 } else if ("SourceNode".equals(name)) {
162 gtSource = (GeneratedType) type;
163 } else if ("DestinationNode".equals(name)) {
164 gtDest = (GeneratedType) type;
165 } else if ("Tunnel".equals(name)) {
166 gtTunnel = (GeneratedType) type;
167 } else if ("TunnelKey".equals(name)) {
168 gtTunnelKey = (GeneratedTransferObject) type;
172 assertNotNull(gtIfcKey);
173 assertNotNull(gtIfc);
174 assertNotNull(gtNetworkLink);
175 assertNotNull(gtSource);
176 assertNotNull(gtDest);
177 assertNotNull(gtTunnel);
178 assertNotNull(gtTunnelKey);
181 final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
182 assertNotNull(gtIfcKeyProps);
183 GeneratedProperty ifcIdProp = null;
184 for (final GeneratedProperty property : gtIfcKeyProps) {
185 if (property.getName().equals("InterfaceId")) {
186 ifcIdProp = property;
189 assertNotNull(ifcIdProp);
190 Type ifcIdPropType = ifcIdProp.getReturnType();
191 assertNotNull(ifcIdPropType);
192 assertFalse(ifcIdPropType.equals("java.lang.Void"));
193 assertTrue(ifcIdPropType.getName().equals("String"));
196 final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
197 assertNotNull(gtIfcMethods);
198 MethodSignature getIfcKey = null;
199 MethodSignature getHigherLayerIf = null;
200 for (final MethodSignature method : gtIfcMethods) {
201 if (method.getName().equals("getInterfaceKey")) {
203 } else if (method.getName().equals("getHigherLayerIf")) {
204 getHigherLayerIf = method;
207 assertNotNull(getIfcKey);
208 Type getIfcKeyType = getIfcKey.getReturnType();
209 assertNotNull(getIfcKeyType);
210 assertFalse(getIfcKeyType.equals("java.lang.Void"));
211 assertTrue(getIfcKeyType.getName().equals("InterfaceKey"));
213 assertNotNull(getHigherLayerIf);
214 Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
215 assertNotNull(getHigherLayerIfType);
216 assertFalse(getHigherLayerIfType.equals("java.lang.Void"));
217 assertTrue(getHigherLayerIfType.getName().equals("List"));
220 final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink
221 .getMethodDefinitions();
222 assertNotNull(gtNetworkLinkMethods);
223 MethodSignature getIfc = null;
224 for (MethodSignature method : gtNetworkLinkMethods) {
225 if (method.getName().equals("getInterface")) {
229 assertNotNull(getIfc);
230 Type getIfcType = getIfc.getReturnType();
231 assertNotNull(getIfcType);
232 assertFalse(getIfcType.equals("java.lang.Void"));
233 assertTrue(getIfcType.getName().equals("String"));
236 final List<MethodSignature> gtSourceMethods = gtSource
237 .getMethodDefinitions();
238 assertNotNull(gtSourceMethods);
239 MethodSignature getIdSource = null;
240 for (MethodSignature method : gtSourceMethods) {
241 if (method.getName().equals("getId")) {
242 getIdSource = method;
245 assertNotNull(getIdSource);
246 Type getIdType = getIdSource.getReturnType();
247 assertNotNull(getIdType);
248 assertFalse(getIdType.equals("java.lang.Void"));
249 assertTrue(getIdType.getName().equals("Uri"));
252 final List<MethodSignature> gtDestMethods = gtDest
253 .getMethodDefinitions();
254 assertNotNull(gtDestMethods);
255 MethodSignature getIdDest = null;
256 for (MethodSignature method : gtDestMethods) {
257 if (method.getName().equals("getId")) {
261 assertNotNull(getIdDest);
262 Type getIdDestType = getIdDest.getReturnType();
263 assertNotNull(getIdDestType);
264 assertFalse(getIdDestType.equals("java.lang.Void"));
265 assertTrue(getIdDestType.getName().equals("Uri"));
268 final List<MethodSignature> gtTunnelMethods = gtTunnel
269 .getMethodDefinitions();
270 assertNotNull(gtTunnelMethods);
271 MethodSignature getTunnelKey = null;
272 for (MethodSignature method : gtTunnelMethods) {
273 if (method.getName().equals("getTunnelKey")) {
274 getTunnelKey = method;
277 assertNotNull(getTunnelKey);
278 Type getTunnelKeyType = getTunnelKey.getReturnType();
279 assertNotNull(getTunnelKeyType);
280 assertFalse(getTunnelKeyType.equals("java.lang.Void"));
281 assertTrue(getTunnelKeyType.getName().equals("TunnelKey"));
284 final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey
286 assertNotNull(gtTunnelKeyProps);
287 GeneratedProperty tunnelId = null;
288 for (final GeneratedProperty property : gtTunnelKeyProps) {
289 if (property.getName().equals("TunnelId")) {
293 assertNotNull(tunnelId);
294 Type tunnelIdType = tunnelId.getReturnType();
295 assertNotNull(tunnelIdType);
296 assertFalse(tunnelIdType.equals("java.lang.Void"));
297 assertTrue(tunnelIdType.getName().equals("Uri"));
301 public void testContainerResolving() {
302 final String filePath = getClass().getResource(
303 "/simple-container-demo.yang").getPath();
304 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
305 assertTrue(context != null);
307 final BindingGenerator bindingGen = new BindingGeneratorImpl();
308 final List<Type> genTypes = bindingGen.generateTypes(context);
310 assertTrue(genTypes != null);
311 assertEquals(3, genTypes.size());
313 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
314 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
316 assertEquals("SimpleContainer", simpleContainer.getName());
317 assertEquals("NestedContainer", nestedContainer.getName());
318 assertEquals(5, simpleContainer.getMethodDefinitions().size());
319 assertEquals(4, nestedContainer.getMethodDefinitions().size());
321 int methodsCount = 0;
322 for (final MethodSignature method : simpleContainer
323 .getMethodDefinitions()) {
324 if (method.getName().equals("getFoo")) {
325 method.getReturnType().getName().equals("Integer");
329 if (method.getName().equals("setFoo")) {
331 final MethodSignature.Parameter param = method.getParameters()
333 assertEquals("foo", param.getName());
334 assertEquals("Integer", param.getType().getName());
337 if (method.getName().equals("getBar")) {
338 method.getReturnType().getName().equals("String");
342 if (method.getName().equals("getNestedContainer")) {
343 method.getReturnType().getName().equals("NestedContainer");
347 assertEquals(4, methodsCount);
350 for (final MethodSignature method : nestedContainer
351 .getMethodDefinitions()) {
352 if (method.getName().equals("getFoo")) {
353 method.getReturnType().getName().equals("Short");
357 if (method.getName().equals("setFoo")) {
359 final MethodSignature.Parameter param = method.getParameters()
361 assertEquals("foo", param.getName());
362 assertEquals("Short", param.getType().getName());
365 if (method.getName().equals("getBar")) {
366 method.getReturnType().getName().equals("String");
370 if (method.getName().equals("setBar")) {
371 method.getReturnType().getName().equals("String");
375 assertEquals(4, methodsCount);
379 public void testLeafListResolving() {
380 final String filePath = getClass().getResource(
381 "/simple-leaf-list-demo.yang").getPath();
382 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
383 assertTrue(context != null);
385 final BindingGenerator bindingGen = new BindingGeneratorImpl();
386 final List<Type> genTypes = bindingGen.generateTypes(context);
388 assertTrue(genTypes != null);
389 assertEquals(3, genTypes.size());
391 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
392 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
394 assertEquals("SimpleContainer", simpleContainer.getName());
395 assertEquals("NestedContainer", nestedContainer.getName());
396 assertEquals(5, simpleContainer.getMethodDefinitions().size());
397 assertEquals(3, nestedContainer.getMethodDefinitions().size());
399 int methodsCount = 0;
400 for (final MethodSignature method : simpleContainer
401 .getMethodDefinitions()) {
402 if (method.getName().equals("getFoo")) {
403 method.getReturnType().getName().equals("List");
407 if (method.getName().equals("setFoo")) {
409 final MethodSignature.Parameter param = method.getParameters()
411 assertEquals("foo", param.getName());
412 assertEquals("List", param.getType().getName());
415 if (method.getName().equals("getBar")) {
416 method.getReturnType().getName().equals("String");
420 if (method.getName().equals("getNestedContainer")) {
421 method.getReturnType().getName().equals("NestedContainer");
425 assertEquals(4, methodsCount);
428 for (final MethodSignature method : nestedContainer
429 .getMethodDefinitions()) {
430 if (method.getName().equals("getFoo")) {
431 method.getReturnType().getName().equals("Short");
435 if (method.getName().equals("setFoo")) {
437 final MethodSignature.Parameter param = method.getParameters()
439 assertEquals("foo", param.getName());
440 assertEquals("Short", param.getType().getName());
443 if (method.getName().equals("getBar")) {
444 method.getReturnType().getName().equals("List");
448 assertEquals(3, methodsCount);
452 public void testListResolving() {
453 final String filePath = getClass()
454 .getResource("/simple-list-demo.yang").getPath();
455 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
456 assertTrue(context != null);
458 final BindingGenerator bindingGen = new BindingGeneratorImpl();
459 final List<Type> genTypes = bindingGen.generateTypes(context);
461 assertTrue(genTypes != null);
462 assertEquals(5, genTypes.size());
464 int genTypesCount = 0;
466 for (final Type type : genTypes) {
467 if (type instanceof GeneratedType
468 && !(type instanceof GeneratedTransferObject)) {
469 final GeneratedType genType = (GeneratedType) type;
470 if (genType.getName().equals("ListParentContainer")) {
471 assertEquals(2, genType.getMethodDefinitions().size());
473 } else if (genType.getName().equals("SimpleList")) {
474 assertEquals(8, genType.getMethodDefinitions().size());
475 final List<MethodSignature> methods = genType
476 .getMethodDefinitions();
477 int methodsCount = 0;
478 for (final MethodSignature method : methods) {
479 if (method.getName().equals("getSimpleListKey")) {
480 assertEquals("SimpleListKey", method
481 .getReturnType().getName());
483 } else if (method.getName().equals(
484 "getListChildContainer")) {
485 assertEquals("ListChildContainer", method
486 .getReturnType().getName());
488 } else if (method.getName().equals("getFoo")) {
490 } else if (method.getName().equals("setFoo")) {
492 } else if (method.getName().equals("getSimpleLeafList")) {
494 } else if (method.getName().equals("setSimpleLeafList")) {
496 } else if (method.getName().equals("getBar")) {
500 assertEquals(7, methodsCount);
502 } else if (genType.getName().equals("ListChildContainer")) {
503 assertEquals(2, genType.getMethodDefinitions().size());
506 } else if (type instanceof GeneratedTransferObject) {
508 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
509 final List<GeneratedProperty> properties = genTO
511 final List<GeneratedProperty> hashProps = genTO
512 .getHashCodeIdentifiers();
513 final List<GeneratedProperty> equalProps = genTO
514 .getEqualsIdentifiers();
516 assertEquals(1, properties.size());
517 assertEquals("ListKey", properties.get(0).getName());
518 assertEquals("Byte", properties.get(0).getReturnType()
520 assertEquals(true, properties.get(0).isReadOnly());
521 assertEquals(1, hashProps.size());
522 assertEquals("ListKey", hashProps.get(0).getName());
523 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
524 assertEquals(1, equalProps.size());
525 assertEquals("ListKey", equalProps.get(0).getName());
526 assertEquals("Byte", equalProps.get(0).getReturnType()
530 assertEquals(3, genTypesCount);
531 assertEquals(1, genTOsCount);
535 public void testListCompositeKeyResolving() {
536 final String filePath = getClass().getResource(
537 "/list-composite-key.yang").getPath();
538 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(7, 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) {
555 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
557 if (genTO.getName().equals("CompositeKeyListKey")) {
558 final List<GeneratedProperty> properties = genTO
560 int propertyCount = 0;
561 for (final GeneratedProperty prop : properties) {
562 if (prop.getName().equals("Key1")) {
564 } else if (prop.getName().equals("Key2")) {
568 assertEquals(2, propertyCount);
570 } else if (genTO.getName().equals("InnerListKey")) {
571 final List<GeneratedProperty> properties = genTO
573 assertEquals(1, properties.size());
579 assertEquals(5, genTypesCount);
580 assertEquals(2, genTOsCount);
584 public void testGeneratedTypes() {
585 final String filePath = getClass().getResource("/demo-topology.yang")
587 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
588 assertTrue(context != null);
590 final BindingGenerator bindingGen = new BindingGeneratorImpl();
591 final List<Type> genTypes = bindingGen.generateTypes(context);
593 assertTrue(genTypes != null);
594 assertEquals(14, genTypes.size());
596 int genTypesCount = 0;
598 for (final Type type : genTypes) {
599 if (type instanceof GeneratedType
600 && !(type instanceof GeneratedTransferObject)) {
602 } else if (type instanceof GeneratedTransferObject) {
607 assertEquals(11, genTypesCount);
608 assertEquals(3, genTOsCount);