blob: 58a0e28f5a666a383206c242dfd7b4af9a3840b1 [file] [log] [blame]
Vinod Kumar S79a374b2016-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 S79a374b2016-04-30 21:09:15 +053020import java.util.List;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053021
Bharat saraswale50edca2016-08-05 01:58:25 +053022import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswalaf413b82016-07-14 15:18:20 +053023import org.onosproject.yangutils.datamodel.YangAugmentableNode;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053024import org.onosproject.yangutils.datamodel.YangCase;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangLeafList;
27import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Bharat saraswalaf413b82016-07-14 15:18:20 +053028import org.onosproject.yangutils.datamodel.YangModule;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053029import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalaf413b82016-07-14 15:18:20 +053030import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswale50edca2016-08-05 01:58:25 +053031import org.onosproject.yangutils.utils.io.YangPluginConfig;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
33import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Shankara-Huaweib7564772016-08-02 18:13:13 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGroupingTranslator;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053035import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053036
Bharat saraswalc2d3be12016-06-16 00:29:12 +053037import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
39import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.DEFAULT_CLASS_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053041import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Bharat saraswalaf413b82016-07-14 15:18:20 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053043import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
44import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053045import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_IMPL_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_INTERFACE_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Bharat saraswale50edca2016-08-05 01:58:25 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EDIT_CONTENT_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053050import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswale50edca2016-08-05 01:58:25 +053051import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK;
52import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_LEAF_MASK;
53import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_NODES_MASK;
Bharat saraswal250a7472016-05-12 13:16:57 +053054import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar S79a374b2016-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;
Bharat saraswal8beac342016-08-04 02:00:03 +053058import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053059import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
60import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
61import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
62import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweib7564772016-08-02 18:13:13 +053063import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
64import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053065import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo;
Bharat saraswal8beac342016-08-04 02:00:03 +053066import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
67import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefinition;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053068import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswalaf413b82016-07-14 15:18:20 +053069import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.sortImports;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053070import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
71import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053072import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateDefaultClassFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053073import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053074import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswald14cbe82016-07-14 13:26:18 +053075import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053076import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddToListMethodImpl;
77import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddToListMethodInterface;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053078import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar S79a374b2016-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 saraswal250a7472016-05-12 13:16:57 +053081import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar S79a374b2016-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;
Bharat saraswale50edca2016-08-05 01:58:25 +053085import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIsFilerContentMatchForLeaf;
86import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIsFilerContentMatchForLeafList;
87import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIsFilterContentForNodes;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053088import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053089import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
90import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
91import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053092import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
Bharat saraswal8beac342016-08-04 02:00:03 +053093import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST_IMPORT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053094import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
Bharat saraswald14cbe82016-07-14 13:26:18 +053095import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053096import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
97import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053098import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
Bharat saraswal8beac342016-08-04 02:00:03 +053099import static org.onosproject.yangutils.utils.UtilConstants.INVOCATION_TARGET_EXCEPTION_IMPORT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530100import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal8beac342016-08-04 02:00:03 +0530101import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530102import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530103import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
104import static org.onosproject.yangutils.utils.UtilConstants.PROTECTED;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530105import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530106import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530107import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530108import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswal8beac342016-08-04 02:00:03 +0530109import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ADD_TO_LIST;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530110import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530111import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530112import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530113import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
114import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
115import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530116import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
117import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530118import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530119
120/**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530121 * Represents implementation of java code fragments temporary implementations. Manages the common temp file required for
122 * Java file(s) generated.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530123 */
124public class TempJavaFragmentFiles {
Bharat saraswal250a7472016-05-12 13:16:57 +0530125
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530126 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530127 * File type extension for java classes.
128 */
129 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswal250a7472016-05-12 13:16:57 +0530130
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530131 /**
132 * File type extension for temporary classes.
133 */
134 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswal250a7472016-05-12 13:16:57 +0530135
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530136 /**
137 * Folder suffix for temporary files folder.
138 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530139 private static final String TEMP_FOLDER_NAME_SUFFIX = "-Temp";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530140
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530141 /**
142 * File name for getter method.
143 */
144 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530145
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530146 /**
147 * File name for setter method.
148 */
149 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530150
151 /**
152 * File name for getter method implementation.
153 */
154 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
155
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530156 /**
157 * File name for setter method implementation.
158 */
159 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530160
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530161 /**
162 * File name for attributes.
163 */
164 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswal250a7472016-05-12 13:16:57 +0530165
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530166 /**
167 * File name for to string method.
168 */
169 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswal250a7472016-05-12 13:16:57 +0530170
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530171 /**
172 * File name for hash code method.
173 */
174 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswal250a7472016-05-12 13:16:57 +0530175
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530176 /**
177 * File name for equals method.
178 */
179 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530180
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530181 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530182 * File name for from string method.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530183 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530184 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530185
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530186 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530187 * File name for from add to list interface method.
188 */
189 private static final String ADD_TO_LIST_INTERFACE_METHOD_FILE_NAME = "addToList";
190
191 /**
192 * File name for from add to list impl method.
193 */
194 private static final String ADD_TO_LIST_IMPL_METHOD_FILE_NAME = "addToListImpl";
195
196 /**
197 * File name for from leaf identifier attributes.
198 */
199 private static final String LEAF_IDENTIFIER_ATTRIBUTES_FILE_NAME = "leafIdentifierAtr";
200
201 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530202 * File name for is filter content leaf match.
203 */
204 private static final String FILTER_CONTENT_MATCH_LEAF_FILE_NAME = "isFilterContentMatchLeafMask";
205
206 /**
207 * File name for is filter content leaf-list match.
208 */
209 private static final String FILTER_CONTENT_MATCH_LEAF_LIST_FILE_NAME = "isFilterContentMatchLeafListMask";
210
211 /**
212 * File name for is filter content node match.
213 */
214 private static final String FILTER_CONTENT_MATCH_NODE_FILE_NAME = "isFilterContentMatchNodeMask";
215
216 /**
217 * File name for edit content file.
218 */
219 private static final String EDIT_CONTENT_FILE_NAME = "editContentFile";
220
221 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530222 * File name for interface java file name suffix.
223 */
224 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswal250a7472016-05-12 13:16:57 +0530225
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530226 /**
227 * File name for builder interface file name suffix.
228 */
229 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswal250a7472016-05-12 13:16:57 +0530230
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530231 /**
232 * File name for builder class file name suffix.
233 */
234 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswal250a7472016-05-12 13:16:57 +0530235
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530236 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530237 * Information about the java files being generated.
238 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530239 private JavaFileInfoTranslator javaFileInfo;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530240
241 /**
242 * Imported class info.
243 */
244 private JavaImportData javaImportData;
245
246 /**
247 * The variable which guides the types of temporary files generated using the temporary generated file types mask.
248 */
249 private int generatedTempFiles;
250
251 /**
252 * Absolute path where the target java file needs to be generated.
253 */
254 private String absoluteDirPath;
255
256 /**
257 * Contains all the interface(s)/class name which will be extended by generated files.
258 */
259 private JavaExtendsListHolder javaExtendsListHolder;
260
Bharat saraswald14cbe82016-07-14 13:26:18 +0530261 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530262 * Java file handle for interface file.
263 */
264 private File interfaceJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530265
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530266 /**
267 * Java file handle for builder interface file.
268 */
269 private File builderInterfaceJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530270
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530271 /**
272 * Java file handle for builder class file.
273 */
274 private File builderClassJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530275
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530276 /**
277 * Java file handle for impl class file.
278 */
279 private File implClassJavaFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530280
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530281 /**
282 * Temporary file handle for attribute.
283 */
284 private File attributesTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530285
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530286 /**
287 * Temporary file handle for getter of interface.
288 */
289 private File getterInterfaceTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530290
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530291 /**
292 * Temporary file handle for setter of interface.
293 */
294 private File setterInterfaceTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530295
296 /**
297 * Temporary file handle for getter of class.
298 */
299 private File getterImplTempFileHandle;
300
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530301 /**
302 * Temporary file handle for setter of class.
303 */
304 private File setterImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530305
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530306 /**
307 * Temporary file handle for hash code method of class.
308 */
309 private File hashCodeImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530310
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530311 /**
312 * Temporary file handle for equals method of class.
313 */
314 private File equalsImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530315
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530316 /**
317 * Temporary file handle for to string method of class.
318 */
319 private File toStringImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530320
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530321 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530322 * Temporary file handle for from string method of class.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530323 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530324 private File fromStringImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530325
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530326 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530327 * Temporary file handle for add to list interface method of class.
328 */
329 private File addToListInterfaceTempFileHandle;
330
331 /**
332 * Temporary file handle for add to list impl method of class.
333 */
334 private File addToListImplTempFileHandle;
335
336 /**
337 * Temporary file handle for leaf id attributes of enum.
338 */
339 private File leafIdAttributeTempFileHandle;
340
341 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530342 * Temporary file handle for is content match method for leaf-list.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530343 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530344 private File isContentMatchLeafListTempFileHandle;
345
346 /**
347 * Temporary file handle for is content match method for node.
348 */
349 private File isContentMatchNodeTempFileHandle;
350
351 /**
352 * Temporary file handle for is content match method for leaf.
353 */
354 private File isContentMatchLeafTempFileHandle;
355
356 /**
357 * Temporary file handle for edit content file.
358 */
359 private File editContentTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530360
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530361 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530362 * Leaf count.
363 */
364 private int leafCount = 0;
365
366 /**
367 * If current node is root node.
368 */
369 private boolean isRooNode;
370
371 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530372 * Is attribute added.
373 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530374 private boolean isAttributePresent;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530375
Bharat saraswalaf413b82016-07-14 15:18:20 +0530376 TempJavaFragmentFiles() {
377 }
378
379 /**
380 * Creates an instance of temporary java code fragment.
381 *
382 * @param javaFileInfo generated java file information
383 * @throws IOException when fails to create new file handle
384 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530385 TempJavaFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Bharat saraswalaf413b82016-07-14 15:18:20 +0530386 throws IOException {
387 setJavaExtendsListHolder(new JavaExtendsListHolder());
388 setJavaImportData(new JavaImportData());
389 setJavaFileInfo(javaFileInfo);
Bharat saraswal8beac342016-08-04 02:00:03 +0530390 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
391 getJavaFileInfo().getPackageFilePath()));
392
Bharat saraswalaf413b82016-07-14 15:18:20 +0530393 /*
394 * Initialize getter when generation file type matches to interface
395 * mask.
396 */
397 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
398 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
Bharat saraswal8beac342016-08-04 02:00:03 +0530399 addGeneratedTempFile(ADD_TO_LIST_INTERFACE_MASK);
400 addGeneratedTempFile(LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530401 }
402
403 /*
404 * Initialize getter and setter when generation file type matches to
405 * builder interface mask.
406 */
407 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
408 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
409 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
410 }
411
412 /*
413 * Initialize getterImpl, setterImpl and attributes when generation file
414 * type matches to builder class mask.
415 */
416 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
417 addGeneratedTempFile(ATTRIBUTES_MASK);
418 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
419 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530420 }
421
422 /*
423 * Initialize getterImpl, attributes, constructor, hash code, equals and
424 * to strings when generation file type matches to impl class mask.
425 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530426 if ((getGeneratedJavaFiles() & DEFAULT_CLASS_MASK) != 0) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530427 addGeneratedTempFile(ATTRIBUTES_MASK);
428 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
429 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
430 addGeneratedTempFile(EQUALS_IMPL_MASK);
431 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswal8beac342016-08-04 02:00:03 +0530432 addGeneratedTempFile(ADD_TO_LIST_IMPL_MASK);
Bharat saraswale50edca2016-08-05 01:58:25 +0530433 addGeneratedTempFile(FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK);
434 addGeneratedTempFile(FILTER_CONTENT_MATCH_FOR_LEAF_MASK);
435 addGeneratedTempFile(FILTER_CONTENT_MATCH_FOR_NODES_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530436 }
437
438 /*
439 * Initialize temp files to generate type class.
440 */
441 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
442 addGeneratedTempFile(ATTRIBUTES_MASK);
443 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
444 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
445 addGeneratedTempFile(EQUALS_IMPL_MASK);
446 addGeneratedTempFile(TO_STRING_IMPL_MASK);
447 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
448 }
449
450 /*
451 * Initialize temp files to generate enum class.
452 */
453 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
454 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
455 }
456
457 /*
458 * Set temporary file handles.
459 */
460 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
461 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
462 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530463 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
464 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
465 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530466 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
467 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
468 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530469 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
470 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
471 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530472 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
473 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
474 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530475 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
476 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
477 }
478 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
479 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
480 }
481 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
482 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
483 }
484 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
485 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
486 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530487 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0) {
488 setAddToListInterfaceTempFileHandle(getTemporaryFileHandle(ADD_TO_LIST_INTERFACE_METHOD_FILE_NAME));
489 }
490 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0) {
491 setAddToListImplTempFileHandle(getTemporaryFileHandle(ADD_TO_LIST_IMPL_METHOD_FILE_NAME));
492 }
493 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0) {
494 setLeafIdAttributeTempFileHandle(getTemporaryFileHandle(LEAF_IDENTIFIER_ATTRIBUTES_FILE_NAME));
495 }
Bharat saraswale50edca2016-08-05 01:58:25 +0530496 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_MASK) != 0) {
497 setIsContentMatchLeafTempFileHandle(getTemporaryFileHandle(FILTER_CONTENT_MATCH_LEAF_FILE_NAME));
498 }
499 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK) != 0) {
500 setIsContentMatchLeafListTempFileHandle(getTemporaryFileHandle(FILTER_CONTENT_MATCH_LEAF_LIST_FILE_NAME));
501 }
502 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_NODES_MASK) != 0) {
503 setIsContentMatchNodeTempFileHandle(getTemporaryFileHandle(FILTER_CONTENT_MATCH_NODE_FILE_NAME));
504 }
505 if ((getGeneratedTempFiles() & EDIT_CONTENT_MASK) != 0) {
506 setEditContentTempFileHandle(getTemporaryFileHandle(EDIT_CONTENT_FILE_NAME));
507 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530508 }
509
510 /**
511 * Adds current node info as and attribute to the parent generated file.
512 *
513 * @param curNode current node which needs to be added as an attribute in the parent generated code
514 * @param isList is list construct
515 * @param pluginConfig plugin configurations
516 * @throws IOException IO operation exception
517 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530518 static void addCurNodeInfoInParentTempFile(YangNode curNode,
519 boolean isList, YangPluginConfig pluginConfig)
Bharat saraswalaf413b82016-07-14 15:18:20 +0530520 throws IOException {
521 YangNode parent = getParentNodeInGenCode(curNode);
522 if (!(parent instanceof JavaCodeGenerator)) {
523 throw new TranslatorException("missing parent node to contain current node info in generated file");
524 }
525
Shankara-Huaweib7564772016-08-02 18:13:13 +0530526 if (parent instanceof YangJavaGroupingTranslator) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530527 /*
528 * In case of grouping, there is no need to add the information, it
529 * will be taken care in uses
530 */
531 return;
532 }
533 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) parent)
534 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
535
536 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
537 parent, isList, tempJavaBeanFragmentFiles);
Bharat saraswal8beac342016-08-04 02:00:03 +0530538 tempJavaBeanFragmentFiles.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530539 }
540
541 /**
542 * Creates an attribute info object corresponding to a data model node and return it.
543 *
544 * @param curNode current data model node for which the java code generation is being handled
545 * @param targetNode target node in which the current node is an attribute
546 * @param isListNode is the current added attribute needs to be a list
547 * @param tempJavaFragmentFiles temp java fragment files
548 * @return AttributeInfo attribute details required to add in temporary files
549 */
550 public static JavaAttributeInfo getCurNodeAsAttributeInTarget(YangNode curNode,
551 YangNode targetNode, boolean isListNode,
552 TempJavaFragmentFiles tempJavaFragmentFiles) {
553 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
554 if (curNodeName == null) {
555 updateJavaFileInfo(curNode, null);
556 curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
557 }
558
559 /*
560 * Get the import info corresponding to the attribute for import in
561 * generated java files or qualified access
562 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530563 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
Bharat saraswalaf413b82016-07-14 15:18:20 +0530564 getCapitalCase(curNodeName));
565 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
566 throw new TranslatorException("Parent node does not have file info");
567 }
568 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
Bharat saraswale50edca2016-08-05 01:58:25 +0530569 JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530570
571 boolean isQualified;
Bharat saraswalaab24b92016-08-02 18:43:16 +0530572 if ((tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles)
Bharat saraswal8beac342016-08-04 02:00:03 +0530573 && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE))
574 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530575
576 isQualified = true;
577 } else {
578 String className;
Bharat saraswalaab24b92016-08-02 18:43:16 +0530579 if (tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530580 className = getCapitalCase(fileInfo.getJavaName()) + SERVICE;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530581 } else {
582 className = getCapitalCase(fileInfo.getJavaName());
583 }
584
585 isQualified = parentImportData.addImportInfo(qualifiedTypeInfo,
586 className, fileInfo.getPackage());
587 }
588
589 if (isListNode) {
590 parentImportData.setIfListImported(true);
591 }
592
593 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
594 }
595
596 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530597 * Returns java attribute for leaf.
598 *
599 * @param tempJavaFragmentFiles temporary generated file
600 * @param leaf YANG leaf
601 * @param yangPluginConfig plugin configurations
602 * @return java attribute for leaf
603 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530604 private static JavaAttributeInfo getJavaAttributeOfLeaf(TempJavaFragmentFiles tempJavaFragmentFiles, YangLeaf leaf,
605 YangPluginConfig yangPluginConfig) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530606 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
607 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
608 javaLeaf.updateJavaQualifiedInfo();
609 return getAttributeInfoForTheData(
610 javaLeaf.getJavaQualifiedInfo(),
611 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
612 javaLeaf.getDataType(),
613 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
614 false);
615 }
616
617 /**
618 * Returns java attribute for leaf-list.
619 *
620 * @param tempJavaFragmentFiles temporary generated file
621 * @param leafList YANG leaf-list
622 * @param yangPluginConfig plugin configurations
623 * @return java attribute for leaf-list
624 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530625 private static JavaAttributeInfo getJavaAttributeOfLeafList(TempJavaFragmentFiles tempJavaFragmentFiles,
626 YangLeafList leafList,
627 YangPluginConfig yangPluginConfig) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530628 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
629 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
630 javaLeaf.updateJavaQualifiedInfo();
631 tempJavaFragmentFiles.getJavaImportData().setIfListImported(true);
632 return getAttributeInfoForTheData(
633 javaLeaf.getJavaQualifiedInfo(),
634 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
635 javaLeaf.getDataType(),
636 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
637 true);
638 }
639
Bharat saraswal039f59c2016-07-14 21:57:13 +0530640 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530641 * Retrieves the absolute path where the file needs to be generated.
642 *
643 * @return absolute path where the file needs to be generated
644 */
645 private String getAbsoluteDirPath() {
646 return absoluteDirPath;
647 }
648
649 /**
650 * Sets absolute path where the file needs to be generated.
651 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530652 * @param absoluteDirPath absolute path where the file needs to be generated.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530653 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530654 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530655 this.absoluteDirPath = absoluteDirPath;
656 }
657
658 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530659 * Retrieves the generated java file information.
660 *
661 * @return generated java file information
662 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530663 public JavaFileInfoTranslator getJavaFileInfo() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530664 return javaFileInfo;
665 }
666
667 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530668 * Sets the generated java file information.
669 *
670 * @param javaFileInfo generated java file information
671 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530672 public void setJavaFileInfo(JavaFileInfoTranslator javaFileInfo) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530673 this.javaFileInfo = javaFileInfo;
674 }
675
676 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530677 * Retrieves the generated temp files.
678 *
679 * @return generated temp files
680 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530681 int getGeneratedTempFiles() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530682 return generatedTempFiles;
683 }
684
685 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530686 * Sets generated file files.
687 *
688 * @param fileType generated file type
689 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530690 private void setGeneratedTempFiles(int fileType) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530691 generatedTempFiles = fileType;
692 }
693
694 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530695 * Adds to generated temporary files.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530696 *
697 * @param generatedTempFile generated file
698 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530699 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530700 generatedTempFiles |= generatedTempFile;
Bharat saraswal250a7472016-05-12 13:16:57 +0530701 setGeneratedTempFiles(generatedTempFiles);
702 }
703
704 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530705 * Retrieves the generated Java files.
706 *
707 * @return generated Java files
708 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530709 int getGeneratedJavaFiles() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530710 return getJavaFileInfo().getGeneratedFileTypes();
711 }
712
713 /**
714 * Retrieves the mapped Java class name.
715 *
716 * @return mapped Java class name
717 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530718 String getGeneratedJavaClassName() {
janani b703cfe42016-05-17 13:12:22 +0530719 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530720 }
721
722 /**
723 * Retrieves the import data for the generated Java file.
724 *
725 * @return import data for the generated Java file
726 */
727 public JavaImportData getJavaImportData() {
728 return javaImportData;
729 }
730
731 /**
732 * Sets import data for the generated Java file.
733 *
734 * @param javaImportData import data for the generated Java file
735 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530736 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530737 this.javaImportData = javaImportData;
738 }
739
740 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530741 * Retrieves the status of any attributes added.
742 *
743 * @return status of any attributes added
744 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530745 boolean isAttributePresent() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530746 return isAttributePresent;
747 }
748
749 /**
750 * Sets status of any attributes added.
751 *
752 * @param attributePresent status of any attributes added
753 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530754 private void setAttributePresent(boolean attributePresent) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530755 isAttributePresent = attributePresent;
756 }
757
758 /**
759 * Returns getter methods's temporary file handle.
760 *
761 * @return temporary file handle
762 */
763 public File getGetterInterfaceTempFileHandle() {
764 return getterInterfaceTempFileHandle;
765 }
766
767 /**
768 * Sets to getter method's temporary file handle.
769 *
770 * @param getterForInterface file handle for to getter method
771 */
772 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
773 getterInterfaceTempFileHandle = getterForInterface;
774 }
775
776 /**
777 * Returns setter method's temporary file handle.
778 *
779 * @return temporary file handle
780 */
781 public File getSetterInterfaceTempFileHandle() {
782 return setterInterfaceTempFileHandle;
783 }
784
785 /**
786 * Sets to setter method's temporary file handle.
787 *
788 * @param setterForInterface file handle for to setter method
789 */
790 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
791 setterInterfaceTempFileHandle = setterForInterface;
792 }
793
794 /**
795 * Returns setter method's impl's temporary file handle.
796 *
797 * @return temporary file handle
798 */
799 public File getSetterImplTempFileHandle() {
800 return setterImplTempFileHandle;
801 }
802
803 /**
804 * Sets to setter method's impl's temporary file handle.
805 *
806 * @param setterImpl file handle for to setter method's implementation class
807 */
808 private void setSetterImplTempFileHandle(File setterImpl) {
809 setterImplTempFileHandle = setterImpl;
810 }
811
812 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530813 * Returns from string method's temporary file handle.
814 *
815 * @return from string method's temporary file handle
816 */
817 public File getFromStringImplTempFileHandle() {
818 return fromStringImplTempFileHandle;
819 }
820
821 /**
822 * Sets from string method's temporary file handle.
823 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530824 * @param fromStringImplTempFileHandle from string method's temporary file handle
Bharat saraswal250a7472016-05-12 13:16:57 +0530825 */
826 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
827 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
828 }
829
830 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530831 * Returns java file handle for interface file.
832 *
833 * @return java file handle for interface file
834 */
835 private File getInterfaceJavaFileHandle() {
836 return interfaceJavaFileHandle;
837 }
838
839 /**
840 * Sets the java file handle for interface file.
841 *
842 * @param interfaceJavaFileHandle java file handle
843 */
844 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
845 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
846 }
847
848 /**
849 * Returns java file handle for builder interface file.
850 *
851 * @return java file handle for builder interface file
852 */
853 private File getBuilderInterfaceJavaFileHandle() {
854 return builderInterfaceJavaFileHandle;
855 }
856
857 /**
858 * Sets the java file handle for builder interface file.
859 *
860 * @param builderInterfaceJavaFileHandle java file handle
861 */
862 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
863 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
864 }
865
866 /**
867 * Returns java file handle for builder class file.
868 *
869 * @return java file handle for builder class file
870 */
871 private File getBuilderClassJavaFileHandle() {
872 return builderClassJavaFileHandle;
873 }
874
875 /**
876 * Sets the java file handle for builder class file.
877 *
878 * @param builderClassJavaFileHandle java file handle
879 */
880 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
881 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
882 }
883
884 /**
885 * Returns java file handle for impl class file.
886 *
887 * @return java file handle for impl class file
888 */
889 private File getImplClassJavaFileHandle() {
890 return implClassJavaFileHandle;
891 }
892
893 /**
894 * Sets the java file handle for impl class file.
895 *
896 * @param implClassJavaFileHandle java file handle
897 */
898 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
899 this.implClassJavaFileHandle = implClassJavaFileHandle;
900 }
901
902 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530903 * Returns attribute's temporary file handle.
904 *
905 * @return temporary file handle
906 */
907 public File getAttributesTempFileHandle() {
908 return attributesTempFileHandle;
909 }
910
911 /**
912 * Sets attribute's temporary file handle.
913 *
914 * @param attributeForClass file handle for attribute
915 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530916 private void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530917 attributesTempFileHandle = attributeForClass;
918 }
919
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530920 /**
921 * Returns getter method's impl's temporary file handle.
922 *
923 * @return temporary file handle
924 */
925 public File getGetterImplTempFileHandle() {
926 return getterImplTempFileHandle;
927 }
928
929 /**
930 * Sets to getter method's impl's temporary file handle.
931 *
932 * @param getterImpl file handle for to getter method's impl
933 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530934 private void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530935 getterImplTempFileHandle = getterImpl;
936 }
937
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530938 /**
939 * Returns hash code method's temporary file handle.
940 *
941 * @return temporary file handle
942 */
943 public File getHashCodeImplTempFileHandle() {
944 return hashCodeImplTempFileHandle;
945 }
946
947 /**
948 * Sets hash code method's temporary file handle.
949 *
950 * @param hashCodeMethod file handle for hash code method
951 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530952 private void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530953 hashCodeImplTempFileHandle = hashCodeMethod;
954 }
955
956 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530957 * Returns equals method's temporary file handle.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530958 *
959 * @return temporary file handle
960 */
961 public File getEqualsImplTempFileHandle() {
962 return equalsImplTempFileHandle;
963 }
964
965 /**
966 * Sets equals method's temporary file handle.
967 *
968 * @param equalsMethod file handle for to equals method
969 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530970 private void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530971 equalsImplTempFileHandle = equalsMethod;
972 }
973
974 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530975 * Returns to string method's temporary file handle.
976 *
977 * @return temporary file handle
978 */
979 public File getToStringImplTempFileHandle() {
980 return toStringImplTempFileHandle;
981 }
982
983 /**
984 * Sets to string method's temporary file handle.
985 *
986 * @param toStringMethod file handle for to string method
987 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530988 private void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530989 toStringImplTempFileHandle = toStringMethod;
990 }
991
992 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530993 * Returns java extends list holder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530994 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530995 * @return java extends list holder
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530996 */
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530997 public JavaExtendsListHolder getJavaExtendsListHolder() {
998 return javaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530999 }
1000
1001 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301002 * Sets java extends list holder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301003 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301004 * @param javaExtendsListHolder java extends list holder
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301005 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301006 void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301007 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301008 }
1009
1010 /**
Bharat saraswale50edca2016-08-05 01:58:25 +05301011 * Adds is filter content match for leaf.
1012 *
1013 * @param attr java attribute
1014 * @throws IOException when fails to do IO operations
1015 */
1016 private void addIsFilerForLeaf(JavaAttributeInfo attr)
1017 throws IOException {
1018 appendToFile(getIsContentMatchLeafTempFileHandle(),
1019 getIsFilerContentMatchForLeaf(attr, attr.getAttributeType()) + NEW_LINE);
1020 }
1021
1022 /**
1023 * Adds is filter content match for leaf-list.
1024 *
1025 * @param attr java attribute
1026 * @throws IOException when fails to do IO operations
1027 */
1028 private void addIsFilerForLeafList(JavaAttributeInfo attr)
1029 throws IOException {
1030 appendToFile(getIsContentMatchLeafTempFileHandle(),
1031 getIsFilerContentMatchForLeafList(attr) + NEW_LINE);
1032 }
1033
1034 /**
1035 * Adds is filter content match for nodes.
1036 *
1037 * @param attr java attribute
1038 * @throws IOException when fails to do IO operations
1039 */
1040 private void addIsFilerForNode(JavaAttributeInfo attr)
1041 throws IOException {
1042 appendToFile(getIsContentMatchLeafTempFileHandle(),
1043 getIsFilterContentForNodes(attr.getAttributeName(), attr.isListAttr()) + NEW_LINE);
1044 }
1045
1046 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301047 * Adds attribute for class.
1048 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301049 * @param attr attribute info
1050 * @param yangPluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301051 * @throws IOException when fails to append to temporary file
1052 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301053 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301054 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301055 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
1056 + FOUR_SPACE_INDENTATION);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301057 }
1058
1059 /**
1060 * Adds getter for interface.
1061 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301062 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301063 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301064 * @throws IOException when fails to append to temporary file
1065 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301066 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301067 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +05301068 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301069 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301070 }
1071
1072 /**
1073 * Adds setter for interface.
1074 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301075 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301076 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301077 * @throws IOException when fails to append to temporary file
1078 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301079 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301080 throws IOException {
1081 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301082 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
1083 + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301084 }
1085
1086 /**
1087 * Adds setter's implementation for class.
1088 *
1089 * @param attr attribute info
1090 * @throws IOException when fails to append to temporary file
1091 */
1092 private void addSetterImpl(JavaAttributeInfo attr)
1093 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301094 if (isRooNode()) {
1095 appendToFile(getSetterImplTempFileHandle(),
1096 getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
1097 + NEW_LINE);
1098 } else {
1099 appendToFile(getSetterImplTempFileHandle(), getOverRideString() +
1100 getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
1101 + NEW_LINE);
1102 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301103 }
1104
1105 /**
1106 * Adds getter method's impl for class.
1107 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301108 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301109 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301110 * @throws IOException when fails to append to temporary file
1111 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301112 void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301113 throws IOException {
1114 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
1115 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301116 if (!isRooNode()) {
1117 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
1118 getGeneratedJavaFiles()) + NEW_LINE);
1119 } else {
1120 appendToFile(getGetterImplTempFileHandle(), getGetterForClass(attr,
1121 getGeneratedJavaFiles()) + NEW_LINE);
1122 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301123 } else {
janani b703cfe42016-05-17 13:12:22 +05301124 appendToFile(getGetterImplTempFileHandle(),
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301125 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
janani b703cfe42016-05-17 13:12:22 +05301126 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301127 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301128 }
1129
1130 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301131 * Adds add to list interface method.
1132 *
1133 * @param attr attribute
1134 * @param pluginConfig plugin configurations
1135 * @throws IOException when fails to do IO operations
1136 */
1137 private void addAddToListInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig) throws IOException {
1138 appendToFile(getAddToListInterfaceTempFileHandle(),
1139 getJavaDoc(ADD_TO_LIST, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
1140 + getAddToListMethodInterface(attr) + NEW_LINE);
1141 }
1142
1143 /**
1144 * Adds add to list interface method.
1145 *
1146 * @param attr attribute
1147 * @throws IOException when fails to do IO operations
1148 */
1149 private void addAddToListImpl(JavaAttributeInfo attr) throws IOException {
1150 appendToFile(getAddToListImplTempFileHandle(),
1151 getAddToListMethodImpl(attr) + NEW_LINE);
1152 }
1153
1154 /**
1155 * Adds leaf identifier enum attributes.
1156 *
1157 * @param attr attribute
1158 * @param value value
1159 * @param yangPluginConfig plugin config
1160 * @throws IOException when fails to do IO operations
1161 */
1162 private void addLeafIdAttributes(JavaAttributeInfo attr, int value, YangPluginConfig yangPluginConfig)
1163 throws IOException {
1164 appendToFile(getLeafIdAttributeTempFileHandle(),
1165 FOUR_SPACE_INDENTATION + generateEnumAttributeString(attr.getAttributeName(),
1166 value, yangPluginConfig));
1167 }
1168
1169 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301170 * Adds build method for interface.
1171 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301172 * @param pluginConfig plugin configurations
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301173 * @return build method for interface
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301174 * @throws IOException when fails to append to temporary file
1175 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301176 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301177 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301178 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301179 }
1180
1181 /**
1182 * Adds build method's implementation for class.
1183 *
1184 * @return build method implementation for class
1185 * @throws IOException when fails to append to temporary file
1186 */
1187 String addBuildMethodImpl()
1188 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301189 return getBuildString(getGeneratedJavaClassName(), isRooNode()) + NEW_LINE;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301190 }
1191
1192 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301193 * Adds default constructor for class.
1194 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301195 * @param modifier modifier for constructor.
1196 * @param toAppend string which need to be appended with the class name
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301197 * @param pluginConfig plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +05301198 * @param isSuffix is value need to be appended as suffix
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301199 * @return default constructor for class
1200 * @throws IOException when fails to append to file
1201 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301202 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig, boolean isSuffix)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301203 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301204 String name = getGeneratedJavaClassName();
1205 if (isRooNode() && !toAppend.equals(BUILDER)) {
1206 name = name + OP_PARAM;
1207 return NEW_LINE
1208 + getDefaultConstructorString(name, modifier,
1209 pluginConfig);
1210 }
1211 if (isSuffix) {
1212 return NEW_LINE +
1213 getDefaultConstructorString(name + toAppend, modifier, pluginConfig);
1214 }
1215 String appended;
1216 if (toAppend.equals(DEFAULT)) {
1217 appended = getCapitalCase(toAppend);
1218 } else {
1219 appended = toAppend;
1220 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301221 return NEW_LINE
Bharat saraswal8beac342016-08-04 02:00:03 +05301222 + getDefaultConstructorString(appended + name, modifier,
1223 pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301224 }
1225
1226 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301227 * Adds hash code method for class.
1228 *
1229 * @param attr attribute info
1230 * @throws IOException when fails to append to temporary file
1231 */
1232 private void addHashCodeMethod(JavaAttributeInfo attr)
1233 throws IOException {
1234 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
1235 }
1236
1237 /**
1238 * Adds equals method for class.
1239 *
1240 * @param attr attribute info
1241 * @throws IOException when fails to append to temporary file
1242 */
1243 private void addEqualsMethod(JavaAttributeInfo attr)
1244 throws IOException {
1245 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
1246 }
1247
1248 /**
1249 * Adds ToString method for class.
1250 *
1251 * @param attr attribute info
1252 * @throws IOException when fails to append to temporary file
1253 */
1254 private void addToStringMethod(JavaAttributeInfo attr)
1255 throws IOException {
1256 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
1257 }
1258
1259 /**
Bharat saraswal250a7472016-05-12 13:16:57 +05301260 * Adds from string method for union class.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301261 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301262 * @param javaAttributeInfo type attribute info
Bharat saraswal250a7472016-05-12 13:16:57 +05301263 * @param fromStringAttributeInfo from string attribute info
1264 * @throws IOException when fails to append to temporary file
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301265 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301266 void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
1267 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301268 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +05301269 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
1270 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301271 }
1272
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301273 /**
1274 * Returns a temporary file handle for the specific file type.
1275 *
1276 * @param fileName file name
1277 * @return temporary file handle
1278 * @throws IOException when fails to create new file handle
1279 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301280 File getTemporaryFileHandle(String fileName)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301281 throws IOException {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301282 String path = getTempDirPath(getAbsoluteDirPath());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301283 File dir = new File(path);
Bharat saraswal8beac342016-08-04 02:00:03 +05301284 boolean isCreated;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301285 if (!dir.exists()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301286 isCreated = dir.mkdirs();
Shankara-Huaweib7564772016-08-02 18:13:13 +05301287 if (!isCreated) {
1288 throw new IOException("failed to create temporary directory for " + fileName);
1289 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301290 }
1291 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1292 if (!file.exists()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301293 isCreated = file.createNewFile();
Shankara-Huaweib7564772016-08-02 18:13:13 +05301294 if (!isCreated) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301295 throw new IOException("failed to create temporary file for " + fileName);
Shankara-Huaweib7564772016-08-02 18:13:13 +05301296 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301297 } else {
1298 throw new IOException(fileName + " is reused due to YANG naming");
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301299 }
1300 return file;
1301 }
1302
1303 /**
1304 * Returns a temporary file handle for the specific file type.
1305 *
1306 * @param fileName file name
1307 * @return temporary file handle
1308 * @throws IOException when fails to create new file handle
1309 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301310 File getJavaFileHandle(String fileName)
1311 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301312 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301313 }
1314
1315 /**
1316 * Returns data from the temporary files.
1317 *
Bharat saraswalaf413b82016-07-14 15:18:20 +05301318 * @param file temporary file handle
Bharat saraswald14cbe82016-07-14 13:26:18 +05301319 * @param absolutePath absolute path
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301320 * @return stored data from temporary files
1321 * @throws IOException when failed to get data from the given file
1322 */
Bharat saraswald14cbe82016-07-14 13:26:18 +05301323 public String getTemporaryDataFromFileHandle(File file, String absolutePath)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301324 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301325
Bharat saraswald14cbe82016-07-14 13:26:18 +05301326 String path = getTempDirPath(absolutePath);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301327 if (new File(path + file.getName()).exists()) {
1328 return readAppendFile(path + file.getName(), EMPTY_STRING);
1329 } else {
1330 throw new IOException("Unable to get data from the given "
1331 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1332 }
1333 }
1334
1335 /**
1336 * Returns temporary directory path.
1337 *
Bharat saraswald14cbe82016-07-14 13:26:18 +05301338 * @param absolutePath absolute path
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301339 * @return directory path
1340 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301341 private String getTempDirPath(String absolutePath) {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301342 return getPackageDirPathFromJavaJPackage(absolutePath) + SLASH + getGeneratedJavaClassName()
Bharat saraswal64e7e232016-07-14 23:33:55 +05301343 + TEMP_FOLDER_NAME_SUFFIX + SLASH;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301344 }
1345
1346 /**
1347 * Parses attribute to get the attribute string.
1348 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301349 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301350 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301351 * @return attribute string
1352 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301353 String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301354 /*
1355 * TODO: check if this utility needs to be called or move to the caller
1356 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301357 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301358 String attributeAccessType = PRIVATE;
1359 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
1360 attributeAccessType = PROTECTED;
1361 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301362 if (attr.isQualifiedName()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301363 return getJavaAttributeDefinition(attr.getImportInfo().getPkgInfo(),
Bharat saraswal250a7472016-05-12 13:16:57 +05301364 attr.getImportInfo().getClassInfo(),
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301365 attributeName, attr.isListAttr(), attributeAccessType);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301366 } else {
Bharat saraswal8beac342016-08-04 02:00:03 +05301367 return getJavaAttributeDefinition(null, attr.getImportInfo().getClassInfo(), attributeName,
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301368 attr.isListAttr(), attributeAccessType);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301369 }
1370 }
1371
1372 /**
1373 * Appends content to temporary file.
1374 *
1375 * @param file temporary file
1376 * @param data data to be appended
1377 * @throws IOException when fails to append to file
1378 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301379 void appendToFile(File file, String data)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301380 throws IOException {
1381 try {
1382 insertDataIntoJavaFile(file, data);
1383 } catch (IOException ex) {
1384 throw new IOException("failed to write in temp file.");
1385 }
1386 }
1387
1388 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301389 * Adds parent's info to current node import list.
1390 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301391 * @param curNode current node for which import list needs to be updated
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301392 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301393 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301394 void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Bharat saraswale50edca2016-08-05 01:58:25 +05301395
1396 JavaQualifiedTypeInfoTranslator caseImportInfo = new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301397 YangNode parent = getParentNodeInGenCode(curNode);
Bharat saraswale50edca2016-08-05 01:58:25 +05301398 if (curNode instanceof YangCase && parent instanceof YangAugment) {
1399 return;
1400 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301401 if (!(parent instanceof JavaCodeGenerator)) {
1402 throw new TranslatorException("missing parent node to contain current node info in generated file");
1403 }
1404 if (!(curNode instanceof JavaFileInfoContainer)) {
1405 throw new TranslatorException("missing java file information to get the package details "
1406 + "of attribute corresponding to child node");
1407 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301408 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1409 pluginConfig.getConflictResolver())));
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301410 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301411
Bharat saraswale50edca2016-08-05 01:58:25 +05301412 JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301413
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301414 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301415 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +05301416 getCapitalCase(fileInfo.getJavaName()), fileInfo.getPackage());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301417 }
1418
1419 /**
1420 * Adds leaf attributes in generated files.
1421 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301422 * @param listOfLeaves list of YANG leaf
1423 * @param yangPluginConfig plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +05301424 * @param curNode current node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301425 * @throws IOException IO operation fail
1426 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301427 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1428 YangPluginConfig yangPluginConfig, YangNode curNode)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301429 throws IOException {
1430 if (listOfLeaves != null) {
1431 for (YangLeaf leaf : listOfLeaves) {
1432 if (!(leaf instanceof JavaLeafInfoContainer)) {
1433 throw new TranslatorException("Leaf does not have java information");
1434 }
Bharat saraswal64e7e232016-07-14 23:33:55 +05301435 if (curNode instanceof YangModule || curNode instanceof YangSubModule) {
1436 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) curNode)
1437 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
1438 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(tempJavaBeanFragmentFiles, leaf,
1439 yangPluginConfig), yangPluginConfig);
1440 } else {
1441 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(this, leaf, yangPluginConfig),
1442 yangPluginConfig);
1443 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301444 }
1445 }
1446 }
1447
1448 /**
1449 * Adds leaf list's attributes in generated files.
1450 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301451 * @param listOfLeafList list of YANG leaves
1452 * @param yangPluginConfig plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +05301453 * @param curNode current node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301454 * @throws IOException IO operation fail
1455 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301456 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig,
1457 YangNode curNode) throws IOException {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301458 if (listOfLeafList != null) {
1459 for (YangLeafList leafList : listOfLeafList) {
1460 if (!(leafList instanceof JavaLeafInfoContainer)) {
1461 throw new TranslatorException("Leaf-list does not have java information");
1462 }
Bharat saraswal64e7e232016-07-14 23:33:55 +05301463 if (curNode instanceof YangModule || curNode instanceof YangSubModule) {
1464 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) curNode)
1465 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
1466 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(tempJavaBeanFragmentFiles,
1467 leafList, yangPluginConfig), yangPluginConfig);
1468 } else {
1469 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(this,
1470 leafList, yangPluginConfig), yangPluginConfig);
1471 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301472 }
1473 }
1474 }
1475
1476 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +05301477 * Adds all the leaves in the current data model node as part of the generated temporary file.
Bharat saraswald14cbe82016-07-14 13:26:18 +05301478 *
Bharat saraswal64e7e232016-07-14 23:33:55 +05301479 * @param curNode current node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301480 * @param yangPluginConfig plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301481 * @throws IOException IO operation fail
1482 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301483 void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
Shankara-Huaweib7564772016-08-02 18:13:13 +05301484 YangPluginConfig yangPluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301485 throws IOException {
1486 if (!(curNode instanceof YangLeavesHolder)) {
1487 throw new TranslatorException("Data model node does not have any leaves");
1488 }
1489 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
Bharat saraswal64e7e232016-07-14 23:33:55 +05301490 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig, curNode);
1491 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig, curNode);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301492 }
1493
1494 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301495 * Adds the new attribute info to the target generated temporary files.
1496 *
Bharat saraswalaf413b82016-07-14 15:18:20 +05301497 * @param newAttrInfo the attribute info that needs to be added to temporary files
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301498 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301499 * @throws IOException IO operation fail
1500 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301501 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301502 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301503 setAttributePresent(true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301504 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301505 addAttribute(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301506 }
1507 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301508 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301509 }
1510 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301511 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301512 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301513 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1514 addSetterImpl(newAttrInfo);
1515 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301516 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1517 addHashCodeMethod(newAttrInfo);
1518 }
1519 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1520 addEqualsMethod(newAttrInfo);
1521 }
1522 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1523 addToStringMethod(newAttrInfo);
1524 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301525 if ((getGeneratedTempFiles() & EDIT_CONTENT_MASK) != 0) {
1526 //TODO: add implementation for edit content match.
1527 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301528 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0 && newAttrInfo.isListAttr()) {
1529 addAddToListImpl(newAttrInfo);
1530 }
1531 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0 && newAttrInfo.isListAttr()) {
1532 addAddToListInterface(newAttrInfo, pluginConfig);
1533 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301534 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_NODES_MASK) != 0
1535 && newAttrInfo.getAttributeType() == null) {
1536 addIsFilerForNode(newAttrInfo);
1537 }
1538 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_MASK) != 0 && !newAttrInfo.isListAttr()
1539 && newAttrInfo.getAttributeType() != null) {
1540 addIsFilerForLeaf(newAttrInfo);
1541 }
1542 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK) != 0 && newAttrInfo.isListAttr()
1543 && newAttrInfo.getAttributeType() != null) {
1544 addIsFilerForLeafList(newAttrInfo);
1545 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301546 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0 && !newAttrInfo.isListAttr()
1547 && newAttrInfo.getAttributeType() != null) {
1548 leafCount++;
1549 addLeafIdAttributes(newAttrInfo, leafCount, pluginConfig);
1550 }
Bharat saraswal64e7e232016-07-14 23:33:55 +05301551 if (!newAttrInfo.isIntConflict() &&
1552 !newAttrInfo.isLongConflict()) {
1553 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1554 addGetterImpl(newAttrInfo, pluginConfig);
1555 }
1556
1557 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Shankara-Huaweib7564772016-08-02 18:13:13 +05301558 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +05301559 pluginConfig.getConflictResolver());
Bharat saraswal250a7472016-05-12 13:16:57 +05301560 /*
1561 * Create a new java attribute info with qualified information of
1562 * wrapper classes.
1563 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301564 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1565 newAttrInfo.getAttributeName(),
1566 newAttrInfo.getAttributeType(),
1567 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
Bharat saraswal250a7472016-05-12 13:16:57 +05301568
Bharat saraswal64e7e232016-07-14 23:33:55 +05301569 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
1570 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301571 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301572 }
1573
1574 /**
1575 * Returns java class name.
1576 *
1577 * @param suffix for the class name based on the file type
1578 * @return java class name
1579 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301580 String getJavaClassName(String suffix) {
janani b703cfe42016-05-17 13:12:22 +05301581 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301582 }
1583
1584 /**
Bharat saraswald14cbe82016-07-14 13:26:18 +05301585 * Returns java class name.
1586 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301587 * @param node YANG node
Bharat saraswald14cbe82016-07-14 13:26:18 +05301588 * @return java class name
1589 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301590 private String getImplClassName(YangNode node) {
1591 if (node instanceof YangModule || node instanceof YangSubModule) {
1592 return getCapitalCase(getJavaFileInfo().getJavaName()) + OP_PARAM;
1593 }
Bharat saraswald14cbe82016-07-14 13:26:18 +05301594 return getCapitalCase(DEFAULT) + getCapitalCase(getJavaFileInfo().getJavaName());
1595 }
1596
1597 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301598 * Returns the directory path.
1599 *
1600 * @return directory path
1601 */
1602 private String getDirPath() {
1603 return getJavaFileInfo().getPackageFilePath();
1604 }
1605
1606 /**
1607 * Constructs java code exit.
1608 *
1609 * @param fileType generated file type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301610 * @param curNode current YANG node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301611 * @throws IOException when fails to generate java files
1612 */
1613 public void generateJavaFile(int fileType, YangNode curNode)
1614 throws IOException {
Bharat saraswalaf413b82016-07-14 15:18:20 +05301615 List<String> imports = ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getBeanTempFiles()
1616 .getJavaImportData().getImports();
Bharat saraswal8beac342016-08-04 02:00:03 +05301617 if (curNode instanceof YangAugmentableNode) {
1618 addImportsForAugmentableClass(imports, true, true);
1619 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301620 createPackage(curNode);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301621 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301622 * Generate java code.
1623 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301624 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1625 BUILDER_INTERFACE_MASK) != 0) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301626 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301627 * Create interface file.
1628 */
1629 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1630 setInterfaceJavaFileHandle(
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301631 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
Bharat saraswal8beac342016-08-04 02:00:03 +05301632 if (!(curNode instanceof YangModule) && !(curNode instanceof YangSubModule)) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301633 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301634 * Create builder interface file.
1635 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301636 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1637 setBuilderInterfaceJavaFileHandle(
1638 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1639 setBuilderInterfaceJavaFileHandle(
1640 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1641 isAttributePresent()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301642 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301643 * Append builder interface file to interface file and close it.
1644 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301645 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1646 validateLineLength(getInterfaceJavaFileHandle());
1647 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301648 }
1649 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswal8beac342016-08-04 02:00:03 +05301650 if (curNode instanceof YangAugmentableNode) {
1651 addImportsForAugmentableClass(imports, false, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +05301652 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301653 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301654 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & DEFAULT_CLASS_MASK) != 0) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301655 if (isAttributePresent()) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301656 addImportsToStringAndHasCodeMethods(imports, true);
Bharat saraswal8beac342016-08-04 02:00:03 +05301657 addArrayListImport(imports);
Bharat saraswalaf413b82016-07-14 15:18:20 +05301658 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301659 addBitsetImport(imports);
Bharat saraswalaf413b82016-07-14 15:18:20 +05301660 if (curNode instanceof YangAugmentableNode) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301661 addImportsForAugmentableClass(imports, true, false);
Bharat saraswale50edca2016-08-05 01:58:25 +05301662 addInvocationExceptionImport(imports);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301663 }
Bharat saraswal250a7472016-05-12 13:16:57 +05301664 sortImports(imports);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301665 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301666 * Create impl class file.
1667 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301668 setImplClassJavaFileHandle(getJavaFileHandle(getImplClassName(curNode)));
Bharat saraswald14cbe82016-07-14 13:26:18 +05301669 setImplClassJavaFileHandle(
Bharat saraswal8beac342016-08-04 02:00:03 +05301670 generateDefaultClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent(), imports));
Bharat saraswald14cbe82016-07-14 13:26:18 +05301671 /*
1672 * Create builder class file.
1673 */
1674 if ((fileType & BUILDER_CLASS_MASK) != 0) {
1675 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1676 setBuilderClassJavaFileHandle(
1677 generateBuilderClassFile(getBuilderClassJavaFileHandle(), curNode,
1678 isAttributePresent()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301679 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301680 * Append impl class to builder class and close it.
1681 */
Bharat saraswald14cbe82016-07-14 13:26:18 +05301682 mergeJavaFiles(getBuilderClassJavaFileHandle(), getImplClassJavaFileHandle());
1683 validateLineLength(getImplClassJavaFileHandle());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301684 }
Bharat saraswald14cbe82016-07-14 13:26:18 +05301685 insertDataIntoJavaFile(getImplClassJavaFileHandle(), getJavaClassDefClose());
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301686
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301687 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301688 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301689 * Close all the file handles.
1690 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301691 freeTemporaryResources(false);
1692 }
1693
Bharat saraswal8beac342016-08-04 02:00:03 +05301694 /*Adds import for array list.*/
1695 private void addArrayListImport(List<String> imports) {
1696 if (imports.contains(getJavaImportData().getImportForList())) {
1697 imports.add(ARRAY_LIST_IMPORT);
1698 }
1699 }
1700
Bharat saraswal8beac342016-08-04 02:00:03 +05301701 private void addBitsetImport(List<String> imports) {
1702 imports.add(getJavaImportData().getImportForToBitSet());
Bharat saraswald14cbe82016-07-14 13:26:18 +05301703 }
1704
1705 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301706 * Adds imports for ToString and HashCodeMethod.
1707 *
Bharat saraswal039f59c2016-07-14 21:57:13 +05301708 * @param imports import list
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301709 * @param operation add or remove
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301710 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301711 void addImportsToStringAndHasCodeMethods(List<String> imports, boolean operation) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301712 if (operation) {
1713 imports.add(getJavaImportData().getImportForHashAndEquals());
1714 imports.add(getJavaImportData().getImportForToString());
1715 } else {
1716 imports.remove(getJavaImportData().getImportForHashAndEquals());
1717 imports.remove(getJavaImportData().getImportForToString());
1718 }
1719 }
1720
1721 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +05301722 * Adds import for map and hash map.
1723 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301724 * @param imports import list
1725 * @param operations true for adding and false for deletion
1726 * @param isInterfaceFile if need to add in interface file
Bharat saraswalaf413b82016-07-14 15:18:20 +05301727 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301728 private void addImportsForAugmentableClass(List<String> imports, boolean operations, boolean isInterfaceFile) {
Bharat saraswal039f59c2016-07-14 21:57:13 +05301729 if (operations) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301730 if (!isInterfaceFile) {
1731 imports.add(getJavaImportData().getHashMapImport());
1732 }
Bharat saraswal039f59c2016-07-14 21:57:13 +05301733 imports.add(getJavaImportData().getMapImport());
Bharat saraswal039f59c2016-07-14 21:57:13 +05301734 } else {
Bharat saraswal8beac342016-08-04 02:00:03 +05301735 if (!isInterfaceFile) {
1736 imports.remove(getJavaImportData().getHashMapImport());
1737 }
Bharat saraswal039f59c2016-07-14 21:57:13 +05301738 imports.remove(getJavaImportData().getMapImport());
Bharat saraswald14cbe82016-07-14 13:26:18 +05301739 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301740 sortImports(imports);
Bharat saraswald14cbe82016-07-14 13:26:18 +05301741 }
1742
1743 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301744 * Adds invocation exception import.
Bharat saraswal039f59c2016-07-14 21:57:13 +05301745 *
1746 * @param imports list of imports
Bharat saraswal039f59c2016-07-14 21:57:13 +05301747 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301748 private void addInvocationExceptionImport(List<String> imports) {
1749 imports.add(INVOCATION_TARGET_EXCEPTION_IMPORT);
Bharat saraswal039f59c2016-07-14 21:57:13 +05301750 }
1751
1752 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301753 * Removes all temporary file handles.
1754 *
Bharat saraswal64e7e232016-07-14 23:33:55 +05301755 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301756 * @throws IOException when failed to delete the temporary files
1757 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301758 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301759 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301760 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301761 * Close all java file handles and when error occurs delete the files.
1762 */
1763 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +05301764 closeFile(getInterfaceJavaFileHandle(), isErrorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301765 }
1766 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301767 closeFile(getBuilderClassJavaFileHandle(), true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301768 }
1769 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1770 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1771 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301772 if ((getGeneratedJavaFiles() & DEFAULT_CLASS_MASK) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +05301773 closeFile(getImplClassJavaFileHandle(), isErrorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301774 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301775 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301776 * Close all temporary file handles and delete the files.
1777 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301778 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1779 closeFile(getGetterImplTempFileHandle(), true);
1780 }
Vinod Kumar S79a374b2016-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 saraswal250a7472016-05-12 13:16:57 +05301793 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1794 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301795 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301796 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0) {
1797 closeFile(getAddToListImplTempFileHandle(), true);
1798 }
1799 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0) {
1800 closeFile(getAddToListInterfaceTempFileHandle(), true);
1801 }
1802 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0) {
1803 closeFile(getLeafIdAttributeTempFileHandle(), true);
1804 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301805 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_MASK) != 0) {
1806 closeFile(getIsContentMatchLeafTempFileHandle(), true);
1807 }
1808 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK) != 0) {
1809 closeFile(getIsContentMatchLeafListTempFileHandle(), true);
1810 }
1811 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_NODES_MASK) != 0) {
1812 closeFile(getIsContentMatchNodeTempFileHandle(), true);
1813 }
1814 if ((getGeneratedTempFiles() & EDIT_CONTENT_MASK) != 0) {
1815 closeFile(getEditContentTempFileHandle(), true);
1816 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301817 }
1818
1819 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +05301820 * Returns if the attribute needs to be accessed in a qualified manner or not, if it needs to be imported, then the
1821 * same needs to be done.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301822 *
1823 * @param importInfo import info for the current attribute being added
1824 * @return status of the qualified access to the attribute
1825 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301826 boolean getIsQualifiedAccessOrAddToImportList(
Shankara-Huaweib7564772016-08-02 18:13:13 +05301827 JavaQualifiedTypeInfoTranslator importInfo) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301828 return getJavaImportData().addImportInfo(importInfo, getGeneratedJavaClassName(),
1829 getJavaFileInfo().getPackage());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301830 }
1831
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301832 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301833 * Returns temp file handle for add to list interface.
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301834 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301835 * @return temp file handle for add to list interface
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301836 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301837 public File getAddToListInterfaceTempFileHandle() {
1838 return addToListInterfaceTempFileHandle;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301839 }
1840
1841 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301842 * Sets temp file handle for add to list interface.
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301843 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301844 * @param addToListInterfaceTempFileHandle temp file handle for add to list interface
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301845 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301846 private void setAddToListInterfaceTempFileHandle(File addToListInterfaceTempFileHandle) {
1847 this.addToListInterfaceTempFileHandle = addToListInterfaceTempFileHandle;
1848 }
1849
1850 /**
1851 * Returns temp file handle for add to list impl.
1852 *
1853 * @return temp file handle for add to list impl
1854 */
1855 public File getAddToListImplTempFileHandle() {
1856 return addToListImplTempFileHandle;
1857 }
1858
1859 /**
1860 * Sets temp file handle for add to list impl.
1861 *
1862 * @param addToListImplTempFileHandle temp file handle for add to list impl
1863 */
1864 private void setAddToListImplTempFileHandle(File addToListImplTempFileHandle) {
1865 this.addToListImplTempFileHandle = addToListImplTempFileHandle;
1866 }
1867
1868 /**
1869 * Returns temp file handle for leaf identifier attributes.
1870 *
1871 * @return temp file handle for leaf identifier attributes
1872 */
1873 public File getLeafIdAttributeTempFileHandle() {
1874 return leafIdAttributeTempFileHandle;
1875 }
1876
1877 /**
1878 * Sets temp file handle for leaf identifier attributes.
1879 *
Bharat saraswale50edca2016-08-05 01:58:25 +05301880 * @param leafIdAttributeTempFileHandle temp file handle for leaf identifier attributes
Bharat saraswal8beac342016-08-04 02:00:03 +05301881 */
1882 private void setLeafIdAttributeTempFileHandle(File leafIdAttributeTempFileHandle) {
1883 this.leafIdAttributeTempFileHandle = leafIdAttributeTempFileHandle;
1884 }
1885
1886 /**
1887 * Returns if root node is set.
1888 *
1889 * @return true if root node
1890 */
1891 private boolean isRooNode() {
1892 return isRooNode;
1893 }
1894
1895 /**
1896 * Sets true if root node.
1897 *
1898 * @param rooNode true if root node
1899 */
Bharat saraswale50edca2016-08-05 01:58:25 +05301900 void setRooNode(boolean rooNode) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301901 isRooNode = rooNode;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301902 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301903
1904 /**
1905 * Returns temp file for is content match.
1906 *
1907 * @return temp file for is content match
1908 */
1909 public File getIsContentMatchLeafTempFileHandle() {
1910 return isContentMatchLeafTempFileHandle;
1911 }
1912
1913 /**
1914 * Sets temp file handle for is content match.
1915 *
1916 * @param isContentMatchLeafTempFileHandle temp file handle for is content match
1917 */
1918 private void setIsContentMatchLeafTempFileHandle(File isContentMatchLeafTempFileHandle) {
1919 this.isContentMatchLeafTempFileHandle = isContentMatchLeafTempFileHandle;
1920 }
1921
1922 /**
1923 * Returns temp file for edit content file.
1924 *
1925 * @return temp file for edit content file
1926 */
1927 public File getEditContentTempFileHandle() {
1928 return editContentTempFileHandle;
1929 }
1930
1931 /**
1932 * Sets temp file for edit content file.
1933 *
1934 * @param editContentTempFileHandle temp file for edit content file
1935 */
1936 private void setEditContentTempFileHandle(File editContentTempFileHandle) {
1937 this.editContentTempFileHandle = editContentTempFileHandle;
1938 }
1939
1940 /**
1941 * Returns temp file for is content match.
1942 *
1943 * @return temp file for is content match
1944 */
1945 public File getIsContentMatchLeafListTempFileHandle() {
1946 return isContentMatchLeafListTempFileHandle;
1947 }
1948
1949 /**
1950 * Sets temp file handle for is content match.
1951 *
1952 * @param isContentMatchLeafListTempFileHandle temp file handle for is content match
1953 */
1954 private void setIsContentMatchLeafListTempFileHandle(File isContentMatchLeafListTempFileHandle) {
1955 this.isContentMatchLeafListTempFileHandle = isContentMatchLeafListTempFileHandle;
1956 }
1957
1958 /**
1959 * Returns temp file for is content match.
1960 *
1961 * @return temp file for is content match
1962 */
1963 public File getIsContentMatchNodeTempFileHandle() {
1964 return isContentMatchNodeTempFileHandle;
1965 }
1966
1967 /**
1968 * Sets temp file handle for is content match.
1969 *
1970 * @param isContentMatchNodeTempFileHandle temp file handle for is content match
1971 */
1972 private void setIsContentMatchNodeTempFileHandle(File isContentMatchNodeTempFileHandle) {
1973 this.isContentMatchNodeTempFileHandle = isContentMatchNodeTempFileHandle;
1974 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301975}