blob: 2cbbb49d190f70fd21fc0a895c3ca1666423714d [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053019import java.io.File;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020import java.io.IOException;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053021import java.util.ArrayList;
22import java.util.List;
23
24import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.datamodel.YangType;
26import org.onosproject.yangutils.datamodel.YangTypeHolder;
27import org.onosproject.yangutils.translator.exception.TranslatorException;
28import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaType;
Bharat saraswal33dfa012016-05-17 19:59:16 +053029import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
32import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
33import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
34import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
36import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
38import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053039import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053040import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
41import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
42import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
43import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
44import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
45import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046
47/**
48 * Represents implementation of java data type code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053049 * Maintains the temp files required specific for user defined data type java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053050 */
51public class TempJavaTypeFragmentFiles
52 extends TempJavaFragmentFiles {
53
54 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053055 * File name for of string method.
56 */
57 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
58
59 /**
60 * File name for construction for special type like union, typedef.
61 */
62 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053063
64 /**
65 * File name for typedef class file name suffix.
66 */
67 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
68
69 /**
70 * File name for generated class file for special type like union, typedef
71 * suffix.
72 */
73 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
74
75 /**
76 * Temporary file handle for of string method of class.
77 */
78 private File ofStringImplTempFileHandle;
79 /**
80 * Temporary file handle for constructor for type class.
81 */
82 private File constructorForTypeTempFileHandle;
83
84 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053085 * Java file handle for typedef class file.
86 */
87 private File typedefClassJavaFileHandle;
88 /**
89 * Java file handle for type class like union, typedef file.
90 */
91 private File typeClassJavaFileHandle;
92
93 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053094 * Creates an instance of temporary java code fragment.
95 *
96 * @param javaFileInfo generated java file info
97 * @throws IOException when fails to create new file handle
98 */
99 public TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
100 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530101
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530102 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530103
104 /*
105 * Initialize getterImpl, attributes, hash code, equals and to strings
106 * when generation file type matches to typeDef class mask.
107 */
108 addGeneratedTempFile(OF_STRING_IMPL_MASK);
109 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
110 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
111
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530112 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
113 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530114
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115 }
116
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530117 /**
118 * Returns type class constructor method's temporary file handle.
119 *
120 * @return type class constructor method's temporary file handle
121 */
122
123 public File getConstructorForTypeTempFileHandle() {
124 return constructorForTypeTempFileHandle;
125 }
126
127 /**
128 * Sets type class constructor method's temporary file handle.
129 *
130 * @param constructorForTypeTempFileHandle type class constructor method's
131 * temporary file handle
132 */
133 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
134 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
135 }
136
137 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530138 * Returns java file handle for typedef class file.
139 *
140 * @return java file handle for typedef class file
141 */
142 File getTypedefClassJavaFileHandle() {
143 return typedefClassJavaFileHandle;
144 }
145
146 /**
147 * Sets the java file handle for typedef class file.
148 *
149 * @param typedefClassJavaFileHandle java file handle
150 */
151 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
152 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
153 }
154
155 /**
156 * Returns java file handle for type class file.
157 *
158 * @return java file handle for type class file
159 */
160 File getTypeClassJavaFileHandle() {
161 return typeClassJavaFileHandle;
162 }
163
164 /**
165 * Sets the java file handle for type class file.
166 *
167 * @param typeClassJavaFileHandle type file handle
168 */
169 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
170 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
171 }
172
173 /**
174 * Returns of string method's temporary file handle.
175 *
176 * @return of string method's temporary file handle
177 */
178
179 public File getOfStringImplTempFileHandle() {
180 return ofStringImplTempFileHandle;
181 }
182
183 /**
184 * Set of string method's temporary file handle.
185 *
186 * @param ofStringImplTempFileHandle of string method's temporary file
187 * handle
188 */
189 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
190 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
191 }
192
193 /**
194 * Adds all the type in the current data model node as part of the generated
195 * temporary file.
196 *
197 * @param yangTypeHolder YANG java data model node which has type info, eg union /
198 * typedef
Bharat saraswal33dfa012016-05-17 19:59:16 +0530199 * @param pluginConfig plugin configurations for naming conventions
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530200 * @throws IOException IO operation fail
201 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530202 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530203 throws IOException {
204
205 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
206 if (typeList != null) {
207 for (YangType<?> yangType : typeList) {
208 if (!(yangType instanceof YangJavaType)) {
209 throw new TranslatorException("Type does not have Java info");
210 }
211 YangJavaType<?> javaType = (YangJavaType<?>) yangType;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530212 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530213 String typeName = javaType.getDataTypeName();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530214 typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530215 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
216 javaType.getJavaQualifiedInfo(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530217 typeName, javaType,
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530218 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
219 false);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530220 addJavaSnippetInfoToApplicableTempFiles((YangNode) yangTypeHolder, javaAttributeInfo,
221 pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530222 }
223 }
224 }
225
226 /**
227 * Adds the new attribute info to the target generated temporary files for
228 * union class.
229 *
230 * @param hasType the node for which the type is being added as an attribute
231 * @param javaAttributeInfo the attribute info that needs to be added to
232 * temporary files
Bharat saraswal33dfa012016-05-17 19:59:16 +0530233 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530234 * @throws IOException IO operation fail
235 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530236 private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo,
237 YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530238 throws IOException {
239
Bharat saraswal33dfa012016-05-17 19:59:16 +0530240 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530241
242 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530243 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530244 }
245 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530246 addTypeConstructor(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530247 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530248 }
249
250 /**
251 * Adds type constructor.
252 *
253 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530254 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530255 * @throws IOException when fails to append to temporary file
256 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530257 private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530258 throws IOException {
259 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530260 getGeneratedJavaClassName(), pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530261 }
262
263 /**
264 * Adds of string for type.
265 *
266 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530267 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530268 * @throws IOException when fails to append to temporary file
269 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530270 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530271 throws IOException {
272 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530273 getGeneratedJavaClassName(), pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530274 + NEW_LINE);
275 }
276
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530277 /**
278 * Removes all temporary file handles.
279 *
280 * @param isErrorOccurred when translator fails to generate java files we
281 * need to close all open file handles include temporary files
282 * and java files.
283 * @throws IOException when failed to delete the temporary files
284 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530285 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530286 public void freeTemporaryResources(boolean isErrorOccurred)
287 throws IOException {
288 boolean isError = isErrorOccurred;
289
290 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
291 closeFile(getTypedefClassJavaFileHandle(), isError);
292 }
293
294 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
295 closeFile(getTypeClassJavaFileHandle(), isError);
296 }
297
298 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
299 closeFile(getConstructorForTypeTempFileHandle(), true);
300 }
301 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
302 closeFile(getOfStringImplTempFileHandle(), true);
303 }
304 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
305 closeFile(getFromStringImplTempFileHandle(), true);
306 }
307
308 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530309
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530310 }
311
312 /**
313 * Constructs java code exit.
314 *
315 * @param fileType generated file type
316 * @param curNode current YANG node
317 * @throws IOException when fails to generate java files
318 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530319 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530320 public void generateJavaFile(int fileType, YangNode curNode)
321 throws IOException {
322 List<String> imports = new ArrayList<>();
323 if (isAttributePresent()) {
324 imports = getJavaImportData().getImports();
325 }
326
327 createPackage(curNode);
328
329 /*
330 * Creates type def class file.
331 */
332 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
333 addImportsToStringAndHasCodeMethods(curNode, imports);
334 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
335 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
336 }
337 /*
338 * Creates type class file.
339 */
340 if ((fileType & GENERATE_UNION_CLASS) != 0) {
341 addImportsToStringAndHasCodeMethods(curNode, imports);
342 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
343 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
344 }
345
346 /*
347 * Close all the file handles.
348 */
349 freeTemporaryResources(false);
350 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530351}