blob: dee7b1d416d57ca4eb93b786b554fdc07eb51d1b [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 org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053020import org.onosproject.yangutils.datamodel.YangTypeHolder;
Bharat saraswal780eca32016-04-05 12:45:45 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053022import org.onosproject.yangutils.utils.io.YangPluginConfig;
23
24import java.io.IOException;
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 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +053058 private TempJavaEnumerationFragmentFiles enumTempFiles;
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 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +053063 private TempJavaEventFragmentFiles eventTempFiles;
Bharat saraswal039f59c2016-07-14 21:57:13 +053064
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
Bharat saraswal9fab16b2016-09-23 23:27:24 +053074 int genType = javaFileInfo.getGeneratedFileTypes();
75 if ((genType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
76 beanTempFiles = new TempJavaBeanFragmentFiles(javaFileInfo);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 }
78
Bharat saraswal9fab16b2016-09-23 23:27:24 +053079 if ((genType & GENERATE_TYPE_CLASS) != 0) {
80 typeTempFiles = new TempJavaTypeFragmentFiles(javaFileInfo);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053081 }
82
Bharat saraswal9fab16b2016-09-23 23:27:24 +053083 if ((genType & GENERATE_ENUM_CLASS) != 0) {
84 enumTempFiles = new TempJavaEnumerationFragmentFiles(javaFileInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +053085 }
86
Bharat saraswal9fab16b2016-09-23 23:27:24 +053087 if ((genType & GENERATE_SERVICE_AND_MANAGER) != 0) {
88 serviceTempFiles = new TempJavaServiceFragmentFiles(javaFileInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +053089 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053090
Bharat saraswal9fab16b2016-09-23 23:27:24 +053091 if ((genType & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
92 eventTempFiles = new TempJavaEventFragmentFiles(javaFileInfo);
Bharat saraswal039f59c2016-07-14 21:57:13 +053093 }
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 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530106 * Retrieves the temp file handle for data type file generation.
107 *
108 * @return temp file handle for data type file generation
109 */
110 public TempJavaTypeFragmentFiles getTypeTempFiles() {
111 return typeTempFiles;
112 }
113
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530114
115 /**
116 * Retrieves the temp file handle for service file generation.
117 *
118 * @return temp file handle for service file generation
119 */
120 public TempJavaServiceFragmentFiles getServiceTempFiles() {
121 return serviceTempFiles;
122 }
123
124 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530125 * Retrieves the temp file handle for enumeration file generation.
126 *
127 * @return temp file handle for enumeration file generation
128 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530129 public TempJavaEnumerationFragmentFiles getEnumTempFiles() {
130 return enumTempFiles;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530131 }
132
133 /**
Bharat saraswal039f59c2016-07-14 21:57:13 +0530134 * Retrieves the temp file handle for event file generation.
135 *
136 * @return temp file handle for enumeration file generation
137 */
138 public TempJavaEventFragmentFiles getEventFragmentFiles() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530139 return eventTempFiles;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530140 }
141
142 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530143 * Constructs java code exit.
144 *
145 * @param fileType generated file type
Bharat saraswal039f59c2016-07-14 21:57:13 +0530146 * @param curNode current YANG node
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530147 * @throws IOException when fails to generate java files
148 */
149 public void generateJavaFile(int fileType, YangNode curNode)
150 throws IOException {
151
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530152 if ((fileType & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530153 beanTempFiles.generateJavaFile(fileType, curNode);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530154 }
155
Bharat saraswal250a7472016-05-12 13:16:57 +0530156 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530157 * Creates user defined data type class file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530158 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530159 if ((fileType & GENERATE_TYPE_CLASS) != 0) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530160 typeTempFiles.generateJavaFile(fileType, curNode);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530161 }
162
Bharat saraswal250a7472016-05-12 13:16:57 +0530163 /*
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530164 * Creates service and manager class file.
Bharat saraswal250a7472016-05-12 13:16:57 +0530165 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530166 if (fileType == GENERATE_SERVICE_AND_MANAGER) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530167 serviceTempFiles.generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
Bharat saraswal250a7472016-05-12 13:16:57 +0530168 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530169
Shankara-Huaweib7564772016-08-02 18:13:13 +0530170 /*
Bharat saraswal039f59c2016-07-14 21:57:13 +0530171 * Creates event, event listener and event subject files.
172 */
173 if (fileType == GENERATE_ALL_EVENT_CLASS_MASK) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530174 eventTempFiles.generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, curNode);
Bharat saraswal039f59c2016-07-14 21:57:13 +0530175 }
176
Bharat saraswal250a7472016-05-12 13:16:57 +0530177 /*
Shankara-Huaweib7564772016-08-02 18:13:13 +0530178 * Creates enumeration class file.
Bharat saraswal250a7472016-05-12 13:16:57 +0530179 */
180 if (fileType == GENERATE_ENUM_CLASS) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530181 enumTempFiles.generateJavaFile(GENERATE_ENUM_CLASS, curNode);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530182 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530183 }
184
185 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530186 * Add all the type in the current data model node as part of the
187 * generated temporary file.
Bharat saraswal84366c52016-03-23 19:40:35 +0530188 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530189 * @param typeHolder YANG java data model node which has type info, eg union / typedef
190 * @param config plugin configurations for naming convention
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530191 * @throws IOException IO operation fail
Bharat saraswal84366c52016-03-23 19:40:35 +0530192 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530193 void addTypeInfoToTempFiles(YangTypeHolder typeHolder, YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530194 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530195 typeTempFiles.addTypeInfoToTempFiles(typeHolder, config);
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530196 }
197
198 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530199 * Adds build method for interface.
200 *
201 * @return build method for interface
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530202 * @throws IOException when fails to append to temporary file
203 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530204 public String addBuildMethodForInterface()
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530205 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530206 if (beanTempFiles != null) {
207 return beanTempFiles.addBuildMethodForInterface();
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530208 }
209 throw new TranslatorException("build method only supported for bean class");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530210 }
211
212 /**
213 * Adds default constructor for class.
214 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530215 * @param modifier modifier for constructor.
216 * @param toAppend string which need to be appended with the class name
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530217 * @return default constructor for class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530218 * @throws IOException when fails to append to file
219 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530220 public String addDefaultConstructor(String modifier, String toAppend)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530221 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530222 boolean isSuffix = false;
223 if (toAppend.equals(BUILDER)) {
224 isSuffix = true;
225 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530226 if (typeTempFiles != null) {
227 return typeTempFiles.addDefaultConstructor(modifier, toAppend,
228 isSuffix);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530229 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530230 if (beanTempFiles != null) {
231 return beanTempFiles.addDefaultConstructor(modifier, toAppend,
232 isSuffix);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530233 }
234
235 throw new TranslatorException("default constructor should not be added");
236 }
237
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530238 /**
239 * Adds build method's implementation for class.
240 *
241 * @return build method implementation for class
242 * @throws IOException when fails to append to temporary file
243 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530244 public String addBuildMethodImpl()
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530245 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530246 if (beanTempFiles != null) {
247 return beanTempFiles.addBuildMethodImpl();
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530248 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530249 throw new TranslatorException("build should not be added");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530250 }
251
252 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530253 * Removes all temporary file handles.
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530254 * when translator fails to generate java files we need to close
255 * all open file handles include temporary files and java files
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530256 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530257 * @param isErrorOccurred if error occurred
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530258 * @throws IOException when failed to delete the temporary files
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530259 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530260 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530261 throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530262
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530263 if (beanTempFiles != null) {
264 beanTempFiles.freeTemporaryResources(isErrorOccurred);
Vidyashree Rama13960652016-04-26 15:06:06 +0530265 }
266
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530267 if (typeTempFiles != null) {
268 typeTempFiles.freeTemporaryResources(isErrorOccurred);
Vidyashree Rama13960652016-04-26 15:06:06 +0530269 }
270
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530271 if (enumTempFiles != null) {
272 enumTempFiles.freeTemporaryResources(isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530273 }
274
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530275 if (serviceTempFiles != null) {
276 serviceTempFiles.freeTemporaryResources(isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530277 }
278
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530279 if (eventTempFiles != null) {
280 eventTempFiles.freeTemporaryResources(isErrorOccurred);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530281 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530282 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530283}