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.assertTrue;
13 import java.util.List;
16 import org.junit.Ignore;
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("/abstract-topology.yang").getPath();
43 final String typesPath = getClass().getResource("/ietf-inet-types@2010-09-24.yang").getPath();
44 final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, typesPath);
45 assertTrue(context != null);
47 final BindingGenerator bindingGen = new BindingGeneratorImpl();
48 final List<Type> genTypes = bindingGen.generateTypes(context);
50 assertTrue(genTypes != null);
51 assertEquals(11, genTypes.size());
58 public void testContainerResolving() {
59 final String filePath = getClass().getResource("/simple-container-demo.yang").getPath();
60 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
61 assertTrue(context != null);
63 final BindingGenerator bindingGen = new BindingGeneratorImpl();
64 final List<Type> genTypes = bindingGen.generateTypes(context);
66 assertTrue(genTypes != null);
67 assertEquals(2, genTypes.size());
69 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
70 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
72 assertEquals("SimpleContainer", simpleContainer.getName());
73 assertEquals("NestedContainer", nestedContainer.getName());
74 assertEquals(4, simpleContainer.getMethodDefinitions().size());
75 assertEquals(4, nestedContainer.getMethodDefinitions().size());
78 for (final MethodSignature method : simpleContainer
79 .getMethodDefinitions()) {
80 if (method.getName().equals("getFoo")) {
81 method.getReturnType().getName().equals("Integer");
85 if (method.getName().equals("setFoo")) {
87 final MethodSignature.Parameter param = method.getParameters()
89 assertEquals("foo", param.getName());
90 assertEquals("Integer", param.getType().getName());
93 if (method.getName().equals("getBar")) {
94 method.getReturnType().getName().equals("String");
98 if (method.getName().equals("getNestedContainer")) {
99 method.getReturnType().getName().equals("NestedContainer");
103 assertEquals(4, methodsCount);
106 for (final MethodSignature method : nestedContainer
107 .getMethodDefinitions()) {
108 if (method.getName().equals("getFoo")) {
109 method.getReturnType().getName().equals("Short");
113 if (method.getName().equals("setFoo")) {
115 final MethodSignature.Parameter param = method.getParameters()
117 assertEquals("foo", param.getName());
118 assertEquals("Short", param.getType().getName());
121 if (method.getName().equals("getBar")) {
122 method.getReturnType().getName().equals("String");
126 if (method.getName().equals("setBar")) {
127 method.getReturnType().getName().equals("String");
131 assertEquals(4, methodsCount);
136 public void testLeafListResolving() {
137 final String filePath = getClass().getResource("/simple-leaf-list-demo.yang").getPath();
138 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
139 assertTrue(context != null);
141 final BindingGenerator bindingGen = new BindingGeneratorImpl();
142 final List<Type> genTypes = bindingGen.generateTypes(context);
144 assertTrue(genTypes != null);
145 assertEquals(2, genTypes.size());
147 final GeneratedType simpleContainer = (GeneratedType) genTypes.get(0);
148 final GeneratedType nestedContainer = (GeneratedType) genTypes.get(1);
150 assertEquals("SimpleContainer", simpleContainer.getName());
151 assertEquals("NestedContainer", nestedContainer.getName());
152 assertEquals(4, simpleContainer.getMethodDefinitions().size());
153 assertEquals(3, nestedContainer.getMethodDefinitions().size());
155 int methodsCount = 0;
156 for (final MethodSignature method : simpleContainer
157 .getMethodDefinitions()) {
158 if (method.getName().equals("getFoo")) {
159 method.getReturnType().getName().equals("List");
163 if (method.getName().equals("setFoo")) {
165 final MethodSignature.Parameter param = method.getParameters()
167 assertEquals("foo", param.getName());
168 assertEquals("List", param.getType().getName());
171 if (method.getName().equals("getBar")) {
172 method.getReturnType().getName().equals("String");
176 if (method.getName().equals("getNestedContainer")) {
177 method.getReturnType().getName().equals("NestedContainer");
181 assertEquals(4, methodsCount);
184 for (final MethodSignature method : nestedContainer
185 .getMethodDefinitions()) {
186 if (method.getName().equals("getFoo")) {
187 method.getReturnType().getName().equals("Short");
191 if (method.getName().equals("setFoo")) {
193 final MethodSignature.Parameter param = method.getParameters()
195 assertEquals("foo", param.getName());
196 assertEquals("Short", param.getType().getName());
199 if (method.getName().equals("getBar")) {
200 method.getReturnType().getName().equals("List");
204 assertEquals(3, methodsCount);
209 public void testListResolving() {
210 final String filePath = getClass().getResource("/simple-list-demo.yang").getPath();
211 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
212 assertTrue(context != null);
214 final BindingGenerator bindingGen = new BindingGeneratorImpl();
215 final List<Type> genTypes = bindingGen.generateTypes(context);
217 assertTrue(genTypes != null);
218 assertEquals(4, genTypes.size());
220 int genTypesCount = 0;
222 for (final Type type : genTypes) {
223 if (type instanceof GeneratedType) {
224 final GeneratedType genType = (GeneratedType) type;
225 if (genType.getName().equals("ListParentContainer")) {
226 assertEquals(2, genType.getMethodDefinitions().size());
228 } else if (genType.getName().equals("SimpleList")) {
229 assertEquals(7, genType.getMethodDefinitions().size());
230 final List<MethodSignature> methods = genType
231 .getMethodDefinitions();
232 int methodsCount = 0;
233 for (final MethodSignature method : methods) {
234 if (method.getName().equals("getSimpleListKey")) {
235 assertEquals("SimpleListKey", method
236 .getReturnType().getName());
238 } else if (method.getName().equals(
239 "getListChildContainer")) {
240 assertEquals("ListChildContainer", method
241 .getReturnType().getName());
243 } else if (method.getName().equals("getFoo")) {
245 } else if (method.getName().equals("setFoo")) {
247 } else if (method.getName().equals("getSimpleLeafList")) {
249 } else if (method.getName().equals("setSimpleLeafList")) {
251 } else if (method.getName().equals("getBar")) {
255 assertEquals(7, methodsCount);
257 } else if (genType.getName().equals("ListChildContainer")) {
258 assertEquals(2, genType.getMethodDefinitions().size());
261 } else if (type instanceof GeneratedTransferObject) {
263 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
264 final List<GeneratedProperty> properties = genTO
266 final List<GeneratedProperty> hashProps = genTO
267 .getHashCodeIdentifiers();
268 final List<GeneratedProperty> equalProps = genTO
269 .getEqualsIdentifiers();
271 assertEquals(1, properties.size());
272 assertEquals("ListKey", properties.get(0).getName());
273 assertEquals("Byte", properties.get(0).getReturnType()
275 assertEquals(true, properties.get(0).isReadOnly());
276 assertEquals(1, hashProps.size());
277 assertEquals("ListKey", hashProps.get(0).getName());
278 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
279 assertEquals(1, equalProps.size());
280 assertEquals("ListKey", equalProps.get(0).getName());
281 assertEquals("Byte", equalProps.get(0).getReturnType()
285 assertEquals(3, genTypesCount);
286 assertEquals(1, genTOsCount);
291 public void testListCompositeKeyResolving() {
292 final String filePath = getClass().getResource("/list-composite-key.yang").getPath();
293 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
295 assertTrue(context != null);
297 final BindingGenerator bindingGen = new BindingGeneratorImpl();
298 final List<Type> genTypes = bindingGen.generateTypes(context);
300 assertTrue(genTypes != null);
301 assertEquals(6, genTypes.size());
303 int genTypesCount = 0;
305 for (final Type type : genTypes) {
306 if (type instanceof GeneratedType) {
308 } else if (type instanceof GeneratedTransferObject) {
309 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
311 if (genTO.getName().equals("CompositeKeyListKey")) {
312 final List<GeneratedProperty> properties = genTO
314 int propertyCount = 0;
315 for (final GeneratedProperty prop : properties) {
316 if (prop.getName().equals("Key1")) {
318 } else if (prop.getName().equals("Key2")) {
322 assertEquals(2, propertyCount);
324 } else if (genTO.getName().equals("InnerListKey")) {
325 final List<GeneratedProperty> properties = genTO
327 assertEquals(1, properties.size());
333 assertEquals(4, genTypesCount);
334 assertEquals(2, genTOsCount);
339 public void testGeneratedTypes() {
340 final String filePath = getClass().getResource("/demo-topology.yang").getPath();
341 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
342 assertTrue(context != null);
344 final BindingGenerator bindingGen = new BindingGeneratorImpl();
345 final List<Type> genTypes = bindingGen.generateTypes(context);
347 assertTrue(genTypes != null);
348 assertEquals(13, genTypes.size());
350 int genTypesCount = 0;
352 for (final Type type : genTypes) {
353 if (type instanceof GeneratedType) {
355 } else if (type instanceof GeneratedTransferObject) {
360 assertEquals(10, genTypesCount);
361 assertEquals(3, genTOsCount);