blob: 9f12346bdfa236266c1f584f3f3b9660bd86097c [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal870c56f2016-02-20 21:57:16 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.translator.tojava.utils;
18
Bharat saraswale2d51d62016-03-23 19:40:35 +053019import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
20import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswald72411a2016-04-19 01:00:16 +053021import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053022import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
23import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
24import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Bharat saraswale2d51d62016-03-23 19:40:35 +053025import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053026import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053027import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
28import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053029import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList;
30import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.isExtendsList;
Bharat saraswale2d51d62016-03-23 19:40:35 +053031import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
32import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053033import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswald72411a2016-04-19 01:00:16 +053034import static org.onosproject.yangutils.utils.UtilConstants.ENUM;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053035import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
37import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
38import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
39import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053040import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Bharat saraswale2d51d62016-03-23 19:40:35 +053041import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
42import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
43import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
44import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053045import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053046import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053047import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Bharat saraswal870c56f2016-02-20 21:57:16 +053048
49/**
Bharat saraswald9822e92016-04-05 15:13:44 +053050 * Represents generator for class definition of generated files.
Bharat saraswal870c56f2016-02-20 21:57:16 +053051 */
52public final class ClassDefinitionGenerator {
53
54 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053055 * Creates an instance of class definition generator.
Bharat saraswal870c56f2016-02-20 21:57:16 +053056 */
57 private ClassDefinitionGenerator() {
58 }
59
60 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053061 * Based on the file type and the YANG name of the file, generate the class
62 * / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053063 *
64 * @param genFileTypes generated file type
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053065 * @param yangName class name
Bharat saraswal870c56f2016-02-20 21:57:16 +053066 * @return class definition
67 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053068 public static String generateClassDefinition(int genFileTypes, String yangName) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053069
70 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053071 * Based on the file type and the YANG name of the file, generate the
Vinod Kumar Sc4216002016-03-03 19:55:30 +053072 * class / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053073 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053074 if ((genFileTypes & INTERFACE_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053075 return getInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053076 } else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
Bharat saraswalc0e04842016-05-12 13:16:57 +053077 return getBuilderClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053078 } else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 return getImplClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053080 } else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053081 return getBuilderInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053082 } else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053083 return getTypeClassDefinition(yangName);
84 } else if ((genFileTypes & GENERATE_UNION_CLASS) != 0) {
85 return getTypeClassDefinition(yangName);
Bharat saraswald72411a2016-04-19 01:00:16 +053086 } else if ((genFileTypes & GENERATE_ENUM_CLASS) != 0) {
87 return getEnumClassDefinition(yangName);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053088 } else if ((genFileTypes & GENERATE_SERVICE_AND_MANAGER) != 0) {
Gaurav Agrawal56527662016-04-20 15:49:17 +053089 return getRpcInterfaceDefinition(yangName);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053090 } else if ((genFileTypes & GENERATE_EVENT_CLASS) != 0) {
91 return getEventDefinition(yangName);
92 } else if ((genFileTypes & GENERATE_EVENT_LISTENER_INTERFACE) != 0) {
93 return getEventListenerDefinition(yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +053094 }
95 return null;
96 }
97
98 /**
Bharat saraswald72411a2016-04-19 01:00:16 +053099 * Returns enum file class definition.
100 *
101 * @param yangName class name
Gaurav Agrawal56527662016-04-20 15:49:17 +0530102 * @return enum file class definition
Bharat saraswald72411a2016-04-19 01:00:16 +0530103 */
104 private static String getEnumClassDefinition(String yangName) {
105 return PUBLIC + SPACE + ENUM + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
106 }
107
108 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530109 * Returns interface file class definition.
110 *
111 * @param yangName file name
112 * @return definition
113 */
114 private static String getInterfaceDefinition(String yangName) {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530115 if (!isExtendsList()) {
116 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
117 }
118 String def = PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE;
119 for (String extend : getExtendsList()) {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530120 def = def + extend + COMMA + SPACE;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530121 }
122 def = trimAtLast(def, COMMA);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530123
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530124 return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 }
126
127 /**
128 * Returns builder interface file class definition.
129 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530130 * @param yangName java class name, corresponding to which the builder class
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530131 * is being generated
Bharat saraswal870c56f2016-02-20 21:57:16 +0530132 * @return definition
133 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530134 private static String getBuilderInterfaceDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530135 return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530136 }
137
138 /**
139 * Returns builder file class definition.
140 *
141 * @param yangName file name
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530142 * @param genFileTypes
Bharat saraswal870c56f2016-02-20 21:57:16 +0530143 * @return definition
144 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530145 private static String getBuilderClassDefinition(String yangName) {
146 return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
147 + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530148 }
149
150 /**
151 * Returns impl file class definition.
152 *
153 * @param yangName file name
154 * @return definition
155 */
156 private static String getImplClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530157 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName
158 + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530159 }
160
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530161 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530162 * Returns type file class definition.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530163 *
164 * @param yangName file name
165 * @return definition
166 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530167 private static String getTypeClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530168 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530169 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530170
171 /**
172 * Returns rpc file interface definition.
173 *
174 * @param yangName file name
175 * @return definition
176 */
177 private static String getRpcInterfaceDefinition(String yangName) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530178 if (yangName.contains(SERVICE)) {
179 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
180 }
181 return PUBLIC + SPACE + CLASS + SPACE + yangName + MANAGER + SPACE + IMPLEMENTS + SPACE + yangName + SERVICE
182 + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Gaurav Agrawal56527662016-04-20 15:49:17 +0530183 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530184
185 /**
186 * Returns event class definition.
187 *
188 * @param javaName file name
189 * @return definition
190 */
191 private static String getEventDefinition(String javaName) {
192 String classDef = PUBLIC + SPACE + CLASS + SPACE + javaName + SPACE + "extends AbstractEvent<"
193 + javaName + ".Type, " + javaName;
194 if (classDef.length() < 5) {
195 throw new RuntimeException("Event class name is error");
196 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530197 classDef = classDef.substring(0, classDef.length() - 5);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530198 classDef = classDef + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
199
200 return classDef;
201 }
202
203 /**
204 * Returns event listener interface definition.
205 *
206 * @param javaName file name
207 * @return definition
208 */
209 private static String getEventListenerDefinition(String javaName) {
210 String intfDef = PUBLIC + SPACE + INTERFACE + SPACE + javaName + SPACE + "extends EventListener<"
211 + javaName;
212 if (intfDef.length() < 8) {
213 throw new RuntimeException("Event listener interface name is error");
214 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530215 intfDef = intfDef.substring(0, intfDef.length() - 8);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530216 intfDef = intfDef + "Event>" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
217
218 return intfDef;
219 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530220}