blob: 326cf55b46328869a0a0b70c0de0ea127815e5d7 [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
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053031import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
33import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
34import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
36import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
37import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053038import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
39import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053040import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053041import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
42import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
43import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
44import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
45import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
46import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047
48/**
49 * Represents implementation of java data type code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053050 * Maintains the temp files required specific for user defined data type java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053051 */
52public class TempJavaTypeFragmentFiles
53 extends TempJavaFragmentFiles {
54
55 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053056 * File name for of string method.
57 */
58 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
59
60 /**
61 * File name for construction for special type like union, typedef.
62 */
63 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053064
65 /**
66 * File name for typedef class file name suffix.
67 */
68 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
69
70 /**
71 * File name for generated class file for special type like union, typedef
72 * suffix.
73 */
74 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
75
76 /**
77 * Temporary file handle for of string method of class.
78 */
79 private File ofStringImplTempFileHandle;
80 /**
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;
89 /**
90 * Java file handle for type class like union, typedef file.
91 */
92 private File typeClassJavaFileHandle;
93
94 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053095 * Creates an instance of temporary java code fragment.
96 *
97 * @param javaFileInfo generated java file info
98 * @throws IOException when fails to create new file handle
99 */
100 public TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
101 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530102
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530103 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530104
105 /*
106 * Initialize getterImpl, attributes, hash code, equals and to strings
107 * when generation file type matches to typeDef class mask.
108 */
109 addGeneratedTempFile(OF_STRING_IMPL_MASK);
110 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
111 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
112
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530113 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
114 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530115
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530116 }
117
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530118 /**
119 * Returns type class constructor method's temporary file handle.
120 *
121 * @return type class constructor method's temporary file handle
122 */
123
124 public File getConstructorForTypeTempFileHandle() {
125 return constructorForTypeTempFileHandle;
126 }
127
128 /**
129 * Sets type class constructor method's temporary file handle.
130 *
131 * @param constructorForTypeTempFileHandle type class constructor method's
132 * temporary file handle
133 */
134 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
135 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
136 }
137
138 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530139 * Returns java file handle for typedef class file.
140 *
141 * @return java file handle for typedef class file
142 */
143 File getTypedefClassJavaFileHandle() {
144 return typedefClassJavaFileHandle;
145 }
146
147 /**
148 * Sets the java file handle for typedef class file.
149 *
150 * @param typedefClassJavaFileHandle java file handle
151 */
152 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
153 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
154 }
155
156 /**
157 * Returns java file handle for type class file.
158 *
159 * @return java file handle for type class file
160 */
161 File getTypeClassJavaFileHandle() {
162 return typeClassJavaFileHandle;
163 }
164
165 /**
166 * Sets the java file handle for type class file.
167 *
168 * @param typeClassJavaFileHandle type file handle
169 */
170 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
171 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
172 }
173
174 /**
175 * Returns of string method's temporary file handle.
176 *
177 * @return of string method's temporary file handle
178 */
179
180 public File getOfStringImplTempFileHandle() {
181 return ofStringImplTempFileHandle;
182 }
183
184 /**
185 * Set of string method's temporary file handle.
186 *
187 * @param ofStringImplTempFileHandle of string method's temporary file
188 * handle
189 */
190 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
191 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
192 }
193
194 /**
195 * Adds all the type in the current data model node as part of the generated
196 * temporary file.
197 *
198 * @param yangTypeHolder YANG java data model node which has type info, eg union /
199 * typedef
Bharat saraswal33dfa012016-05-17 19:59:16 +0530200 * @param pluginConfig plugin configurations for naming conventions
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530201 * @throws IOException IO operation fail
202 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530203 public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530204 throws IOException {
205
206 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
207 if (typeList != null) {
208 for (YangType<?> yangType : typeList) {
209 if (!(yangType instanceof YangJavaType)) {
210 throw new TranslatorException("Type does not have Java info");
211 }
212 YangJavaType<?> javaType = (YangJavaType<?>) yangType;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530213 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530214 String typeName = javaType.getDataTypeName();
215
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530216 if (javaType.getDataType().equals(DERIVED)) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530217 typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530218 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530219 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
220 javaType.getJavaQualifiedInfo(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530221 typeName, javaType,
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530222 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
223 false);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530224 addJavaSnippetInfoToApplicableTempFiles((YangNode) yangTypeHolder, javaAttributeInfo,
225 pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530226 }
227 }
228 }
229
230 /**
231 * Adds the new attribute info to the target generated temporary files for
232 * union class.
233 *
234 * @param hasType the node for which the type is being added as an attribute
235 * @param javaAttributeInfo the attribute info that needs to be added to
236 * temporary files
Bharat saraswal33dfa012016-05-17 19:59:16 +0530237 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530238 * @throws IOException IO operation fail
239 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530240 private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo,
241 YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530242 throws IOException {
243
Bharat saraswal33dfa012016-05-17 19:59:16 +0530244 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530245
246 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530247 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530248 }
249 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530250 addTypeConstructor(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530251 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530252 }
253
254 /**
255 * Adds type constructor.
256 *
257 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530258 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530259 * @throws IOException when fails to append to temporary file
260 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530261 private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530262 throws IOException {
263 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530264 getGeneratedJavaClassName(), pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530265 }
266
267 /**
268 * Adds of string for type.
269 *
270 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530271 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530272 * @throws IOException when fails to append to temporary file
273 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530274 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530275 throws IOException {
276 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530277 getGeneratedJavaClassName(), pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530278 + NEW_LINE);
279 }
280
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530281 /**
282 * Removes all temporary file handles.
283 *
284 * @param isErrorOccurred when translator fails to generate java files we
285 * need to close all open file handles include temporary files
286 * and java files.
287 * @throws IOException when failed to delete the temporary files
288 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530289 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530290 public void freeTemporaryResources(boolean isErrorOccurred)
291 throws IOException {
292 boolean isError = isErrorOccurred;
293
294 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
295 closeFile(getTypedefClassJavaFileHandle(), isError);
296 }
297
298 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
299 closeFile(getTypeClassJavaFileHandle(), isError);
300 }
301
302 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
303 closeFile(getConstructorForTypeTempFileHandle(), true);
304 }
305 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
306 closeFile(getOfStringImplTempFileHandle(), true);
307 }
308 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
309 closeFile(getFromStringImplTempFileHandle(), true);
310 }
311
312 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530313
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530314 }
315
316 /**
317 * Constructs java code exit.
318 *
319 * @param fileType generated file type
320 * @param curNode current YANG node
321 * @throws IOException when fails to generate java files
322 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530323 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530324 public void generateJavaFile(int fileType, YangNode curNode)
325 throws IOException {
326 List<String> imports = new ArrayList<>();
327 if (isAttributePresent()) {
328 imports = getJavaImportData().getImports();
329 }
330
331 createPackage(curNode);
332
333 /*
334 * Creates type def class file.
335 */
336 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
337 addImportsToStringAndHasCodeMethods(curNode, imports);
338 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
339 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
340 }
341 /*
342 * Creates type class file.
343 */
344 if ((fileType & GENERATE_UNION_CLASS) != 0) {
345 addImportsToStringAndHasCodeMethods(curNode, imports);
346 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
347 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
348 }
349
350 /*
351 * Close all the file handles.
352 */
353 freeTemporaryResources(false);
354 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530355}