blob: d73fa6300be50aac88d06a166045b68b0b477f10 [file] [log] [blame]
Vinod Kumar S9f26ae52016-03-23 15:30:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S9f26ae52016-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 S9f26ae52016-03-23 15:30:27 +053019import java.io.IOException;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053020
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053021import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053022import org.onosproject.yangutils.datamodel.YangTypeHolder;
Bharat saraswale50edca2016-08-05 01:58:25 +053023import org.onosproject.yangutils.utils.io.YangPluginConfig;
Bharat saraswal780eca32016-04-05 12:45:45 +053024import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053025
Bharat saraswal039f59c2016-07-14 21:57:13 +053026import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ALL_EVENT_CLASS_MASK;
Bharat saraswal68fa0d12016-04-19 01:00:16 +053027import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053029import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053030import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Bharat saraswal8beac342016-08-04 02:00:03 +053031import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053032
33/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053034 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huawei6266db32016-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 S9f26ae52016-03-23 15:30:27 +053037 */
38public class TempJavaCodeFragmentFiles {
39
40 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053041 * Has the temporary files required for bean generated classes.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053042 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053043 private TempJavaBeanFragmentFiles beanTempFiles;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053044
45 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053046 * Has the temporary files required for bean generated classes.
Bharat saraswal84366c52016-03-23 19:40:35 +053047 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053048 private TempJavaTypeFragmentFiles typeTempFiles;
Bharat saraswal84366c52016-03-23 19:40:35 +053049
50 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053051 * Has the temporary files required for service generated classes.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053052 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053053 private TempJavaServiceFragmentFiles serviceTempFiles;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053054
55 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053056 * Has the temporary files required for enumeration generated classes.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053057 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053058 private TempJavaEnumerationFragmentFiles enumerationTempFiles;
Bharat saraswal68fa0d12016-04-19 01:00:16 +053059
Bharat saraswald532a4c2016-03-25 18:19:46 +053060 /**
Bharat saraswal039f59c2016-07-14 21:57:13 +053061 * Has the temporary files required for enumeration generated classes.
62 */
63 private TempJavaEventFragmentFiles tempJavaEventFragmentFiles;
64
65 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053066 * Creates an instance of temporary java code fragment.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053067 *
Vinod Kumar S79a374b2016-04-30 21:09:15 +053068 * @param javaFileInfo generated java file info
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053069 * @throws IOException when fails to create new file handle
70 */
Bharat saraswale50edca2016-08-05 01:58:25 +053071 public TempJavaCodeFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Bharat saraswal84366c52016-03-23 19:40:35 +053072 throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053073
Vinod Kumar S79a374b2016-04-30 21:09:15 +053074 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
75 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053076 }
77
Vinod Kumar S79a374b2016-04-30 21:09:15 +053078 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
79 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053080 }
81
Vinod Kumar S79a374b2016-04-30 21:09:15 +053082 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
83 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
84 }
85
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053086 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +053087 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
88 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053089
Bharat saraswal039f59c2016-07-14 21:57:13 +053090 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
91 setEventFragmentFiles(new TempJavaEventFragmentFiles(javaFileInfo));
92 }
93
Vinod Kumar S79a374b2016-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-Huaweib7564772016-08-02 18:13:13 +0530110 private void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530111 this.beanTempFiles = beanTempFiles;
112 }
113
Vinod Kumar S79a374b2016-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 S79a374b2016-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-Huaweib7564772016-08-02 18:13:13 +0530128 private void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
Vinod Kumar S79a374b2016-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-Huaweib7564772016-08-02 18:13:13 +0530146 private void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
Vinod Kumar S79a374b2016-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-Huaweib7564772016-08-02 18:13:13 +0530164 private void setEnumerationTempFiles(
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530165 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
166 this.enumerationTempFiles = enumerationTempFiles;
167 }
168
169 /**
Bharat saraswal039f59c2016-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-Huaweib7564772016-08-02 18:13:13 +0530183 private void setEventFragmentFiles(TempJavaEventFragmentFiles tempJavaEventFragmentFiles) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530184 this.tempJavaEventFragmentFiles = tempJavaEventFragmentFiles;
185 }
186
187
188 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530189 * Constructs java code exit.
190 *
191 * @param fileType generated file type
Bharat saraswal039f59c2016-07-14 21:57:13 +0530192 * @param curNode current YANG node
Vinod Kumar S79a374b2016-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-Huawei6266db32016-05-10 17:58:57 +0530198 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530199 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530200 }
201
Bharat saraswal250a7472016-05-12 13:16:57 +0530202 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530203 * Creates user defined data type class file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530204 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530205 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530206 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530207 }
208
Bharat saraswal250a7472016-05-12 13:16:57 +0530209 /*
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530210 * Creates service and manager class file.
Bharat saraswal250a7472016-05-12 13:16:57 +0530211 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530212 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530213 getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
Bharat saraswal250a7472016-05-12 13:16:57 +0530214 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530215
Shankara-Huaweib7564772016-08-02 18:13:13 +0530216 /*
Bharat saraswal039f59c2016-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 saraswal250a7472016-05-12 13:16:57 +0530223 /*
Shankara-Huaweib7564772016-08-02 18:13:13 +0530224 * Creates enumeration class file.
Bharat saraswal250a7472016-05-12 13:16:57 +0530225 */
226 if (fileType == GENERATE_ENUM_CLASS) {
227 getEnumerationTempFiles().generateJavaFile(GENERATE_ENUM_CLASS, curNode);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530228 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530229 }
230
231 /**
Vinod Kumar S79a374b2016-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 saraswal84366c52016-03-23 19:40:35 +0530234 *
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530235 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Shankara-Huaweib7564772016-08-02 18:13:13 +0530236 * @param pluginConfig plugin configurations for naming convention
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530237 * @throws IOException IO operation fail
Bharat saraswal84366c52016-03-23 19:40:35 +0530238 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530239 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530240 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530241 getTypeTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530242 .addTypeInfoToTempFiles(yangTypeHolder, pluginConfig);
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530243 }
244
245 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530246 * Adds build method for interface.
247 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530248 * @param pluginConfig plugin configurations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530249 * @return build method for interface
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530250 * @throws IOException when fails to append to temporary file
251 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530252 public String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530253 throws IOException {
254 if (getBeanTempFiles() != null) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530255 return getBeanTempFiles().addBuildMethodForInterface(pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530256 }
257 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530258 }
259
260 /**
261 * Adds default constructor for class.
262 *
Shankara-Huaweib7564772016-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 saraswal715d3fc2016-05-17 19:59:16 +0530265 * @param pluginConfig plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +0530266 * @param curNode YANG node
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530267 * @return default constructor for class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530268 * @throws IOException when fails to append to file
269 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530270 public String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig,
271 YangNode curNode)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530272 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530273 boolean isSuffix = false;
274 if (toAppend.equals(BUILDER)) {
275 isSuffix = true;
276 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530277 if (getTypeTempFiles() != null) {
278 return getTypeTempFiles()
Bharat saraswal8beac342016-08-04 02:00:03 +0530279 .addDefaultConstructor(modifier, toAppend, pluginConfig, isSuffix);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530280 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530281
282 if (getBeanTempFiles() != null) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530283 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend, pluginConfig, isSuffix);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530284 }
285
286 throw new TranslatorException("default constructor should not be added");
287 }
288
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530289 /**
290 * Adds build method's implementation for class.
291 *
Bharat saraswal8beac342016-08-04 02:00:03 +0530292 * @param curNode YANG node
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530293 * @return build method implementation for class
294 * @throws IOException when fails to append to temporary file
295 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530296 public String addBuildMethodImpl(YangNode curNode)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530297 throws IOException {
298 if (getBeanTempFiles() != null) {
299 return getBeanTempFiles().addBuildMethodImpl();
300 }
301
302 throw new TranslatorException("build should not be added");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530303 }
304
305 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530306 * Removes all temporary file handles.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530307 *
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530308 * @param isErrorOccurred when translator fails to generate java files we need to close
Shankara-Huaweib7564772016-08-02 18:13:13 +0530309 * all open file handles include temporary files and java files.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530310 * @throws IOException when failed to delete the temporary files
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530311 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530312 void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530313 throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530314
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530315 if (getBeanTempFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530316 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama13960652016-04-26 15:06:06 +0530317 }
318
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530319 if (getTypeTempFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530320 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama13960652016-04-26 15:06:06 +0530321 }
322
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530323 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530324 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
325 }
326
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530327 if (getServiceTempFiles() != null) {
328 getServiceTempFiles().freeTemporaryResources(isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530329 }
330
Bharat saraswal64e7e232016-07-14 23:33:55 +0530331 if (getEventFragmentFiles() != null) {
332 getEventFragmentFiles().freeTemporaryResources(isErrorOccurred);
333 }
334
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530335 }
336
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530337}