blob: 33ca972700d52adbd8bbd26d0c1904d4e5b3ef37 [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 saraswald50c6382016-07-14 21:57:13 +053022import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswalb551aae2016-07-14 15:18:20 +053023import org.onosproject.yangutils.datamodel.YangAugmentableNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053024import org.onosproject.yangutils.datamodel.YangCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangLeafList;
27import org.onosproject.yangutils.datamodel.YangLeavesHolder;
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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053031import org.onosproject.yangutils.translator.exception.TranslatorException;
32import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Bharat saraswalcad0e652016-05-26 23:48:38 +053033import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping;
Bharat saraswalcad0e652016-05-26 23:48:38 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
Bharat saraswalcad0e652016-05-26 23:48:38 +053035import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053036import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053037import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053038
Bharat saraswal96dfef02016-06-16 00:29:12 +053039import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
42import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Bharat saraswalb551aae2016-07-14 15:18:20 +053043import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
45import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.OPERATION_BUILDER_CLASS_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053050import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.AUGMENTE_CLASS_CONSTRUCTOR_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053051import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswalc0e04842016-05-12 13:16:57 +053052import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
54import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
55import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053056import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
57import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
58import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
59import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswalc0e04842016-05-12 13:16:57 +053060import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053061import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053062import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053063import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
64import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswalb551aae2016-07-14 15:18:20 +053065import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.sortImports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053066import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
67import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
69import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053070import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateOpParamBuilderClassFile;
71import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateOpParamImplClassFile;
Bharat saraswald50c6382016-07-14 21:57:13 +053072import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.addResolvedAugmentedDataNodeImports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053073import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053074import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
75import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAugmentedConstructor;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053077import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
78import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswalc0e04842016-05-12 13:16:57 +053079import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
81import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
82import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053084import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
85import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
86import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053088import static org.onosproject.yangutils.utils.UtilConstants.ACTIVATE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053089import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053090import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT;
91import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053092import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
94import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
95import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
96import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
97import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053098import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053099import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswald50c6382016-07-14 21:57:13 +0530100import static org.onosproject.yangutils.utils.UtilConstants.OPERATION;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswald50c6382016-07-14 21:57:13 +0530102import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
103import static org.onosproject.yangutils.utils.UtilConstants.PROTECTED;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530104import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE;
105import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE_CARDINALITY;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530106import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530107import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswald50c6382016-07-14 21:57:13 +0530109import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530110import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530111import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
112import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530113import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530114import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530115import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
116import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
117import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
119import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Bharat saraswal96dfef02016-06-16 00:29:12 +0530120import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121
122/**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530123 * Represents implementation of java code fragments temporary implementations. Manages the common temp file required for
124 * Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530125 */
126public class TempJavaFragmentFiles {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530127
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530128 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530129 * File type extension for java classes.
130 */
131 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530132
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530133 /**
134 * File type extension for temporary classes.
135 */
136 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530137
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530138 /**
139 * Folder suffix for temporary files folder.
140 */
141 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530142
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530143 /**
144 * File name for getter method.
145 */
146 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530147
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530148 /**
149 * File name for setter method.
150 */
151 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530152
153 /**
154 * File name for getter method implementation.
155 */
156 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
157
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530158 /**
159 * File name for setter method implementation.
160 */
161 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530162
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530163 /**
164 * File name for attributes.
165 */
166 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530167
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530168 /**
169 * File name for to string method.
170 */
171 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530172
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530173 /**
174 * File name for hash code method.
175 */
176 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530177
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530178 /**
179 * File name for equals method.
180 */
181 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530182
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530183 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530184 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530185 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530186 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530187
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530188 /**
189 * File name for interface java file name suffix.
190 */
191 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530192
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530193 /**
194 * File name for builder interface file name suffix.
195 */
196 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530197
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530198 /**
199 * File name for builder class file name suffix.
200 */
201 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530202
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530203 /**
204 * File name for impl class file name suffix.
205 */
206 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530207
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530208 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530209 * File name for augment copy constructor method.
210 */
211 private static final String AUGMENT_CONSTRUCTOR_FILE_NAME = "AugmentConstructor";
Bharat saraswalb551aae2016-07-14 15:18:20 +0530212
213 /**
214 * Information about the java files being generated.
215 */
216 private JavaFileInfo javaFileInfo;
217
218 /**
219 * Imported class info.
220 */
221 private JavaImportData javaImportData;
222
223 /**
224 * The variable which guides the types of temporary files generated using the temporary generated file types mask.
225 */
226 private int generatedTempFiles;
227
228 /**
229 * Absolute path where the target java file needs to be generated.
230 */
231 private String absoluteDirPath;
232
233 /**
234 * Contains all the interface(s)/class name which will be extended by generated files.
235 */
236 private JavaExtendsListHolder javaExtendsListHolder;
237
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530238 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530239 * Java file handle for interface file.
240 */
241 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530242
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530243 /**
244 * Java file handle for builder interface file.
245 */
246 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530247
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530248 /**
249 * Java file handle for builder class file.
250 */
251 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530252
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530253 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530254 * Retrieves the temporary file handle of op param builder class.
255 *
256 * @return op param builder temporary file handle
257 */
258 public File getBuilderOpParmClassJavaFileHandle() {
259 return builderOpParmClassJavaFileHandle;
260 }
261
262 /**
263 * Sets the java file handle for op param builder class.
264 *
265 * @param builderOpParmClassJavaFileHandle java file handle
266 */
267 public void setBuilderOpParmClassJavaFileHandle(File builderOpParmClassJavaFileHandle) {
268 this.builderOpParmClassJavaFileHandle = builderOpParmClassJavaFileHandle;
269 }
270
271 /**
272 * Java file handle for builder op param class file.
273 */
274 private File builderOpParmClassJavaFileHandle;
275
276 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530277 * Java file handle for impl class file.
278 */
279 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530280
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530281 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530282 * Java file handle for op param class file.
283 */
284 private File opParamClassJavaFileHandle;
285
286 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530287 * Temporary file handle for attribute.
288 */
289 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530290
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530291 /**
292 * Temporary file handle for getter of interface.
293 */
294 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530295
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530296 /**
297 * Temporary file handle for setter of interface.
298 */
299 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530300
301 /**
302 * Temporary file handle for getter of class.
303 */
304 private File getterImplTempFileHandle;
305
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530306 /**
307 * Temporary file handle for setter of class.
308 */
309 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530310
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530311 /**
312 * Temporary file handle for hash code method of class.
313 */
314 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530315
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530316 /**
317 * Temporary file handle for equals method of class.
318 */
319 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530320
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530321 /**
322 * Temporary file handle for to string method of class.
323 */
324 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530325
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530326 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530327 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530328 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530329 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530330
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530331 /**
332 * Import info for case.
333 */
334 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530335
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530336 /**
337 * Is attribute added.
338 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530339 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530340
Bharat saraswalc0e04842016-05-12 13:16:57 +0530341 /**
Bharat saraswald50c6382016-07-14 21:57:13 +0530342 * Base code gen path.
343 */
344 private String baseCodePath;
345
346 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530347 * Temporary file handle for augments copy constructor method of class.
348 */
349 private File augmentConstructorImplTempFileHandle;
350
Bharat saraswalb551aae2016-07-14 15:18:20 +0530351 TempJavaFragmentFiles() {
352 }
353
354 /**
355 * Creates an instance of temporary java code fragment.
356 *
357 * @param javaFileInfo generated java file information
358 * @throws IOException when fails to create new file handle
359 */
360 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
361 throws IOException {
362 setJavaExtendsListHolder(new JavaExtendsListHolder());
363 setJavaImportData(new JavaImportData());
364 setJavaFileInfo(javaFileInfo);
Bharat saraswald50c6382016-07-14 21:57:13 +0530365 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
366 setBaseCodePath(getJavaFileInfo().getPluginConfig().getCodeGenDir());
367 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getPluginConfig().getCodeGenDir(),
368 getJavaFileInfo().getPackageFilePath()));
369 } else {
370 setBaseCodePath(getJavaFileInfo().getBaseCodeGenPath());
371 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
372 getJavaFileInfo().getPackageFilePath()));
373 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530374 /*
375 * Initialize getter when generation file type matches to interface
376 * mask.
377 */
378 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
379 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
380 }
381
382 /*
383 * Initialize getter and setter when generation file type matches to
384 * builder interface mask.
385 */
386 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
387 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
388 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
389 }
390
391 /*
392 * Initialize getterImpl, setterImpl and attributes when generation file
393 * type matches to builder class mask.
394 */
395 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
396 addGeneratedTempFile(ATTRIBUTES_MASK);
397 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
398 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
399 addGeneratedTempFile(AUGMENTE_CLASS_CONSTRUCTOR_MASK);
400 }
401
402 /*
403 * Initialize getterImpl, attributes, constructor, hash code, equals and
404 * to strings when generation file type matches to impl class mask.
405 */
406 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
407 addGeneratedTempFile(ATTRIBUTES_MASK);
408 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
409 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
410 addGeneratedTempFile(EQUALS_IMPL_MASK);
411 addGeneratedTempFile(TO_STRING_IMPL_MASK);
412 }
413
414 /*
415 * Initialize temp files to generate type class.
416 */
417 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
418 addGeneratedTempFile(ATTRIBUTES_MASK);
419 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
420 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
421 addGeneratedTempFile(EQUALS_IMPL_MASK);
422 addGeneratedTempFile(TO_STRING_IMPL_MASK);
423 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
424 }
425
426 /*
427 * Initialize temp files to generate enum class.
428 */
429 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
430 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
431 }
432
433 /*
434 * Set temporary file handles.
435 */
436 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
437 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
438 }
439
440 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
441 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
442 }
443
444 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
445 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
446 }
447
448 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
449 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
450 }
451
452 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
453 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
454 }
455
456 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
457 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
458 }
459 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
460 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
461 }
462 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
463 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
464 }
465 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
466 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
467 }
468 if ((getGeneratedTempFiles() & AUGMENTE_CLASS_CONSTRUCTOR_MASK) != 0) {
469 setAugmentConstructorImplTempFileHandle(getTemporaryFileHandle(AUGMENT_CONSTRUCTOR_FILE_NAME));
470 }
471
472 }
473
474 /**
475 * Adds current node info as and attribute to the parent generated file.
476 *
477 * @param curNode current node which needs to be added as an attribute in the parent generated code
478 * @param isList is list construct
479 * @param pluginConfig plugin configurations
480 * @throws IOException IO operation exception
481 */
482 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
483 boolean isList, YangPluginConfig pluginConfig)
484 throws IOException {
485 YangNode parent = getParentNodeInGenCode(curNode);
486 if (!(parent instanceof JavaCodeGenerator)) {
487 throw new TranslatorException("missing parent node to contain current node info in generated file");
488 }
489
490 if (parent instanceof YangJavaGrouping) {
491 /*
492 * In case of grouping, there is no need to add the information, it
493 * will be taken care in uses
494 */
495 return;
496 }
497 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) parent)
498 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
499
500 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
501 parent, isList, tempJavaBeanFragmentFiles);
502 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
503 throw new TranslatorException("missing parent temp file handle");
504 }
505 getNodesInterfaceFragmentFiles(parent, javaAttributeInfo, pluginConfig);
506 }
507
508 /**
509 * Creates an attribute info object corresponding to a data model node and return it.
510 *
511 * @param curNode current data model node for which the java code generation is being handled
512 * @param targetNode target node in which the current node is an attribute
513 * @param isListNode is the current added attribute needs to be a list
514 * @param tempJavaFragmentFiles temp java fragment files
515 * @return AttributeInfo attribute details required to add in temporary files
516 */
517 public static JavaAttributeInfo getCurNodeAsAttributeInTarget(YangNode curNode,
518 YangNode targetNode, boolean isListNode,
519 TempJavaFragmentFiles tempJavaFragmentFiles) {
520 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
521 if (curNodeName == null) {
522 updateJavaFileInfo(curNode, null);
523 curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
524 }
525
526 /*
527 * Get the import info corresponding to the attribute for import in
528 * generated java files or qualified access
529 */
530 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
531 getCapitalCase(curNodeName));
532 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
533 throw new TranslatorException("Parent node does not have file info");
534 }
535 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
536 JavaFileInfo fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
537
538 boolean isQualified;
539 if ((targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule)
540 && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE)
541 || qualifiedTypeInfo.getClassInfo().contentEquals(COMPONENT)
542 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(ACTIVATE))
543 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(DEACTIVATE))
544 || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE_CARDINALITY)
545 || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE))
546 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))
547 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + MANAGER))) {
548
549 isQualified = true;
550 } else {
551 String className;
552 if (targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule) {
553 className = getCapitalCase(fileInfo.getJavaName()) + "Service";
554 } else {
555 className = getCapitalCase(fileInfo.getJavaName());
556 }
557
558 isQualified = parentImportData.addImportInfo(qualifiedTypeInfo,
559 className, fileInfo.getPackage());
560 }
561
562 if (isListNode) {
563 parentImportData.setIfListImported(true);
564 }
565
566 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
567 }
568
569 /**
570 * Adds java code snippet for corresponding files.
571 *
572 * @param node java node
573 * @param attr attribute info
574 * @param config plugin configurations
575 * @throws IOException when fails to do IO operations
576 */
577 public static void getNodesInterfaceFragmentFiles(YangNode node, JavaAttributeInfo attr,
578 YangPluginConfig config) throws IOException {
579 TempJavaFragmentFiles tempJavaFragmentFiles;
580 JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) node).getJavaFileInfo();
581 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_SERVICE_AND_MANAGER) != 0) {
582 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
583 .getTempJavaCodeFragmentFiles()
584 .getServiceTempFiles();
585 tempJavaFragmentFiles.addJavaSnippetInfoToApplicableTempFiles(attr, config);
586 }
587 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
588 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
589 .getTempJavaCodeFragmentFiles()
590 .getBeanTempFiles();
591 tempJavaFragmentFiles.addJavaSnippetInfoToApplicableTempFiles(attr, config);
592 }
593
594 }
595
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530596 /**
Bharat saraswald50c6382016-07-14 21:57:13 +0530597 * Returns base code path.
598 *
599 * @return base code path
600 */
601 public String getBaseCodePath() {
602 return baseCodePath;
603 }
604
605 /**
606 * Sets base code path.
607 *
608 * @param baseCodePath base code path
609 */
610 public void setBaseCodePath(String baseCodePath) {
611 this.baseCodePath = baseCodePath;
612 }
613
614 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530615 * Retrieves the absolute path where the file needs to be generated.
616 *
617 * @return absolute path where the file needs to be generated
618 */
619 private String getAbsoluteDirPath() {
620 return absoluteDirPath;
621 }
622
623 /**
624 * Sets absolute path where the file needs to be generated.
625 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530626 * @param absoluteDirPath absolute path where the file needs to be generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530627 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530628 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530629 this.absoluteDirPath = absoluteDirPath;
630 }
631
632 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530633 * Retrieves the generated java file information.
634 *
635 * @return generated java file information
636 */
637 public JavaFileInfo getJavaFileInfo() {
638 return javaFileInfo;
639 }
640
641 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530642 * Sets the generated java file information.
643 *
644 * @param javaFileInfo generated java file information
645 */
646 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
647 this.javaFileInfo = javaFileInfo;
648 }
649
650 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530651 * Retrieves the generated temp files.
652 *
653 * @return generated temp files
654 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530655 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530656 return generatedTempFiles;
657 }
658
659 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530660 * Sets generated file files.
661 *
662 * @param fileType generated file type
663 */
664 void setGeneratedTempFiles(int fileType) {
665 generatedTempFiles = fileType;
666 }
667
668 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530669 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530670 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530671 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530672 generatedTempFiles = 0;
673 }
674
675 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530676 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530677 *
678 * @param generatedTempFile generated file
679 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530680 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530681 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530682 setGeneratedTempFiles(generatedTempFiles);
683 }
684
685 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530686 * Retrieves the generated Java files.
687 *
688 * @return generated Java files
689 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530690 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530691 return getJavaFileInfo().getGeneratedFileTypes();
692 }
693
694 /**
695 * Retrieves the mapped Java class name.
696 *
697 * @return mapped Java class name
698 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530699 String getGeneratedJavaClassName() {
janani b4a6711a2016-05-17 13:12:22 +0530700 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530701 }
702
703 /**
704 * Retrieves the import data for the generated Java file.
705 *
706 * @return import data for the generated Java file
707 */
708 public JavaImportData getJavaImportData() {
709 return javaImportData;
710 }
711
712 /**
713 * Sets import data for the generated Java file.
714 *
715 * @param javaImportData import data for the generated Java file
716 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530717 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530718 this.javaImportData = javaImportData;
719 }
720
721 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530722 * Retrieves the status of any attributes added.
723 *
724 * @return status of any attributes added
725 */
726 public boolean isAttributePresent() {
727 return isAttributePresent;
728 }
729
730 /**
731 * Sets status of any attributes added.
732 *
733 * @param attributePresent status of any attributes added
734 */
735 public void setAttributePresent(boolean attributePresent) {
736 isAttributePresent = attributePresent;
737 }
738
739 /**
740 * Returns getter methods's temporary file handle.
741 *
742 * @return temporary file handle
743 */
744 public File getGetterInterfaceTempFileHandle() {
745 return getterInterfaceTempFileHandle;
746 }
747
748 /**
749 * Sets to getter method's temporary file handle.
750 *
751 * @param getterForInterface file handle for to getter method
752 */
753 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
754 getterInterfaceTempFileHandle = getterForInterface;
755 }
756
757 /**
758 * Returns setter method's temporary file handle.
759 *
760 * @return temporary file handle
761 */
762 public File getSetterInterfaceTempFileHandle() {
763 return setterInterfaceTempFileHandle;
764 }
765
766 /**
767 * Sets to setter method's temporary file handle.
768 *
769 * @param setterForInterface file handle for to setter method
770 */
771 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
772 setterInterfaceTempFileHandle = setterForInterface;
773 }
774
775 /**
776 * Returns setter method's impl's temporary file handle.
777 *
778 * @return temporary file handle
779 */
780 public File getSetterImplTempFileHandle() {
781 return setterImplTempFileHandle;
782 }
783
784 /**
785 * Sets to setter method's impl's temporary file handle.
786 *
787 * @param setterImpl file handle for to setter method's implementation class
788 */
789 private void setSetterImplTempFileHandle(File setterImpl) {
790 setterImplTempFileHandle = setterImpl;
791 }
792
793 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530794 * Returns from string method's temporary file handle.
795 *
796 * @return from string method's temporary file handle
797 */
798 public File getFromStringImplTempFileHandle() {
799 return fromStringImplTempFileHandle;
800 }
801
802 /**
803 * Sets from string method's temporary file handle.
804 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530805 * @param fromStringImplTempFileHandle from string method's temporary file handle
Bharat saraswalc0e04842016-05-12 13:16:57 +0530806 */
807 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
808 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
809 }
810
811 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530812 * Returns java file handle for interface file.
813 *
814 * @return java file handle for interface file
815 */
816 private File getInterfaceJavaFileHandle() {
817 return interfaceJavaFileHandle;
818 }
819
820 /**
821 * Sets the java file handle for interface file.
822 *
823 * @param interfaceJavaFileHandle java file handle
824 */
825 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
826 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
827 }
828
829 /**
830 * Returns java file handle for builder interface file.
831 *
832 * @return java file handle for builder interface file
833 */
834 private File getBuilderInterfaceJavaFileHandle() {
835 return builderInterfaceJavaFileHandle;
836 }
837
838 /**
839 * Sets the java file handle for builder interface file.
840 *
841 * @param builderInterfaceJavaFileHandle java file handle
842 */
843 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
844 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
845 }
846
847 /**
848 * Returns java file handle for builder class file.
849 *
850 * @return java file handle for builder class file
851 */
852 private File getBuilderClassJavaFileHandle() {
853 return builderClassJavaFileHandle;
854 }
855
856 /**
857 * Sets the java file handle for builder class file.
858 *
859 * @param builderClassJavaFileHandle java file handle
860 */
861 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
862 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
863 }
864
865 /**
866 * Returns java file handle for impl class file.
867 *
868 * @return java file handle for impl class file
869 */
870 private File getImplClassJavaFileHandle() {
871 return implClassJavaFileHandle;
872 }
873
874 /**
875 * Sets the java file handle for impl class file.
876 *
877 * @param implClassJavaFileHandle java file handle
878 */
879 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
880 this.implClassJavaFileHandle = implClassJavaFileHandle;
881 }
882
883 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530884 * Returns attribute's temporary file handle.
885 *
886 * @return temporary file handle
887 */
888 public File getAttributesTempFileHandle() {
889 return attributesTempFileHandle;
890 }
891
892 /**
893 * Sets attribute's temporary file handle.
894 *
895 * @param attributeForClass file handle for attribute
896 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530897 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530898 attributesTempFileHandle = attributeForClass;
899 }
900
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530901 /**
902 * Returns getter method's impl's temporary file handle.
903 *
904 * @return temporary file handle
905 */
906 public File getGetterImplTempFileHandle() {
907 return getterImplTempFileHandle;
908 }
909
910 /**
911 * Sets to getter method's impl's temporary file handle.
912 *
913 * @param getterImpl file handle for to getter method's impl
914 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530915 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530916 getterImplTempFileHandle = getterImpl;
917 }
918
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530919 /**
920 * Returns hash code method's temporary file handle.
921 *
922 * @return temporary file handle
923 */
924 public File getHashCodeImplTempFileHandle() {
925 return hashCodeImplTempFileHandle;
926 }
927
928 /**
929 * Sets hash code method's temporary file handle.
930 *
931 * @param hashCodeMethod file handle for hash code method
932 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530933 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530934 hashCodeImplTempFileHandle = hashCodeMethod;
935 }
936
937 /**
938 * Returns equals mehtod's temporary file handle.
939 *
940 * @return temporary file handle
941 */
942 public File getEqualsImplTempFileHandle() {
943 return equalsImplTempFileHandle;
944 }
945
946 /**
947 * Sets equals method's temporary file handle.
948 *
949 * @param equalsMethod file handle for to equals method
950 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530951 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530952 equalsImplTempFileHandle = equalsMethod;
953 }
954
955 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530956 * Returns to string method's temporary file handle.
957 *
958 * @return temporary file handle
959 */
960 public File getToStringImplTempFileHandle() {
961 return toStringImplTempFileHandle;
962 }
963
964 /**
965 * Sets to string method's temporary file handle.
966 *
967 * @param toStringMethod file handle for to string method
968 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530969 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530970 toStringImplTempFileHandle = toStringMethod;
971 }
972
973 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530974 * Returns java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530975 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530976 * @return java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530977 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530978 public JavaExtendsListHolder getJavaExtendsListHolder() {
979 return javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530980 }
981
982 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530983 * Sets java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530984 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530985 * @param javaExtendsListHolder java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530986 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530987 public void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
988 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530989 }
990
991 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530992 * Returns augments copy constructor method impl class file.
993 *
994 * @return augments copy constructor method impl class file
995 */
996 public File getAugmentConstructorImplTempFileHandle() {
997 return augmentConstructorImplTempFileHandle;
998 }
999
1000 /**
1001 * Sets augments copy constructor method impl class.
1002 *
1003 * @param augmentConstructorImplTempFileHandle augments copy constructor method impl class file
1004 */
1005 public void setAugmentConstructorImplTempFileHandle(File augmentConstructorImplTempFileHandle) {
1006 this.augmentConstructorImplTempFileHandle = augmentConstructorImplTempFileHandle;
1007 }
1008
1009 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301010 * Adds attribute for class.
1011 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301012 * @param attr attribute info
1013 * @param yangPluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301014 * @throws IOException when fails to append to temporary file
1015 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301016 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301017 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301018 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
1019 + FOUR_SPACE_INDENTATION);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301020 }
1021
1022 /**
1023 * Adds getter for interface.
1024 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301025 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301026 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301027 * @throws IOException when fails to append to temporary file
1028 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301029 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301030 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301031 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +05301032 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301033 }
1034
1035 /**
1036 * Adds setter for interface.
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
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301040 * @throws IOException when fails to append to temporary file
1041 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301042 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301043 throws IOException {
1044 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +05301045 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
1046 + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301047 }
1048
1049 /**
1050 * Adds setter's implementation for class.
1051 *
1052 * @param attr attribute info
1053 * @throws IOException when fails to append to temporary file
1054 */
1055 private void addSetterImpl(JavaAttributeInfo attr)
1056 throws IOException {
1057 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +05301058 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
1059 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301060 NEW_LINE);
1061 }
1062
1063 /**
1064 * Adds getter method's impl for class.
1065 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301066 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301067 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301068 * @throws IOException when fails to append to temporary file
1069 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301070 private void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301071 throws IOException {
1072 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
1073 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
1074 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
1075 getGeneratedJavaFiles()) + NEW_LINE);
1076 } else {
janani b4a6711a2016-05-17 13:12:22 +05301077 appendToFile(getGetterImplTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +05301078 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
janani b4a6711a2016-05-17 13:12:22 +05301079 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301080 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301081 }
1082
1083 /**
1084 * Adds build method for interface.
1085 *
Bharat saraswal33dfa012016-05-17 19:59:16 +05301086 * @param pluginConfig plugin configurations
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301087 * @return build method for interface
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301088 * @throws IOException when fails to append to temporary file
1089 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301090 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301091 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301092 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301093 }
1094
1095 /**
1096 * Adds build method's implementation for class.
1097 *
1098 * @return build method implementation for class
1099 * @throws IOException when fails to append to temporary file
1100 */
1101 String addBuildMethodImpl()
1102 throws IOException {
1103 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
1104 }
1105
1106 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301107 * Adds default constructor for class.
1108 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301109 * @param modifier modifier for constructor.
1110 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +05301111 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301112 * @return default constructor for class
1113 * @throws IOException when fails to append to file
1114 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301115 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301116 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301117 return NEW_LINE
1118 + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301119 }
1120
1121 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301122 * Adds hash code method for class.
1123 *
1124 * @param attr attribute info
1125 * @throws IOException when fails to append to temporary file
1126 */
1127 private void addHashCodeMethod(JavaAttributeInfo attr)
1128 throws IOException {
1129 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
1130 }
1131
1132 /**
1133 * Adds equals method for class.
1134 *
1135 * @param attr attribute info
1136 * @throws IOException when fails to append to temporary file
1137 */
1138 private void addEqualsMethod(JavaAttributeInfo attr)
1139 throws IOException {
1140 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
1141 }
1142
1143 /**
1144 * Adds ToString method for class.
1145 *
1146 * @param attr attribute info
1147 * @throws IOException when fails to append to temporary file
1148 */
1149 private void addToStringMethod(JavaAttributeInfo attr)
1150 throws IOException {
1151 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
1152 }
1153
1154 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +05301155 * Adds from string method for union class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301156 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301157 * @param javaAttributeInfo type attribute info
Bharat saraswalc0e04842016-05-12 13:16:57 +05301158 * @param fromStringAttributeInfo from string attribute info
1159 * @throws IOException when fails to append to temporary file
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301160 */
Bharat saraswalc0e04842016-05-12 13:16:57 +05301161 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301162 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301163 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301164 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
1165 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301166 }
1167
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301168 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301169 * Adds constructor for augment class.
1170 *
1171 * @param attr attribute info
1172 * @throws IOException when fails to append to temporary file
1173 */
1174 private void addAugmentConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
1175 throws IOException {
1176 appendToFile(getAugmentConstructorImplTempFileHandle(), getAugmentedConstructor(attr, pluginConfig));
1177 }
1178
1179 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301180 * Returns a temporary file handle for the specific file type.
1181 *
1182 * @param fileName file name
1183 * @return temporary file handle
1184 * @throws IOException when fails to create new file handle
1185 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301186 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301187 throws IOException {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301188 String path = getTempDirPath(getAbsoluteDirPath());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301189 File dir = new File(path);
1190 if (!dir.exists()) {
1191 dir.mkdirs();
1192 }
1193 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1194 if (!file.exists()) {
1195 file.createNewFile();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301196 } else {
1197 throw new IOException(fileName + " is reused due to YANG naming");
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301198 }
1199 return file;
1200 }
1201
1202 /**
1203 * Returns a temporary file handle for the specific file type.
1204 *
1205 * @param fileName file name
1206 * @return temporary file handle
1207 * @throws IOException when fails to create new file handle
1208 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301209 File getJavaFileHandle(String fileName)
1210 throws IOException {
Bharat saraswald50c6382016-07-14 21:57:13 +05301211 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getBaseCodePath());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301212 }
1213
1214 /**
1215 * Returns data from the temporary files.
1216 *
Bharat saraswalb551aae2016-07-14 15:18:20 +05301217 * @param file temporary file handle
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301218 * @param absolutePath absolute path
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301219 * @return stored data from temporary files
1220 * @throws IOException when failed to get data from the given file
1221 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301222 public String getTemporaryDataFromFileHandle(File file, String absolutePath)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301223 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301224
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301225 String path = getTempDirPath(absolutePath);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301226 if (new File(path + file.getName()).exists()) {
1227 return readAppendFile(path + file.getName(), EMPTY_STRING);
1228 } else {
1229 throw new IOException("Unable to get data from the given "
1230 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1231 }
1232 }
1233
1234 /**
1235 * Returns temporary directory path.
1236 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301237 * @param absolutePath absolute path
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301238 * @return directory path
1239 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301240 String getTempDirPath(String absolutePath) {
1241 return getPackageDirPathFromJavaJPackage(absolutePath) + SLASH + getGeneratedJavaClassName()
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301242 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1243 }
1244
1245 /**
1246 * Parses attribute to get the attribute string.
1247 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301248 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301249 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301250 * @return attribute string
1251 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301252 public String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301253 /*
1254 * TODO: check if this utility needs to be called or move to the caller
1255 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301256 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301257 String attributeAccessType = PRIVATE;
1258 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
1259 attributeAccessType = PROTECTED;
1260 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301261 if (attr.isQualifiedName()) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301262 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
1263 attr.getImportInfo().getClassInfo(),
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301264 attributeName, attr.isListAttr(), attributeAccessType);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301265 } else {
1266 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301267 attr.isListAttr(), attributeAccessType);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301268 }
1269 }
1270
1271 /**
1272 * Appends content to temporary file.
1273 *
1274 * @param file temporary file
1275 * @param data data to be appended
1276 * @throws IOException when fails to append to file
1277 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301278 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301279 throws IOException {
1280 try {
1281 insertDataIntoJavaFile(file, data);
1282 } catch (IOException ex) {
1283 throw new IOException("failed to write in temp file.");
1284 }
1285 }
1286
1287 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301288 * Adds parent's info to current node import list.
1289 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301290 * @param curNode current node for which import list needs to be updated
Bharat saraswal33dfa012016-05-17 19:59:16 +05301291 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301292 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301293 public void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301294 caseImportInfo = new JavaQualifiedTypeInfo();
1295 YangNode parent = getParentNodeInGenCode(curNode);
1296 if (!(parent instanceof JavaCodeGenerator)) {
1297 throw new TranslatorException("missing parent node to contain current node info in generated file");
1298 }
1299 if (!(curNode instanceof JavaFileInfoContainer)) {
1300 throw new TranslatorException("missing java file information to get the package details "
1301 + "of attribute corresponding to child node");
1302 }
Bharat saraswal33dfa012016-05-17 19:59:16 +05301303 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1304 pluginConfig.getConflictResolver())));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301305 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301306
1307 JavaFileInfo fileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1308
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301309 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301310 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301311 getCapitalCase(fileInfo.getJavaName()), fileInfo.getPackage());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301312 }
1313
1314 /**
1315 * Adds leaf attributes in generated files.
1316 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301317 * @param listOfLeaves list of YANG leaf
1318 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301319 * @throws IOException IO operation fail
1320 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301321 public void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301322 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301323 throws IOException {
1324 if (listOfLeaves != null) {
1325 for (YangLeaf leaf : listOfLeaves) {
1326 if (!(leaf instanceof JavaLeafInfoContainer)) {
1327 throw new TranslatorException("Leaf does not have java information");
1328 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301329 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(this, leaf, yangPluginConfig),
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301330 yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301331 }
1332 }
1333 }
1334
1335 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301336 * Returns java attribute for leaf.
1337 *
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301338 * @param tempJavaFragmentFiles temporary generated file
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301339 * @param leaf YANG leaf
1340 * @param yangPluginConfig plugin configurations
1341 * @return java attribute for leaf
1342 */
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301343 public static JavaAttributeInfo getJavaAttributeOfLeaf(TempJavaFragmentFiles tempJavaFragmentFiles, YangLeaf leaf,
1344 YangPluginConfig yangPluginConfig) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301345 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
1346 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
1347 javaLeaf.updateJavaQualifiedInfo();
1348 return getAttributeInfoForTheData(
1349 javaLeaf.getJavaQualifiedInfo(),
1350 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1351 javaLeaf.getDataType(),
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301352 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301353 false);
1354 }
1355
1356 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301357 * Adds leaf list's attributes in generated files.
1358 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301359 * @param listOfLeafList list of YANG leaves
1360 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301361 * @throws IOException IO operation fail
1362 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301363 public void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301364 throws IOException {
1365 if (listOfLeafList != null) {
1366 for (YangLeafList leafList : listOfLeafList) {
1367 if (!(leafList instanceof JavaLeafInfoContainer)) {
1368 throw new TranslatorException("Leaf-list does not have java information");
1369 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301370 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(this, leafList, yangPluginConfig),
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301371 yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301372 }
1373 }
1374 }
1375
1376 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301377 * Returns java attribute for leaf-list.
1378 *
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301379 * @param tempJavaFragmentFiles temporary generated file
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301380 * @param leafList YANG leaf-list
1381 * @param yangPluginConfig plugin configurations
1382 * @return java attribute for leaf-list
1383 */
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301384 public static JavaAttributeInfo getJavaAttributeOfLeafList(TempJavaFragmentFiles tempJavaFragmentFiles,
1385 YangLeafList leafList, YangPluginConfig yangPluginConfig) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301386 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
1387 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
1388 javaLeaf.updateJavaQualifiedInfo();
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301389 tempJavaFragmentFiles.getJavaImportData().setIfListImported(true);
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301390 return getAttributeInfoForTheData(
1391 javaLeaf.getJavaQualifiedInfo(),
1392 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1393 javaLeaf.getDataType(),
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301394 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301395 true);
1396 }
1397
1398 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301399 * Adds all the leaves in the current data model node as part of the
1400 * generated temporary file.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301401 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301402 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301403 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301404 * @throws IOException IO operation fail
1405 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301406 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301407 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301408 throws IOException {
1409 if (!(curNode instanceof YangLeavesHolder)) {
1410 throw new TranslatorException("Data model node does not have any leaves");
1411 }
1412 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301413 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1414 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301415 }
1416
1417 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301418 * Adds the new attribute info to the target generated temporary files.
1419 *
Bharat saraswalb551aae2016-07-14 15:18:20 +05301420 * @param newAttrInfo the attribute info that needs to be added to temporary files
Bharat saraswal33dfa012016-05-17 19:59:16 +05301421 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301422 * @throws IOException IO operation fail
1423 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301424 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301425 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301426 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301427 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301428 addAttribute(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301429 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301430
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301431 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301432 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301433 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301434
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301435 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301436 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301437 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301438
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301439 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1440 addSetterImpl(newAttrInfo);
1441 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301442
1443 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301444 addGetterImpl(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301445 }
1446 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1447 addHashCodeMethod(newAttrInfo);
1448 }
1449 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1450 addEqualsMethod(newAttrInfo);
1451 }
1452 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1453 addToStringMethod(newAttrInfo);
1454 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301455
1456 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Bharat saraswal96dfef02016-06-16 00:29:12 +05301457 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
1458 pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +05301459 /*
1460 * Create a new java attribute info with qualified information of
1461 * wrapper classes.
1462 */
1463 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1464 newAttrInfo.getAttributeName(),
1465 newAttrInfo.getAttributeType(),
1466 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
1467
1468 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301469 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301470 }
1471
1472 /**
1473 * Returns java class name.
1474 *
1475 * @param suffix for the class name based on the file type
1476 * @return java class name
1477 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301478 String getJavaClassName(String suffix) {
janani b4a6711a2016-05-17 13:12:22 +05301479 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301480 }
1481
1482 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301483 * Returns java class name.
1484 *
1485 * @return java class name
1486 */
1487 String getImplClassName() {
1488 return getCapitalCase(DEFAULT) + getCapitalCase(getJavaFileInfo().getJavaName());
1489 }
1490
1491 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301492 * Returns op param java class name.
1493 *
1494 * @return op param java class name
1495 */
1496 String getOpParamImplClassName() {
1497 return getCapitalCase(getJavaFileInfo().getJavaName()) + OPERATION;
1498 }
1499
1500 /**
1501 * Returns op param builder java class name.
1502 *
1503 * @return op param builder java class name
1504 */
1505 String getOpParamBuilderImplClassName() {
1506 return getCapitalCase(getJavaFileInfo().getJavaName()) + OPERATION + BUILDER;
1507 }
1508
1509 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301510 * Returns the directory path.
1511 *
1512 * @return directory path
1513 */
1514 private String getDirPath() {
1515 return getJavaFileInfo().getPackageFilePath();
1516 }
1517
1518 /**
1519 * Constructs java code exit.
1520 *
1521 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301522 * @param curNode current YANG node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301523 * @throws IOException when fails to generate java files
1524 */
1525 public void generateJavaFile(int fileType, YangNode curNode)
1526 throws IOException {
Bharat saraswalb551aae2016-07-14 15:18:20 +05301527
1528 if (curNode instanceof YangModule || curNode instanceof YangSubModule) {
1529 addResolvedAugmentedDataNodeImports(curNode);
1530 }
Bharat saraswald50c6382016-07-14 21:57:13 +05301531 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) curNode)
1532 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
Bharat saraswalb551aae2016-07-14 15:18:20 +05301533 List<String> imports = ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getBeanTempFiles()
1534 .getJavaImportData().getImports();
Bharat saraswal33dfa012016-05-17 19:59:16 +05301535
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301536 JavaFileInfo curInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1537
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301538 createPackage(curNode);
1539
1540 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301541 * Generate java code.
1542 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301543 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1544 BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301545
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301546 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301547 * Create interface file.
1548 */
1549 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1550 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301551 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1552 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301553 * Create builder interface file.
1554 */
1555 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1556 setBuilderInterfaceJavaFileHandle(
1557 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1558 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301559 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1560 isAttributePresent()));
1561 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301562 * Append builder interface file to interface file and close it.
1563 */
1564 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
Bharat saraswal96dfef02016-06-16 00:29:12 +05301565 validateLineLength(getInterfaceJavaFileHandle());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301566 }
1567 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301568
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301569 if (curNode instanceof YangCase) {
1570 removeCaseImport(imports);
1571 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301572
Bharat saraswald50c6382016-07-14 21:57:13 +05301573 if (curNode instanceof YangAugment) {
1574 removeAugmentedInfoImport(imports);
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301575 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301576 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301577 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301578 if (isAttributePresent()) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301579 addImportsToStringAndHasCodeMethods(imports, true);
Bharat saraswalb551aae2016-07-14 15:18:20 +05301580 }
1581 if (curNode instanceof YangAugmentableNode) {
Bharat saraswald50c6382016-07-14 21:57:13 +05301582 addImportsForAugmentableClass(imports, true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301583 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301584 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301585 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301586 * Create impl class file.
1587 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301588 setImplClassJavaFileHandle(getJavaFileHandle(getImplClassName()));
1589 setImplClassJavaFileHandle(
1590 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent(), imports));
1591
1592 /*
1593 * Create builder class file.
1594 */
1595 if ((fileType & BUILDER_CLASS_MASK) != 0) {
1596 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1597 setBuilderClassJavaFileHandle(
1598 generateBuilderClassFile(getBuilderClassJavaFileHandle(), curNode,
1599 isAttributePresent()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301600 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301601 * Append impl class to builder class and close it.
1602 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301603 mergeJavaFiles(getBuilderClassJavaFileHandle(), getImplClassJavaFileHandle());
1604 validateLineLength(getImplClassJavaFileHandle());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301605 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301606 insertDataIntoJavaFile(getImplClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswald50c6382016-07-14 21:57:13 +05301607 if (curNode instanceof YangAugmentableNode) {
1608 addImportsForAugmentableClass(imports, false);
1609 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301610 if (!(curNode instanceof YangModule)) {
1611 if (isAttributePresent()) {
1612 addImportsToStringAndHasCodeMethods(imports, false);
1613 }
Bharat saraswald50c6382016-07-14 21:57:13 +05301614 if (curNode instanceof YangAugmentableNode) {
1615 addYangAugmentedOpParamInfoImport(imports);
1616 }
1617 JavaQualifiedTypeInfo qualifiedTypeInfo = new JavaQualifiedTypeInfo();
1618 qualifiedTypeInfo.setClassInfo(getCapitalCase(DEFAULT) + getCapitalCase(getJavaFileInfo()
1619 .getJavaName()));
1620 qualifiedTypeInfo.setPkgInfo(getJavaFileInfo().getPackage());
1621 getJavaExtendsListHolder().addToExtendsList(qualifiedTypeInfo, curNode,
1622 tempJavaBeanFragmentFiles);
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301623 addBitsetImport(imports, true);
Bharat saraswald50c6382016-07-14 21:57:13 +05301624 if (curNode instanceof YangAugment) {
1625 addYangAugmentedOpParamInfoImport(imports);
1626 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301627 /*
1628 * Create impl class file.
1629 */
1630 setOpParamClassJavaFileHandle(getJavaFileHandle(getOpParamImplClassName()));
1631 setOpParamClassJavaFileHandle(
1632 generateOpParamImplClassFile(getOpParamClassJavaFileHandle(), curNode,
Bharat saraswald50c6382016-07-14 21:57:13 +05301633 isAttributePresent(), imports));
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301634
1635 /*
1636 * Create builder class file.
1637 */
1638 if ((fileType & BUILDER_CLASS_MASK) != 0) {
1639 setBuilderOpParmClassJavaFileHandle(getJavaFileHandle(getOpParamBuilderImplClassName()));
1640 setBuilderOpParmClassJavaFileHandle(
1641 generateOpParamBuilderClassFile(getBuilderOpParmClassJavaFileHandle(), curNode,
Bharat saraswald50c6382016-07-14 21:57:13 +05301642 isAttributePresent()));
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301643 /*
1644 * Append impl class to builder class and close it.
1645 */
1646 mergeJavaFiles(getBuilderOpParmClassJavaFileHandle(), getOpParamClassJavaFileHandle());
1647 validateLineLength(getOpParamClassJavaFileHandle());
1648
1649 addBitsetImport(imports, false);
1650 }
1651 insertDataIntoJavaFile(getOpParamClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswald50c6382016-07-14 21:57:13 +05301652
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301653 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301654 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301655 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301656 * Close all the file handles.
1657 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301658 freeTemporaryResources(false);
1659 }
1660
Bharat saraswald50c6382016-07-14 21:57:13 +05301661 /*Adds import for YANG augmented op param info.*/
1662 private void addYangAugmentedOpParamInfoImport(List<String> imports) {
1663 imports.add(getJavaImportData().getYangAugmentedOpParamInfoImport());
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301664 }
1665
1666 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301667 * Adds imports for ToString and HashCodeMethod.
1668 *
Bharat saraswald50c6382016-07-14 21:57:13 +05301669 * @param imports import list
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301670 * @param operation add or remove
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301671 */
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301672 public void addImportsToStringAndHasCodeMethods(List<String> imports, boolean operation) {
1673 if (operation) {
1674 imports.add(getJavaImportData().getImportForHashAndEquals());
1675 imports.add(getJavaImportData().getImportForToString());
1676 } else {
1677 imports.remove(getJavaImportData().getImportForHashAndEquals());
1678 imports.remove(getJavaImportData().getImportForToString());
1679 }
1680 }
1681
1682 /**
1683 * Adds imports for bitset method.
1684 *
Bharat saraswald50c6382016-07-14 21:57:13 +05301685 * @param imports import list
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301686 * @param operation add or remove
1687 */
1688 public void addBitsetImport(List<String> imports, boolean operation) {
1689 if (operation) {
1690 imports.add(getJavaImportData().getImportForToBitSet());
1691 } else {
1692 imports.remove(getJavaImportData().getImportForToBitSet());
1693 }
Bharat saraswalb551aae2016-07-14 15:18:20 +05301694 }
1695
1696
1697 /**
1698 * Adds import for map and hash map.
1699 *
Bharat saraswald50c6382016-07-14 21:57:13 +05301700 * @param imports import list
1701 * @param operations true for adding and false for deletion
Bharat saraswalb551aae2016-07-14 15:18:20 +05301702 */
Bharat saraswald50c6382016-07-14 21:57:13 +05301703 public void addImportsForAugmentableClass(List<String> imports, boolean operations) {
1704 if (operations) {
1705 imports.add(getJavaImportData().getHashMapImport());
1706 imports.add(getJavaImportData().getMapImport());
1707 imports.add(getJavaImportData().getYangAugmentedInfoImport());
1708 } else {
1709 imports.remove(getJavaImportData().getHashMapImport());
1710 imports.remove(getJavaImportData().getMapImport());
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301711 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301712 }
1713
1714 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301715 * Removes case import info from import list.
1716 *
1717 * @param imports list of imports
1718 * @return import for class
1719 */
1720 private List<String> removeCaseImport(List<String> imports) {
1721 if (imports != null && caseImportInfo != null) {
1722 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1723 SEMI_COLAN + NEW_LINE;
1724 imports.remove(caseImport);
1725 }
1726 return imports;
1727 }
1728
1729 /**
Bharat saraswald50c6382016-07-14 21:57:13 +05301730 * Removes case import info from import list.
1731 *
1732 * @param imports list of imports
1733 * @return import for class
1734 */
1735 private void removeAugmentedInfoImport(List<String> imports) {
1736 imports.remove(getJavaImportData().getYangAugmentedInfoImport());
1737
1738 for (JavaQualifiedTypeInfo type : getJavaImportData().getImportSet()) {
1739 if (type.getClassInfo().equals(YANG_AUGMENTED_INFO)) {
1740 getJavaImportData().getImportSet().remove(type);
1741 getJavaExtendsListHolder().getExtendsList().remove(type);
1742 }
1743 }
1744 }
1745
1746 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301747 * Removes all temporary file handles.
1748 *
Bharat saraswalb551aae2016-07-14 15:18:20 +05301749 * @param isErrorOccurred when translator fails to generate java files we need to close all open file handles
1750 * include temporary files and java files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301751 * @throws IOException when failed to delete the temporary files
1752 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301753 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301754 throws IOException {
1755 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301756 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301757 * Close all java file handles and when error occurs delete the files.
1758 */
1759 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1760 closeFile(getInterfaceJavaFileHandle(), isError);
1761 }
1762 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301763 closeFile(getBuilderClassJavaFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301764 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301765 if ((getGeneratedJavaFiles() & OPERATION_BUILDER_CLASS_MASK) != 0) {
1766 closeFile(getBuilderOpParmClassJavaFileHandle(), true);
1767 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301768 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1769 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1770 }
1771 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301772 closeFile(getImplClassJavaFileHandle(), isError);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301773 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301774
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301775 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301776 * Close all temporary file handles and delete the files.
1777 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301778 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1779 closeFile(getGetterImplTempFileHandle(), true);
1780 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301781 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1782 closeFile(getAttributesTempFileHandle(), true);
1783 }
1784 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1785 closeFile(getHashCodeImplTempFileHandle(), true);
1786 }
1787 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1788 closeFile(getToStringImplTempFileHandle(), true);
1789 }
1790 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1791 closeFile(getEqualsImplTempFileHandle(), true);
1792 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301793 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1794 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301795 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301796 if ((getGeneratedTempFiles() & AUGMENTE_CLASS_CONSTRUCTOR_MASK) != 0) {
1797 closeFile(getAugmentConstructorImplTempFileHandle(), true);
1798 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301799 }
1800
1801 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +05301802 * Returns if the attribute needs to be accessed in a qualified manner or not, if it needs to be imported, then the
1803 * same needs to be done.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301804 *
1805 * @param importInfo import info for the current attribute being added
1806 * @return status of the qualified access to the attribute
1807 */
1808 public boolean getIsQualifiedAccessOrAddToImportList(
1809 JavaQualifiedTypeInfo importInfo) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301810
1811 return getJavaImportData().addImportInfo(importInfo, getGeneratedJavaClassName(),
1812 getJavaFileInfo().getPackage());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301813 }
1814
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301815 /**
1816 * Returns java file handle for op param class file.
1817 *
1818 * @return java file handle for op param class file
1819 */
1820 public File getOpParamClassJavaFileHandle() {
1821 return opParamClassJavaFileHandle;
1822 }
1823
1824 /**
1825 * Sets the java file handle for op param class file.
1826 *
Bharat saraswald50c6382016-07-14 21:57:13 +05301827 * @param opParamClassJavaFileHandle java file handle
Shankara-Huaweib9999eb2016-07-14 16:53:09 +05301828 */
1829 public void setOpParamClassJavaFileHandle(File opParamClassJavaFileHandle) {
1830 this.opParamClassJavaFileHandle = opParamClassJavaFileHandle;
1831 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301832}