blob: f24391cfd4740828bdb32bb84e2957ba66faf067 [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
70
71 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053072 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +053073 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053074 * @param javaFileInfo generated java file info
Vinod Kumar S38046502016-03-23 15:30:27 +053075 * @throws IOException when fails to create new file handle
76 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053077 public TempJavaCodeFragmentFiles(JavaFileInfo javaFileInfo)
Bharat saraswale2d51d62016-03-23 19:40:35 +053078 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053079
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
81 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053082 }
83
84 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +053086 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
88 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053089 }
90
91 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092 * Creates enumeration class file.
Vinod Kumar S38046502016-03-23 15:30:27 +053093 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053094 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
95 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
96 }
97
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053098 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053099 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
100 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530101
102 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_EVENT_CLASS) != 0) {
103 setEventTempFiles(new TempJavaEventFragmentFiles(javaFileInfo));
104 }
105
106 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_EVENT_LISTENER_INTERFACE) != 0) {
107 setEventListenerTempFiles(new TempJavaEventListenerFragmentFiles(javaFileInfo));
108 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530109 }
110
111 /**
112 * Retrieves the temp file handle for bean file generation.
113 *
114 * @return temp file handle for bean file generation
115 */
116 public TempJavaBeanFragmentFiles getBeanTempFiles() {
117 return beanTempFiles;
118 }
119
120 /**
121 * Sets temp file handle for bean file generation.
122 *
123 * @param beanTempFiles temp file handle for bean file generation
124 */
125 public void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
126 this.beanTempFiles = beanTempFiles;
127 }
128
129
130 /**
131 * Retrieves the temp file handle for data type file generation.
132 *
133 * @return temp file handle for data type file generation
134 */
135 public TempJavaTypeFragmentFiles getTypeTempFiles() {
136 return typeTempFiles;
137 }
138
139
140 /**
141 * Sets temp file handle for data type file generation.
142 *
143 * @param typeTempFiles temp file handle for data type file generation
144 */
145 public void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
146 this.typeTempFiles = typeTempFiles;
147 }
148
149 /**
150 * Retrieves the temp file handle for service file generation.
151 *
152 * @return temp file handle for service file generation
153 */
154 public TempJavaServiceFragmentFiles getServiceTempFiles() {
155 return serviceTempFiles;
156 }
157
158 /**
159 * Sets temp file handle for service file generation.
160 *
161 * @param serviceTempFiles temp file handle for service file generation
162 */
163 public void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
164 this.serviceTempFiles = serviceTempFiles;
165 }
166
167 /**
168 * Retrieves the temp file handle for enumeration file generation.
169 *
170 * @return temp file handle for enumeration file generation
171 */
172 public TempJavaEnumerationFragmentFiles getEnumerationTempFiles() {
173 return enumerationTempFiles;
174 }
175
176 /**
177 * Sets temp file handle for enumeration file generation.
178 *
179 * @param enumerationTempFiles temp file handle for enumeration file generation
180 */
181 public void setEnumerationTempFiles(
182 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
183 this.enumerationTempFiles = enumerationTempFiles;
184 }
185
186 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530187 * Retrieves the temp file handle for event file generation.
188 *
189 * @return temp file handle for event file generation
190 */
191 public TempJavaEventFragmentFiles getEventTempFiles() {
192 return eventTempFiles;
193 }
194
195 /**
196 * Sets temp file handle for event file generation.
197 *
198 * @param eventTempFiles temp file handle for event file generation
199 */
200 public void setEventTempFiles(TempJavaEventFragmentFiles eventTempFiles) {
201 this.eventTempFiles = eventTempFiles;
202 }
203
204 /**
205 * Retrieves the temp file handle for event listener file generation.
206 *
207 * @return temp file handle for event listener file generation
208 */
209 public TempJavaEventListenerFragmentFiles getEventListenerTempFiles() {
210 return eventListenerTempFiles;
211 }
212
213 /**
214 * Sets temp file handle for event listener file generation.
215 *
216 * @param eventListenerTempFiles temp file handle for event listener file generation
217 */
218 public void setEventListenerTempFiles(
219 TempJavaEventListenerFragmentFiles eventListenerTempFiles) {
220 this.eventListenerTempFiles = eventListenerTempFiles;
221 }
222
223 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530224 * Constructs java code exit.
225 *
226 * @param fileType generated file type
227 * @param curNode current YANG node
228 * @throws IOException when fails to generate java files
229 */
230 public void generateJavaFile(int fileType, YangNode curNode)
231 throws IOException {
232
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530233 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530234 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530235 }
236
237 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530238 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +0530239 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530240 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530241 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530242 }
243
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530244
245 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
246
247 getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
248
249
250 }
251
252 if ((fileType & GENERATE_EVENT_CLASS) != 0) {
253 /**
254 * Creates event class file.
255 */
256 if (getEventTempFiles() != null) {
257 getEventTempFiles().generateJavaFile(fileType, curNode);
258 }
259 }
260
261 if ((fileType & GENERATE_EVENT_LISTENER_INTERFACE) != 0) {
262 /**
263 * Creates event listener class file.
264 */
265 getEventListenerTempFiles().generateJavaFile(fileType, curNode);
266 }
267
268 freeTemporaryResources(false);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530269 }
270
271 /**
272 * Adds the new attribute info to the target generated temporary files.
273 *
274 * @param newAttrInfo the attribute info that needs to be added to temporary
275 * files
276 * @throws IOException IO operation fail
277 */
278 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
279 throws IOException {
280
281 if (getBeanTempFiles() != null) {
282 getBeanTempFiles()
283 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530284 }
285
Bharat saraswale2d51d62016-03-23 19:40:35 +0530286 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530287 * Creates user defined data type class file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530288 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530289 if (getTypeTempFiles() != null) {
290 getTypeTempFiles()
291 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530292 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530293 }
294
295 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530296 * Add all the type in the current data model node as part of the
297 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530298 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530299 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530300 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530301 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530302 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530303 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530304 getTypeTempFiles()
305 .addTypeInfoToTempFiles(yangTypeHolder);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530306 }
307
308 /**
309 * Adds class to the extends list.
310 *
311 * @param extend class to be extended
312 */
313 public void addToExtendsList(String extend) {
314 getExtendsList().add(extend);
315 }
316
317 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530318 * Adds build method for interface.
319 *
320 * @return build method for interface
Vinod Kumar S38046502016-03-23 15:30:27 +0530321 * @throws IOException when fails to append to temporary file
322 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530323 public String addBuildMethodForInterface()
324 throws IOException {
325 if (getBeanTempFiles() != null) {
326 return getBeanTempFiles().addBuildMethodForInterface();
327 }
328 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S38046502016-03-23 15:30:27 +0530329 }
330
331 /**
332 * Adds default constructor for class.
333 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530334 * @param modifier modifier for constructor.
335 * @param toAppend string which need to be appended with the class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530336 * @return default constructor for class
Vinod Kumar S38046502016-03-23 15:30:27 +0530337 * @throws IOException when fails to append to file
338 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530339 public String addDefaultConstructor(String modifier, String toAppend)
340 throws IOException {
341 if (getTypeTempFiles() != null) {
342 return getTypeTempFiles()
343 .addDefaultConstructor(modifier, toAppend);
Vinod Kumar S38046502016-03-23 15:30:27 +0530344 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530345
346 if (getBeanTempFiles() != null) {
347 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend);
348 }
349
350 throw new TranslatorException("default constructor should not be added");
351 }
352
353
354 /**
355 * Adds build method's implementation for class.
356 *
357 * @return build method implementation for class
358 * @throws IOException when fails to append to temporary file
359 */
360 public String addBuildMethodImpl()
361 throws IOException {
362 if (getBeanTempFiles() != null) {
363 return getBeanTempFiles().addBuildMethodImpl();
364 }
365
366 throw new TranslatorException("build should not be added");
Vinod Kumar S38046502016-03-23 15:30:27 +0530367 }
368
369 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530370 * Removes all temporary file handles.
Vinod Kumar S38046502016-03-23 15:30:27 +0530371 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530372 * @param isErrorOccurred when translator fails to generate java files we need to close
373 * all open file handles include temporary files and java files.
374 * @throws IOException when failed to delete the temporary files
Vinod Kumar S38046502016-03-23 15:30:27 +0530375 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530376 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530377 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530378
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530379 if (getBeanTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530380 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530381 }
382
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530383 if (getTypeTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530384 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530385 }
386
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530387 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530388 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
389 }
390
391 if (getEventTempFiles() != null) {
392 getEventTempFiles().freeTemporaryResources(isErrorOccurred);
393 }
394
395 if (getEventListenerTempFiles() != null) {
396 getEventListenerTempFiles().freeTemporaryResources(isErrorOccurred);
Vinod Kumar S38046502016-03-23 15:30:27 +0530397 }
398 }
399
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530400
401 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530402 * Adds enum attributes to temporary files.
403 *
404 * @param curNode current YANG node
405 * @throws IOException when fails to do IO operations
406 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530407 public void addEnumAttributeToTempFiles(YangNode curNode)
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530408 throws IOException {
409
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530410 if (getEnumerationTempFiles() != null) {
411 getEnumerationTempFiles().addEnumAttributeToTempFiles(curNode);
412 return;
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530413 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530414
415 throw new TranslatorException("build should not be added");
Bharat saraswale2d51d62016-03-23 19:40:35 +0530416 }
417
Vinod Kumar S38046502016-03-23 15:30:27 +0530418}