blob: 930b6b4c697368b16aa60a2aaca9067adc50def1 [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;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053079
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053080 /**
81 * Temporary file handle for constructor for type class.
82 */
83 private File constructorForTypeTempFileHandle;
84
85 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053086 * Java file handle for typedef class file.
87 */
88 private File typedefClassJavaFileHandle;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053089
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053090 /**
91 * Java file handle for type class like union, typedef file.
92 */
93 private File typeClassJavaFileHandle;
94
95 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053096 * Creates an instance of temporary java code fragment.
97 *
98 * @param javaFileInfo generated java file info
99 * @throws IOException when fails to create new file handle
100 */
101 public TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
102 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530103
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530104 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530105
106 /*
107 * Initialize getterImpl, attributes, hash code, equals and to strings
108 * when generation file type matches to typeDef class mask.
109 */
110 addGeneratedTempFile(OF_STRING_IMPL_MASK);
111 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
112 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
113
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530114 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
115 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530116
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530117 }
118
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530119 /**
120 * Returns type class constructor method's temporary file handle.
121 *
122 * @return type class constructor method's temporary file handle
123 */
124
125 public File getConstructorForTypeTempFileHandle() {
126 return constructorForTypeTempFileHandle;
127 }
128
129 /**
130 * Sets type class constructor method's temporary file handle.
131 *
132 * @param constructorForTypeTempFileHandle type class constructor method's
133 * temporary file handle
134 */
135 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
136 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
137 }
138
139 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530140 * Returns java file handle for typedef class file.
141 *
142 * @return java file handle for typedef class file
143 */
144 File getTypedefClassJavaFileHandle() {
145 return typedefClassJavaFileHandle;
146 }
147
148 /**
149 * Sets the java file handle for typedef class file.
150 *
151 * @param typedefClassJavaFileHandle java file handle
152 */
153 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
154 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
155 }
156
157 /**
158 * Returns java file handle for type class file.
159 *
160 * @return java file handle for type class file
161 */
162 File getTypeClassJavaFileHandle() {
163 return typeClassJavaFileHandle;
164 }
165
166 /**
167 * Sets the java file handle for type class file.
168 *
169 * @param typeClassJavaFileHandle type file handle
170 */
171 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
172 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
173 }
174
175 /**
176 * Returns of string method's temporary file handle.
177 *
178 * @return of string method's temporary file handle
179 */
180
181 public File getOfStringImplTempFileHandle() {
182 return ofStringImplTempFileHandle;
183 }
184
185 /**
186 * Set of string method's temporary file handle.
187 *
188 * @param ofStringImplTempFileHandle of string method's temporary file
189 * handle
190 */
191 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
192 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
193 }
194
195 /**
196 * Adds all the type in the current data model node as part of the generated
197 * temporary file.
198 *
199 * @param yangTypeHolder YANG java data model node which has type info, eg union /
200 * typedef
Bharat saraswal33dfa012016-05-17 19:59:16 +0530201 * @param pluginConfig plugin configurations for naming conventions
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530202 * @throws IOException IO operation fail
203 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530204 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530205 throws IOException {
206
207 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
208 if (typeList != null) {
209 for (YangType<?> yangType : typeList) {
210 if (!(yangType instanceof YangJavaType)) {
211 throw new TranslatorException("Type does not have Java info");
212 }
213 YangJavaType<?> javaType = (YangJavaType<?>) yangType;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530214 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530215 String typeName = javaType.getDataTypeName();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530216 typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530217 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
218 javaType.getJavaQualifiedInfo(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530219 typeName, javaType,
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530220 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
221 false);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530222 addJavaSnippetInfoToApplicableTempFiles((YangNode) yangTypeHolder, javaAttributeInfo,
223 pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530224 }
225 }
226 }
227
228 /**
229 * Adds the new attribute info to the target generated temporary files for
230 * union class.
231 *
232 * @param hasType the node for which the type is being added as an attribute
233 * @param javaAttributeInfo the attribute info that needs to be added to
234 * temporary files
Bharat saraswal33dfa012016-05-17 19:59:16 +0530235 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530236 * @throws IOException IO operation fail
237 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530238 private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo,
239 YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530240 throws IOException {
241
Bharat saraswal33dfa012016-05-17 19:59:16 +0530242 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530243
244 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530245 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530246 }
247 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530248 addTypeConstructor(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530249 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530250 }
251
252 /**
253 * Adds type constructor.
254 *
255 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530256 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530257 * @throws IOException when fails to append to temporary file
258 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530259 private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530260 throws IOException {
261 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530262 getGeneratedJavaClassName(), pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530263 }
264
265 /**
266 * Adds of string for type.
267 *
268 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530269 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530270 * @throws IOException when fails to append to temporary file
271 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530272 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530273 throws IOException {
274 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530275 getGeneratedJavaClassName(), pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530276 + NEW_LINE);
277 }
278
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530279 /**
280 * Removes all temporary file handles.
281 *
282 * @param isErrorOccurred when translator fails to generate java files we
283 * need to close all open file handles include temporary files
284 * and java files.
285 * @throws IOException when failed to delete the temporary files
286 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530287 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530288 public void freeTemporaryResources(boolean isErrorOccurred)
289 throws IOException {
290 boolean isError = isErrorOccurred;
291
292 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
293 closeFile(getTypedefClassJavaFileHandle(), isError);
294 }
295
296 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
297 closeFile(getTypeClassJavaFileHandle(), isError);
298 }
299
300 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
301 closeFile(getConstructorForTypeTempFileHandle(), true);
302 }
303 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
304 closeFile(getOfStringImplTempFileHandle(), true);
305 }
306 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
307 closeFile(getFromStringImplTempFileHandle(), true);
308 }
309
310 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530311
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530312 }
313
314 /**
315 * Constructs java code exit.
316 *
317 * @param fileType generated file type
318 * @param curNode current YANG node
319 * @throws IOException when fails to generate java files
320 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530321 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530322 public void generateJavaFile(int fileType, YangNode curNode)
323 throws IOException {
324 List<String> imports = new ArrayList<>();
325 if (isAttributePresent()) {
326 imports = getJavaImportData().getImports();
327 }
328
329 createPackage(curNode);
330
331 /*
332 * Creates type def class file.
333 */
334 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
335 addImportsToStringAndHasCodeMethods(curNode, imports);
336 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
337 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
338 }
339 /*
340 * Creates type class file.
341 */
342 if ((fileType & GENERATE_UNION_CLASS) != 0) {
343 addImportsToStringAndHasCodeMethods(curNode, imports);
344 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
345 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
346 }
347
348 /*
349 * Close all the file handles.
350 */
351 freeTemporaryResources(false);
352 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530353}