blob: 22d95392bd053e629ee0fdb506c8f4a2926a3dbc [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 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 }
193
194 /**
195 * Adds the new attribute info to the target generated temporary files.
196 *
197 * @param newAttrInfo the attribute info that needs to be added to temporary
198 * files
Bharat saraswal33dfa012016-05-17 19:59:16 +0530199 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530200 * @throws IOException IO operation fail
201 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530202 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo,
203 YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530204 throws IOException {
205
206 if (getBeanTempFiles() != null) {
207 getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530208 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530209 }
210
Bharat saraswale2d51d62016-03-23 19:40:35 +0530211 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530212 * Creates user defined data type class file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530213 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530214 if (getTypeTempFiles() != null) {
215 getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530216 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo, pluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530217 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530218 }
219
220 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530221 * Add all the type in the current data model node as part of the
222 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530223 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530224 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Bharat saraswal33dfa012016-05-17 19:59:16 +0530225 * @param pluginConfig plugin configurations for naming convention
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530226 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530227 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530228 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530229 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530230 getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530231 .addTypeInfoToTempFiles(yangTypeHolder, pluginConfig);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530232 }
233
234 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530235 * Adds build method for interface.
236 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530237 * @param pluginConfig plugin configurations
Vinod Kumar S38046502016-03-23 15:30:27 +0530238 * @return build method for interface
Vinod Kumar S38046502016-03-23 15:30:27 +0530239 * @throws IOException when fails to append to temporary file
240 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530241 public String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530242 throws IOException {
243 if (getBeanTempFiles() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530244 return getBeanTempFiles().addBuildMethodForInterface(pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530245 }
246 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S38046502016-03-23 15:30:27 +0530247 }
248
249 /**
250 * Adds default constructor for class.
251 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530252 * @param modifier modifier for constructor.
253 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530254 * @param pluginConfig plugin configurations
Vinod Kumar S38046502016-03-23 15:30:27 +0530255 * @return default constructor for class
Vinod Kumar S38046502016-03-23 15:30:27 +0530256 * @throws IOException when fails to append to file
257 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530258 public String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530259 throws IOException {
260 if (getTypeTempFiles() != null) {
261 return getTypeTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530262 .addDefaultConstructor(modifier, toAppend, pluginConfig);
Vinod Kumar S38046502016-03-23 15:30:27 +0530263 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530264
265 if (getBeanTempFiles() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530266 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530267 }
268
269 throw new TranslatorException("default constructor should not be added");
270 }
271
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530272 /**
273 * Adds build method's implementation for class.
274 *
275 * @return build method implementation for class
276 * @throws IOException when fails to append to temporary file
277 */
278 public String addBuildMethodImpl()
279 throws IOException {
280 if (getBeanTempFiles() != null) {
281 return getBeanTempFiles().addBuildMethodImpl();
282 }
283
284 throw new TranslatorException("build should not be added");
Vinod Kumar S38046502016-03-23 15:30:27 +0530285 }
286
287 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530288 * Removes all temporary file handles.
Vinod Kumar S38046502016-03-23 15:30:27 +0530289 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530290 * @param isErrorOccurred when translator fails to generate java files we need to close
291 * all open file handles include temporary files and java files.
292 * @throws IOException when failed to delete the temporary files
Vinod Kumar S38046502016-03-23 15:30:27 +0530293 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530294 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530295 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530296
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530297 if (getBeanTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530298 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530299 }
300
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530301 if (getTypeTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530302 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530303 }
304
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530305 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530306 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
307 }
308
Bharat saraswal33dfa012016-05-17 19:59:16 +0530309 if (getServiceTempFiles() != null) {
310 getServiceTempFiles().freeTemporaryResources(isErrorOccurred);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530311 }
312
Vinod Kumar S38046502016-03-23 15:30:27 +0530313 }
314
Vinod Kumar S38046502016-03-23 15:30:27 +0530315}