blob: a1bb80161461060398f0b56068d1b69dea132bf4 [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
21import org.onosproject.yangutils.datamodel.YangTypeContainer;
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.YangNode;
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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053026import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Gaurav Agrawal56527662016-04-20 15:49:17 +053027import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_RPC_INTERFACE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
29import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList;
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.
Vinod Kumar S38046502016-03-23 15:30:27 +053033 */
34public class TempJavaCodeFragmentFiles {
35
36 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053037 * Has the temporary files required for bean generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053038 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039 private TempJavaBeanFragmentFiles beanTempFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053040
41 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042 * Has the temporary files required for bean generated classes.
Bharat saraswale2d51d62016-03-23 19:40:35 +053043 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044 private TempJavaTypeFragmentFiles typeTempFiles;
Bharat saraswale2d51d62016-03-23 19:40:35 +053045
46 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047 * Has the temporary files required for service generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053048 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049 private TempJavaServiceFragmentFiles serviceTempFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053050
51 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052 * Has the temporary files required for enumeration generated classes.
Vinod Kumar S38046502016-03-23 15:30:27 +053053 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053054 private TempJavaEnumerationFragmentFiles enumerationTempFiles;
Bharat saraswald72411a2016-04-19 01:00:16 +053055
Bharat saraswal2f11f652016-03-25 18:19:46 +053056 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +053058 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059 * @param javaFileInfo generated java file info
Vinod Kumar S38046502016-03-23 15:30:27 +053060 * @throws IOException when fails to create new file handle
61 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053062 public TempJavaCodeFragmentFiles(JavaFileInfo javaFileInfo)
Bharat saraswale2d51d62016-03-23 19:40:35 +053063 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053064
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053065 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
66 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053067 }
68
69 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053070 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +053071 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053072 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
73 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S38046502016-03-23 15:30:27 +053074 }
75
76 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053077 * Creates enumeration class file.
Vinod Kumar S38046502016-03-23 15:30:27 +053078 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
80 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
81 }
82
83 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_RPC_INTERFACE) != 0) {
84 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
85 }
86 }
87
88 /**
89 * Retrieves the temp file handle for bean file generation.
90 *
91 * @return temp file handle for bean file generation
92 */
93 public TempJavaBeanFragmentFiles getBeanTempFiles() {
94 return beanTempFiles;
95 }
96
97 /**
98 * Sets temp file handle for bean file generation.
99 *
100 * @param beanTempFiles temp file handle for bean file generation
101 */
102 public void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
103 this.beanTempFiles = beanTempFiles;
104 }
105
106
107 /**
108 * Retrieves the temp file handle for data type file generation.
109 *
110 * @return temp file handle for data type file generation
111 */
112 public TempJavaTypeFragmentFiles getTypeTempFiles() {
113 return typeTempFiles;
114 }
115
116
117 /**
118 * Sets temp file handle for data type file generation.
119 *
120 * @param typeTempFiles temp file handle for data type file generation
121 */
122 public void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
123 this.typeTempFiles = typeTempFiles;
124 }
125
126 /**
127 * Retrieves the temp file handle for service file generation.
128 *
129 * @return temp file handle for service file generation
130 */
131 public TempJavaServiceFragmentFiles getServiceTempFiles() {
132 return serviceTempFiles;
133 }
134
135 /**
136 * Sets temp file handle for service file generation.
137 *
138 * @param serviceTempFiles temp file handle for service file generation
139 */
140 public void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
141 this.serviceTempFiles = serviceTempFiles;
142 }
143
144 /**
145 * Retrieves the temp file handle for enumeration file generation.
146 *
147 * @return temp file handle for enumeration file generation
148 */
149 public TempJavaEnumerationFragmentFiles getEnumerationTempFiles() {
150 return enumerationTempFiles;
151 }
152
153 /**
154 * Sets temp file handle for enumeration file generation.
155 *
156 * @param enumerationTempFiles temp file handle for enumeration file generation
157 */
158 public void setEnumerationTempFiles(
159 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
160 this.enumerationTempFiles = enumerationTempFiles;
161 }
162
163 /**
164 * Constructs java code exit.
165 *
166 * @param fileType generated file type
167 * @param curNode current YANG node
168 * @throws IOException when fails to generate java files
169 */
170 public void generateJavaFile(int fileType, YangNode curNode)
171 throws IOException {
172
173 if (getBeanTempFiles() != null) {
174 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530175 }
176
177 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530178 * Creates user defined data type class file.
Vinod Kumar S38046502016-03-23 15:30:27 +0530179 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 if (getTypeTempFiles() != null) {
181 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530182 }
183
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530184 }
185
186 /**
187 * Adds the new attribute info to the target generated temporary files.
188 *
189 * @param newAttrInfo the attribute info that needs to be added to temporary
190 * files
191 * @throws IOException IO operation fail
192 */
193 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
194 throws IOException {
195
196 if (getBeanTempFiles() != null) {
197 getBeanTempFiles()
198 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530199 }
200
Bharat saraswale2d51d62016-03-23 19:40:35 +0530201 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530202 * Creates user defined data type class file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530203 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530204 if (getTypeTempFiles() != null) {
205 getTypeTempFiles()
206 .addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530207 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530208 }
209
210 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530211 * Adds all the leaves in the current data model node as part of the
212 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530213 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530214 * @param curNode java file info of the generated file
215 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530216 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530217 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode)
218 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530219
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530220 if (getBeanTempFiles() != null) {
221 getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(curNode);
Bharat saraswal2f11f652016-03-25 18:19:46 +0530222 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530223
Bharat saraswale2d51d62016-03-23 19:40:35 +0530224 }
225
226 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530227 * Add all the type in the current data model node as part of the
228 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530229 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530230 * @param yangTypeContainer YANG java data model node which has type info, eg union / typedef
231 * @throws IOException IO operation fail
Bharat saraswale2d51d62016-03-23 19:40:35 +0530232 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530233 public void addTypeInfoToTempFiles(YangTypeContainer yangTypeContainer)
234 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530235
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530236 if (getTypeTempFiles() != null) {
237 getTypeTempFiles()
238 .addTypeInfoToTempFiles(yangTypeContainer);
239 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530240 }
241
242 /**
243 * Adds class to the extends list.
244 *
245 * @param extend class to be extended
246 */
247 public void addToExtendsList(String extend) {
248 getExtendsList().add(extend);
249 }
250
251 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530252 * Adds build method for interface.
253 *
254 * @return build method for interface
Vinod Kumar S38046502016-03-23 15:30:27 +0530255 * @throws IOException when fails to append to temporary file
256 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530257 public String addBuildMethodForInterface()
258 throws IOException {
259 if (getBeanTempFiles() != null) {
260 return getBeanTempFiles().addBuildMethodForInterface();
261 }
262 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S38046502016-03-23 15:30:27 +0530263 }
264
265 /**
266 * Adds default constructor for class.
267 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530268 * @param modifier modifier for constructor.
269 * @param toAppend string which need to be appended with the class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530270 * @return default constructor for class
Vinod Kumar S38046502016-03-23 15:30:27 +0530271 * @throws IOException when fails to append to file
272 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530273 public String addDefaultConstructor(String modifier, String toAppend)
274 throws IOException {
275 if (getTypeTempFiles() != null) {
276 return getTypeTempFiles()
277 .addDefaultConstructor(modifier, toAppend);
Vinod Kumar S38046502016-03-23 15:30:27 +0530278 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530279
280 if (getBeanTempFiles() != null) {
281 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend);
282 }
283
284 throw new TranslatorException("default constructor should not be added");
285 }
286
287
288 /**
289 * Adds build method's implementation for class.
290 *
291 * @return build method implementation for class
292 * @throws IOException when fails to append to temporary file
293 */
294 public String addBuildMethodImpl()
295 throws IOException {
296 if (getBeanTempFiles() != null) {
297 return getBeanTempFiles().addBuildMethodImpl();
298 }
299
300 throw new TranslatorException("build should not be added");
Vinod Kumar S38046502016-03-23 15:30:27 +0530301 }
302
303 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530304 * Removes all temporary file handles.
Vinod Kumar S38046502016-03-23 15:30:27 +0530305 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530306 * @param isErrorOccurred when translator fails to generate java files we need to close
307 * all open file handles include temporary files and java files.
308 * @throws IOException when failed to delete the temporary files
Vinod Kumar S38046502016-03-23 15:30:27 +0530309 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530310 public void close(boolean isErrorOccurred)
311 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530312
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530313 if (getBeanTempFiles() != null) {
314 getBeanTempFiles().close(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530315 }
316
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530317 if (getTypeTempFiles() != null) {
318 getTypeTempFiles().close(isErrorOccurred);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530319 }
320
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530321 if (getEnumerationTempFiles() != null) {
322 getEnumerationTempFiles().close(isErrorOccurred);
Vinod Kumar S38046502016-03-23 15:30:27 +0530323 }
324 }
325
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530326
327 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530328 * Adds enum attributes to temporary files.
329 *
330 * @param curNode current YANG node
331 * @throws IOException when fails to do IO operations
332 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530333 public void addEnumAttributeToTempFiles(YangNode curNode)
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530334 throws IOException {
335
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530336 if (getEnumerationTempFiles() != null) {
337 getEnumerationTempFiles().addEnumAttributeToTempFiles(curNode);
338 return;
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530339 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530340
341 throw new TranslatorException("build should not be added");
Bharat saraswale2d51d62016-03-23 19:40:35 +0530342 }
343
Vinod Kumar S38046502016-03-23 15:30:27 +0530344}