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.yangtools.sal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
22 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
29 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
31 public class GenEnumResolvingTest {
33 private SchemaContext resolveSchemaContextFromFiles(
34 final URI... 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() throws URISyntaxException {
48 final URI ietfInterfacesPath = getClass().getResource(
49 "/enum-test-models/ietf-interfaces@2012-11-15.yang").toURI();
50 final URI ifTypePath = getClass().getResource(
51 "/enum-test-models/iana-if-type@2012-06-05.yang").toURI();
52 final URI yangTypesPath = getClass().getResource(
53 "/enum-test-models/ietf-yang-types@2010-09-24.yang").toURI();
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", 20, genTypes.size());
65 GeneratedType genInterface = null;
66 for (final Type type : genTypes) {
67 if (type instanceof GeneratedType) {
68 if (type.getName().equals("Interface")) {
69 genInterface = (GeneratedType) type;
73 assertNotNull("Generated Type Interface is not present in list of " +
74 "Generated Types", genInterface);
76 Enumeration linkUpDownTrapEnable = null;
77 Enumeration operStatus = null;
78 final List<Enumeration> enums = genInterface.getEnumerations();
79 assertNotNull("Generated Type Interface cannot contain NULL reference" +
80 " to Enumeration types!", enums);
81 assertEquals("Generated Type Interface MUST contain 2 Enumeration " +
82 "Types", 2, enums.size());
83 for (final Enumeration e : enums) {
84 if (e.getName().equals("LinkUpDownTrapEnable")) {
85 linkUpDownTrapEnable = e;
86 } else if (e.getName().equals("OperStatus")) {
91 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!",
92 linkUpDownTrapEnable);
93 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
95 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition " +
96 "not NULL reference!", linkUpDownTrapEnable.getValues());
97 assertNotNull("Enum OperStatus MUST contain Values definition not " +
98 "NULL reference!", operStatus.getValues());
99 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2,
100 linkUpDownTrapEnable.getValues().size());
101 assertEquals("Enum OperStatus MUST contain 7 values!", 7,
102 operStatus.getValues().size());
104 final List<MethodSignature> methods = genInterface
105 .getMethodDefinitions();
107 assertNotNull("Generated Interface cannot contain NULL reference for " +
108 "Method Signature Definitions!", methods);
110 assertEquals("Expected count of method signature definitions is 15",
112 Enumeration ianaIfType = null;
113 for (final MethodSignature method : methods) {
114 if (method.getName().equals("getType")) {
115 if (method.getReturnType() instanceof Enumeration) {
116 ianaIfType = (Enumeration)method.getReturnType();
121 assertNotNull("Method getType MUST return Enumeration Type, " +
122 "not NULL reference!", ianaIfType);
123 assertEquals("Enumeration getType MUST contain 272 values!", 272,
124 ianaIfType.getValues().size());
128 public void testTypedefEnumResolving() throws URISyntaxException {
129 final URI ianaIfTypePath = getClass().getResource(
130 "/leafref-test-models/iana-if-type@2012-06-05.yang").toURI();
132 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
133 assertTrue(context != null);
134 final BindingGenerator bindingGen = new BindingGeneratorImpl();
135 final List<Type> genTypes = bindingGen.generateTypes(context);
136 assertTrue(genTypes != null);
137 assertEquals(1, genTypes.size());
139 final Type type = genTypes.get(0);
140 assertTrue(type instanceof Enumeration);
142 final Enumeration enumer = (Enumeration) type;
143 assertEquals("Enumeration type MUST contain 272 values!", 272,
144 enumer.getValues().size());
148 public void testLeafrefEnumResolving() throws URISyntaxException {
149 final URI ietfInterfacesPath = getClass().getResource(
150 "/enum-test-models/ietf-interfaces@2012-11-15.yang").toURI();
151 final URI ifTypePath = getClass().getResource(
152 "/enum-test-models/iana-if-type@2012-06-05.yang").toURI();
153 final URI yangTypesPath = getClass().getResource(
154 "/enum-test-models/ietf-yang-types@2010-09-24.yang").toURI();
155 final URI topologyPath = getClass().getResource(
156 "/enum-test-models/abstract-topology@2013-02-08.yang")
158 final URI inetTypesPath = getClass().getResource(
159 "/enum-test-models/ietf-inet-types@2010-09-24.yang")
161 final SchemaContext context = resolveSchemaContextFromFiles(
162 ietfInterfacesPath, ifTypePath, yangTypesPath, topologyPath,
165 assertNotNull(context);
166 final BindingGenerator bindingGen = new BindingGeneratorImpl();
167 final List<Type> genTypes = bindingGen.generateTypes(context);
168 assertNotNull(genTypes);
169 assertTrue(!genTypes.isEmpty());
171 GeneratedType genInterface = null;
172 for (final Type type : genTypes) {
173 if (type instanceof GeneratedType) {
174 if (type.getPackageName().equals("org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
175 && type.getName().equals("Interface")) {
176 genInterface = (GeneratedType) type;
180 assertNotNull("Generated Type Interface is not present in list of " +
181 "Generated Types", genInterface);
183 Type linkUpDownTrapEnable = null;
184 Type operStatus = null;
185 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
186 assertNotNull("Generated Type Interface cannot contain NULL reference" +
187 " to Enumeration types!", methods);
188 assertEquals("Generated Type Interface MUST contain 5 Methods ",
190 for (final MethodSignature method : methods) {
191 if (method.getName().equals("getLinkUpDownTrapEnable")) {
192 linkUpDownTrapEnable = method.getReturnType();
193 } else if (method.getName().equals("getOperStatus")) {
194 operStatus = method.getReturnType();
198 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!",
199 linkUpDownTrapEnable);
200 assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
201 assertEquals(linkUpDownTrapEnable.getPackageName(),
202 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
204 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!",
206 assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
207 assertEquals(operStatus.getPackageName(),
208 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");