blob: 8af0ecd5c20b0689477d1b4ea0d243bcf53c23d8 [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;
29
30import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
32import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
33import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
34import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
35import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
36import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
37import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
38import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
39import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
40import 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";
63 /**
64 * File name for from string method.
65 */
66 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
67
68 /**
69 * File name for typedef class file name suffix.
70 */
71 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
72
73 /**
74 * File name for generated class file for special type like union, typedef
75 * suffix.
76 */
77 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
78
79 /**
80 * Temporary file handle for of string method of class.
81 */
82 private File ofStringImplTempFileHandle;
83 /**
84 * Temporary file handle for constructor for type class.
85 */
86 private File constructorForTypeTempFileHandle;
87
88 /**
89 * Temporary file handle for from string method of class.
90 */
91 private File fromStringImplTempFileHandle;
92
93 /**
94 * Java file handle for typedef class file.
95 */
96 private File typedefClassJavaFileHandle;
97 /**
98 * Java file handle for type class like union, typedef file.
99 */
100 private File typeClassJavaFileHandle;
101
102 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530103 * Creates an instance of temporary java code fragment.
104 *
105 * @param javaFileInfo generated java file info
106 * @throws IOException when fails to create new file handle
107 */
108 public TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
109 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530110
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530111 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530112
113 /*
114 * Initialize getterImpl, attributes, hash code, equals and to strings
115 * when generation file type matches to typeDef class mask.
116 */
117 addGeneratedTempFile(OF_STRING_IMPL_MASK);
118 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
119 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
120
121
122 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
123 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
124 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
125
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530126 }
127
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530128 /**
129 * Returns type class constructor method's temporary file handle.
130 *
131 * @return type class constructor method's temporary file handle
132 */
133
134 public File getConstructorForTypeTempFileHandle() {
135 return constructorForTypeTempFileHandle;
136 }
137
138 /**
139 * Sets type class constructor method's temporary file handle.
140 *
141 * @param constructorForTypeTempFileHandle type class constructor method's
142 * temporary file handle
143 */
144 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
145 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
146 }
147
148 /**
149 * Returns from string method's temporary file handle.
150 *
151 * @return from string method's temporary file handle
152 */
153 public File getFromStringImplTempFileHandle() {
154 return fromStringImplTempFileHandle;
155 }
156
157 /**
158 * Sets from string method's temporary file handle.
159 *
160 * @param fromStringImplTempFileHandle from string method's temporary file
161 * handle
162 */
163 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
164 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
165 }
166
167 /**
168 * Returns java file handle for typedef class file.
169 *
170 * @return java file handle for typedef class file
171 */
172 File getTypedefClassJavaFileHandle() {
173 return typedefClassJavaFileHandle;
174 }
175
176 /**
177 * Sets the java file handle for typedef class file.
178 *
179 * @param typedefClassJavaFileHandle java file handle
180 */
181 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
182 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
183 }
184
185 /**
186 * Returns java file handle for type class file.
187 *
188 * @return java file handle for type class file
189 */
190 File getTypeClassJavaFileHandle() {
191 return typeClassJavaFileHandle;
192 }
193
194 /**
195 * Sets the java file handle for type class file.
196 *
197 * @param typeClassJavaFileHandle type file handle
198 */
199 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
200 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
201 }
202
203 /**
204 * Returns of string method's temporary file handle.
205 *
206 * @return of string method's temporary file handle
207 */
208
209 public File getOfStringImplTempFileHandle() {
210 return ofStringImplTempFileHandle;
211 }
212
213 /**
214 * Set of string method's temporary file handle.
215 *
216 * @param ofStringImplTempFileHandle of string method's temporary file
217 * handle
218 */
219 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
220 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
221 }
222
223 /**
224 * Adds all the type in the current data model node as part of the generated
225 * temporary file.
226 *
227 * @param yangTypeHolder YANG java data model node which has type info, eg union /
228 * typedef
229 * @throws IOException IO operation fail
230 */
231 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder)
232 throws IOException {
233
234 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
235 if (typeList != null) {
236 for (YangType<?> yangType : typeList) {
237 if (!(yangType instanceof YangJavaType)) {
238 throw new TranslatorException("Type does not have Java info");
239 }
240 YangJavaType<?> javaType = (YangJavaType<?>) yangType;
241 javaType.updateJavaQualifiedInfo();
242 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
243 javaType.getJavaQualifiedInfo(),
244 javaType.getDataTypeName(), javaType,
245 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
246 false);
247 addJavaSnippetInfoToApplicableTempFiles((YangNode) yangTypeHolder, javaAttributeInfo);
248 }
249 }
250 }
251
252 /**
253 * Adds the new attribute info to the target generated temporary files for
254 * union class.
255 *
256 * @param hasType the node for which the type is being added as an attribute
257 * @param javaAttributeInfo the attribute info that needs to be added to
258 * temporary files
259 * @throws IOException IO operation fail
260 */
261 private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo)
262 throws IOException {
263
264 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
265
266 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
267 addOfStringMethod(javaAttributeInfo);
268 }
269 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
270 addTypeConstructor(javaAttributeInfo);
271 }
272
273 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(javaAttributeInfo);
274 /*
275 * Create a new java attribute info with qualified information of
276 * wrapper classes.
277 */
278 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
279 javaAttributeInfo.getAttributeName(),
280 javaAttributeInfo.getAttributeType(),
281 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
282 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
283 addFromStringMethod(javaAttributeInfo, fromStringAttributeInfo);
284 }
285 }
286
287
288 /**
289 * Adds from string method for union class.
290 *
291 * @param javaAttributeInfo type attribute info
292 * @param fromStringAttributeInfo from string attribute info
293 * @throws IOException when fails to append to temporary file
294 */
295 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
296 JavaAttributeInfo fromStringAttributeInfo)
297 throws IOException {
298 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
299 fromStringAttributeInfo) + NEW_LINE);
300 }
301
302 /**
303 * Adds type constructor.
304 *
305 * @param attr attribute info
306 * @throws IOException when fails to append to temporary file
307 */
308 private void addTypeConstructor(JavaAttributeInfo attr)
309 throws IOException {
310 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
311 getGeneratedJavaClassName()) + NEW_LINE);
312 }
313
314 /**
315 * Adds of string for type.
316 *
317 * @param attr attribute info
318 * @throws IOException when fails to append to temporary file
319 */
320 private void addOfStringMethod(JavaAttributeInfo attr)
321 throws IOException {
322 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
323 getGeneratedJavaClassName())
324 + NEW_LINE);
325 }
326
327
328 /**
329 * Removes all temporary file handles.
330 *
331 * @param isErrorOccurred when translator fails to generate java files we
332 * need to close all open file handles include temporary files
333 * and java files.
334 * @throws IOException when failed to delete the temporary files
335 */
336 public void freeTemporaryResources(boolean isErrorOccurred)
337 throws IOException {
338 boolean isError = isErrorOccurred;
339
340 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
341 closeFile(getTypedefClassJavaFileHandle(), isError);
342 }
343
344 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
345 closeFile(getTypeClassJavaFileHandle(), isError);
346 }
347
348 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
349 closeFile(getConstructorForTypeTempFileHandle(), true);
350 }
351 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
352 closeFile(getOfStringImplTempFileHandle(), true);
353 }
354 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
355 closeFile(getFromStringImplTempFileHandle(), true);
356 }
357
358 super.freeTemporaryResources(isErrorOccurred);
359 }
360
361 /**
362 * Constructs java code exit.
363 *
364 * @param fileType generated file type
365 * @param curNode current YANG node
366 * @throws IOException when fails to generate java files
367 */
368 public void generateJavaFile(int fileType, YangNode curNode)
369 throws IOException {
370 List<String> imports = new ArrayList<>();
371 if (isAttributePresent()) {
372 imports = getJavaImportData().getImports();
373 }
374
375 createPackage(curNode);
376
377 /*
378 * Creates type def class file.
379 */
380 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
381 addImportsToStringAndHasCodeMethods(curNode, imports);
382 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
383 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
384 }
385 /*
386 * Creates type class file.
387 */
388 if ((fileType & GENERATE_UNION_CLASS) != 0) {
389 addImportsToStringAndHasCodeMethods(curNode, imports);
390 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
391 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
392 }
393
394 /*
395 * Close all the file handles.
396 */
397 freeTemporaryResources(false);
398 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530399}