blob: b7e7d4a88187cba952f2fd9ebb1e2335375055b5 [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;
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 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053060 * Has the temporary files required for generated event classes.
61 */
62 private TempJavaEventFragmentFiles eventTempFiles;
63
64 /**
65 * Has the temporary files required for generated event listenerclasses.
66 */
67 private TempJavaEventListenerFragmentFiles eventListenerTempFiles;
68
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053069 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +053071 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053072 * @param javaFileInfo generated java file info
Vinod Kumar S38046502016-03-23 15:30:27 +053073 * @throws IOException when fails to create new file handle
74 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075 public TempJavaCodeFragmentFiles(JavaFileInfo javaFileInfo)
Bharat saraswale2d51d62016-03-23 19:40:35 +053076 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053077
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053078 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
79 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053080 }
81
82 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +053084 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
86 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053087 }
88
89 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053090 * Creates enumeration class file.
Vinod Kumar S38046502016-03-23 15:30:27 +053091 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
93 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
94 }
95
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053096 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053097 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
98 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053099
100 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_EVENT_CLASS) != 0) {
101 setEventTempFiles(new TempJavaEventFragmentFiles(javaFileInfo));
102 }
103
104 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_EVENT_LISTENER_INTERFACE) != 0) {
105 setEventListenerTempFiles(new TempJavaEventListenerFragmentFiles(javaFileInfo));
106 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107 }
108
109 /**
110 * Retrieves the temp file handle for bean file generation.
111 *
112 * @return temp file handle for bean file generation
113 */
114 public TempJavaBeanFragmentFiles getBeanTempFiles() {
115 return beanTempFiles;
116 }
117
118 /**
119 * Sets temp file handle for bean file generation.
120 *
121 * @param beanTempFiles temp file handle for bean file generation
122 */
123 public void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
124 this.beanTempFiles = beanTempFiles;
125 }
126
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127 /**
128 * Retrieves the temp file handle for data type file generation.
129 *
130 * @return temp file handle for data type file generation
131 */
132 public TempJavaTypeFragmentFiles getTypeTempFiles() {
133 return typeTempFiles;
134 }
135
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 /**
137 * Sets temp file handle for data type file generation.
138 *
139 * @param typeTempFiles temp file handle for data type file generation
140 */
141 public void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
142 this.typeTempFiles = typeTempFiles;
143 }
144
145 /**
146 * Retrieves the temp file handle for service file generation.
147 *
148 * @return temp file handle for service file generation
149 */
150 public TempJavaServiceFragmentFiles getServiceTempFiles() {
151 return serviceTempFiles;
152 }
153
154 /**
155 * Sets temp file handle for service file generation.
156 *
157 * @param serviceTempFiles temp file handle for service file generation
158 */
159 public void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
160 this.serviceTempFiles = serviceTempFiles;
161 }
162
163 /**
164 * Retrieves the temp file handle for enumeration file generation.
165 *
166 * @return temp file handle for enumeration file generation
167 */
168 public TempJavaEnumerationFragmentFiles getEnumerationTempFiles() {
169 return enumerationTempFiles;
170 }
171
172 /**
173 * Sets temp file handle for enumeration file generation.
174 *
175 * @param enumerationTempFiles temp file handle for enumeration file generation
176 */
177 public void setEnumerationTempFiles(
178 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
179 this.enumerationTempFiles = enumerationTempFiles;
180 }
181
182 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183 * Retrieves the temp file handle for event file generation.
184 *
185 * @return temp file handle for event file generation
186 */
187 public TempJavaEventFragmentFiles getEventTempFiles() {
188 return eventTempFiles;
189 }
190
191 /**
192 * Sets temp file handle for event file generation.
193 *
194 * @param eventTempFiles temp file handle for event file generation
195 */
196 public void setEventTempFiles(TempJavaEventFragmentFiles eventTempFiles) {
197 this.eventTempFiles = eventTempFiles;
198 }
199
200 /**
201 * Retrieves the temp file handle for event listener file generation.
202 *
203 * @return temp file handle for event listener file generation
204 */
205 public TempJavaEventListenerFragmentFiles getEventListenerTempFiles() {
206 return eventListenerTempFiles;
207 }
208
209 /**
210 * Sets temp file handle for event listener file generation.
211 *
212 * @param eventListenerTempFiles temp file handle for event listener file generation
213 */
214 public void setEventListenerTempFiles(
215 TempJavaEventListenerFragmentFiles eventListenerTempFiles) {
216 this.eventListenerTempFiles = eventListenerTempFiles;
217 }
218
219 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530220 * Constructs java code exit.
221 *
222 * @param fileType generated file type
223 * @param curNode current YANG node
224 * @throws IOException when fails to generate java files
225 */
226 public void generateJavaFile(int fileType, YangNode curNode)
227 throws IOException {
228
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530229 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530230 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530231 }
232
Bharat saraswalc0e04842016-05-12 13:16:57 +0530233 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530234 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +0530235 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530236 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530237 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530238 }
239
Bharat saraswalc0e04842016-05-12 13:16:57 +0530240 /*
241 * Creats service and manager class file.
242 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530243 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530244 getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530245 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530246
Bharat saraswalc0e04842016-05-12 13:16:57 +0530247 /*
248 * Creats enumeration class file.
249 */
250 if (fileType == GENERATE_ENUM_CLASS) {
251 getEnumerationTempFiles().generateJavaFile(GENERATE_ENUM_CLASS, curNode);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530252 }
253
254 if ((fileType & GENERATE_EVENT_CLASS) != 0) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530255 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530256 * Creates event class file.
257 */
258 if (getEventTempFiles() != null) {
259 getEventTempFiles().generateJavaFile(fileType, curNode);
260 }
261 }
262
263 if ((fileType & GENERATE_EVENT_LISTENER_INTERFACE) != 0) {
264 /**
265 * Creates event listener class file.
266 */
267 getEventListenerTempFiles().generateJavaFile(fileType, curNode);
268 }
269
270 freeTemporaryResources(false);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530271 }
272
273 /**
274 * Adds the new attribute info to the target generated temporary files.
275 *
276 * @param newAttrInfo the attribute info that needs to be added to temporary
277 * files
278 * @throws IOException IO operation fail
279 */
280 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
281 throws IOException {
282
283 if (getBeanTempFiles() != null) {
284 getBeanTempFiles()
285 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530286 }
287
Bharat saraswale2d51d62016-03-23 19:40:35 +0530288 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530289 * Creates user defined data type class file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530290 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530291 if (getTypeTempFiles() != null) {
292 getTypeTempFiles()
293 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530294 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530295 }
296
297 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530298 * Add all the type in the current data model node as part of the
299 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530300 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530301 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530302 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530303 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530304 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530305 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530306 getTypeTempFiles()
307 .addTypeInfoToTempFiles(yangTypeHolder);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530308 }
309
310 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530311 * Adds build method for interface.
312 *
313 * @return build method for interface
Vinod Kumar S38046502016-03-23 15:30:27 +0530314 * @throws IOException when fails to append to temporary file
315 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530316 public String addBuildMethodForInterface()
317 throws IOException {
318 if (getBeanTempFiles() != null) {
319 return getBeanTempFiles().addBuildMethodForInterface();
320 }
321 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S38046502016-03-23 15:30:27 +0530322 }
323
324 /**
325 * Adds default constructor for class.
326 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530327 * @param modifier modifier for constructor.
328 * @param toAppend string which need to be appended with the class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530329 * @return default constructor for class
Vinod Kumar S38046502016-03-23 15:30:27 +0530330 * @throws IOException when fails to append to file
331 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530332 public String addDefaultConstructor(String modifier, String toAppend)
333 throws IOException {
334 if (getTypeTempFiles() != null) {
335 return getTypeTempFiles()
336 .addDefaultConstructor(modifier, toAppend);
Vinod Kumar S38046502016-03-23 15:30:27 +0530337 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530338
339 if (getBeanTempFiles() != null) {
340 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend);
341 }
342
343 throw new TranslatorException("default constructor should not be added");
344 }
345
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530346 /**
347 * Adds build method's implementation for class.
348 *
349 * @return build method implementation for class
350 * @throws IOException when fails to append to temporary file
351 */
352 public String addBuildMethodImpl()
353 throws IOException {
354 if (getBeanTempFiles() != null) {
355 return getBeanTempFiles().addBuildMethodImpl();
356 }
357
358 throw new TranslatorException("build should not be added");
Vinod Kumar S38046502016-03-23 15:30:27 +0530359 }
360
361 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530362 * Removes all temporary file handles.
Vinod Kumar S38046502016-03-23 15:30:27 +0530363 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530364 * @param isErrorOccurred when translator fails to generate java files we need to close
365 * all open file handles include temporary files and java files.
366 * @throws IOException when failed to delete the temporary files
Vinod Kumar S38046502016-03-23 15:30:27 +0530367 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530368 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530369 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530370
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530371 if (getBeanTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530372 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530373 }
374
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530375 if (getTypeTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530376 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530377 }
378
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530379 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530380 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
381 }
382
383 if (getEventTempFiles() != null) {
384 getEventTempFiles().freeTemporaryResources(isErrorOccurred);
385 }
386
387 if (getEventListenerTempFiles() != null) {
388 getEventListenerTempFiles().freeTemporaryResources(isErrorOccurred);
Vinod Kumar S38046502016-03-23 15:30:27 +0530389 }
390 }
391
Vinod Kumar S38046502016-03-23 15:30:27 +0530392}