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