blob: beb8de08e7cb806afe670cdafeaf87e58567b7f4 [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 saraswald72411a2016-04-19 01:00:16 +053026import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053027import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar S38046502016-03-23 15:30:27 +053030
31/**
Bharat saraswald9822e92016-04-05 15:13:44 +053032 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053033 * Contains fragment file object of different types of java file.
34 * Uses required object(s) to generate the target java file(s).
Vinod Kumar S38046502016-03-23 15:30:27 +053035 */
36public class TempJavaCodeFragmentFiles {
37
38 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039 * Has the temporary files required for bean generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053040 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041 private TempJavaBeanFragmentFiles beanTempFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053042
43 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044 * Has the temporary files required for bean generated classes.
Bharat saraswale2d51d62016-03-23 19:40:35 +053045 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046 private TempJavaTypeFragmentFiles typeTempFiles;
Bharat saraswale2d51d62016-03-23 19:40:35 +053047
48 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049 * Has the temporary files required for service generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053050 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053051 private TempJavaServiceFragmentFiles serviceTempFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053052
53 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053054 * Has the temporary files required for enumeration generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053055 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053056 private TempJavaEnumerationFragmentFiles enumerationTempFiles;
Bharat saraswald72411a2016-04-19 01:00:16 +053057
Bharat saraswal2f11f652016-03-25 18:19:46 +053058 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053059 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +053060 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053061 * @param javaFileInfo generated java file info
Vinod Kumar S38046502016-03-23 15:30:27 +053062 * @throws IOException when fails to create new file handle
63 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053064 public TempJavaCodeFragmentFiles(JavaFileInfo javaFileInfo)
Bharat saraswale2d51d62016-03-23 19:40:35 +053065 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053066
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
68 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053069 }
70
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
72 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053073 }
74
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
76 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
77 }
78
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053079 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
81 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053082
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083 }
84
85 /**
86 * Retrieves the temp file handle for bean file generation.
87 *
88 * @return temp file handle for bean file generation
89 */
90 public TempJavaBeanFragmentFiles getBeanTempFiles() {
91 return beanTempFiles;
92 }
93
94 /**
95 * Sets temp file handle for bean file generation.
96 *
97 * @param beanTempFiles temp file handle for bean file generation
98 */
99 public void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
100 this.beanTempFiles = beanTempFiles;
101 }
102
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530103 /**
104 * Retrieves the temp file handle for data type file generation.
105 *
106 * @return temp file handle for data type file generation
107 */
108 public TempJavaTypeFragmentFiles getTypeTempFiles() {
109 return typeTempFiles;
110 }
111
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530112 /**
113 * Sets temp file handle for data type file generation.
114 *
115 * @param typeTempFiles temp file handle for data type file generation
116 */
117 public void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
118 this.typeTempFiles = typeTempFiles;
119 }
120
121 /**
122 * Retrieves the temp file handle for service file generation.
123 *
124 * @return temp file handle for service file generation
125 */
126 public TempJavaServiceFragmentFiles getServiceTempFiles() {
127 return serviceTempFiles;
128 }
129
130 /**
131 * Sets temp file handle for service file generation.
132 *
133 * @param serviceTempFiles temp file handle for service file generation
134 */
135 public void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
136 this.serviceTempFiles = serviceTempFiles;
137 }
138
139 /**
140 * Retrieves the temp file handle for enumeration file generation.
141 *
142 * @return temp file handle for enumeration file generation
143 */
144 public TempJavaEnumerationFragmentFiles getEnumerationTempFiles() {
145 return enumerationTempFiles;
146 }
147
148 /**
149 * Sets temp file handle for enumeration file generation.
150 *
151 * @param enumerationTempFiles temp file handle for enumeration file generation
152 */
153 public void setEnumerationTempFiles(
154 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
155 this.enumerationTempFiles = enumerationTempFiles;
156 }
157
158 /**
159 * Constructs java code exit.
160 *
161 * @param fileType generated file type
162 * @param curNode current YANG node
163 * @throws IOException when fails to generate java files
164 */
165 public void generateJavaFile(int fileType, YangNode curNode)
166 throws IOException {
167
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530168 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530169 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530170 }
171
Bharat saraswalc0e04842016-05-12 13:16:57 +0530172 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530173 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +0530174 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530175 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530176 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530177 }
178
Bharat saraswalc0e04842016-05-12 13:16:57 +0530179 /*
Bharat saraswal33dfa012016-05-17 19:59:16 +0530180 * Creates service and manager class file.
Bharat saraswalc0e04842016-05-12 13:16:57 +0530181 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530182 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183 getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530184 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530185
Bharat saraswalc0e04842016-05-12 13:16:57 +0530186 /*
187 * Creats enumeration class file.
188 */
189 if (fileType == GENERATE_ENUM_CLASS) {
190 getEnumerationTempFiles().generateJavaFile(GENERATE_ENUM_CLASS, curNode);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530191 }
192
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530193 freeTemporaryResources(false);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530194 }
195
196 /**
197 * Adds the new attribute info to the target generated temporary files.
198 *
199 * @param newAttrInfo the attribute info that needs to be added to temporary
200 * files
Bharat saraswal33dfa012016-05-17 19:59:16 +0530201 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530202 * @throws IOException IO operation fail
203 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530204 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo,
205 YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206 throws IOException {
207
208 if (getBeanTempFiles() != null) {
209 getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530210 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530211 }
212
Bharat saraswale2d51d62016-03-23 19:40:35 +0530213 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530214 * Creates user defined data type class file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530215 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530216 if (getTypeTempFiles() != null) {
217 getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530218 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530219 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530220 }
221
222 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530223 * Add all the type in the current data model node as part of the
224 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530225 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530226 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Bharat saraswal33dfa012016-05-17 19:59:16 +0530227 * @param pluginConfig plugin configurations for naming convention
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530228 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530229 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530230 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530231 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530232 getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530233 .addTypeInfoToTempFiles(yangTypeHolder, pluginConfig);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530234 }
235
236 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530237 * Adds build method for interface.
238 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530239 * @param pluginConfig plugin configurations
Vinod Kumar S38046502016-03-23 15:30:27 +0530240 * @return build method for interface
Vinod Kumar S38046502016-03-23 15:30:27 +0530241 * @throws IOException when fails to append to temporary file
242 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530243 public String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530244 throws IOException {
245 if (getBeanTempFiles() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530246 return getBeanTempFiles().addBuildMethodForInterface(pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530247 }
248 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S38046502016-03-23 15:30:27 +0530249 }
250
251 /**
252 * Adds default constructor for class.
253 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530254 * @param modifier modifier for constructor.
255 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530256 * @param pluginConfig plugin configurations
Vinod Kumar S38046502016-03-23 15:30:27 +0530257 * @return default constructor for class
Vinod Kumar S38046502016-03-23 15:30:27 +0530258 * @throws IOException when fails to append to file
259 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530260 public String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530261 throws IOException {
262 if (getTypeTempFiles() != null) {
263 return getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530264 .addDefaultConstructor(modifier, toAppend, pluginConfig);
Vinod Kumar S38046502016-03-23 15:30:27 +0530265 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530266
267 if (getBeanTempFiles() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530268 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530269 }
270
271 throw new TranslatorException("default constructor should not be added");
272 }
273
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530274 /**
275 * Adds build method's implementation for class.
276 *
277 * @return build method implementation for class
278 * @throws IOException when fails to append to temporary file
279 */
280 public String addBuildMethodImpl()
281 throws IOException {
282 if (getBeanTempFiles() != null) {
283 return getBeanTempFiles().addBuildMethodImpl();
284 }
285
286 throw new TranslatorException("build should not be added");
Vinod Kumar S38046502016-03-23 15:30:27 +0530287 }
288
289 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530290 * Removes all temporary file handles.
Vinod Kumar S38046502016-03-23 15:30:27 +0530291 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530292 * @param isErrorOccurred when translator fails to generate java files we need to close
293 * all open file handles include temporary files and java files.
294 * @throws IOException when failed to delete the temporary files
Vinod Kumar S38046502016-03-23 15:30:27 +0530295 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530296 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530297 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530298
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530299 if (getBeanTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530300 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530301 }
302
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530303 if (getTypeTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530304 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530305 }
306
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530307 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530308 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
309 }
310
Bharat saraswal33dfa012016-05-17 19:59:16 +0530311 if (getServiceTempFiles() != null) {
312 getServiceTempFiles().freeTemporaryResources(isErrorOccurred);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530313 }
314
Vinod Kumar S38046502016-03-23 15:30:27 +0530315 }
316
Vinod Kumar S38046502016-03-23 15:30:27 +0530317}