blob: 90b2daf4b3b284be77aebdc349c575d258b6e6fc [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;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053023import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053024import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vinod Kumar S38046502016-03-23 15:30:27 +053025
Bharat saraswald50c6382016-07-14 21:57:13 +053026import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ALL_EVENT_CLASS_MASK;
Bharat saraswald72411a2016-04-19 01:00:16 +053027import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053029import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053030import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar S38046502016-03-23 15:30:27 +053031
32/**
Bharat saraswald9822e92016-04-05 15:13:44 +053033 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034 * Contains fragment file object of different types of java file.
35 * Uses required object(s) to generate the target java file(s).
Vinod Kumar S38046502016-03-23 15:30:27 +053036 */
37public class TempJavaCodeFragmentFiles {
38
39 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053040 * Has the temporary files required for bean generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053041 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042 private TempJavaBeanFragmentFiles beanTempFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053043
44 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053045 * Has the temporary files required for bean generated classes.
Bharat saraswale2d51d62016-03-23 19:40:35 +053046 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047 private TempJavaTypeFragmentFiles typeTempFiles;
Bharat saraswale2d51d62016-03-23 19:40:35 +053048
49 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053050 * Has the temporary files required for service generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053051 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052 private TempJavaServiceFragmentFiles serviceTempFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053053
54 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053055 * Has the temporary files required for enumeration generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053056 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053057 private TempJavaEnumerationFragmentFiles enumerationTempFiles;
Bharat saraswald72411a2016-04-19 01:00:16 +053058
Bharat saraswal2f11f652016-03-25 18:19:46 +053059 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053060 * Has the temporary files required for enumeration generated classes.
61 */
62 private TempJavaEventFragmentFiles tempJavaEventFragmentFiles;
63
64 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053065 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +053066 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067 * @param javaFileInfo generated java file info
Vinod Kumar S38046502016-03-23 15:30:27 +053068 * @throws IOException when fails to create new file handle
69 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053070 public TempJavaCodeFragmentFiles(JavaFileInfo javaFileInfo)
Bharat saraswale2d51d62016-03-23 19:40:35 +053071 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053072
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053073 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
74 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053075 }
76
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053077 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
78 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053079 }
80
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053081 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
82 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
83 }
84
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053085 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
87 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053088
Bharat saraswald50c6382016-07-14 21:57:13 +053089 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
90 setEventFragmentFiles(new TempJavaEventFragmentFiles(javaFileInfo));
91 }
92
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093 }
94
95 /**
96 * Retrieves the temp file handle for bean file generation.
97 *
98 * @return temp file handle for bean file generation
99 */
100 public TempJavaBeanFragmentFiles getBeanTempFiles() {
101 return beanTempFiles;
102 }
103
104 /**
105 * Sets temp file handle for bean file generation.
106 *
107 * @param beanTempFiles temp file handle for bean file generation
108 */
109 public void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
110 this.beanTempFiles = beanTempFiles;
111 }
112
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530113 /**
114 * Retrieves the temp file handle for data type file generation.
115 *
116 * @return temp file handle for data type file generation
117 */
118 public TempJavaTypeFragmentFiles getTypeTempFiles() {
119 return typeTempFiles;
120 }
121
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530122 /**
123 * Sets temp file handle for data type file generation.
124 *
125 * @param typeTempFiles temp file handle for data type file generation
126 */
127 public void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
128 this.typeTempFiles = typeTempFiles;
129 }
130
131 /**
132 * Retrieves the temp file handle for service file generation.
133 *
134 * @return temp file handle for service file generation
135 */
136 public TempJavaServiceFragmentFiles getServiceTempFiles() {
137 return serviceTempFiles;
138 }
139
140 /**
141 * Sets temp file handle for service file generation.
142 *
143 * @param serviceTempFiles temp file handle for service file generation
144 */
145 public void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
146 this.serviceTempFiles = serviceTempFiles;
147 }
148
149 /**
150 * Retrieves the temp file handle for enumeration file generation.
151 *
152 * @return temp file handle for enumeration file generation
153 */
154 public TempJavaEnumerationFragmentFiles getEnumerationTempFiles() {
155 return enumerationTempFiles;
156 }
157
158 /**
159 * Sets temp file handle for enumeration file generation.
160 *
161 * @param enumerationTempFiles temp file handle for enumeration file generation
162 */
163 public void setEnumerationTempFiles(
164 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
165 this.enumerationTempFiles = enumerationTempFiles;
166 }
167
168 /**
Bharat saraswald50c6382016-07-14 21:57:13 +0530169 * Retrieves the temp file handle for event file generation.
170 *
171 * @return temp file handle for enumeration file generation
172 */
173 public TempJavaEventFragmentFiles getEventFragmentFiles() {
174 return tempJavaEventFragmentFiles;
175 }
176
177 /**
178 * Sets temp file handle for event file generation.
179 *
180 * @param tempJavaEventFragmentFiles temp file handle for event file generation
181 */
182 public void setEventFragmentFiles(TempJavaEventFragmentFiles tempJavaEventFragmentFiles) {
183 this.tempJavaEventFragmentFiles = tempJavaEventFragmentFiles;
184 }
185
186
187 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530188 * Constructs java code exit.
189 *
190 * @param fileType generated file type
Bharat saraswald50c6382016-07-14 21:57:13 +0530191 * @param curNode current YANG node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 * @throws IOException when fails to generate java files
193 */
194 public void generateJavaFile(int fileType, YangNode curNode)
195 throws IOException {
196
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530197 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530198 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530199 }
200
Bharat saraswalc0e04842016-05-12 13:16:57 +0530201 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530202 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +0530203 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530204 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530205 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530206 }
207
Bharat saraswalc0e04842016-05-12 13:16:57 +0530208 /*
Bharat saraswal33dfa012016-05-17 19:59:16 +0530209 * Creates service and manager class file.
Bharat saraswalc0e04842016-05-12 13:16:57 +0530210 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530211 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530212 getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530213 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530214
Bharat saraswald50c6382016-07-14 21:57:13 +0530215 /**
216 * Creates event, event listener and event subject files.
217 */
218 if (fileType == GENERATE_ALL_EVENT_CLASS_MASK) {
219 getEventFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, curNode);
220 }
221
Bharat saraswalc0e04842016-05-12 13:16:57 +0530222 /*
223 * Creats enumeration class file.
224 */
225 if (fileType == GENERATE_ENUM_CLASS) {
226 getEnumerationTempFiles().generateJavaFile(GENERATE_ENUM_CLASS, curNode);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530227 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530228 }
229
230 /**
231 * Adds the new attribute info to the target generated temporary files.
232 *
233 * @param newAttrInfo the attribute info that needs to be added to temporary
234 * files
Bharat saraswal33dfa012016-05-17 19:59:16 +0530235 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530236 * @throws IOException IO operation fail
237 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530238 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo,
239 YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 throws IOException {
241
242 if (getBeanTempFiles() != null) {
243 getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530244 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530245 }
246
Bharat saraswale2d51d62016-03-23 19:40:35 +0530247 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530248 * Creates user defined data type class file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530249 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530250 if (getTypeTempFiles() != null) {
251 getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530252 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530253 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530254 }
255
256 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530257 * Add all the type in the current data model node as part of the
258 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530259 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530260 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Bharat saraswal33dfa012016-05-17 19:59:16 +0530261 * @param pluginConfig plugin configurations for naming convention
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530262 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530263 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530264 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530265 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530266 getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530267 .addTypeInfoToTempFiles(yangTypeHolder, pluginConfig);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530268 }
269
270 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530271 * Adds build method for interface.
272 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530273 * @param pluginConfig plugin configurations
Vinod Kumar S38046502016-03-23 15:30:27 +0530274 * @return build method for interface
Vinod Kumar S38046502016-03-23 15:30:27 +0530275 * @throws IOException when fails to append to temporary file
276 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530277 public String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530278 throws IOException {
279 if (getBeanTempFiles() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530280 return getBeanTempFiles().addBuildMethodForInterface(pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530281 }
282 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S38046502016-03-23 15:30:27 +0530283 }
284
285 /**
286 * Adds default constructor for class.
287 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530288 * @param modifier modifier for constructor.
289 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530290 * @param pluginConfig plugin configurations
Vinod Kumar S38046502016-03-23 15:30:27 +0530291 * @return default constructor for class
Vinod Kumar S38046502016-03-23 15:30:27 +0530292 * @throws IOException when fails to append to file
293 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530294 public String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530295 throws IOException {
296 if (getTypeTempFiles() != null) {
297 return getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530298 .addDefaultConstructor(modifier, toAppend, pluginConfig);
Vinod Kumar S38046502016-03-23 15:30:27 +0530299 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530300
301 if (getBeanTempFiles() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530302 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530303 }
304
305 throw new TranslatorException("default constructor should not be added");
306 }
307
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530308 /**
309 * Adds build method's implementation for class.
310 *
311 * @return build method implementation for class
312 * @throws IOException when fails to append to temporary file
313 */
314 public String addBuildMethodImpl()
315 throws IOException {
316 if (getBeanTempFiles() != null) {
317 return getBeanTempFiles().addBuildMethodImpl();
318 }
319
320 throw new TranslatorException("build should not be added");
Vinod Kumar S38046502016-03-23 15:30:27 +0530321 }
322
323 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530324 * Removes all temporary file handles.
Vinod Kumar S38046502016-03-23 15:30:27 +0530325 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530326 * @param isErrorOccurred when translator fails to generate java files we need to close
327 * all open file handles include temporary files and java files.
328 * @throws IOException when failed to delete the temporary files
Vinod Kumar S38046502016-03-23 15:30:27 +0530329 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530330 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530331 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530332
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530333 if (getBeanTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530334 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530335 }
336
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530337 if (getTypeTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530338 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530339 }
340
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530341 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530342 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
343 }
344
Bharat saraswal33dfa012016-05-17 19:59:16 +0530345 if (getServiceTempFiles() != null) {
346 getServiceTempFiles().freeTemporaryResources(isErrorOccurred);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530347 }
348
Bharat saraswale707f032016-07-14 23:33:55 +0530349 if (getEventFragmentFiles() != null) {
350 getEventFragmentFiles().freeTemporaryResources(isErrorOccurred);
351 }
352
Vinod Kumar S38046502016-03-23 15:30:27 +0530353 }
354
Vinod Kumar S38046502016-03-23 15:30:27 +0530355}