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 org.junit.Test;
11 import org.opendaylight.controller.binding.generator.util.ReferencedTypeImpl;
12 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
13 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
14 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
15 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
16 import org.opendaylight.controller.sal.binding.model.api.Type;
17 import org.opendaylight.controller.yang.model.api.Module;
18 import org.opendaylight.controller.yang.model.api.SchemaContext;
19 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
20 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
23 import java.util.ArrayList;
24 import java.util.List;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 public class GenEnumResolvingTest {
33 private SchemaContext resolveSchemaContextFromFiles(
34 final String... yangFiles) {
35 final YangModelParser parser = new YangParserImpl();
37 final List<File> inputFiles = new ArrayList<File>();
38 for (int i = 0; i < yangFiles.length; ++i) {
39 inputFiles.add(new File(yangFiles[i]));
42 final Set<Module> modules = parser.parseYangModels(inputFiles);
43 return parser.resolveSchemaContext(modules);
47 public void testLeafEnumResolving() {
48 final String ietfInterfacesPath = getClass().getResource(
49 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
50 final String ifTypePath = getClass().getResource(
51 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
52 final String yangTypesPath = getClass().getResource(
53 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
55 final SchemaContext context = resolveSchemaContextFromFiles(
56 ietfInterfacesPath, ifTypePath, yangTypesPath);
57 assertTrue(context != null);
59 final BindingGenerator bindingGen = new BindingGeneratorImpl();
60 final List<Type> genTypes = bindingGen.generateTypes(context);
61 assertTrue(genTypes != null);
63 assertEquals("Expected count of all Generated Types from yang models " +
64 "is 22", 25, genTypes.size());
66 GeneratedType genInterface = null;
67 for (final Type type : genTypes) {
68 if (type instanceof GeneratedType) {
69 if (type.getName().equals("Interface")) {
70 genInterface = (GeneratedType) type;
74 assertNotNull("Generated Type Interface is not present in list of " +
75 "Generated Types", genInterface);
77 Enumeration linkUpDownTrapEnable = null;
78 Enumeration operStatus = null;
79 final List<Enumeration> enums = genInterface.getEnumerations();
80 assertNotNull("Generated Type Interface cannot contain NULL reference" +
81 " to Enumeration types!", enums);
82 assertEquals("Generated Type Interface MUST contain 2 Enumeration " +
83 "Types", 2, enums.size());
84 for (final Enumeration e : enums) {
85 if (e.getName().equals("LinkUpDownTrapEnable")) {
86 linkUpDownTrapEnable = e;
87 } else if (e.getName().equals("OperStatus")) {
92 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!",
93 linkUpDownTrapEnable);
94 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
96 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition " +
97 "not NULL reference!", linkUpDownTrapEnable.getValues());
98 assertNotNull("Enum OperStatus MUST contain Values definition not " +
99 "NULL reference!", operStatus.getValues());
100 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2,
101 linkUpDownTrapEnable.getValues().size());
102 assertEquals("Enum OperStatus MUST contain 7 values!", 7,
103 operStatus.getValues().size());
105 final List<MethodSignature> methods = genInterface
106 .getMethodDefinitions();
108 assertNotNull("Generated Interface cannot contain NULL reference for " +
109 "Method Signature Definitions!", methods);
111 assertEquals("Expected count of method signature definitions is 26",
113 Enumeration ianaIfType = null;
114 for (final MethodSignature method : methods) {
115 if (method.getName().equals("getType")) {
116 if (method.getReturnType() instanceof Enumeration) {
117 ianaIfType = (Enumeration)method.getReturnType();
122 assertNotNull("Method getType MUST return Enumeration Type, " +
123 "not NULL reference!", ianaIfType);
124 assertEquals("Enumeration getType MUST contain 272 values!", 272,
125 ianaIfType.getValues().size());
129 public void testTypedefEnumResolving() {
130 final String ianaIfTypePath = getClass().getResource(
131 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
133 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
134 assertTrue(context != null);
135 final BindingGenerator bindingGen = new BindingGeneratorImpl();
136 final List<Type> genTypes = bindingGen.generateTypes(context);
137 assertTrue(genTypes != null);
138 assertEquals(3, genTypes.size());
140 final Type type = genTypes.get(1);
141 assertTrue(type instanceof Enumeration);
143 final Enumeration enumer = (Enumeration) type;
144 assertEquals("Enumeration type MUST contain 272 values!", 272,
145 enumer.getValues().size());
149 public void testLeafrefEnumResolving() {
150 final String ietfInterfacesPath = getClass().getResource(
151 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
152 final String ifTypePath = getClass().getResource(
153 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
154 final String yangTypesPath = getClass().getResource(
155 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
156 final String topologyPath = getClass().getResource(
157 "/enum-test-models/abstract-topology@2013-02-08.yang")
159 final String inetTypesPath = getClass().getResource(
160 "/enum-test-models/ietf-inet-types@2010-09-24.yang")
162 final SchemaContext context = resolveSchemaContextFromFiles(
163 ietfInterfacesPath, ifTypePath, yangTypesPath, topologyPath,
166 assertNotNull(context);
167 final BindingGenerator bindingGen = new BindingGeneratorImpl();
168 final List<Type> genTypes = bindingGen.generateTypes(context);
169 assertNotNull(genTypes);
170 assertTrue(!genTypes.isEmpty());
172 GeneratedType genInterface = null;
173 for (final Type type : genTypes) {
174 if (type instanceof GeneratedType) {
175 if (type.getPackageName().equals("org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev201328.topology.interfaces")
176 && type.getName().equals("Interface")) {
177 genInterface = (GeneratedType) type;
181 assertNotNull("Generated Type Interface is not present in list of " +
182 "Generated Types", genInterface);
184 Type linkUpDownTrapEnable = null;
185 Type operStatus = null;
186 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
187 assertNotNull("Generated Type Interface cannot contain NULL reference" +
188 " to Enumeration types!", methods);
189 assertEquals("Generated Type Interface MUST contain 7 Methods ",
191 for (final MethodSignature method : methods) {
192 if (method.getName().equals("getLinkUpDownTrapEnable")) {
193 linkUpDownTrapEnable = method.getReturnType();
194 } else if (method.getName().equals("getOperStatus")) {
195 operStatus = method.getReturnType();
199 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!",
200 linkUpDownTrapEnable);
201 assertTrue("Expected LinkUpDownTrapEnable of type ReferencedTypeImpl",
202 linkUpDownTrapEnable instanceof ReferencedTypeImpl);
203 assertEquals(linkUpDownTrapEnable.getPackageName(),
204 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20121115.interfaces.Interface");
206 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!",
208 assertTrue("Expected OperStatus of type ReferencedTypeImpl",
209 operStatus instanceof ReferencedTypeImpl);
210 assertEquals(operStatus.getPackageName(),
211 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20121115.interfaces.Interface");