blob: d4bb8881df6dc10678106d63254f4749035d1239 [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 */
16package org.onosproject.yangutils.translator.tojava;
17
18import java.io.File;
19import java.io.IOException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020import java.util.List;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053021
Bharat saraswalb551aae2016-07-14 15:18:20 +053022import org.onosproject.yangutils.datamodel.YangAugmentableNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053023import org.onosproject.yangutils.datamodel.YangCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053024import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangLeavesHolder;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053027import org.onosproject.yangutils.datamodel.YangList;
Bharat saraswalb551aae2016-07-14 15:18:20 +053028import org.onosproject.yangutils.datamodel.YangModule;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalb551aae2016-07-14 15:18:20 +053030import org.onosproject.yangutils.datamodel.YangSubModule;
31import org.onosproject.yangutils.datamodel.YangUses;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
33import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment;
Bharat saraswalcad0e652016-05-26 23:48:38 +053035import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping;
Bharat saraswalcad0e652016-05-26 23:48:38 +053036import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
Bharat saraswalcad0e652016-05-26 23:48:38 +053037import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053038import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053039import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053040
Bharat saraswal96dfef02016-06-16 00:29:12 +053041import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
43import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
44import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Bharat saraswalb551aae2016-07-14 15:18:20 +053045import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
47import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
49import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053050import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.OPERATION_BUILDER_CLASS_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053051import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053052import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.AUGMENTE_CLASS_CONSTRUCTOR_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswalc0e04842016-05-12 13:16:57 +053054import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053055import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
56import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
57import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053058import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
59import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
60import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
61import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswalc0e04842016-05-12 13:16:57 +053062import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053063import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053064import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053065import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
66import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswalb551aae2016-07-14 15:18:20 +053067import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.sortImports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
69import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053070import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
71import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Bharat saraswalb551aae2016-07-14 15:18:20 +053072import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.addResolvedAugmentedDataNodeImports;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053073import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateOpParamBuilderClassFile;
74import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateOpParamImplClassFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053076import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
77import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAugmentedConstructor;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053078import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
80import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswalc0e04842016-05-12 13:16:57 +053081import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053082import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
83import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
84import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
87import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
88import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053089import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053090import static org.onosproject.yangutils.utils.UtilConstants.ACTIVATE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053091import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053092import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT;
93import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053094import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053095import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
96import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
97import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
98import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
99import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530100import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530102import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530103import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE;
104import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE_CARDINALITY;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530105import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530106import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530108import static org.onosproject.yangutils.utils.UtilConstants.OPERATION;
109import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
110import static org.onosproject.yangutils.utils.UtilConstants.PROTECTED;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530111import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530112import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
113import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530114import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530116import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
117import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
118import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530119import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
120import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Bharat saraswal96dfef02016-06-16 00:29:12 +0530121import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530122
123/**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530124 * Represents implementation of java code fragments temporary implementations. Manages the common temp file required for
125 * Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530126 */
127public class TempJavaFragmentFiles {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530128
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530129 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530130 * File type extension for java classes.
131 */
132 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530133
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530134 /**
135 * File type extension for temporary classes.
136 */
137 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530138
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530139 /**
140 * Folder suffix for temporary files folder.
141 */
142 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530143
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530144 /**
145 * File name for getter method.
146 */
147 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530148
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530149 /**
150 * File name for setter method.
151 */
152 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530153
154 /**
155 * File name for getter method implementation.
156 */
157 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
158
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530159 /**
160 * File name for setter method implementation.
161 */
162 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530163
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530164 /**
165 * File name for attributes.
166 */
167 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530168
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530169 /**
170 * File name for to string method.
171 */
172 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530173
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530174 /**
175 * File name for hash code method.
176 */
177 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530178
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530179 /**
180 * File name for equals method.
181 */
182 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530184 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530185 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530186 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530187 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530188
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530189 /**
190 * File name for interface java file name suffix.
191 */
192 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530193
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530194 /**
195 * File name for builder interface file name suffix.
196 */
197 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530198
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530199 /**
200 * File name for builder class file name suffix.
201 */
202 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530203
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530204 /**
205 * File name for impl class file name suffix.
206 */
207 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530208
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530209 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530210 * File name for augment copy constructor method.
211 */
212 private static final String AUGMENT_CONSTRUCTOR_FILE_NAME = "AugmentConstructor";
Bharat saraswalb551aae2016-07-14 15:18:20 +0530213
214 /**
215 * Information about the java files being generated.
216 */
217 private JavaFileInfo javaFileInfo;
218
219 /**
220 * Imported class info.
221 */
222 private JavaImportData javaImportData;
223
224 /**
225 * The variable which guides the types of temporary files generated using the temporary generated file types mask.
226 */
227 private int generatedTempFiles;
228
229 /**
230 * Absolute path where the target java file needs to be generated.
231 */
232 private String absoluteDirPath;
233
234 /**
235 * Contains all the interface(s)/class name which will be extended by generated files.
236 */
237 private JavaExtendsListHolder javaExtendsListHolder;
238
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530239 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 * Java file handle for interface file.
241 */
242 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530243
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530244 /**
245 * Java file handle for builder interface file.
246 */
247 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530248
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530249 /**
250 * Java file handle for builder class file.
251 */
252 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530253
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530254 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530255 * Retrieves the temporary file handle of op param builder class.
256 *
257 * @return op param builder temporary file handle
258 */
259 public File getBuilderOpParmClassJavaFileHandle() {
260 return builderOpParmClassJavaFileHandle;
261 }
262
263 /**
264 * Sets the java file handle for op param builder class.
265 *
266 * @param builderOpParmClassJavaFileHandle java file handle
267 */
268 public void setBuilderOpParmClassJavaFileHandle(File builderOpParmClassJavaFileHandle) {
269 this.builderOpParmClassJavaFileHandle = builderOpParmClassJavaFileHandle;
270 }
271
272 /**
273 * Java file handle for builder op param class file.
274 */
275 private File builderOpParmClassJavaFileHandle;
276
277 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530278 * Java file handle for impl class file.
279 */
280 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530281
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530282 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530283 * Java file handle for op param class file.
284 */
285 private File opParamClassJavaFileHandle;
286
287 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530288 * Temporary file handle for attribute.
289 */
290 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530291
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530292 /**
293 * Temporary file handle for getter of interface.
294 */
295 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530296
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530297 /**
298 * Temporary file handle for setter of interface.
299 */
300 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530301
302 /**
303 * Temporary file handle for getter of class.
304 */
305 private File getterImplTempFileHandle;
306
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530307 /**
308 * Temporary file handle for setter of class.
309 */
310 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530311
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530312 /**
313 * Temporary file handle for hash code method of class.
314 */
315 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530316
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530317 /**
318 * Temporary file handle for equals method of class.
319 */
320 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530321
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530322 /**
323 * Temporary file handle for to string method of class.
324 */
325 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530326
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530327 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530328 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530329 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530330 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530331
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530332 /**
333 * Import info for case.
334 */
335 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530336
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530337 /**
338 * Is attribute added.
339 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530340 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530341
Bharat saraswalc0e04842016-05-12 13:16:57 +0530342 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530343 * Temporary file handle for augments copy constructor method of class.
344 */
345 private File augmentConstructorImplTempFileHandle;
346
Bharat saraswalb551aae2016-07-14 15:18:20 +0530347 TempJavaFragmentFiles() {
348 }
349
350 /**
351 * Creates an instance of temporary java code fragment.
352 *
353 * @param javaFileInfo generated java file information
354 * @throws IOException when fails to create new file handle
355 */
356 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
357 throws IOException {
358 setJavaExtendsListHolder(new JavaExtendsListHolder());
359 setJavaImportData(new JavaImportData());
360 setJavaFileInfo(javaFileInfo);
361 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
362 getJavaFileInfo().getPackageFilePath()));
363
364 /*
365 * Initialize getter when generation file type matches to interface
366 * mask.
367 */
368 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
369 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
370 }
371
372 /*
373 * Initialize getter and setter when generation file type matches to
374 * builder interface mask.
375 */
376 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
377 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
378 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
379 }
380
381 /*
382 * Initialize getterImpl, setterImpl and attributes when generation file
383 * type matches to builder class mask.
384 */
385 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
386 addGeneratedTempFile(ATTRIBUTES_MASK);
387 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
388 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
389 addGeneratedTempFile(AUGMENTE_CLASS_CONSTRUCTOR_MASK);
390 }
391
392 /*
393 * Initialize getterImpl, attributes, constructor, hash code, equals and
394 * to strings when generation file type matches to impl class mask.
395 */
396 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
397 addGeneratedTempFile(ATTRIBUTES_MASK);
398 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
399 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
400 addGeneratedTempFile(EQUALS_IMPL_MASK);
401 addGeneratedTempFile(TO_STRING_IMPL_MASK);
402 }
403
404 /*
405 * Initialize temp files to generate type class.
406 */
407 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
408 addGeneratedTempFile(ATTRIBUTES_MASK);
409 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
410 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
411 addGeneratedTempFile(EQUALS_IMPL_MASK);
412 addGeneratedTempFile(TO_STRING_IMPL_MASK);
413 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
414 }
415
416 /*
417 * Initialize temp files to generate enum class.
418 */
419 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
420 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
421 }
422
423 /*
424 * Set temporary file handles.
425 */
426 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
427 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
428 }
429
430 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
431 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
432 }
433
434 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
435 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
436 }
437
438 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
439 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
440 }
441
442 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
443 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
444 }
445
446 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
447 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
448 }
449 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
450 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
451 }
452 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
453 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
454 }
455 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
456 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
457 }
458 if ((getGeneratedTempFiles() & AUGMENTE_CLASS_CONSTRUCTOR_MASK) != 0) {
459 setAugmentConstructorImplTempFileHandle(getTemporaryFileHandle(AUGMENT_CONSTRUCTOR_FILE_NAME));
460 }
461
462 }
463
464 /**
465 * Adds current node info as and attribute to the parent generated file.
466 *
467 * @param curNode current node which needs to be added as an attribute in the parent generated code
468 * @param isList is list construct
469 * @param pluginConfig plugin configurations
470 * @throws IOException IO operation exception
471 */
472 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
473 boolean isList, YangPluginConfig pluginConfig)
474 throws IOException {
475 YangNode parent = getParentNodeInGenCode(curNode);
476 if (!(parent instanceof JavaCodeGenerator)) {
477 throw new TranslatorException("missing parent node to contain current node info in generated file");
478 }
479
480 if (parent instanceof YangJavaGrouping) {
481 /*
482 * In case of grouping, there is no need to add the information, it
483 * will be taken care in uses
484 */
485 return;
486 }
487 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) parent)
488 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
489
490 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
491 parent, isList, tempJavaBeanFragmentFiles);
492 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
493 throw new TranslatorException("missing parent temp file handle");
494 }
495 getNodesInterfaceFragmentFiles(parent, javaAttributeInfo, pluginConfig);
496 }
497
498 /**
499 * Creates an attribute info object corresponding to a data model node and return it.
500 *
501 * @param curNode current data model node for which the java code generation is being handled
502 * @param targetNode target node in which the current node is an attribute
503 * @param isListNode is the current added attribute needs to be a list
504 * @param tempJavaFragmentFiles temp java fragment files
505 * @return AttributeInfo attribute details required to add in temporary files
506 */
507 public static JavaAttributeInfo getCurNodeAsAttributeInTarget(YangNode curNode,
508 YangNode targetNode, boolean isListNode,
509 TempJavaFragmentFiles tempJavaFragmentFiles) {
510 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
511 if (curNodeName == null) {
512 updateJavaFileInfo(curNode, null);
513 curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
514 }
515
516 /*
517 * Get the import info corresponding to the attribute for import in
518 * generated java files or qualified access
519 */
520 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
521 getCapitalCase(curNodeName));
522 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
523 throw new TranslatorException("Parent node does not have file info");
524 }
525 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
526 JavaFileInfo fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
527
528 boolean isQualified;
529 if ((targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule)
530 && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE)
531 || qualifiedTypeInfo.getClassInfo().contentEquals(COMPONENT)
532 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(ACTIVATE))
533 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(DEACTIVATE))
534 || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE_CARDINALITY)
535 || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE))
536 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))
537 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + MANAGER))) {
538
539 isQualified = true;
540 } else {
541 String className;
542 if (targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule) {
543 className = getCapitalCase(fileInfo.getJavaName()) + "Service";
544 } else {
545 className = getCapitalCase(fileInfo.getJavaName());
546 }
547
548 isQualified = parentImportData.addImportInfo(qualifiedTypeInfo,
549 className, fileInfo.getPackage());
550 }
551
552 if (isListNode) {
553 parentImportData.setIfListImported(true);
554 }
555
556 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
557 }
558
559 /**
560 * Adds java code snippet for corresponding files.
561 *
562 * @param node java node
563 * @param attr attribute info
564 * @param config plugin configurations
565 * @throws IOException when fails to do IO operations
566 */
567 public static void getNodesInterfaceFragmentFiles(YangNode node, JavaAttributeInfo attr,
568 YangPluginConfig config) throws IOException {
569 TempJavaFragmentFiles tempJavaFragmentFiles;
570 JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) node).getJavaFileInfo();
571 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
572 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
573 .getTempJavaCodeFragmentFiles()
574 .getServiceTempFiles();
575 tempJavaFragmentFiles.addJavaSnippetInfoToApplicableTempFiles(attr, config);
576 }
577 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
578 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
579 .getTempJavaCodeFragmentFiles()
580 .getBeanTempFiles();
581 tempJavaFragmentFiles.addJavaSnippetInfoToApplicableTempFiles(attr, config);
582 }
583
584 }
585
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530586 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530587 * Retrieves the absolute path where the file needs to be generated.
588 *
589 * @return absolute path where the file needs to be generated
590 */
591 private String getAbsoluteDirPath() {
592 return absoluteDirPath;
593 }
594
595 /**
596 * Sets absolute path where the file needs to be generated.
597 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530598 * @param absoluteDirPath absolute path where the file needs to be generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530599 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530600 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530601 this.absoluteDirPath = absoluteDirPath;
602 }
603
604 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530605 * Retrieves the generated java file information.
606 *
607 * @return generated java file information
608 */
609 public JavaFileInfo getJavaFileInfo() {
610 return javaFileInfo;
611 }
612
613 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530614 * Sets the generated java file information.
615 *
616 * @param javaFileInfo generated java file information
617 */
618 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
619 this.javaFileInfo = javaFileInfo;
620 }
621
622 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530623 * Retrieves the generated temp files.
624 *
625 * @return generated temp files
626 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530627 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530628 return generatedTempFiles;
629 }
630
631 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530632 * Sets generated file files.
633 *
634 * @param fileType generated file type
635 */
636 void setGeneratedTempFiles(int fileType) {
637 generatedTempFiles = fileType;
638 }
639
640 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530641 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530642 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530643 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530644 generatedTempFiles = 0;
645 }
646
647 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530648 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530649 *
650 * @param generatedTempFile generated file
651 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530652 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530653 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530654 setGeneratedTempFiles(generatedTempFiles);
655 }
656
657 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530658 * Retrieves the generated Java files.
659 *
660 * @return generated Java files
661 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530662 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530663 return getJavaFileInfo().getGeneratedFileTypes();
664 }
665
666 /**
667 * Retrieves the mapped Java class name.
668 *
669 * @return mapped Java class name
670 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530671 String getGeneratedJavaClassName() {
janani b4a6711a2016-05-17 13:12:22 +0530672 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530673 }
674
675 /**
676 * Retrieves the import data for the generated Java file.
677 *
678 * @return import data for the generated Java file
679 */
680 public JavaImportData getJavaImportData() {
681 return javaImportData;
682 }
683
684 /**
685 * Sets import data for the generated Java file.
686 *
687 * @param javaImportData import data for the generated Java file
688 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530689 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530690 this.javaImportData = javaImportData;
691 }
692
693 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530694 * Retrieves the status of any attributes added.
695 *
696 * @return status of any attributes added
697 */
698 public boolean isAttributePresent() {
699 return isAttributePresent;
700 }
701
702 /**
703 * Sets status of any attributes added.
704 *
705 * @param attributePresent status of any attributes added
706 */
707 public void setAttributePresent(boolean attributePresent) {
708 isAttributePresent = attributePresent;
709 }
710
711 /**
712 * Returns getter methods's temporary file handle.
713 *
714 * @return temporary file handle
715 */
716 public File getGetterInterfaceTempFileHandle() {
717 return getterInterfaceTempFileHandle;
718 }
719
720 /**
721 * Sets to getter method's temporary file handle.
722 *
723 * @param getterForInterface file handle for to getter method
724 */
725 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
726 getterInterfaceTempFileHandle = getterForInterface;
727 }
728
729 /**
730 * Returns setter method's temporary file handle.
731 *
732 * @return temporary file handle
733 */
734 public File getSetterInterfaceTempFileHandle() {
735 return setterInterfaceTempFileHandle;
736 }
737
738 /**
739 * Sets to setter method's temporary file handle.
740 *
741 * @param setterForInterface file handle for to setter method
742 */
743 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
744 setterInterfaceTempFileHandle = setterForInterface;
745 }
746
747 /**
748 * Returns setter method's impl's temporary file handle.
749 *
750 * @return temporary file handle
751 */
752 public File getSetterImplTempFileHandle() {
753 return setterImplTempFileHandle;
754 }
755
756 /**
757 * Sets to setter method's impl's temporary file handle.
758 *
759 * @param setterImpl file handle for to setter method's implementation class
760 */
761 private void setSetterImplTempFileHandle(File setterImpl) {
762 setterImplTempFileHandle = setterImpl;
763 }
764
765 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530766 * Returns from string method's temporary file handle.
767 *
768 * @return from string method's temporary file handle
769 */
770 public File getFromStringImplTempFileHandle() {
771 return fromStringImplTempFileHandle;
772 }
773
774 /**
775 * Sets from string method's temporary file handle.
776 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530777 * @param fromStringImplTempFileHandle from string method's temporary file handle
Bharat saraswalc0e04842016-05-12 13:16:57 +0530778 */
779 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
780 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
781 }
782
783 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530784 * Returns java file handle for interface file.
785 *
786 * @return java file handle for interface file
787 */
788 private File getInterfaceJavaFileHandle() {
789 return interfaceJavaFileHandle;
790 }
791
792 /**
793 * Sets the java file handle for interface file.
794 *
795 * @param interfaceJavaFileHandle java file handle
796 */
797 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
798 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
799 }
800
801 /**
802 * Returns java file handle for builder interface file.
803 *
804 * @return java file handle for builder interface file
805 */
806 private File getBuilderInterfaceJavaFileHandle() {
807 return builderInterfaceJavaFileHandle;
808 }
809
810 /**
811 * Sets the java file handle for builder interface file.
812 *
813 * @param builderInterfaceJavaFileHandle java file handle
814 */
815 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
816 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
817 }
818
819 /**
820 * Returns java file handle for builder class file.
821 *
822 * @return java file handle for builder class file
823 */
824 private File getBuilderClassJavaFileHandle() {
825 return builderClassJavaFileHandle;
826 }
827
828 /**
829 * Sets the java file handle for builder class file.
830 *
831 * @param builderClassJavaFileHandle java file handle
832 */
833 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
834 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
835 }
836
837 /**
838 * Returns java file handle for impl class file.
839 *
840 * @return java file handle for impl class file
841 */
842 private File getImplClassJavaFileHandle() {
843 return implClassJavaFileHandle;
844 }
845
846 /**
847 * Sets the java file handle for impl class file.
848 *
849 * @param implClassJavaFileHandle java file handle
850 */
851 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
852 this.implClassJavaFileHandle = implClassJavaFileHandle;
853 }
854
855 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530856 * Returns attribute's temporary file handle.
857 *
858 * @return temporary file handle
859 */
860 public File getAttributesTempFileHandle() {
861 return attributesTempFileHandle;
862 }
863
864 /**
865 * Sets attribute's temporary file handle.
866 *
867 * @param attributeForClass file handle for attribute
868 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530869 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530870 attributesTempFileHandle = attributeForClass;
871 }
872
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530873 /**
874 * Returns getter method's impl's temporary file handle.
875 *
876 * @return temporary file handle
877 */
878 public File getGetterImplTempFileHandle() {
879 return getterImplTempFileHandle;
880 }
881
882 /**
883 * Sets to getter method's impl's temporary file handle.
884 *
885 * @param getterImpl file handle for to getter method's impl
886 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530887 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530888 getterImplTempFileHandle = getterImpl;
889 }
890
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530891 /**
892 * Returns hash code method's temporary file handle.
893 *
894 * @return temporary file handle
895 */
896 public File getHashCodeImplTempFileHandle() {
897 return hashCodeImplTempFileHandle;
898 }
899
900 /**
901 * Sets hash code method's temporary file handle.
902 *
903 * @param hashCodeMethod file handle for hash code method
904 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530905 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530906 hashCodeImplTempFileHandle = hashCodeMethod;
907 }
908
909 /**
910 * Returns equals mehtod's temporary file handle.
911 *
912 * @return temporary file handle
913 */
914 public File getEqualsImplTempFileHandle() {
915 return equalsImplTempFileHandle;
916 }
917
918 /**
919 * Sets equals method's temporary file handle.
920 *
921 * @param equalsMethod file handle for to equals method
922 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530923 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530924 equalsImplTempFileHandle = equalsMethod;
925 }
926
927 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530928 * Returns to string method's temporary file handle.
929 *
930 * @return temporary file handle
931 */
932 public File getToStringImplTempFileHandle() {
933 return toStringImplTempFileHandle;
934 }
935
936 /**
937 * Sets to string method's temporary file handle.
938 *
939 * @param toStringMethod file handle for to string method
940 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530941 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530942 toStringImplTempFileHandle = toStringMethod;
943 }
944
945 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530946 * Returns java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530947 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530948 * @return java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530949 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530950 public JavaExtendsListHolder getJavaExtendsListHolder() {
951 return javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530952 }
953
954 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530955 * Sets java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530956 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530957 * @param javaExtendsListHolder java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530958 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530959 public void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
960 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530961 }
962
963 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530964 * Returns augments copy constructor method impl class file.
965 *
966 * @return augments copy constructor method impl class file
967 */
968 public File getAugmentConstructorImplTempFileHandle() {
969 return augmentConstructorImplTempFileHandle;
970 }
971
972 /**
973 * Sets augments copy constructor method impl class.
974 *
975 * @param augmentConstructorImplTempFileHandle augments copy constructor method impl class file
976 */
977 public void setAugmentConstructorImplTempFileHandle(File augmentConstructorImplTempFileHandle) {
978 this.augmentConstructorImplTempFileHandle = augmentConstructorImplTempFileHandle;
979 }
980
981 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530982 * Adds attribute for class.
983 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530984 * @param attr attribute info
985 * @param yangPluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530986 * @throws IOException when fails to append to temporary file
987 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530988 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530989 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530990 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
991 + FOUR_SPACE_INDENTATION);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530992 }
993
994 /**
995 * Adds getter for interface.
996 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530997 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530998 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530999 * @throws IOException when fails to append to temporary file
1000 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301001 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301002 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301003 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +05301004 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301005 }
1006
1007 /**
1008 * Adds setter for interface.
1009 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301010 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301011 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301012 * @throws IOException when fails to append to temporary file
1013 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301014 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301015 throws IOException {
1016 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +05301017 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
1018 + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301019 }
1020
1021 /**
1022 * Adds setter's implementation for class.
1023 *
1024 * @param attr attribute info
1025 * @throws IOException when fails to append to temporary file
1026 */
1027 private void addSetterImpl(JavaAttributeInfo attr)
1028 throws IOException {
1029 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +05301030 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
1031 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301032 NEW_LINE);
1033 }
1034
1035 /**
1036 * Adds getter method's impl for class.
1037 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301038 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301039 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301040 * @throws IOException when fails to append to temporary file
1041 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301042 private void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301043 throws IOException {
1044 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
1045 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
1046 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
1047 getGeneratedJavaFiles()) + NEW_LINE);
1048 } else {
janani b4a6711a2016-05-17 13:12:22 +05301049 appendToFile(getGetterImplTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +05301050 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
janani b4a6711a2016-05-17 13:12:22 +05301051 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301052 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301053 }
1054
1055 /**
1056 * Adds build method for interface.
1057 *
Bharat saraswal33dfa012016-05-17 19:59:16 +05301058 * @param pluginConfig plugin configurations
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301059 * @return build method for interface
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301060 * @throws IOException when fails to append to temporary file
1061 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301062 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301063 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301064 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301065 }
1066
1067 /**
1068 * Adds build method's implementation for class.
1069 *
1070 * @return build method implementation for class
1071 * @throws IOException when fails to append to temporary file
1072 */
1073 String addBuildMethodImpl()
1074 throws IOException {
1075 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
1076 }
1077
1078 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301079 * Adds default constructor for class.
1080 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301081 * @param modifier modifier for constructor.
1082 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +05301083 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301084 * @return default constructor for class
1085 * @throws IOException when fails to append to file
1086 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301087 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301088 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301089 return NEW_LINE
1090 + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301091 }
1092
1093 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301094 * Adds hash code method for class.
1095 *
1096 * @param attr attribute info
1097 * @throws IOException when fails to append to temporary file
1098 */
1099 private void addHashCodeMethod(JavaAttributeInfo attr)
1100 throws IOException {
1101 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
1102 }
1103
1104 /**
1105 * Adds equals method for class.
1106 *
1107 * @param attr attribute info
1108 * @throws IOException when fails to append to temporary file
1109 */
1110 private void addEqualsMethod(JavaAttributeInfo attr)
1111 throws IOException {
1112 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
1113 }
1114
1115 /**
1116 * Adds ToString method for class.
1117 *
1118 * @param attr attribute info
1119 * @throws IOException when fails to append to temporary file
1120 */
1121 private void addToStringMethod(JavaAttributeInfo attr)
1122 throws IOException {
1123 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
1124 }
1125
1126 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +05301127 * Adds from string method for union class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301128 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301129 * @param javaAttributeInfo type attribute info
Bharat saraswalc0e04842016-05-12 13:16:57 +05301130 * @param fromStringAttributeInfo from string attribute info
1131 * @throws IOException when fails to append to temporary file
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301132 */
Bharat saraswalc0e04842016-05-12 13:16:57 +05301133 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301134 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301135 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301136 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
1137 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301138 }
1139
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301140 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301141 * Adds constructor for augment class.
1142 *
1143 * @param attr attribute info
1144 * @throws IOException when fails to append to temporary file
1145 */
1146 private void addAugmentConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
1147 throws IOException {
1148 appendToFile(getAugmentConstructorImplTempFileHandle(), getAugmentedConstructor(attr, pluginConfig));
1149 }
1150
1151 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301152 * Returns a temporary file handle for the specific file type.
1153 *
1154 * @param fileName file name
1155 * @return temporary file handle
1156 * @throws IOException when fails to create new file handle
1157 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301158 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301159 throws IOException {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301160 String path = getTempDirPath(getAbsoluteDirPath());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301161 File dir = new File(path);
1162 if (!dir.exists()) {
1163 dir.mkdirs();
1164 }
1165 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1166 if (!file.exists()) {
1167 file.createNewFile();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301168 } else {
1169 throw new IOException(fileName + " is reused due to YANG naming");
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301170 }
1171 return file;
1172 }
1173
1174 /**
1175 * Returns a temporary file handle for the specific file type.
1176 *
1177 * @param fileName file name
1178 * @return temporary file handle
1179 * @throws IOException when fails to create new file handle
1180 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301181 File getJavaFileHandle(String fileName)
1182 throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301183 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
1184 }
1185
1186 /**
1187 * Returns data from the temporary files.
1188 *
Bharat saraswalb551aae2016-07-14 15:18:20 +05301189 * @param file temporary file handle
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301190 * @param absolutePath absolute path
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301191 * @return stored data from temporary files
1192 * @throws IOException when failed to get data from the given file
1193 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301194 public String getTemporaryDataFromFileHandle(File file, String absolutePath)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301195 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301196
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301197 String path = getTempDirPath(absolutePath);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301198 if (new File(path + file.getName()).exists()) {
1199 return readAppendFile(path + file.getName(), EMPTY_STRING);
1200 } else {
1201 throw new IOException("Unable to get data from the given "
1202 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1203 }
1204 }
1205
1206 /**
1207 * Returns temporary directory path.
1208 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301209 * @param absolutePath absolute path
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301210 * @return directory path
1211 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301212 String getTempDirPath(String absolutePath) {
1213 return getPackageDirPathFromJavaJPackage(absolutePath) + SLASH + getGeneratedJavaClassName()
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301214 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1215 }
1216
1217 /**
1218 * Parses attribute to get the attribute string.
1219 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301220 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301221 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301222 * @return attribute string
1223 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301224 public String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301225 /*
1226 * TODO: check if this utility needs to be called or move to the caller
1227 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301228 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301229 String attributeAccessType = PRIVATE;
1230 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
1231 attributeAccessType = PROTECTED;
1232 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301233 if (attr.isQualifiedName()) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301234 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
1235 attr.getImportInfo().getClassInfo(),
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301236 attributeName, attr.isListAttr(), attributeAccessType);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301237 } else {
1238 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301239 attr.isListAttr(), attributeAccessType);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301240 }
1241 }
1242
1243 /**
1244 * Appends content to temporary file.
1245 *
1246 * @param file temporary file
1247 * @param data data to be appended
1248 * @throws IOException when fails to append to file
1249 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301250 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301251 throws IOException {
1252 try {
1253 insertDataIntoJavaFile(file, data);
1254 } catch (IOException ex) {
1255 throw new IOException("failed to write in temp file.");
1256 }
1257 }
1258
1259 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301260 * Adds parent's info to current node import list.
1261 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301262 * @param curNode current node for which import list needs to be updated
Bharat saraswal33dfa012016-05-17 19:59:16 +05301263 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301264 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301265 public void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301266 caseImportInfo = new JavaQualifiedTypeInfo();
1267 YangNode parent = getParentNodeInGenCode(curNode);
1268 if (!(parent instanceof JavaCodeGenerator)) {
1269 throw new TranslatorException("missing parent node to contain current node info in generated file");
1270 }
1271 if (!(curNode instanceof JavaFileInfoContainer)) {
1272 throw new TranslatorException("missing java file information to get the package details "
1273 + "of attribute corresponding to child node");
1274 }
Bharat saraswal33dfa012016-05-17 19:59:16 +05301275 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1276 pluginConfig.getConflictResolver())));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301277 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301278
1279 JavaFileInfo fileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1280
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301281 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301282 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301283 getCapitalCase(fileInfo.getJavaName()), fileInfo.getPackage());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301284 }
1285
1286 /**
1287 * Adds leaf attributes in generated files.
1288 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301289 * @param listOfLeaves list of YANG leaf
1290 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301291 * @throws IOException IO operation fail
1292 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301293 public void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301294 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301295 throws IOException {
1296 if (listOfLeaves != null) {
1297 for (YangLeaf leaf : listOfLeaves) {
1298 if (!(leaf instanceof JavaLeafInfoContainer)) {
1299 throw new TranslatorException("Leaf does not have java information");
1300 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301301 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(this, leaf, yangPluginConfig),
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301302 yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301303 }
1304 }
1305 }
1306
1307 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301308 * Returns java attribute for leaf.
1309 *
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301310 * @param tempJavaFragmentFiles temporary generated file
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301311 * @param leaf YANG leaf
1312 * @param yangPluginConfig plugin configurations
1313 * @return java attribute for leaf
1314 */
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301315 public static JavaAttributeInfo getJavaAttributeOfLeaf(TempJavaFragmentFiles tempJavaFragmentFiles, YangLeaf leaf,
1316 YangPluginConfig yangPluginConfig) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301317 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
1318 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
1319 javaLeaf.updateJavaQualifiedInfo();
1320 return getAttributeInfoForTheData(
1321 javaLeaf.getJavaQualifiedInfo(),
1322 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1323 javaLeaf.getDataType(),
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301324 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301325 false);
1326 }
1327
1328 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301329 * Adds leaf list's attributes in generated files.
1330 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301331 * @param listOfLeafList list of YANG leaves
1332 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301333 * @throws IOException IO operation fail
1334 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301335 public void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301336 throws IOException {
1337 if (listOfLeafList != null) {
1338 for (YangLeafList leafList : listOfLeafList) {
1339 if (!(leafList instanceof JavaLeafInfoContainer)) {
1340 throw new TranslatorException("Leaf-list does not have java information");
1341 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301342 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(this, leafList, yangPluginConfig),
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301343 yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301344 }
1345 }
1346 }
1347
1348 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301349 * Returns java attribute for leaf-list.
1350 *
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301351 * @param tempJavaFragmentFiles temporary generated file
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301352 * @param leafList YANG leaf-list
1353 * @param yangPluginConfig plugin configurations
1354 * @return java attribute for leaf-list
1355 */
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301356 public static JavaAttributeInfo getJavaAttributeOfLeafList(TempJavaFragmentFiles tempJavaFragmentFiles,
1357 YangLeafList leafList, YangPluginConfig yangPluginConfig) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301358 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
1359 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
1360 javaLeaf.updateJavaQualifiedInfo();
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301361 tempJavaFragmentFiles.getJavaImportData().setIfListImported(true);
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301362 return getAttributeInfoForTheData(
1363 javaLeaf.getJavaQualifiedInfo(),
1364 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1365 javaLeaf.getDataType(),
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301366 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301367 true);
1368 }
1369
1370 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301371 * Adds all the leaves in the current data model node as part of the
1372 * generated temporary file.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301373 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301374 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301375 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301376 * @throws IOException IO operation fail
1377 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301378 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301379 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301380 throws IOException {
1381 if (!(curNode instanceof YangLeavesHolder)) {
1382 throw new TranslatorException("Data model node does not have any leaves");
1383 }
1384 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301385 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1386 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301387 }
1388
1389 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301390 * Adds the new attribute info to the target generated temporary files.
1391 *
Bharat saraswalb551aae2016-07-14 15:18:20 +05301392 * @param newAttrInfo the attribute info that needs to be added to temporary files
Bharat saraswal33dfa012016-05-17 19:59:16 +05301393 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301394 * @throws IOException IO operation fail
1395 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301396 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301397 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301398 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301399 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301400 addAttribute(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301401 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301402
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301403 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301404 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301405 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301406
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301407 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301408 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301409 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301410
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301411 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1412 addSetterImpl(newAttrInfo);
1413 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301414
1415 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301416 addGetterImpl(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301417 }
1418 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1419 addHashCodeMethod(newAttrInfo);
1420 }
1421 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1422 addEqualsMethod(newAttrInfo);
1423 }
1424 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1425 addToStringMethod(newAttrInfo);
1426 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301427
1428 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Bharat saraswal96dfef02016-06-16 00:29:12 +05301429 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
1430 pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +05301431 /*
1432 * Create a new java attribute info with qualified information of
1433 * wrapper classes.
1434 */
1435 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1436 newAttrInfo.getAttributeName(),
1437 newAttrInfo.getAttributeType(),
1438 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
1439
1440 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301441 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301442 }
1443
1444 /**
1445 * Returns java class name.
1446 *
1447 * @param suffix for the class name based on the file type
1448 * @return java class name
1449 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301450 String getJavaClassName(String suffix) {
janani b4a6711a2016-05-17 13:12:22 +05301451 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301452 }
1453
1454 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301455 * Returns java class name.
1456 *
1457 * @return java class name
1458 */
1459 String getImplClassName() {
1460 return getCapitalCase(DEFAULT) + getCapitalCase(getJavaFileInfo().getJavaName());
1461 }
1462
1463 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301464 * Returns op param java class name.
1465 *
1466 * @return op param java class name
1467 */
1468 String getOpParamImplClassName() {
1469 return getCapitalCase(getJavaFileInfo().getJavaName()) + OPERATION;
1470 }
1471
1472 /**
1473 * Returns op param builder java class name.
1474 *
1475 * @return op param builder java class name
1476 */
1477 String getOpParamBuilderImplClassName() {
1478 return getCapitalCase(getJavaFileInfo().getJavaName()) + OPERATION + BUILDER;
1479 }
1480
1481 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301482 * Returns the directory path.
1483 *
1484 * @return directory path
1485 */
1486 private String getDirPath() {
1487 return getJavaFileInfo().getPackageFilePath();
1488 }
1489
1490 /**
1491 * Constructs java code exit.
1492 *
1493 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301494 * @param curNode current YANG node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301495 * @throws IOException when fails to generate java files
1496 */
1497 public void generateJavaFile(int fileType, YangNode curNode)
1498 throws IOException {
Bharat saraswalb551aae2016-07-14 15:18:20 +05301499
1500 if (curNode instanceof YangModule || curNode instanceof YangSubModule) {
1501 addResolvedAugmentedDataNodeImports(curNode);
1502 }
1503 List<String> imports = ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getBeanTempFiles()
1504 .getJavaImportData().getImports();
Bharat saraswal33dfa012016-05-17 19:59:16 +05301505
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301506 JavaFileInfo curInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1507
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301508 createPackage(curNode);
1509
1510 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301511 * Generate java code.
1512 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301513 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1514 BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301515
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301516 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301517 * Create interface file.
1518 */
1519 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1520 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301521 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1522 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301523 * Create builder interface file.
1524 */
1525 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1526 setBuilderInterfaceJavaFileHandle(
1527 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1528 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301529 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1530 isAttributePresent()));
1531 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301532 * Append builder interface file to interface file and close it.
1533 */
1534 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
Bharat saraswal96dfef02016-06-16 00:29:12 +05301535 validateLineLength(getInterfaceJavaFileHandle());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301536 }
1537 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301538
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301539 if (curNode instanceof YangCase) {
1540 removeCaseImport(imports);
1541 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301542 if (curNode instanceof YangJavaAugment) {
1543 updateAugmentConstructorTempFile(curNode, curInfo.getPluginConfig());
1544 YangJavaAugment augment = (YangJavaAugment) curNode;
1545 List<JavaQualifiedTypeInfo> infoList = augment.getExtendedClassInfo();
1546 for (JavaQualifiedTypeInfo info : infoList) {
1547 if (info.getClassInfo()
1548 .equals(getCapitalCase(getCamelCase(augment.getAugmentedNode().getName(), null)))) {
1549 removeAugmentedImport(imports, info);
1550 }
1551
1552 }
1553 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301554 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301555 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301556 if (isAttributePresent()) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301557 addImportsToStringAndHasCodeMethods(imports, true);
Bharat saraswalb551aae2016-07-14 15:18:20 +05301558 }
1559 if (curNode instanceof YangAugmentableNode) {
1560 addImportsForAugmentableClass(imports);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301561 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301562 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301563 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301564 * Create impl class file.
1565 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301566 setImplClassJavaFileHandle(getJavaFileHandle(getImplClassName()));
1567 setImplClassJavaFileHandle(
1568 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent(), imports));
1569
1570 /*
1571 * Create builder class file.
1572 */
1573 if ((fileType & BUILDER_CLASS_MASK) != 0) {
1574 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1575 setBuilderClassJavaFileHandle(
1576 generateBuilderClassFile(getBuilderClassJavaFileHandle(), curNode,
1577 isAttributePresent()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301578 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301579 * Append impl class to builder class and close it.
1580 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301581 mergeJavaFiles(getBuilderClassJavaFileHandle(), getImplClassJavaFileHandle());
1582 validateLineLength(getImplClassJavaFileHandle());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301583 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301584 insertDataIntoJavaFile(getImplClassJavaFileHandle(), getJavaClassDefClose());
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301585
1586 if (!(curNode instanceof YangModule)) {
1587 if (isAttributePresent()) {
1588 addImportsToStringAndHasCodeMethods(imports, false);
1589 }
1590 addBitsetImport(imports, true);
1591 /*
1592 * Create impl class file.
1593 */
1594 setOpParamClassJavaFileHandle(getJavaFileHandle(getOpParamImplClassName()));
1595 setOpParamClassJavaFileHandle(
1596 generateOpParamImplClassFile(getOpParamClassJavaFileHandle(), curNode,
1597 isAttributePresent(), imports));
1598
1599 /*
1600 * Create builder class file.
1601 */
1602 if ((fileType & BUILDER_CLASS_MASK) != 0) {
1603 setBuilderOpParmClassJavaFileHandle(getJavaFileHandle(getOpParamBuilderImplClassName()));
1604 setBuilderOpParmClassJavaFileHandle(
1605 generateOpParamBuilderClassFile(getBuilderOpParmClassJavaFileHandle(), curNode,
1606 isAttributePresent()));
1607 /*
1608 * Append impl class to builder class and close it.
1609 */
1610 mergeJavaFiles(getBuilderOpParmClassJavaFileHandle(), getOpParamClassJavaFileHandle());
1611 validateLineLength(getOpParamClassJavaFileHandle());
1612
1613 addBitsetImport(imports, false);
1614 }
1615 insertDataIntoJavaFile(getOpParamClassJavaFileHandle(), getJavaClassDefClose());
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301616 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301617 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301618 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301619 * Close all the file handles.
1620 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301621 freeTemporaryResources(false);
1622 }
1623
1624 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301625 * Updates augment constructor temp file.
1626 *
1627 * @param curNode current augment node
1628 * @param pluginConfig plugin configurations
1629 * @throws IOException when fails to do IO operations
1630 */
1631 private void updateAugmentConstructorTempFile(YangNode curNode, YangPluginConfig pluginConfig)
1632 throws IOException {
1633 YangJavaAugment augment = (YangJavaAugment) curNode;
1634
Bharat saraswalb551aae2016-07-14 15:18:20 +05301635 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) curNode)
1636 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301637 YangNode augmentedNode = augment.getAugmentedNode();
1638 if (augmentedNode instanceof YangLeavesHolder) {
1639 YangLeavesHolder holder = (YangLeavesHolder) augmentedNode;
1640 if (holder.getListOfLeaf() != null) {
1641 for (YangLeaf leaf : holder.getListOfLeaf()) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301642 addAugmentConstructor(getJavaAttributeOfLeaf(this, leaf,
1643 pluginConfig), pluginConfig);
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301644 }
1645
1646 }
1647 if (holder.getListOfLeafList() != null) {
1648 for (YangLeafList leafList : holder.getListOfLeafList()) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301649 addAugmentConstructor(getJavaAttributeOfLeafList(this, leafList,
1650 pluginConfig), pluginConfig);
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301651 }
1652
1653 }
1654 }
1655 augmentedNode = augmentedNode.getChild();
1656 boolean isList = false;
1657 while (augmentedNode != null) {
1658 if (augmentedNode instanceof YangList) {
1659 isList = true;
1660 }
Bharat saraswalb551aae2016-07-14 15:18:20 +05301661 if (!(augmentedNode instanceof YangUses)) {
1662 addAugmentConstructor(getCurNodeAsAttributeInTarget(augmentedNode, augment, isList,
1663 tempJavaBeanFragmentFiles), pluginConfig);
1664 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301665 augmentedNode = augmentedNode.getNextSibling();
1666 }
1667
1668 }
1669
1670 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301671 * Adds imports for ToString and HashCodeMethod.
1672 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301673 * @param imports import list
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301674 * @param operation add or remove
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301675 */
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301676 public void addImportsToStringAndHasCodeMethods(List<String> imports, boolean operation) {
1677 if (operation) {
1678 imports.add(getJavaImportData().getImportForHashAndEquals());
1679 imports.add(getJavaImportData().getImportForToString());
1680 } else {
1681 imports.remove(getJavaImportData().getImportForHashAndEquals());
1682 imports.remove(getJavaImportData().getImportForToString());
1683 }
1684 }
1685
1686 /**
1687 * Adds imports for bitset method.
1688 *
1689 * @param imports import list
1690 * @param operation add or remove
1691 */
1692 public void addBitsetImport(List<String> imports, boolean operation) {
1693 if (operation) {
1694 imports.add(getJavaImportData().getImportForToBitSet());
1695 } else {
1696 imports.remove(getJavaImportData().getImportForToBitSet());
1697 }
Bharat saraswalb551aae2016-07-14 15:18:20 +05301698 }
1699
1700
1701 /**
1702 * Adds import for map and hash map.
1703 *
1704 * @param imports import list
1705 */
1706 public void addImportsForAugmentableClass(List<String> imports) {
1707 imports.add(getJavaImportData().getHashMapImport());
1708 imports.add(getJavaImportData().getMapImport());
1709 imports.add(getJavaImportData().getYangAugmentedInfoImport());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301710 }
1711
1712 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301713 * Removes augmented node import info from import list.
1714 *
1715 * @param imports list of imports
1716 * @return import for class
1717 */
1718 private List<String> removeAugmentedImport(List<String> imports, JavaQualifiedTypeInfo augmentedInfo) {
Bharat saraswalb551aae2016-07-14 15:18:20 +05301719 String augmentedNodeImport;
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301720 if (imports != null && augmentedInfo != null) {
1721 augmentedNodeImport = IMPORT + augmentedInfo.getPkgInfo() + PERIOD + getCapitalCase(DEFAULT)
1722 + augmentedInfo.getClassInfo() + PERIOD + augmentedInfo.getClassInfo()
1723 + BUILDER + SEMI_COLAN + NEW_LINE;
1724 imports.remove(augmentedNodeImport);
1725 augmentedNodeImport = IMPORT + augmentedInfo.getPkgInfo() + PERIOD
1726 + augmentedInfo.getClassInfo() + PERIOD + augmentedInfo.getClassInfo()
1727 + BUILDER + SEMI_COLAN + NEW_LINE;
1728 imports.remove(augmentedNodeImport);
Bharat saraswalb551aae2016-07-14 15:18:20 +05301729 imports.remove(getJavaImportData().getYangAugmentedInfoImport());
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301730 }
1731 return imports;
1732 }
1733
1734 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301735 * Removes case import info from import list.
1736 *
1737 * @param imports list of imports
1738 * @return import for class
1739 */
1740 private List<String> removeCaseImport(List<String> imports) {
1741 if (imports != null && caseImportInfo != null) {
1742 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1743 SEMI_COLAN + NEW_LINE;
1744 imports.remove(caseImport);
1745 }
1746 return imports;
1747 }
1748
1749 /**
1750 * Removes all temporary file handles.
1751 *
Bharat saraswalb551aae2016-07-14 15:18:20 +05301752 * @param isErrorOccurred when translator fails to generate java files we need to close all open file handles
1753 * include temporary files and java files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301754 * @throws IOException when failed to delete the temporary files
1755 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301756 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301757 throws IOException {
1758 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301759 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301760 * Close all java file handles and when error occurs delete the files.
1761 */
1762 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1763 closeFile(getInterfaceJavaFileHandle(), isError);
1764 }
1765 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301766 closeFile(getBuilderClassJavaFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301767 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301768 if ((getGeneratedJavaFiles() & OPERATION_BUILDER_CLASS_MASK) != 0) {
1769 closeFile(getBuilderOpParmClassJavaFileHandle(), true);
1770 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301771 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1772 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1773 }
1774 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301775 closeFile(getImplClassJavaFileHandle(), isError);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301776 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301777
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301778 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301779 * Close all temporary file handles and delete the files.
1780 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301781 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1782 closeFile(getGetterImplTempFileHandle(), true);
1783 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301784 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1785 closeFile(getAttributesTempFileHandle(), true);
1786 }
1787 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1788 closeFile(getHashCodeImplTempFileHandle(), true);
1789 }
1790 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1791 closeFile(getToStringImplTempFileHandle(), true);
1792 }
1793 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1794 closeFile(getEqualsImplTempFileHandle(), true);
1795 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301796 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1797 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301798 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301799 if ((getGeneratedTempFiles() & AUGMENTE_CLASS_CONSTRUCTOR_MASK) != 0) {
1800 closeFile(getAugmentConstructorImplTempFileHandle(), true);
1801 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301802 }
1803
1804 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +05301805 * Returns if the attribute needs to be accessed in a qualified manner or not, if it needs to be imported, then the
1806 * same needs to be done.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301807 *
1808 * @param importInfo import info for the current attribute being added
1809 * @return status of the qualified access to the attribute
1810 */
1811 public boolean getIsQualifiedAccessOrAddToImportList(
1812 JavaQualifiedTypeInfo importInfo) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301813
1814 return getJavaImportData().addImportInfo(importInfo, getGeneratedJavaClassName(),
1815 getJavaFileInfo().getPackage());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301816 }
1817
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301818 /**
1819 * Returns java file handle for op param class file.
1820 *
1821 * @return java file handle for op param class file
1822 */
1823 public File getOpParamClassJavaFileHandle() {
1824 return opParamClassJavaFileHandle;
1825 }
1826
1827 /**
1828 * Sets the java file handle for op param class file.
1829 *
1830 * @param opParamClassJavaFileHandle java file handle
1831 */
1832 public void setOpParamClassJavaFileHandle(File opParamClassJavaFileHandle) {
1833 this.opParamClassJavaFileHandle = opParamClassJavaFileHandle;
1834 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301835}