blob: d9a44d6fd9214658bbe7e6306fd6a0c74c65019b [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
Bharat saraswalc0e04842016-05-12 13:16:57 +053024import org.onosproject.yangutils.datamodel.YangDataTypes;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053025import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.datamodel.YangType;
27import org.onosproject.yangutils.datamodel.YangTypeHolder;
28import org.onosproject.yangutils.translator.exception.TranslatorException;
29import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaType;
30
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
199 * @throws IOException IO operation fail
200 */
201 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder)
202 throws IOException {
203
204 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
205 if (typeList != null) {
206 for (YangType<?> yangType : typeList) {
207 if (!(yangType instanceof YangJavaType)) {
208 throw new TranslatorException("Type does not have Java info");
209 }
210 YangJavaType<?> javaType = (YangJavaType<?>) yangType;
211 javaType.updateJavaQualifiedInfo();
Bharat saraswalc0e04842016-05-12 13:16:57 +0530212 String typeName = javaType.getDataTypeName();
213
214 if (javaType.getDataType().equals(YangDataTypes.DERIVED)) {
215 typeName = getCamelCase(typeName, null);
216 }
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);
222 addJavaSnippetInfoToApplicableTempFiles((YangNode) yangTypeHolder, javaAttributeInfo);
223 }
224 }
225 }
226
227 /**
228 * Adds the new attribute info to the target generated temporary files for
229 * union class.
230 *
231 * @param hasType the node for which the type is being added as an attribute
232 * @param javaAttributeInfo the attribute info that needs to be added to
233 * temporary files
234 * @throws IOException IO operation fail
235 */
236 private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo)
237 throws IOException {
238
239 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
240
241 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
242 addOfStringMethod(javaAttributeInfo);
243 }
244 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
245 addTypeConstructor(javaAttributeInfo);
246 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530247 }
248
249 /**
250 * Adds type constructor.
251 *
252 * @param attr attribute info
253 * @throws IOException when fails to append to temporary file
254 */
255 private void addTypeConstructor(JavaAttributeInfo attr)
256 throws IOException {
257 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
258 getGeneratedJavaClassName()) + NEW_LINE);
259 }
260
261 /**
262 * Adds of string for type.
263 *
264 * @param attr attribute info
265 * @throws IOException when fails to append to temporary file
266 */
267 private void addOfStringMethod(JavaAttributeInfo attr)
268 throws IOException {
269 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
270 getGeneratedJavaClassName())
271 + NEW_LINE);
272 }
273
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530274 /**
275 * Removes all temporary file handles.
276 *
277 * @param isErrorOccurred when translator fails to generate java files we
278 * need to close all open file handles include temporary files
279 * and java files.
280 * @throws IOException when failed to delete the temporary files
281 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530282 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530283 public void freeTemporaryResources(boolean isErrorOccurred)
284 throws IOException {
285 boolean isError = isErrorOccurred;
286
287 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
288 closeFile(getTypedefClassJavaFileHandle(), isError);
289 }
290
291 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
292 closeFile(getTypeClassJavaFileHandle(), isError);
293 }
294
295 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
296 closeFile(getConstructorForTypeTempFileHandle(), true);
297 }
298 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
299 closeFile(getOfStringImplTempFileHandle(), true);
300 }
301 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
302 closeFile(getFromStringImplTempFileHandle(), true);
303 }
304
305 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530306
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530307 }
308
309 /**
310 * Constructs java code exit.
311 *
312 * @param fileType generated file type
313 * @param curNode current YANG node
314 * @throws IOException when fails to generate java files
315 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530316 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530317 public void generateJavaFile(int fileType, YangNode curNode)
318 throws IOException {
319 List<String> imports = new ArrayList<>();
320 if (isAttributePresent()) {
321 imports = getJavaImportData().getImports();
322 }
323
324 createPackage(curNode);
325
326 /*
327 * Creates type def class file.
328 */
329 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
330 addImportsToStringAndHasCodeMethods(curNode, imports);
331 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
332 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
333 }
334 /*
335 * Creates type class file.
336 */
337 if ((fileType & GENERATE_UNION_CLASS) != 0) {
338 addImportsToStringAndHasCodeMethods(curNode, imports);
339 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
340 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
341 }
342
343 /*
344 * Close all the file handles.
345 */
346 freeTemporaryResources(false);
347 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530348}