blob: 7a0ea112a23b2f183f3277117bf38e0d53f83a18 [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;
Shankara-Huaweib7564772016-08-02 18:13:13 +053023import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
24import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswal780eca32016-04-05 12:45:45 +053025import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053026
Bharat saraswal039f59c2016-07-14 21:57:13 +053027import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ALL_EVENT_CLASS_MASK;
Bharat saraswal68fa0d12016-04-19 01:00:16 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053029import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053030import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053031import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Bharat saraswal8beac342016-08-04 02:00:03 +053032import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053033
34/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053035 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053036 * Contains fragment file object of different types of java file.
37 * Uses required object(s) to generate the target java file(s).
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053038 */
39public class TempJavaCodeFragmentFiles {
40
41 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053042 * Has the temporary files required for bean generated classes.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053043 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053044 private TempJavaBeanFragmentFiles beanTempFiles;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053045
46 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053047 * Has the temporary files required for bean generated classes.
Bharat saraswal84366c52016-03-23 19:40:35 +053048 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053049 private TempJavaTypeFragmentFiles typeTempFiles;
Bharat saraswal84366c52016-03-23 19:40:35 +053050
51 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053052 * Has the temporary files required for service generated classes.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053053 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053054 private TempJavaServiceFragmentFiles serviceTempFiles;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053055
56 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053057 * Has the temporary files required for enumeration generated classes.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053058 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053059 private TempJavaEnumerationFragmentFiles enumerationTempFiles;
Bharat saraswal68fa0d12016-04-19 01:00:16 +053060
Bharat saraswald532a4c2016-03-25 18:19:46 +053061 /**
Bharat saraswal039f59c2016-07-14 21:57:13 +053062 * Has the temporary files required for enumeration generated classes.
63 */
64 private TempJavaEventFragmentFiles tempJavaEventFragmentFiles;
65
66 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053067 * Creates an instance of temporary java code fragment.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053068 *
Vinod Kumar S79a374b2016-04-30 21:09:15 +053069 * @param javaFileInfo generated java file info
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053070 * @throws IOException when fails to create new file handle
71 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053072 public TempJavaCodeFragmentFiles(JavaFileInfo javaFileInfo)
Bharat saraswal84366c52016-03-23 19:40:35 +053073 throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053074
Vinod Kumar S79a374b2016-04-30 21:09:15 +053075 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
76 setBeanTempFiles(new TempJavaBeanFragmentFiles(javaFileInfo));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 }
78
Vinod Kumar S79a374b2016-04-30 21:09:15 +053079 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_TYPE_CLASS) != 0) {
80 setTypeTempFiles(new TempJavaTypeFragmentFiles(javaFileInfo));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053081 }
82
Vinod Kumar S79a374b2016-04-30 21:09:15 +053083 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ENUM_CLASS) != 0) {
84 setEnumerationTempFiles(new TempJavaEnumerationFragmentFiles(javaFileInfo));
85 }
86
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053087 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +053088 setServiceTempFiles(new TempJavaServiceFragmentFiles(javaFileInfo));
89 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053090
Bharat saraswal039f59c2016-07-14 21:57:13 +053091 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
92 setEventFragmentFiles(new TempJavaEventFragmentFiles(javaFileInfo));
93 }
94
Vinod Kumar S79a374b2016-04-30 21:09:15 +053095 }
96
97 /**
98 * Retrieves the temp file handle for bean file generation.
99 *
100 * @return temp file handle for bean file generation
101 */
102 public TempJavaBeanFragmentFiles getBeanTempFiles() {
103 return beanTempFiles;
104 }
105
106 /**
107 * Sets temp file handle for bean file generation.
108 *
109 * @param beanTempFiles temp file handle for bean file generation
110 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530111 private void setBeanTempFiles(TempJavaBeanFragmentFiles beanTempFiles) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530112 this.beanTempFiles = beanTempFiles;
113 }
114
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530115 /**
116 * Retrieves the temp file handle for data type file generation.
117 *
118 * @return temp file handle for data type file generation
119 */
120 public TempJavaTypeFragmentFiles getTypeTempFiles() {
121 return typeTempFiles;
122 }
123
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530124 /**
125 * Sets temp file handle for data type file generation.
126 *
127 * @param typeTempFiles temp file handle for data type file generation
128 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530129 private void setTypeTempFiles(TempJavaTypeFragmentFiles typeTempFiles) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530130 this.typeTempFiles = typeTempFiles;
131 }
132
133 /**
134 * Retrieves the temp file handle for service file generation.
135 *
136 * @return temp file handle for service file generation
137 */
138 public TempJavaServiceFragmentFiles getServiceTempFiles() {
139 return serviceTempFiles;
140 }
141
142 /**
143 * Sets temp file handle for service file generation.
144 *
145 * @param serviceTempFiles temp file handle for service file generation
146 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530147 private void setServiceTempFiles(TempJavaServiceFragmentFiles serviceTempFiles) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530148 this.serviceTempFiles = serviceTempFiles;
149 }
150
151 /**
152 * Retrieves the temp file handle for enumeration file generation.
153 *
154 * @return temp file handle for enumeration file generation
155 */
156 public TempJavaEnumerationFragmentFiles getEnumerationTempFiles() {
157 return enumerationTempFiles;
158 }
159
160 /**
161 * Sets temp file handle for enumeration file generation.
162 *
163 * @param enumerationTempFiles temp file handle for enumeration file generation
164 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530165 private void setEnumerationTempFiles(
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530166 TempJavaEnumerationFragmentFiles enumerationTempFiles) {
167 this.enumerationTempFiles = enumerationTempFiles;
168 }
169
170 /**
Bharat saraswal039f59c2016-07-14 21:57:13 +0530171 * Retrieves the temp file handle for event file generation.
172 *
173 * @return temp file handle for enumeration file generation
174 */
175 public TempJavaEventFragmentFiles getEventFragmentFiles() {
176 return tempJavaEventFragmentFiles;
177 }
178
179 /**
180 * Sets temp file handle for event file generation.
181 *
182 * @param tempJavaEventFragmentFiles temp file handle for event file generation
183 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530184 private void setEventFragmentFiles(TempJavaEventFragmentFiles tempJavaEventFragmentFiles) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530185 this.tempJavaEventFragmentFiles = tempJavaEventFragmentFiles;
186 }
187
188
189 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530190 * Constructs java code exit.
191 *
192 * @param fileType generated file type
Bharat saraswal039f59c2016-07-14 21:57:13 +0530193 * @param curNode current YANG node
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530194 * @throws IOException when fails to generate java files
195 */
196 public void generateJavaFile(int fileType, YangNode curNode)
197 throws IOException {
198
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530199 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530200 getBeanTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530201 }
202
Bharat saraswal250a7472016-05-12 13:16:57 +0530203 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530204 * Creates user defined data type class file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530205 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530206 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530207 getTypeTempFiles().generateJavaFile(fileType, curNode);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530208 }
209
Bharat saraswal250a7472016-05-12 13:16:57 +0530210 /*
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530211 * Creates service and manager class file.
Bharat saraswal250a7472016-05-12 13:16:57 +0530212 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530213 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530214 getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
Bharat saraswal250a7472016-05-12 13:16:57 +0530215 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530216
Shankara-Huaweib7564772016-08-02 18:13:13 +0530217 /*
Bharat saraswal039f59c2016-07-14 21:57:13 +0530218 * Creates event, event listener and event subject files.
219 */
220 if (fileType == GENERATE_ALL_EVENT_CLASS_MASK) {
221 getEventFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, curNode);
222 }
223
Bharat saraswal250a7472016-05-12 13:16:57 +0530224 /*
Shankara-Huaweib7564772016-08-02 18:13:13 +0530225 * Creates enumeration class file.
Bharat saraswal250a7472016-05-12 13:16:57 +0530226 */
227 if (fileType == GENERATE_ENUM_CLASS) {
228 getEnumerationTempFiles().generateJavaFile(GENERATE_ENUM_CLASS, curNode);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530229 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530230 }
231
232 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530233 * Add all the type in the current data model node as part of the
234 * generated temporary file.
Bharat saraswal84366c52016-03-23 19:40:35 +0530235 *
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530236 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Shankara-Huaweib7564772016-08-02 18:13:13 +0530237 * @param pluginConfig plugin configurations for naming convention
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530238 * @throws IOException IO operation fail
Bharat saraswal84366c52016-03-23 19:40:35 +0530239 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530240 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530241 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530242 getTypeTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530243 .addTypeInfoToTempFiles(yangTypeHolder, pluginConfig);
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530244 }
245
246 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530247 * Adds build method for interface.
248 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530249 * @param pluginConfig plugin configurations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530250 * @return build method for interface
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530251 * @throws IOException when fails to append to temporary file
252 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530253 public String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530254 throws IOException {
255 if (getBeanTempFiles() != null) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530256 return getBeanTempFiles().addBuildMethodForInterface(pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530257 }
258 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530259 }
260
261 /**
262 * Adds default constructor for class.
263 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530264 * @param modifier modifier for constructor.
265 * @param toAppend string which need to be appended with the class name
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530266 * @param pluginConfig plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +0530267 * @param curNode YANG node
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530268 * @return default constructor for class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530269 * @throws IOException when fails to append to file
270 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530271 public String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig,
272 YangNode curNode)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530273 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530274 boolean isSuffix = false;
275 if (toAppend.equals(BUILDER)) {
276 isSuffix = true;
277 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530278 if (getTypeTempFiles() != null) {
279 return getTypeTempFiles()
Bharat saraswal8beac342016-08-04 02:00:03 +0530280 .addDefaultConstructor(modifier, toAppend, pluginConfig, isSuffix);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530281 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530282
283 if (getBeanTempFiles() != null) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530284 return getBeanTempFiles().addDefaultConstructor(modifier, toAppend, pluginConfig, isSuffix);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530285 }
286
287 throw new TranslatorException("default constructor should not be added");
288 }
289
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530290 /**
291 * Adds build method's implementation for class.
292 *
Bharat saraswal8beac342016-08-04 02:00:03 +0530293 * @param curNode YANG node
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530294 * @return build method implementation for class
295 * @throws IOException when fails to append to temporary file
296 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530297 public String addBuildMethodImpl(YangNode curNode)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530298 throws IOException {
299 if (getBeanTempFiles() != null) {
300 return getBeanTempFiles().addBuildMethodImpl();
301 }
302
303 throw new TranslatorException("build should not be added");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530304 }
305
306 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530307 * Removes all temporary file handles.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530308 *
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530309 * @param isErrorOccurred when translator fails to generate java files we need to close
Shankara-Huaweib7564772016-08-02 18:13:13 +0530310 * all open file handles include temporary files and java files.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530311 * @throws IOException when failed to delete the temporary files
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530312 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530313 void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530314 throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530315
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530316 if (getBeanTempFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530317 getBeanTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama13960652016-04-26 15:06:06 +0530318 }
319
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530320 if (getTypeTempFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530321 getTypeTempFiles().freeTemporaryResources(isErrorOccurred);
Vidyashree Rama13960652016-04-26 15:06:06 +0530322 }
323
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530324 if (getEnumerationTempFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530325 getEnumerationTempFiles().freeTemporaryResources(isErrorOccurred);
326 }
327
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530328 if (getServiceTempFiles() != null) {
329 getServiceTempFiles().freeTemporaryResources(isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530330 }
331
Bharat saraswal64e7e232016-07-14 23:33:55 +0530332 if (getEventFragmentFiles() != null) {
333 getEventFragmentFiles().freeTemporaryResources(isErrorOccurred);
334 }
335
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530336 }
337
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530338}