blob: b1aa5d9db36e8aaa9f687088c6d85140971e59f7 [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;
Vinod Kumar S38046502016-03-23 15:30:27 +053024
Bharat saraswald72411a2016-04-19 01:00:16 +053025import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053026import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
27import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
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;
31import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList;
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 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053061 * Has the temporary files required for generated event classes.
62 */
63 private TempJavaEventFragmentFiles eventTempFiles;
64
65 /**
66 * Has the temporary files required for generated event listenerclasses.
67 */
68 private TempJavaEventListenerFragmentFiles eventListenerTempFiles;
69
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053070 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053071 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +053072 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053073 * @param javaFileInfo generated java file info
Vinod Kumar S38046502016-03-23 15:30:27 +053074 * @throws IOException when fails to create new file handle
75 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076 public TempJavaCodeFragmentFiles(JavaFileInfo javaFileInfo)
Bharat saraswale2d51d62016-03-23 19:40:35 +053077 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053078
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
80 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053081 }
82
83 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053084 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +053085 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
87 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053088 }
89
90 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053091 * Creates enumeration class file.
Vinod Kumar S38046502016-03-23 15:30:27 +053092 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
94 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
95 }
96
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053097 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053098 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
99 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530100
101 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_EVENT_CLASS) != 0) {
102 setEventTempFiles(new TempJavaEventFragmentFiles(javaFileInfo));
103 }
104
105 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_EVENT_LISTENER_INTERFACE) != 0) {
106 setEventListenerTempFiles(new TempJavaEventListenerFragmentFiles(javaFileInfo));
107 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108 }
109
110 /**
111 * Retrieves the temp file handle for bean file generation.
112 *
113 * @return temp file handle for bean file generation
114 */
115 public TempJavaBeanFragmentFiles getBeanTempFiles() {
116 return beanTempFiles;
117 }
118
119 /**
120 * Sets temp file handle for bean file generation.
121 *
122 * @param beanTempFiles temp file handle for bean file generation
123 */
124 public void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
125 this.beanTempFiles = beanTempFiles;
126 }
127
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530128 /**
129 * Retrieves the temp file handle for data type file generation.
130 *
131 * @return temp file handle for data type file generation
132 */
133 public TempJavaTypeFragmentFiles getTypeTempFiles() {
134 return typeTempFiles;
135 }
136
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530137 /**
138 * Sets temp file handle for data type file generation.
139 *
140 * @param typeTempFiles temp file handle for data type file generation
141 */
142 public void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
143 this.typeTempFiles = typeTempFiles;
144 }
145
146 /**
147 * Retrieves the temp file handle for service file generation.
148 *
149 * @return temp file handle for service file generation
150 */
151 public TempJavaServiceFragmentFiles getServiceTempFiles() {
152 return serviceTempFiles;
153 }
154
155 /**
156 * Sets temp file handle for service file generation.
157 *
158 * @param serviceTempFiles temp file handle for service file generation
159 */
160 public void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
161 this.serviceTempFiles = serviceTempFiles;
162 }
163
164 /**
165 * Retrieves the temp file handle for enumeration file generation.
166 *
167 * @return temp file handle for enumeration file generation
168 */
169 public TempJavaEnumerationFragmentFiles getEnumerationTempFiles() {
170 return enumerationTempFiles;
171 }
172
173 /**
174 * Sets temp file handle for enumeration file generation.
175 *
176 * @param enumerationTempFiles temp file handle for enumeration file generation
177 */
178 public void setEnumerationTempFiles(
179 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
180 this.enumerationTempFiles = enumerationTempFiles;
181 }
182
183 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530184 * Retrieves the temp file handle for event file generation.
185 *
186 * @return temp file handle for event file generation
187 */
188 public TempJavaEventFragmentFiles getEventTempFiles() {
189 return eventTempFiles;
190 }
191
192 /**
193 * Sets temp file handle for event file generation.
194 *
195 * @param eventTempFiles temp file handle for event file generation
196 */
197 public void setEventTempFiles(TempJavaEventFragmentFiles eventTempFiles) {
198 this.eventTempFiles = eventTempFiles;
199 }
200
201 /**
202 * Retrieves the temp file handle for event listener file generation.
203 *
204 * @return temp file handle for event listener file generation
205 */
206 public TempJavaEventListenerFragmentFiles getEventListenerTempFiles() {
207 return eventListenerTempFiles;
208 }
209
210 /**
211 * Sets temp file handle for event listener file generation.
212 *
213 * @param eventListenerTempFiles temp file handle for event listener file generation
214 */
215 public void setEventListenerTempFiles(
216 TempJavaEventListenerFragmentFiles eventListenerTempFiles) {
217 this.eventListenerTempFiles = eventListenerTempFiles;
218 }
219
220 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530221 * Constructs java code exit.
222 *
223 * @param fileType generated file type
224 * @param curNode current YANG node
225 * @throws IOException when fails to generate java files
226 */
227 public void generateJavaFile(int fileType, YangNode curNode)
228 throws IOException {
229
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530230 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530231 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530232 }
233
Bharat saraswalc0e04842016-05-12 13:16:57 +0530234 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530235 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +0530236 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530237 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530238 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530239 }
240
Bharat saraswalc0e04842016-05-12 13:16:57 +0530241 /*
242 * Creats service and manager class file.
243 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530244 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530245 getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530246 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530247
Bharat saraswalc0e04842016-05-12 13:16:57 +0530248 /*
249 * Creats enumeration class file.
250 */
251 if (fileType == GENERATE_ENUM_CLASS) {
252 getEnumerationTempFiles().generateJavaFile(GENERATE_ENUM_CLASS, curNode);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530253 }
254
255 if ((fileType & GENERATE_EVENT_CLASS) != 0) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530256 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530257 * Creates event class file.
258 */
259 if (getEventTempFiles() != null) {
260 getEventTempFiles().generateJavaFile(fileType, curNode);
261 }
262 }
263
264 if ((fileType & GENERATE_EVENT_LISTENER_INTERFACE) != 0) {
265 /**
266 * Creates event listener class file.
267 */
268 getEventListenerTempFiles().generateJavaFile(fileType, curNode);
269 }
270
271 freeTemporaryResources(false);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530272 }
273
274 /**
275 * Adds the new attribute info to the target generated temporary files.
276 *
277 * @param newAttrInfo the attribute info that needs to be added to temporary
278 * files
279 * @throws IOException IO operation fail
280 */
281 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
282 throws IOException {
283
284 if (getBeanTempFiles() != null) {
285 getBeanTempFiles()
286 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530287 }
288
Bharat saraswale2d51d62016-03-23 19:40:35 +0530289 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530290 * Creates user defined data type class file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530291 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530292 if (getTypeTempFiles() != null) {
293 getTypeTempFiles()
294 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530295 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530296 }
297
298 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530299 * Add all the type in the current data model node as part of the
300 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530301 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530302 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530303 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530304 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530305 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530306 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530307 getTypeTempFiles()
308 .addTypeInfoToTempFiles(yangTypeHolder);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530309 }
310
311 /**
312 * Adds class to the extends list.
313 *
314 * @param extend class to be extended
315 */
316 public void addToExtendsList(String extend) {
317 getExtendsList().add(extend);
318 }
319
320 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530321 * Adds build method for interface.
322 *
323 * @return build method for interface
Vinod Kumar S38046502016-03-23 15:30:27 +0530324 * @throws IOException when fails to append to temporary file
325 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530326 public String addBuildMethodForInterface()
327 throws IOException {
328 if (getBeanTempFiles() != null) {
329 return getBeanTempFiles().addBuildMethodForInterface();
330 }
331 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S38046502016-03-23 15:30:27 +0530332 }
333
334 /**
335 * Adds default constructor for class.
336 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530337 * @param modifier modifier for constructor.
338 * @param toAppend string which need to be appended with the class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530339 * @return default constructor for class
Vinod Kumar S38046502016-03-23 15:30:27 +0530340 * @throws IOException when fails to append to file
341 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530342 public String addDefaultConstructor(String modifier, String toAppend)
343 throws IOException {
344 if (getTypeTempFiles() != null) {
345 return getTypeTempFiles()
346 .addDefaultConstructor(modifier, toAppend);
Vinod Kumar S38046502016-03-23 15:30:27 +0530347 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530348
349 if (getBeanTempFiles() != null) {
350 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend);
351 }
352
353 throw new TranslatorException("default constructor should not be added");
354 }
355
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530356 /**
357 * Adds build method's implementation for class.
358 *
359 * @return build method implementation for class
360 * @throws IOException when fails to append to temporary file
361 */
362 public String addBuildMethodImpl()
363 throws IOException {
364 if (getBeanTempFiles() != null) {
365 return getBeanTempFiles().addBuildMethodImpl();
366 }
367
368 throw new TranslatorException("build should not be added");
Vinod Kumar S38046502016-03-23 15:30:27 +0530369 }
370
371 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530372 * Removes all temporary file handles.
Vinod Kumar S38046502016-03-23 15:30:27 +0530373 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530374 * @param isErrorOccurred when translator fails to generate java files we need to close
375 * all open file handles include temporary files and java files.
376 * @throws IOException when failed to delete the temporary files
Vinod Kumar S38046502016-03-23 15:30:27 +0530377 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530378 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530379 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530380
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530381 if (getBeanTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530382 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530383 }
384
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530385 if (getTypeTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530386 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530387 }
388
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530389 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530390 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
391 }
392
393 if (getEventTempFiles() != null) {
394 getEventTempFiles().freeTemporaryResources(isErrorOccurred);
395 }
396
397 if (getEventListenerTempFiles() != null) {
398 getEventListenerTempFiles().freeTemporaryResources(isErrorOccurred);
Vinod Kumar S38046502016-03-23 15:30:27 +0530399 }
400 }
401
Vinod Kumar S38046502016-03-23 15:30:27 +0530402}