blob: 3a284bde2f2c109d23926586fe08307809a0402e [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +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;
18
Vinod Kumar S38046502016-03-23 15:30:27 +053019import java.io.IOException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020
Vinod Kumar S38046502016-03-23 15:30:27 +053021import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053022import org.onosproject.yangutils.datamodel.YangTypeHolder;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053023import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
24import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053025import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053026
Bharat saraswald50c6382016-07-14 21:57:13 +053027import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ALL_EVENT_CLASS_MASK;
Bharat saraswald72411a2016-04-19 01:00:16 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053031import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar S38046502016-03-23 15:30:27 +053032
33/**
Bharat saraswald9822e92016-04-05 15:13:44 +053034 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053035 * Contains fragment file object of different types of java file.
36 * Uses required object(s) to generate the target java file(s).
Vinod Kumar S38046502016-03-23 15:30:27 +053037 */
38public class TempJavaCodeFragmentFiles {
39
40 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041 * Has the temporary files required for bean generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053042 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053043 private TempJavaBeanFragmentFiles beanTempFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053044
45 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046 * Has the temporary files required for bean generated classes.
Bharat saraswale2d51d62016-03-23 19:40:35 +053047 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053048 private TempJavaTypeFragmentFiles typeTempFiles;
Bharat saraswale2d51d62016-03-23 19:40:35 +053049
50 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053051 * Has the temporary files required for service generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053052 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053 private TempJavaServiceFragmentFiles serviceTempFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053054
55 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053056 * Has the temporary files required for enumeration generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053057 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053058 private TempJavaEnumerationFragmentFiles enumerationTempFiles;
Bharat saraswald72411a2016-04-19 01:00:16 +053059
Bharat saraswal2f11f652016-03-25 18:19:46 +053060 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053061 * Has the temporary files required for enumeration generated classes.
62 */
63 private TempJavaEventFragmentFiles tempJavaEventFragmentFiles;
64
65 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +053067 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068 * @param javaFileInfo generated java file info
Vinod Kumar S38046502016-03-23 15:30:27 +053069 * @throws IOException when fails to create new file handle
70 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 public TempJavaCodeFragmentFiles(JavaFileInfo javaFileInfo)
Bharat saraswale2d51d62016-03-23 19:40:35 +053072 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053073
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053074 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
75 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053076 }
77
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053078 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
79 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053080 }
81
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053082 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
83 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
84 }
85
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053086 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
88 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053089
Bharat saraswald50c6382016-07-14 21:57:13 +053090 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
91 setEventFragmentFiles(new TempJavaEventFragmentFiles(javaFileInfo));
92 }
93
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053094 }
95
96 /**
97 * Retrieves the temp file handle for bean file generation.
98 *
99 * @return temp file handle for bean file generation
100 */
101 public TempJavaBeanFragmentFiles getBeanTempFiles() {
102 return beanTempFiles;
103 }
104
105 /**
106 * Sets temp file handle for bean file generation.
107 *
108 * @param beanTempFiles temp file handle for bean file generation
109 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530110 private void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530111 this.beanTempFiles = beanTempFiles;
112 }
113
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530114 /**
115 * Retrieves the temp file handle for data type file generation.
116 *
117 * @return temp file handle for data type file generation
118 */
119 public TempJavaTypeFragmentFiles getTypeTempFiles() {
120 return typeTempFiles;
121 }
122
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530123 /**
124 * Sets temp file handle for data type file generation.
125 *
126 * @param typeTempFiles temp file handle for data type file generation
127 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530128 private void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530129 this.typeTempFiles = typeTempFiles;
130 }
131
132 /**
133 * Retrieves the temp file handle for service file generation.
134 *
135 * @return temp file handle for service file generation
136 */
137 public TempJavaServiceFragmentFiles getServiceTempFiles() {
138 return serviceTempFiles;
139 }
140
141 /**
142 * Sets temp file handle for service file generation.
143 *
144 * @param serviceTempFiles temp file handle for service file generation
145 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530146 private void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530147 this.serviceTempFiles = serviceTempFiles;
148 }
149
150 /**
151 * Retrieves the temp file handle for enumeration file generation.
152 *
153 * @return temp file handle for enumeration file generation
154 */
155 public TempJavaEnumerationFragmentFiles getEnumerationTempFiles() {
156 return enumerationTempFiles;
157 }
158
159 /**
160 * Sets temp file handle for enumeration file generation.
161 *
162 * @param enumerationTempFiles temp file handle for enumeration file generation
163 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530164 private void setEnumerationTempFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530165 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
166 this.enumerationTempFiles = enumerationTempFiles;
167 }
168
169 /**
Bharat saraswald50c6382016-07-14 21:57:13 +0530170 * Retrieves the temp file handle for event file generation.
171 *
172 * @return temp file handle for enumeration file generation
173 */
174 public TempJavaEventFragmentFiles getEventFragmentFiles() {
175 return tempJavaEventFragmentFiles;
176 }
177
178 /**
179 * Sets temp file handle for event file generation.
180 *
181 * @param tempJavaEventFragmentFiles temp file handle for event file generation
182 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530183 private void setEventFragmentFiles(TempJavaEventFragmentFiles tempJavaEventFragmentFiles) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530184 this.tempJavaEventFragmentFiles = tempJavaEventFragmentFiles;
185 }
186
187
188 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530189 * Constructs java code exit.
190 *
191 * @param fileType generated file type
Bharat saraswald50c6382016-07-14 21:57:13 +0530192 * @param curNode current YANG node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530193 * @throws IOException when fails to generate java files
194 */
195 public void generateJavaFile(int fileType, YangNode curNode)
196 throws IOException {
197
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530198 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530199 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530200 }
201
Bharat saraswalc0e04842016-05-12 13:16:57 +0530202 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530203 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +0530204 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530205 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530207 }
208
Bharat saraswalc0e04842016-05-12 13:16:57 +0530209 /*
Bharat saraswal33dfa012016-05-17 19:59:16 +0530210 * Creates service and manager class file.
Bharat saraswalc0e04842016-05-12 13:16:57 +0530211 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530212 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530213 getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530214 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530215
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530216 /*
Bharat saraswald50c6382016-07-14 21:57:13 +0530217 * Creates event, event listener and event subject files.
218 */
219 if (fileType == GENERATE_ALL_EVENT_CLASS_MASK) {
220 getEventFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, curNode);
221 }
222
Bharat saraswalc0e04842016-05-12 13:16:57 +0530223 /*
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530224 * Creates enumeration class file.
Bharat saraswalc0e04842016-05-12 13:16:57 +0530225 */
226 if (fileType == GENERATE_ENUM_CLASS) {
227 getEnumerationTempFiles().generateJavaFile(GENERATE_ENUM_CLASS, curNode);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530228 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530229 }
230
231 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530232 * Add all the type in the current data model node as part of the
233 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530234 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530235 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530236 * @param pluginConfig plugin configurations for naming convention
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530237 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530238 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530239 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530241 getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530242 .addTypeInfoToTempFiles(yangTypeHolder, pluginConfig);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530243 }
244
245 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530246 * Adds build method for interface.
247 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530248 * @param pluginConfig plugin configurations
Vinod Kumar S38046502016-03-23 15:30:27 +0530249 * @return build method for interface
Vinod Kumar S38046502016-03-23 15:30:27 +0530250 * @throws IOException when fails to append to temporary file
251 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530252 public String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530253 throws IOException {
254 if (getBeanTempFiles() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530255 return getBeanTempFiles().addBuildMethodForInterface(pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530256 }
257 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S38046502016-03-23 15:30:27 +0530258 }
259
260 /**
261 * Adds default constructor for class.
262 *
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530263 * @param modifier modifier for constructor.
264 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530265 * @param pluginConfig plugin configurations
Vinod Kumar S38046502016-03-23 15:30:27 +0530266 * @return default constructor for class
Vinod Kumar S38046502016-03-23 15:30:27 +0530267 * @throws IOException when fails to append to file
268 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530269 public String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530270 throws IOException {
271 if (getTypeTempFiles() != null) {
272 return getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530273 .addDefaultConstructor(modifier, toAppend, pluginConfig);
Vinod Kumar S38046502016-03-23 15:30:27 +0530274 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530275
276 if (getBeanTempFiles() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530277 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530278 }
279
280 throw new TranslatorException("default constructor should not be added");
281 }
282
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530283 /**
284 * Adds build method's implementation for class.
285 *
286 * @return build method implementation for class
287 * @throws IOException when fails to append to temporary file
288 */
289 public String addBuildMethodImpl()
290 throws IOException {
291 if (getBeanTempFiles() != null) {
292 return getBeanTempFiles().addBuildMethodImpl();
293 }
294
295 throw new TranslatorException("build should not be added");
Vinod Kumar S38046502016-03-23 15:30:27 +0530296 }
297
298 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530299 * Removes all temporary file handles.
Vinod Kumar S38046502016-03-23 15:30:27 +0530300 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530301 * @param isErrorOccurred when translator fails to generate java files we need to close
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530302 * all open file handles include temporary files and java files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530303 * @throws IOException when failed to delete the temporary files
Vinod Kumar S38046502016-03-23 15:30:27 +0530304 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530305 void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530306 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530307
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530308 if (getBeanTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530309 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530310 }
311
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530312 if (getTypeTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530313 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530314 }
315
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530316 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530317 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
318 }
319
Bharat saraswal33dfa012016-05-17 19:59:16 +0530320 if (getServiceTempFiles() != null) {
321 getServiceTempFiles().freeTemporaryResources(isErrorOccurred);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530322 }
323
Bharat saraswale707f032016-07-14 23:33:55 +0530324 if (getEventFragmentFiles() != null) {
325 getEventFragmentFiles().freeTemporaryResources(isErrorOccurred);
326 }
327
Vinod Kumar S38046502016-03-23 15:30:27 +0530328 }
329
Vinod Kumar S38046502016-03-23 15:30:27 +0530330}