blob: a151f03579ca4473f353d463b147b41f23e963b4 [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;
Vidyashree Ramab3670472016-08-06 15:49:56 +053025import org.onosproject.yangutils.datamodel.YangModule;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053026import org.onosproject.yangutils.datamodel.YangLeaf;
27import org.onosproject.yangutils.datamodel.YangLeafList;
28import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Vidyashree Ramab3670472016-08-06 15:49:56 +053029import org.onosproject.yangutils.datamodel.YangList;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053030import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalaf413b82016-07-14 15:18:20 +053031import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswale50edca2016-08-05 01:58:25 +053032import org.onosproject.yangutils.utils.io.YangPluginConfig;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053033import org.onosproject.yangutils.translator.exception.TranslatorException;
34import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Shankara-Huaweib7564772016-08-02 18:13:13 +053035import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGroupingTranslator;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053036import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053037
Bharat saraswalc2d3be12016-06-16 00:29:12 +053038import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053041import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.DEFAULT_CLASS_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Bharat saraswalaf413b82016-07-14 15:18:20 +053043import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
45import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053047import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_IMPL_MASK;
48import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_INTERFACE_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Bharat saraswale50edca2016-08-05 01:58:25 +053050import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EDIT_CONTENT_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053051import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswale50edca2016-08-05 01:58:25 +053052import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK;
53import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_LEAF_MASK;
54import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_NODES_MASK;
Bharat saraswal250a7472016-05-12 13:16:57 +053055import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053056import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
57import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
58import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053059import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053060import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
61import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
62import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
63import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweib7564772016-08-02 18:13:13 +053064import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
65import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053066import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo;
Bharat saraswal8beac342016-08-04 02:00:03 +053067import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
68import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefinition;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053069import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswalaf413b82016-07-14 15:18:20 +053070import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.sortImports;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053071import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
72import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053073import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateDefaultClassFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053074import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053075import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswald14cbe82016-07-14 13:26:18 +053076import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053077import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddToListMethodImpl;
78import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddToListMethodInterface;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053080import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
81import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswal250a7472016-05-12 13:16:57 +053082import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053083import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
84import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
85import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
Bharat saraswale50edca2016-08-05 01:58:25 +053086import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIsFilerContentMatchForLeaf;
87import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIsFilerContentMatchForLeafList;
88import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIsFilterContentForNodes;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053089import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053090import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
91import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
92import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053093import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
Bharat saraswal8beac342016-08-04 02:00:03 +053094import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST_IMPORT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053095import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
Bharat saraswald14cbe82016-07-14 13:26:18 +053096import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053097import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
98import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053099import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
Bharat saraswal8beac342016-08-04 02:00:03 +0530100import static org.onosproject.yangutils.utils.UtilConstants.INVOCATION_TARGET_EXCEPTION_IMPORT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530101import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal8beac342016-08-04 02:00:03 +0530102import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530103import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530104import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
105import static org.onosproject.yangutils.utils.UtilConstants.PROTECTED;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530106import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530107import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530108import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530109import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswal8beac342016-08-04 02:00:03 +0530110import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ADD_TO_LIST;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530111import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530112import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530113import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530114import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
115import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
116import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530117import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
118import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530119import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530120
121/**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530122 * Represents implementation of java code fragments temporary implementations. Manages the common temp file required for
123 * Java file(s) generated.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530124 */
125public class TempJavaFragmentFiles {
Bharat saraswal250a7472016-05-12 13:16:57 +0530126
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530127 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530128 * File type extension for java classes.
129 */
130 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswal250a7472016-05-12 13:16:57 +0530131
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530132 /**
133 * File type extension for temporary classes.
134 */
135 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswal250a7472016-05-12 13:16:57 +0530136
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530137 /**
138 * Folder suffix for temporary files folder.
139 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530140 private static final String TEMP_FOLDER_NAME_SUFFIX = "-Temp";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530141
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530142 /**
143 * File name for getter method.
144 */
145 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530146
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530147 /**
148 * File name for setter method.
149 */
150 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530151
152 /**
153 * File name for getter method implementation.
154 */
155 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
156
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530157 /**
158 * File name for setter method implementation.
159 */
160 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530161
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530162 /**
163 * File name for attributes.
164 */
165 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswal250a7472016-05-12 13:16:57 +0530166
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530167 /**
168 * File name for to string method.
169 */
170 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswal250a7472016-05-12 13:16:57 +0530171
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530172 /**
173 * File name for hash code method.
174 */
175 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswal250a7472016-05-12 13:16:57 +0530176
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530177 /**
178 * File name for equals method.
179 */
180 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530181
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530182 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530183 * File name for from string method.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530184 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530185 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530186
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530187 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530188 * File name for from add to list interface method.
189 */
190 private static final String ADD_TO_LIST_INTERFACE_METHOD_FILE_NAME = "addToList";
191
192 /**
193 * File name for from add to list impl method.
194 */
195 private static final String ADD_TO_LIST_IMPL_METHOD_FILE_NAME = "addToListImpl";
196
197 /**
198 * File name for from leaf identifier attributes.
199 */
200 private static final String LEAF_IDENTIFIER_ATTRIBUTES_FILE_NAME = "leafIdentifierAtr";
201
202 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530203 * File name for is filter content leaf match.
204 */
205 private static final String FILTER_CONTENT_MATCH_LEAF_FILE_NAME = "isFilterContentMatchLeafMask";
206
207 /**
208 * File name for is filter content leaf-list match.
209 */
210 private static final String FILTER_CONTENT_MATCH_LEAF_LIST_FILE_NAME = "isFilterContentMatchLeafListMask";
211
212 /**
213 * File name for is filter content node match.
214 */
215 private static final String FILTER_CONTENT_MATCH_NODE_FILE_NAME = "isFilterContentMatchNodeMask";
216
217 /**
218 * File name for edit content file.
219 */
220 private static final String EDIT_CONTENT_FILE_NAME = "editContentFile";
221
222 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530223 * File name for interface java file name suffix.
224 */
225 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswal250a7472016-05-12 13:16:57 +0530226
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530227 /**
228 * File name for builder interface file name suffix.
229 */
230 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswal250a7472016-05-12 13:16:57 +0530231
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530232 /**
233 * File name for builder class file name suffix.
234 */
235 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswal250a7472016-05-12 13:16:57 +0530236
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530237 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530238 * Information about the java files being generated.
239 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530240 private JavaFileInfoTranslator javaFileInfo;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530241
242 /**
243 * Imported class info.
244 */
245 private JavaImportData javaImportData;
246
247 /**
248 * The variable which guides the types of temporary files generated using the temporary generated file types mask.
249 */
250 private int generatedTempFiles;
251
252 /**
253 * Absolute path where the target java file needs to be generated.
254 */
255 private String absoluteDirPath;
256
257 /**
258 * Contains all the interface(s)/class name which will be extended by generated files.
259 */
260 private JavaExtendsListHolder javaExtendsListHolder;
261
Bharat saraswald14cbe82016-07-14 13:26:18 +0530262 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530263 * Java file handle for interface file.
264 */
265 private File interfaceJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530266
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530267 /**
268 * Java file handle for builder interface file.
269 */
270 private File builderInterfaceJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530271
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530272 /**
273 * Java file handle for builder class file.
274 */
275 private File builderClassJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530276
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530277 /**
278 * Java file handle for impl class file.
279 */
280 private File implClassJavaFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530281
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530282 /**
283 * Temporary file handle for attribute.
284 */
285 private File attributesTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530286
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530287 /**
288 * Temporary file handle for getter of interface.
289 */
290 private File getterInterfaceTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530291
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530292 /**
293 * Temporary file handle for setter of interface.
294 */
295 private File setterInterfaceTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530296
297 /**
298 * Temporary file handle for getter of class.
299 */
300 private File getterImplTempFileHandle;
301
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530302 /**
303 * Temporary file handle for setter of class.
304 */
305 private File setterImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530306
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530307 /**
308 * Temporary file handle for hash code method of class.
309 */
310 private File hashCodeImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530311
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530312 /**
313 * Temporary file handle for equals method of class.
314 */
315 private File equalsImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530316
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530317 /**
318 * Temporary file handle for to string method of class.
319 */
320 private File toStringImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530321
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530322 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530323 * Temporary file handle for from string method of class.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530324 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530325 private File fromStringImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530326
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530327 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530328 * Temporary file handle for add to list interface method of class.
329 */
330 private File addToListInterfaceTempFileHandle;
331
332 /**
333 * Temporary file handle for add to list impl method of class.
334 */
335 private File addToListImplTempFileHandle;
336
337 /**
338 * Temporary file handle for leaf id attributes of enum.
339 */
340 private File leafIdAttributeTempFileHandle;
341
342 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530343 * Temporary file handle for is content match method for leaf-list.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530344 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530345 private File isContentMatchLeafListTempFileHandle;
346
347 /**
348 * Temporary file handle for is content match method for node.
349 */
350 private File isContentMatchNodeTempFileHandle;
351
352 /**
353 * Temporary file handle for is content match method for leaf.
354 */
355 private File isContentMatchLeafTempFileHandle;
356
357 /**
358 * Temporary file handle for edit content file.
359 */
360 private File editContentTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530361
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530362 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530363 * Leaf count.
364 */
365 private int leafCount = 0;
366
367 /**
368 * If current node is root node.
369 */
370 private boolean isRooNode;
371
372 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530373 * Is attribute added.
374 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530375 private boolean isAttributePresent;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530376
Bharat saraswalaf413b82016-07-14 15:18:20 +0530377 TempJavaFragmentFiles() {
378 }
379
380 /**
381 * Creates an instance of temporary java code fragment.
382 *
383 * @param javaFileInfo generated java file information
384 * @throws IOException when fails to create new file handle
385 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530386 TempJavaFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Bharat saraswalaf413b82016-07-14 15:18:20 +0530387 throws IOException {
388 setJavaExtendsListHolder(new JavaExtendsListHolder());
389 setJavaImportData(new JavaImportData());
390 setJavaFileInfo(javaFileInfo);
Bharat saraswal8beac342016-08-04 02:00:03 +0530391 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
392 getJavaFileInfo().getPackageFilePath()));
393
Bharat saraswalaf413b82016-07-14 15:18:20 +0530394 /*
395 * Initialize getter when generation file type matches to interface
396 * mask.
397 */
398 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
399 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
Bharat saraswal8beac342016-08-04 02:00:03 +0530400 addGeneratedTempFile(ADD_TO_LIST_INTERFACE_MASK);
401 addGeneratedTempFile(LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530402 }
403
404 /*
405 * Initialize getter and setter when generation file type matches to
406 * builder interface mask.
407 */
408 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
409 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
410 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
411 }
412
413 /*
414 * Initialize getterImpl, setterImpl and attributes when generation file
415 * type matches to builder class mask.
416 */
417 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
418 addGeneratedTempFile(ATTRIBUTES_MASK);
419 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
420 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530421 }
422
423 /*
424 * Initialize getterImpl, attributes, constructor, hash code, equals and
425 * to strings when generation file type matches to impl class mask.
426 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530427 if ((getGeneratedJavaFiles() & DEFAULT_CLASS_MASK) != 0) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530428 addGeneratedTempFile(ATTRIBUTES_MASK);
429 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
430 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
431 addGeneratedTempFile(EQUALS_IMPL_MASK);
432 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswal8beac342016-08-04 02:00:03 +0530433 addGeneratedTempFile(ADD_TO_LIST_IMPL_MASK);
Bharat saraswale50edca2016-08-05 01:58:25 +0530434 addGeneratedTempFile(FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK);
435 addGeneratedTempFile(FILTER_CONTENT_MATCH_FOR_LEAF_MASK);
436 addGeneratedTempFile(FILTER_CONTENT_MATCH_FOR_NODES_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530437 }
438
439 /*
440 * Initialize temp files to generate type class.
441 */
442 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
443 addGeneratedTempFile(ATTRIBUTES_MASK);
444 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
445 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
446 addGeneratedTempFile(EQUALS_IMPL_MASK);
447 addGeneratedTempFile(TO_STRING_IMPL_MASK);
448 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
449 }
450
Vidyashree Ramab3670472016-08-06 15:49:56 +0530451 //Initialize temp files to generate enum class.
Bharat saraswalaf413b82016-07-14 15:18:20 +0530452 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
453 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
454 }
455
Vidyashree Ramab3670472016-08-06 15:49:56 +0530456 //Set temporary file handles
Bharat saraswalaf413b82016-07-14 15:18:20 +0530457 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
458 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
459 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530460 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
461 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
462 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530463 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
464 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
465 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530466 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
467 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
468 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530469 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
470 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
471 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530472 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
473 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
474 }
475 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
476 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
477 }
478 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
479 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
480 }
481 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
482 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
483 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530484 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0) {
485 setAddToListInterfaceTempFileHandle(getTemporaryFileHandle(ADD_TO_LIST_INTERFACE_METHOD_FILE_NAME));
486 }
487 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0) {
488 setAddToListImplTempFileHandle(getTemporaryFileHandle(ADD_TO_LIST_IMPL_METHOD_FILE_NAME));
489 }
490 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0) {
491 setLeafIdAttributeTempFileHandle(getTemporaryFileHandle(LEAF_IDENTIFIER_ATTRIBUTES_FILE_NAME));
492 }
Bharat saraswale50edca2016-08-05 01:58:25 +0530493 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_MASK) != 0) {
494 setIsContentMatchLeafTempFileHandle(getTemporaryFileHandle(FILTER_CONTENT_MATCH_LEAF_FILE_NAME));
495 }
496 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK) != 0) {
497 setIsContentMatchLeafListTempFileHandle(getTemporaryFileHandle(FILTER_CONTENT_MATCH_LEAF_LIST_FILE_NAME));
498 }
499 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_NODES_MASK) != 0) {
500 setIsContentMatchNodeTempFileHandle(getTemporaryFileHandle(FILTER_CONTENT_MATCH_NODE_FILE_NAME));
501 }
502 if ((getGeneratedTempFiles() & EDIT_CONTENT_MASK) != 0) {
503 setEditContentTempFileHandle(getTemporaryFileHandle(EDIT_CONTENT_FILE_NAME));
504 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530505 }
506
507 /**
508 * Adds current node info as and attribute to the parent generated file.
509 *
510 * @param curNode current node which needs to be added as an attribute in the parent generated code
511 * @param isList is list construct
512 * @param pluginConfig plugin configurations
513 * @throws IOException IO operation exception
514 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530515 static void addCurNodeInfoInParentTempFile(YangNode curNode,
516 boolean isList, YangPluginConfig pluginConfig)
Bharat saraswalaf413b82016-07-14 15:18:20 +0530517 throws IOException {
518 YangNode parent = getParentNodeInGenCode(curNode);
519 if (!(parent instanceof JavaCodeGenerator)) {
520 throw new TranslatorException("missing parent node to contain current node info in generated file");
521 }
522
Shankara-Huaweib7564772016-08-02 18:13:13 +0530523 if (parent instanceof YangJavaGroupingTranslator) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530524 /*
525 * In case of grouping, there is no need to add the information, it
526 * will be taken care in uses
527 */
528 return;
529 }
530 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) parent)
531 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
532
533 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
534 parent, isList, tempJavaBeanFragmentFiles);
Bharat saraswal8beac342016-08-04 02:00:03 +0530535 tempJavaBeanFragmentFiles.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530536 }
537
538 /**
539 * Creates an attribute info object corresponding to a data model node and return it.
540 *
541 * @param curNode current data model node for which the java code generation is being handled
542 * @param targetNode target node in which the current node is an attribute
543 * @param isListNode is the current added attribute needs to be a list
544 * @param tempJavaFragmentFiles temp java fragment files
545 * @return AttributeInfo attribute details required to add in temporary files
546 */
547 public static JavaAttributeInfo getCurNodeAsAttributeInTarget(YangNode curNode,
548 YangNode targetNode, boolean isListNode,
549 TempJavaFragmentFiles tempJavaFragmentFiles) {
550 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
551 if (curNodeName == null) {
552 updateJavaFileInfo(curNode, null);
553 curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
554 }
555
556 /*
557 * Get the import info corresponding to the attribute for import in
558 * generated java files or qualified access
559 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530560 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
Bharat saraswalaf413b82016-07-14 15:18:20 +0530561 getCapitalCase(curNodeName));
562 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
563 throw new TranslatorException("Parent node does not have file info");
564 }
565 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
Bharat saraswale50edca2016-08-05 01:58:25 +0530566 JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530567
568 boolean isQualified;
Bharat saraswalaab24b92016-08-02 18:43:16 +0530569 if ((tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles)
Bharat saraswal8beac342016-08-04 02:00:03 +0530570 && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE))
571 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530572
573 isQualified = true;
574 } else {
575 String className;
Bharat saraswalaab24b92016-08-02 18:43:16 +0530576 if (tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530577 className = getCapitalCase(fileInfo.getJavaName()) + SERVICE;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530578 } else {
579 className = getCapitalCase(fileInfo.getJavaName());
580 }
581
582 isQualified = parentImportData.addImportInfo(qualifiedTypeInfo,
583 className, fileInfo.getPackage());
584 }
585
Vidyashree Ramab3670472016-08-06 15:49:56 +0530586 boolean collectionSetFlag = false;
587 if (curNode instanceof YangList) {
588 YangList yangList = (YangList) curNode;
589 if (yangList.getCompilerAnnotation() != null && yangList.getCompilerAnnotation()
590 .getYangAppDataStructure() != null) {
591 switch (yangList.getCompilerAnnotation().getYangAppDataStructure().getDataStructure()) {
592 case QUEUE: {
593 parentImportData.setQueueToImport(true);
594 collectionSetFlag = true;
595 break;
596 }
597 case SET: {
598 parentImportData.setSetToImport(true);
599 collectionSetFlag = true;
600 break;
601 }
602 default: {
603 // TODO : to be implemented
604 }
605 }
606 }
607 }
608
609 if (isListNode && !(collectionSetFlag)) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530610 parentImportData.setIfListImported(true);
611 }
612
Vidyashree Ramab3670472016-08-06 15:49:56 +0530613 if (curNode instanceof YangList) {
614 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode,
615 ((YangList) curNode).getCompilerAnnotation());
616 }
617
Bharat saraswalaf413b82016-07-14 15:18:20 +0530618 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
619 }
620
621 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530622 * Returns java attribute for leaf.
623 *
624 * @param tempJavaFragmentFiles temporary generated file
625 * @param leaf YANG leaf
626 * @param yangPluginConfig plugin configurations
627 * @return java attribute for leaf
628 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530629 private static JavaAttributeInfo getJavaAttributeOfLeaf(TempJavaFragmentFiles tempJavaFragmentFiles, YangLeaf leaf,
630 YangPluginConfig yangPluginConfig) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530631 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
632 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
633 javaLeaf.updateJavaQualifiedInfo();
634 return getAttributeInfoForTheData(
635 javaLeaf.getJavaQualifiedInfo(),
636 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
637 javaLeaf.getDataType(),
638 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
639 false);
640 }
641
642 /**
643 * Returns java attribute for leaf-list.
644 *
645 * @param tempJavaFragmentFiles temporary generated file
646 * @param leafList YANG leaf-list
647 * @param yangPluginConfig plugin configurations
648 * @return java attribute for leaf-list
649 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530650 private static JavaAttributeInfo getJavaAttributeOfLeafList(TempJavaFragmentFiles tempJavaFragmentFiles,
651 YangLeafList leafList,
652 YangPluginConfig yangPluginConfig) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530653 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
654 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
655 javaLeaf.updateJavaQualifiedInfo();
656 tempJavaFragmentFiles.getJavaImportData().setIfListImported(true);
657 return getAttributeInfoForTheData(
658 javaLeaf.getJavaQualifiedInfo(),
659 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
660 javaLeaf.getDataType(),
661 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
662 true);
663 }
664
Bharat saraswal039f59c2016-07-14 21:57:13 +0530665 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530666 * Retrieves the absolute path where the file needs to be generated.
667 *
668 * @return absolute path where the file needs to be generated
669 */
670 private String getAbsoluteDirPath() {
671 return absoluteDirPath;
672 }
673
674 /**
675 * Sets absolute path where the file needs to be generated.
676 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530677 * @param absoluteDirPath absolute path where the file needs to be generated.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530678 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530679 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530680 this.absoluteDirPath = absoluteDirPath;
681 }
682
683 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530684 * Retrieves the generated java file information.
685 *
686 * @return generated java file information
687 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530688 public JavaFileInfoTranslator getJavaFileInfo() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530689 return javaFileInfo;
690 }
691
692 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530693 * Sets the generated java file information.
694 *
695 * @param javaFileInfo generated java file information
696 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530697 public void setJavaFileInfo(JavaFileInfoTranslator javaFileInfo) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530698 this.javaFileInfo = javaFileInfo;
699 }
700
701 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530702 * Retrieves the generated temp files.
703 *
704 * @return generated temp files
705 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530706 int getGeneratedTempFiles() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530707 return generatedTempFiles;
708 }
709
710 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530711 * Sets generated file files.
712 *
713 * @param fileType generated file type
714 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530715 private void setGeneratedTempFiles(int fileType) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530716 generatedTempFiles = fileType;
717 }
718
719 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530720 * Adds to generated temporary files.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530721 *
722 * @param generatedTempFile generated file
723 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530724 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530725 generatedTempFiles |= generatedTempFile;
Bharat saraswal250a7472016-05-12 13:16:57 +0530726 setGeneratedTempFiles(generatedTempFiles);
727 }
728
729 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530730 * Retrieves the generated Java files.
731 *
732 * @return generated Java files
733 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530734 int getGeneratedJavaFiles() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530735 return getJavaFileInfo().getGeneratedFileTypes();
736 }
737
738 /**
739 * Retrieves the mapped Java class name.
740 *
741 * @return mapped Java class name
742 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530743 String getGeneratedJavaClassName() {
janani b703cfe42016-05-17 13:12:22 +0530744 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530745 }
746
747 /**
748 * Retrieves the import data for the generated Java file.
749 *
750 * @return import data for the generated Java file
751 */
752 public JavaImportData getJavaImportData() {
753 return javaImportData;
754 }
755
756 /**
757 * Sets import data for the generated Java file.
758 *
759 * @param javaImportData import data for the generated Java file
760 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530761 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530762 this.javaImportData = javaImportData;
763 }
764
765 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530766 * Retrieves the status of any attributes added.
767 *
768 * @return status of any attributes added
769 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530770 boolean isAttributePresent() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530771 return isAttributePresent;
772 }
773
774 /**
775 * Sets status of any attributes added.
776 *
777 * @param attributePresent status of any attributes added
778 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530779 private void setAttributePresent(boolean attributePresent) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530780 isAttributePresent = attributePresent;
781 }
782
783 /**
784 * Returns getter methods's temporary file handle.
785 *
786 * @return temporary file handle
787 */
788 public File getGetterInterfaceTempFileHandle() {
789 return getterInterfaceTempFileHandle;
790 }
791
792 /**
793 * Sets to getter method's temporary file handle.
794 *
795 * @param getterForInterface file handle for to getter method
796 */
797 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
798 getterInterfaceTempFileHandle = getterForInterface;
799 }
800
801 /**
802 * Returns setter method's temporary file handle.
803 *
804 * @return temporary file handle
805 */
806 public File getSetterInterfaceTempFileHandle() {
807 return setterInterfaceTempFileHandle;
808 }
809
810 /**
811 * Sets to setter method's temporary file handle.
812 *
813 * @param setterForInterface file handle for to setter method
814 */
815 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
816 setterInterfaceTempFileHandle = setterForInterface;
817 }
818
819 /**
820 * Returns setter method's impl's temporary file handle.
821 *
822 * @return temporary file handle
823 */
824 public File getSetterImplTempFileHandle() {
825 return setterImplTempFileHandle;
826 }
827
828 /**
829 * Sets to setter method's impl's temporary file handle.
830 *
831 * @param setterImpl file handle for to setter method's implementation class
832 */
833 private void setSetterImplTempFileHandle(File setterImpl) {
834 setterImplTempFileHandle = setterImpl;
835 }
836
837 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530838 * Returns from string method's temporary file handle.
839 *
840 * @return from string method's temporary file handle
841 */
842 public File getFromStringImplTempFileHandle() {
843 return fromStringImplTempFileHandle;
844 }
845
846 /**
847 * Sets from string method's temporary file handle.
848 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530849 * @param fromStringImplTempFileHandle from string method's temporary file handle
Bharat saraswal250a7472016-05-12 13:16:57 +0530850 */
851 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
852 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
853 }
854
855 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530856 * Returns java file handle for interface file.
857 *
858 * @return java file handle for interface file
859 */
860 private File getInterfaceJavaFileHandle() {
861 return interfaceJavaFileHandle;
862 }
863
864 /**
865 * Sets the java file handle for interface file.
866 *
867 * @param interfaceJavaFileHandle java file handle
868 */
869 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
870 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
871 }
872
873 /**
874 * Returns java file handle for builder interface file.
875 *
876 * @return java file handle for builder interface file
877 */
878 private File getBuilderInterfaceJavaFileHandle() {
879 return builderInterfaceJavaFileHandle;
880 }
881
882 /**
883 * Sets the java file handle for builder interface file.
884 *
885 * @param builderInterfaceJavaFileHandle java file handle
886 */
887 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
888 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
889 }
890
891 /**
892 * Returns java file handle for builder class file.
893 *
894 * @return java file handle for builder class file
895 */
896 private File getBuilderClassJavaFileHandle() {
897 return builderClassJavaFileHandle;
898 }
899
900 /**
901 * Sets the java file handle for builder class file.
902 *
903 * @param builderClassJavaFileHandle java file handle
904 */
905 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
906 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
907 }
908
909 /**
910 * Returns java file handle for impl class file.
911 *
912 * @return java file handle for impl class file
913 */
914 private File getImplClassJavaFileHandle() {
915 return implClassJavaFileHandle;
916 }
917
918 /**
919 * Sets the java file handle for impl class file.
920 *
921 * @param implClassJavaFileHandle java file handle
922 */
923 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
924 this.implClassJavaFileHandle = implClassJavaFileHandle;
925 }
926
927 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530928 * Returns attribute's temporary file handle.
929 *
930 * @return temporary file handle
931 */
932 public File getAttributesTempFileHandle() {
933 return attributesTempFileHandle;
934 }
935
936 /**
937 * Sets attribute's temporary file handle.
938 *
939 * @param attributeForClass file handle for attribute
940 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530941 private void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530942 attributesTempFileHandle = attributeForClass;
943 }
944
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530945 /**
946 * Returns getter method's impl's temporary file handle.
947 *
948 * @return temporary file handle
949 */
950 public File getGetterImplTempFileHandle() {
951 return getterImplTempFileHandle;
952 }
953
954 /**
955 * Sets to getter method's impl's temporary file handle.
956 *
957 * @param getterImpl file handle for to getter method's impl
958 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530959 private void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530960 getterImplTempFileHandle = getterImpl;
961 }
962
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530963 /**
964 * Returns hash code method's temporary file handle.
965 *
966 * @return temporary file handle
967 */
968 public File getHashCodeImplTempFileHandle() {
969 return hashCodeImplTempFileHandle;
970 }
971
972 /**
973 * Sets hash code method's temporary file handle.
974 *
975 * @param hashCodeMethod file handle for hash code method
976 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530977 private void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530978 hashCodeImplTempFileHandle = hashCodeMethod;
979 }
980
981 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530982 * Returns equals method's temporary file handle.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530983 *
984 * @return temporary file handle
985 */
986 public File getEqualsImplTempFileHandle() {
987 return equalsImplTempFileHandle;
988 }
989
990 /**
991 * Sets equals method's temporary file handle.
992 *
993 * @param equalsMethod file handle for to equals method
994 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530995 private void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530996 equalsImplTempFileHandle = equalsMethod;
997 }
998
999 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301000 * Returns to string method's temporary file handle.
1001 *
1002 * @return temporary file handle
1003 */
1004 public File getToStringImplTempFileHandle() {
1005 return toStringImplTempFileHandle;
1006 }
1007
1008 /**
1009 * Sets to string method's temporary file handle.
1010 *
1011 * @param toStringMethod file handle for to string method
1012 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301013 private void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301014 toStringImplTempFileHandle = toStringMethod;
1015 }
1016
1017 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301018 * Returns java extends list holder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301019 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301020 * @return java extends list holder
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301021 */
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301022 public JavaExtendsListHolder getJavaExtendsListHolder() {
1023 return javaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301024 }
1025
1026 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301027 * Sets java extends list holder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301028 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301029 * @param javaExtendsListHolder java extends list holder
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301030 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301031 void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301032 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301033 }
1034
1035 /**
Bharat saraswale50edca2016-08-05 01:58:25 +05301036 * Adds is filter content match for leaf.
1037 *
1038 * @param attr java attribute
1039 * @throws IOException when fails to do IO operations
1040 */
1041 private void addIsFilerForLeaf(JavaAttributeInfo attr)
1042 throws IOException {
1043 appendToFile(getIsContentMatchLeafTempFileHandle(),
1044 getIsFilerContentMatchForLeaf(attr, attr.getAttributeType()) + NEW_LINE);
1045 }
1046
1047 /**
1048 * Adds is filter content match for leaf-list.
1049 *
1050 * @param attr java attribute
1051 * @throws IOException when fails to do IO operations
1052 */
1053 private void addIsFilerForLeafList(JavaAttributeInfo attr)
1054 throws IOException {
1055 appendToFile(getIsContentMatchLeafTempFileHandle(),
1056 getIsFilerContentMatchForLeafList(attr) + NEW_LINE);
1057 }
1058
1059 /**
1060 * Adds is filter content match for nodes.
1061 *
1062 * @param attr java attribute
1063 * @throws IOException when fails to do IO operations
1064 */
1065 private void addIsFilerForNode(JavaAttributeInfo attr)
1066 throws IOException {
1067 appendToFile(getIsContentMatchLeafTempFileHandle(),
1068 getIsFilterContentForNodes(attr.getAttributeName(), attr.isListAttr()) + NEW_LINE);
1069 }
1070
1071 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301072 * Adds attribute for class.
1073 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301074 * @param attr attribute info
1075 * @param yangPluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301076 * @throws IOException when fails to append to temporary file
1077 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301078 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301079 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301080 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
1081 + FOUR_SPACE_INDENTATION);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301082 }
1083
1084 /**
1085 * Adds getter for interface.
1086 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301087 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301088 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301089 * @throws IOException when fails to append to temporary file
1090 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301091 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301092 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +05301093 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301094 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301095 }
1096
1097 /**
1098 * Adds setter for interface.
1099 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301100 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301101 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301102 * @throws IOException when fails to append to temporary file
1103 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301104 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301105 throws IOException {
1106 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301107 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
1108 + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301109 }
1110
1111 /**
1112 * Adds setter's implementation for class.
1113 *
1114 * @param attr attribute info
1115 * @throws IOException when fails to append to temporary file
1116 */
1117 private void addSetterImpl(JavaAttributeInfo attr)
1118 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301119 if (isRooNode()) {
1120 appendToFile(getSetterImplTempFileHandle(),
1121 getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
1122 + NEW_LINE);
1123 } else {
1124 appendToFile(getSetterImplTempFileHandle(), getOverRideString() +
1125 getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
1126 + NEW_LINE);
1127 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301128 }
1129
1130 /**
1131 * Adds getter method's impl for class.
1132 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301133 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301134 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301135 * @throws IOException when fails to append to temporary file
1136 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301137 void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301138 throws IOException {
1139 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
1140 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301141 if (!isRooNode()) {
1142 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
1143 getGeneratedJavaFiles()) + NEW_LINE);
1144 } else {
1145 appendToFile(getGetterImplTempFileHandle(), getGetterForClass(attr,
1146 getGeneratedJavaFiles()) + NEW_LINE);
1147 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301148 } else {
Vidyashree Ramab3670472016-08-06 15:49:56 +05301149 String appDataStructure = null;
1150 if (attr.getCompilerAnnotation() != null) {
1151 appDataStructure = attr.getCompilerAnnotation().getYangAppDataStructure().getDataStructure().name();
1152 }
janani b703cfe42016-05-17 13:12:22 +05301153 appendToFile(getGetterImplTempFileHandle(),
Vidyashree Ramab3670472016-08-06 15:49:56 +05301154 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig,
1155 appDataStructure) + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301156 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301157 }
1158
1159 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301160 * Adds add to list interface method.
1161 *
1162 * @param attr attribute
1163 * @param pluginConfig plugin configurations
1164 * @throws IOException when fails to do IO operations
1165 */
1166 private void addAddToListInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig) throws IOException {
1167 appendToFile(getAddToListInterfaceTempFileHandle(),
Vidyashree Ramab3670472016-08-06 15:49:56 +05301168 getJavaDoc(ADD_TO_LIST, getCapitalCase(attr.getAttributeName()), false, pluginConfig, null)
Bharat saraswal8beac342016-08-04 02:00:03 +05301169 + getAddToListMethodInterface(attr) + NEW_LINE);
1170 }
1171
1172 /**
1173 * Adds add to list interface method.
1174 *
1175 * @param attr attribute
1176 * @throws IOException when fails to do IO operations
1177 */
1178 private void addAddToListImpl(JavaAttributeInfo attr) throws IOException {
1179 appendToFile(getAddToListImplTempFileHandle(),
1180 getAddToListMethodImpl(attr) + NEW_LINE);
1181 }
1182
1183 /**
1184 * Adds leaf identifier enum attributes.
1185 *
1186 * @param attr attribute
1187 * @param value value
1188 * @param yangPluginConfig plugin config
1189 * @throws IOException when fails to do IO operations
1190 */
1191 private void addLeafIdAttributes(JavaAttributeInfo attr, int value, YangPluginConfig yangPluginConfig)
1192 throws IOException {
1193 appendToFile(getLeafIdAttributeTempFileHandle(),
1194 FOUR_SPACE_INDENTATION + generateEnumAttributeString(attr.getAttributeName(),
1195 value, yangPluginConfig));
1196 }
1197
1198 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301199 * Adds build method for interface.
1200 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301201 * @param pluginConfig plugin configurations
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301202 * @return build method for interface
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301203 * @throws IOException when fails to append to temporary file
1204 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301205 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301206 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301207 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301208 }
1209
1210 /**
1211 * Adds build method's implementation for class.
1212 *
1213 * @return build method implementation for class
1214 * @throws IOException when fails to append to temporary file
1215 */
1216 String addBuildMethodImpl()
1217 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301218 return getBuildString(getGeneratedJavaClassName(), isRooNode()) + NEW_LINE;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301219 }
1220
1221 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301222 * Adds default constructor for class.
1223 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301224 * @param modifier modifier for constructor.
1225 * @param toAppend string which need to be appended with the class name
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301226 * @param pluginConfig plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +05301227 * @param isSuffix is value need to be appended as suffix
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301228 * @return default constructor for class
1229 * @throws IOException when fails to append to file
1230 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301231 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig, boolean isSuffix)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301232 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301233 String name = getGeneratedJavaClassName();
1234 if (isRooNode() && !toAppend.equals(BUILDER)) {
1235 name = name + OP_PARAM;
1236 return NEW_LINE
1237 + getDefaultConstructorString(name, modifier,
1238 pluginConfig);
1239 }
1240 if (isSuffix) {
1241 return NEW_LINE +
1242 getDefaultConstructorString(name + toAppend, modifier, pluginConfig);
1243 }
1244 String appended;
1245 if (toAppend.equals(DEFAULT)) {
1246 appended = getCapitalCase(toAppend);
1247 } else {
1248 appended = toAppend;
1249 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301250 return NEW_LINE
Bharat saraswal8beac342016-08-04 02:00:03 +05301251 + getDefaultConstructorString(appended + name, modifier,
1252 pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301253 }
1254
1255 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301256 * Adds hash code method for class.
1257 *
1258 * @param attr attribute info
1259 * @throws IOException when fails to append to temporary file
1260 */
1261 private void addHashCodeMethod(JavaAttributeInfo attr)
1262 throws IOException {
1263 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
1264 }
1265
1266 /**
1267 * Adds equals method for class.
1268 *
1269 * @param attr attribute info
1270 * @throws IOException when fails to append to temporary file
1271 */
1272 private void addEqualsMethod(JavaAttributeInfo attr)
1273 throws IOException {
1274 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
1275 }
1276
1277 /**
1278 * Adds ToString method for class.
1279 *
1280 * @param attr attribute info
1281 * @throws IOException when fails to append to temporary file
1282 */
1283 private void addToStringMethod(JavaAttributeInfo attr)
1284 throws IOException {
1285 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
1286 }
1287
1288 /**
Bharat saraswal250a7472016-05-12 13:16:57 +05301289 * Adds from string method for union class.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301290 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301291 * @param javaAttributeInfo type attribute info
Bharat saraswal250a7472016-05-12 13:16:57 +05301292 * @param fromStringAttributeInfo from string attribute info
1293 * @throws IOException when fails to append to temporary file
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301294 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301295 void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
1296 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301297 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +05301298 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
1299 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301300 }
1301
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301302 /**
1303 * Returns a temporary file handle for the specific file type.
1304 *
1305 * @param fileName file name
1306 * @return temporary file handle
1307 * @throws IOException when fails to create new file handle
1308 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301309 File getTemporaryFileHandle(String fileName)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301310 throws IOException {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301311 String path = getTempDirPath(getAbsoluteDirPath());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301312 File dir = new File(path);
Bharat saraswal8beac342016-08-04 02:00:03 +05301313 boolean isCreated;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301314 if (!dir.exists()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301315 isCreated = dir.mkdirs();
Shankara-Huaweib7564772016-08-02 18:13:13 +05301316 if (!isCreated) {
1317 throw new IOException("failed to create temporary directory for " + fileName);
1318 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301319 }
1320 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1321 if (!file.exists()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301322 isCreated = file.createNewFile();
Shankara-Huaweib7564772016-08-02 18:13:13 +05301323 if (!isCreated) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301324 throw new IOException("failed to create temporary file for " + fileName);
Shankara-Huaweib7564772016-08-02 18:13:13 +05301325 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301326 } else {
1327 throw new IOException(fileName + " is reused due to YANG naming");
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301328 }
1329 return file;
1330 }
1331
1332 /**
1333 * Returns a temporary file handle for the specific file type.
1334 *
1335 * @param fileName file name
1336 * @return temporary file handle
1337 * @throws IOException when fails to create new file handle
1338 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301339 File getJavaFileHandle(String fileName)
1340 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301341 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301342 }
1343
1344 /**
1345 * Returns data from the temporary files.
1346 *
Bharat saraswalaf413b82016-07-14 15:18:20 +05301347 * @param file temporary file handle
Bharat saraswald14cbe82016-07-14 13:26:18 +05301348 * @param absolutePath absolute path
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301349 * @return stored data from temporary files
1350 * @throws IOException when failed to get data from the given file
1351 */
Bharat saraswald14cbe82016-07-14 13:26:18 +05301352 public String getTemporaryDataFromFileHandle(File file, String absolutePath)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301353 throws IOException {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301354 String path = getTempDirPath(absolutePath);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301355 if (new File(path + file.getName()).exists()) {
1356 return readAppendFile(path + file.getName(), EMPTY_STRING);
1357 } else {
1358 throw new IOException("Unable to get data from the given "
1359 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1360 }
1361 }
1362
1363 /**
1364 * Returns temporary directory path.
1365 *
Bharat saraswald14cbe82016-07-14 13:26:18 +05301366 * @param absolutePath absolute path
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301367 * @return directory path
1368 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301369 private String getTempDirPath(String absolutePath) {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301370 return getPackageDirPathFromJavaJPackage(absolutePath) + SLASH + getGeneratedJavaClassName()
Bharat saraswal64e7e232016-07-14 23:33:55 +05301371 + TEMP_FOLDER_NAME_SUFFIX + SLASH;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301372 }
1373
1374 /**
1375 * Parses attribute to get the attribute string.
1376 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301377 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301378 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301379 * @return attribute string
1380 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301381 String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vidyashree Ramab3670472016-08-06 15:49:56 +05301382 //TODO: check if this utility needs to be called or move to the caller
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301383 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301384 String attributeAccessType = PRIVATE;
1385 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
1386 attributeAccessType = PROTECTED;
1387 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301388 if (attr.isQualifiedName()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301389 return getJavaAttributeDefinition(attr.getImportInfo().getPkgInfo(),
Bharat saraswal250a7472016-05-12 13:16:57 +05301390 attr.getImportInfo().getClassInfo(),
Vidyashree Ramab3670472016-08-06 15:49:56 +05301391 attributeName, attr.isListAttr(), attributeAccessType, attr.getCompilerAnnotation());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301392 } else {
Bharat saraswal8beac342016-08-04 02:00:03 +05301393 return getJavaAttributeDefinition(null, attr.getImportInfo().getClassInfo(), attributeName,
Vidyashree Ramab3670472016-08-06 15:49:56 +05301394 attr.isListAttr(), attributeAccessType, attr.getCompilerAnnotation());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301395 }
1396 }
1397
1398 /**
1399 * Appends content to temporary file.
1400 *
1401 * @param file temporary file
1402 * @param data data to be appended
1403 * @throws IOException when fails to append to file
1404 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301405 void appendToFile(File file, String data)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301406 throws IOException {
1407 try {
1408 insertDataIntoJavaFile(file, data);
1409 } catch (IOException ex) {
1410 throw new IOException("failed to write in temp file.");
1411 }
1412 }
1413
1414 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301415 * Adds parent's info to current node import list.
1416 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301417 * @param curNode current node for which import list needs to be updated
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301418 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301419 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301420 void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Bharat saraswale50edca2016-08-05 01:58:25 +05301421 JavaQualifiedTypeInfoTranslator caseImportInfo = new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301422 YangNode parent = getParentNodeInGenCode(curNode);
Bharat saraswale50edca2016-08-05 01:58:25 +05301423 if (curNode instanceof YangCase && parent instanceof YangAugment) {
1424 return;
1425 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301426 if (!(parent instanceof JavaCodeGenerator)) {
1427 throw new TranslatorException("missing parent node to contain current node info in generated file");
1428 }
1429 if (!(curNode instanceof JavaFileInfoContainer)) {
1430 throw new TranslatorException("missing java file information to get the package details "
1431 + "of attribute corresponding to child node");
1432 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301433 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1434 pluginConfig.getConflictResolver())));
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301435 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301436
Bharat saraswale50edca2016-08-05 01:58:25 +05301437 JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301438
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301439 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301440 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +05301441 getCapitalCase(fileInfo.getJavaName()), fileInfo.getPackage());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301442 }
1443
1444 /**
1445 * Adds leaf attributes in generated files.
1446 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301447 * @param listOfLeaves list of YANG leaf
1448 * @param yangPluginConfig plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +05301449 * @param curNode current node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301450 * @throws IOException IO operation fail
1451 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301452 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1453 YangPluginConfig yangPluginConfig, YangNode curNode)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301454 throws IOException {
1455 if (listOfLeaves != null) {
1456 for (YangLeaf leaf : listOfLeaves) {
1457 if (!(leaf instanceof JavaLeafInfoContainer)) {
1458 throw new TranslatorException("Leaf does not have java information");
1459 }
Bharat saraswal64e7e232016-07-14 23:33:55 +05301460 if (curNode instanceof YangModule || curNode instanceof YangSubModule) {
1461 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) curNode)
1462 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
1463 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(tempJavaBeanFragmentFiles, leaf,
1464 yangPluginConfig), yangPluginConfig);
1465 } else {
1466 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(this, leaf, yangPluginConfig),
1467 yangPluginConfig);
1468 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301469 }
1470 }
1471 }
1472
1473 /**
1474 * Adds leaf list's attributes in generated files.
1475 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301476 * @param listOfLeafList list of YANG leaves
1477 * @param yangPluginConfig plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +05301478 * @param curNode current node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301479 * @throws IOException IO operation fail
1480 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301481 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig,
1482 YangNode curNode) throws IOException {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301483 if (listOfLeafList != null) {
1484 for (YangLeafList leafList : listOfLeafList) {
1485 if (!(leafList instanceof JavaLeafInfoContainer)) {
1486 throw new TranslatorException("Leaf-list does not have java information");
1487 }
Bharat saraswal64e7e232016-07-14 23:33:55 +05301488 if (curNode instanceof YangModule || curNode instanceof YangSubModule) {
1489 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) curNode)
1490 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
1491 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(tempJavaBeanFragmentFiles,
1492 leafList, yangPluginConfig), yangPluginConfig);
1493 } else {
1494 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(this,
1495 leafList, yangPluginConfig), yangPluginConfig);
1496 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301497 }
1498 }
1499 }
1500
1501 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +05301502 * Adds all the leaves in the current data model node as part of the generated temporary file.
Bharat saraswald14cbe82016-07-14 13:26:18 +05301503 *
Bharat saraswal64e7e232016-07-14 23:33:55 +05301504 * @param curNode current node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301505 * @param yangPluginConfig plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301506 * @throws IOException IO operation fail
1507 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301508 void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
Shankara-Huaweib7564772016-08-02 18:13:13 +05301509 YangPluginConfig yangPluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301510 throws IOException {
1511 if (!(curNode instanceof YangLeavesHolder)) {
1512 throw new TranslatorException("Data model node does not have any leaves");
1513 }
1514 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
Bharat saraswal64e7e232016-07-14 23:33:55 +05301515 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig, curNode);
1516 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig, curNode);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301517 }
1518
1519 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301520 * Adds the new attribute info to the target generated temporary files.
1521 *
Bharat saraswalaf413b82016-07-14 15:18:20 +05301522 * @param newAttrInfo the attribute info that needs to be added to temporary files
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301523 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301524 * @throws IOException IO operation fail
1525 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301526 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301527 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301528 setAttributePresent(true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301529 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301530 addAttribute(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301531 }
1532 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301533 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301534 }
1535 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301536 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301537 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301538 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1539 addSetterImpl(newAttrInfo);
1540 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301541 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1542 addHashCodeMethod(newAttrInfo);
1543 }
1544 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1545 addEqualsMethod(newAttrInfo);
1546 }
1547 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1548 addToStringMethod(newAttrInfo);
1549 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301550 if ((getGeneratedTempFiles() & EDIT_CONTENT_MASK) != 0) {
1551 //TODO: add implementation for edit content match.
1552 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301553 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0 && newAttrInfo.isListAttr()) {
1554 addAddToListImpl(newAttrInfo);
1555 }
1556 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0 && newAttrInfo.isListAttr()) {
1557 addAddToListInterface(newAttrInfo, pluginConfig);
1558 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301559 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_NODES_MASK) != 0
1560 && newAttrInfo.getAttributeType() == null) {
1561 addIsFilerForNode(newAttrInfo);
1562 }
1563 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_MASK) != 0 && !newAttrInfo.isListAttr()
1564 && newAttrInfo.getAttributeType() != null) {
1565 addIsFilerForLeaf(newAttrInfo);
1566 }
1567 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK) != 0 && newAttrInfo.isListAttr()
1568 && newAttrInfo.getAttributeType() != null) {
1569 addIsFilerForLeafList(newAttrInfo);
1570 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301571 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0 && !newAttrInfo.isListAttr()
1572 && newAttrInfo.getAttributeType() != null) {
1573 leafCount++;
1574 addLeafIdAttributes(newAttrInfo, leafCount, pluginConfig);
1575 }
Bharat saraswal64e7e232016-07-14 23:33:55 +05301576 if (!newAttrInfo.isIntConflict() &&
1577 !newAttrInfo.isLongConflict()) {
1578 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1579 addGetterImpl(newAttrInfo, pluginConfig);
1580 }
1581
1582 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Shankara-Huaweib7564772016-08-02 18:13:13 +05301583 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +05301584 pluginConfig.getConflictResolver());
Bharat saraswal250a7472016-05-12 13:16:57 +05301585 /*
1586 * Create a new java attribute info with qualified information of
1587 * wrapper classes.
1588 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301589 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1590 newAttrInfo.getAttributeName(),
1591 newAttrInfo.getAttributeType(),
1592 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
Bharat saraswal250a7472016-05-12 13:16:57 +05301593
Bharat saraswal64e7e232016-07-14 23:33:55 +05301594 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
1595 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301596 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301597 }
1598
1599 /**
1600 * Returns java class name.
1601 *
1602 * @param suffix for the class name based on the file type
1603 * @return java class name
1604 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301605 String getJavaClassName(String suffix) {
janani b703cfe42016-05-17 13:12:22 +05301606 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301607 }
1608
1609 /**
Bharat saraswald14cbe82016-07-14 13:26:18 +05301610 * Returns java class name.
1611 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301612 * @param node YANG node
Bharat saraswald14cbe82016-07-14 13:26:18 +05301613 * @return java class name
1614 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301615 private String getImplClassName(YangNode node) {
1616 if (node instanceof YangModule || node instanceof YangSubModule) {
1617 return getCapitalCase(getJavaFileInfo().getJavaName()) + OP_PARAM;
1618 }
Bharat saraswald14cbe82016-07-14 13:26:18 +05301619 return getCapitalCase(DEFAULT) + getCapitalCase(getJavaFileInfo().getJavaName());
1620 }
1621
1622 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301623 * Returns the directory path.
1624 *
1625 * @return directory path
1626 */
1627 private String getDirPath() {
1628 return getJavaFileInfo().getPackageFilePath();
1629 }
1630
1631 /**
1632 * Constructs java code exit.
1633 *
1634 * @param fileType generated file type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301635 * @param curNode current YANG node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301636 * @throws IOException when fails to generate java files
1637 */
1638 public void generateJavaFile(int fileType, YangNode curNode)
1639 throws IOException {
Bharat saraswalaf413b82016-07-14 15:18:20 +05301640 List<String> imports = ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getBeanTempFiles()
1641 .getJavaImportData().getImports();
Bharat saraswal8beac342016-08-04 02:00:03 +05301642 if (curNode instanceof YangAugmentableNode) {
1643 addImportsForAugmentableClass(imports, true, true);
1644 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301645 createPackage(curNode);
Vidyashree Ramab3670472016-08-06 15:49:56 +05301646
1647 //Generate java code.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301648 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1649 BUILDER_INTERFACE_MASK) != 0) {
Vidyashree Ramab3670472016-08-06 15:49:56 +05301650
1651 //Create interface file.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301652 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1653 setInterfaceJavaFileHandle(
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301654 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
Bharat saraswal8beac342016-08-04 02:00:03 +05301655 if (!(curNode instanceof YangModule) && !(curNode instanceof YangSubModule)) {
Vidyashree Ramab3670472016-08-06 15:49:56 +05301656
1657 //Create builder interface file.
Bharat saraswal8beac342016-08-04 02:00:03 +05301658 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1659 setBuilderInterfaceJavaFileHandle(
1660 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1661 setBuilderInterfaceJavaFileHandle(
1662 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1663 isAttributePresent()));
Vidyashree Ramab3670472016-08-06 15:49:56 +05301664
1665 //Append builder interface file to interface file and close it.
Bharat saraswal8beac342016-08-04 02:00:03 +05301666 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1667 validateLineLength(getInterfaceJavaFileHandle());
1668 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301669 }
1670 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswal8beac342016-08-04 02:00:03 +05301671 if (curNode instanceof YangAugmentableNode) {
1672 addImportsForAugmentableClass(imports, false, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +05301673 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301674 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301675 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & DEFAULT_CLASS_MASK) != 0) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301676 if (isAttributePresent()) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301677 addImportsToStringAndHasCodeMethods(imports, true);
Bharat saraswal8beac342016-08-04 02:00:03 +05301678 addArrayListImport(imports);
Bharat saraswalaf413b82016-07-14 15:18:20 +05301679 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301680 addBitsetImport(imports);
Bharat saraswalaf413b82016-07-14 15:18:20 +05301681 if (curNode instanceof YangAugmentableNode) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301682 addImportsForAugmentableClass(imports, true, false);
Bharat saraswale50edca2016-08-05 01:58:25 +05301683 addInvocationExceptionImport(imports);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301684 }
Bharat saraswal250a7472016-05-12 13:16:57 +05301685 sortImports(imports);
Vidyashree Ramab3670472016-08-06 15:49:56 +05301686
1687 //Create impl class file.
Bharat saraswal8beac342016-08-04 02:00:03 +05301688 setImplClassJavaFileHandle(getJavaFileHandle(getImplClassName(curNode)));
Bharat saraswald14cbe82016-07-14 13:26:18 +05301689 setImplClassJavaFileHandle(
Bharat saraswal8beac342016-08-04 02:00:03 +05301690 generateDefaultClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent(), imports));
Vidyashree Ramab3670472016-08-06 15:49:56 +05301691
1692 //Create builder class file.
Bharat saraswald14cbe82016-07-14 13:26:18 +05301693 if ((fileType & BUILDER_CLASS_MASK) != 0) {
1694 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1695 setBuilderClassJavaFileHandle(
1696 generateBuilderClassFile(getBuilderClassJavaFileHandle(), curNode,
1697 isAttributePresent()));
Vidyashree Ramab3670472016-08-06 15:49:56 +05301698
1699 //Append impl class to builder class and close it.
Bharat saraswald14cbe82016-07-14 13:26:18 +05301700 mergeJavaFiles(getBuilderClassJavaFileHandle(), getImplClassJavaFileHandle());
1701 validateLineLength(getImplClassJavaFileHandle());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301702 }
Bharat saraswald14cbe82016-07-14 13:26:18 +05301703 insertDataIntoJavaFile(getImplClassJavaFileHandle(), getJavaClassDefClose());
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301704
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301705 }
Vidyashree Ramab3670472016-08-06 15:49:56 +05301706
1707 //Close all the file handles.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301708 freeTemporaryResources(false);
1709 }
1710
Vidyashree Ramab3670472016-08-06 15:49:56 +05301711 //Adds import for array list.
Bharat saraswal8beac342016-08-04 02:00:03 +05301712 private void addArrayListImport(List<String> imports) {
1713 if (imports.contains(getJavaImportData().getImportForList())) {
1714 imports.add(ARRAY_LIST_IMPORT);
1715 }
1716 }
1717
Bharat saraswal8beac342016-08-04 02:00:03 +05301718 private void addBitsetImport(List<String> imports) {
1719 imports.add(getJavaImportData().getImportForToBitSet());
Bharat saraswald14cbe82016-07-14 13:26:18 +05301720 }
1721
1722 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301723 * Adds imports for ToString and HashCodeMethod.
1724 *
Bharat saraswal039f59c2016-07-14 21:57:13 +05301725 * @param imports import list
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301726 * @param operation add or remove
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301727 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301728 void addImportsToStringAndHasCodeMethods(List<String> imports, boolean operation) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301729 if (operation) {
1730 imports.add(getJavaImportData().getImportForHashAndEquals());
1731 imports.add(getJavaImportData().getImportForToString());
1732 } else {
1733 imports.remove(getJavaImportData().getImportForHashAndEquals());
1734 imports.remove(getJavaImportData().getImportForToString());
1735 }
1736 }
1737
1738 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +05301739 * Adds import for map and hash map.
1740 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301741 * @param imports import list
1742 * @param operations true for adding and false for deletion
1743 * @param isInterfaceFile if need to add in interface file
Bharat saraswalaf413b82016-07-14 15:18:20 +05301744 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301745 private void addImportsForAugmentableClass(List<String> imports, boolean operations, boolean isInterfaceFile) {
Bharat saraswal039f59c2016-07-14 21:57:13 +05301746 if (operations) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301747 if (!isInterfaceFile) {
1748 imports.add(getJavaImportData().getHashMapImport());
1749 }
Bharat saraswal039f59c2016-07-14 21:57:13 +05301750 imports.add(getJavaImportData().getMapImport());
Bharat saraswal039f59c2016-07-14 21:57:13 +05301751 } else {
Bharat saraswal8beac342016-08-04 02:00:03 +05301752 if (!isInterfaceFile) {
1753 imports.remove(getJavaImportData().getHashMapImport());
1754 }
Bharat saraswal039f59c2016-07-14 21:57:13 +05301755 imports.remove(getJavaImportData().getMapImport());
Bharat saraswald14cbe82016-07-14 13:26:18 +05301756 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301757 sortImports(imports);
Bharat saraswald14cbe82016-07-14 13:26:18 +05301758 }
1759
1760 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301761 * Adds invocation exception import.
Bharat saraswal039f59c2016-07-14 21:57:13 +05301762 *
1763 * @param imports list of imports
Bharat saraswal039f59c2016-07-14 21:57:13 +05301764 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301765 private void addInvocationExceptionImport(List<String> imports) {
1766 imports.add(INVOCATION_TARGET_EXCEPTION_IMPORT);
Bharat saraswal039f59c2016-07-14 21:57:13 +05301767 }
1768
1769 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301770 * Removes all temporary file handles.
1771 *
Bharat saraswal64e7e232016-07-14 23:33:55 +05301772 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301773 * @throws IOException when failed to delete the temporary files
1774 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301775 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301776 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301777 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301778 * Close all java file handles and when error occurs delete the files.
1779 */
1780 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +05301781 closeFile(getInterfaceJavaFileHandle(), isErrorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301782 }
1783 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301784 closeFile(getBuilderClassJavaFileHandle(), true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301785 }
1786 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1787 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1788 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301789 if ((getGeneratedJavaFiles() & DEFAULT_CLASS_MASK) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +05301790 closeFile(getImplClassJavaFileHandle(), isErrorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301791 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301792 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301793 * Close all temporary file handles and delete the files.
1794 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301795 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1796 closeFile(getGetterImplTempFileHandle(), true);
1797 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301798 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1799 closeFile(getAttributesTempFileHandle(), true);
1800 }
1801 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1802 closeFile(getHashCodeImplTempFileHandle(), true);
1803 }
1804 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1805 closeFile(getToStringImplTempFileHandle(), true);
1806 }
1807 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1808 closeFile(getEqualsImplTempFileHandle(), true);
1809 }
Bharat saraswal250a7472016-05-12 13:16:57 +05301810 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1811 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301812 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301813 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0) {
1814 closeFile(getAddToListImplTempFileHandle(), true);
1815 }
1816 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0) {
1817 closeFile(getAddToListInterfaceTempFileHandle(), true);
1818 }
1819 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0) {
1820 closeFile(getLeafIdAttributeTempFileHandle(), true);
1821 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301822 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_MASK) != 0) {
1823 closeFile(getIsContentMatchLeafTempFileHandle(), true);
1824 }
1825 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK) != 0) {
1826 closeFile(getIsContentMatchLeafListTempFileHandle(), true);
1827 }
1828 if ((getGeneratedTempFiles() & FILTER_CONTENT_MATCH_FOR_NODES_MASK) != 0) {
1829 closeFile(getIsContentMatchNodeTempFileHandle(), true);
1830 }
1831 if ((getGeneratedTempFiles() & EDIT_CONTENT_MASK) != 0) {
1832 closeFile(getEditContentTempFileHandle(), true);
1833 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301834 }
1835
1836 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +05301837 * Returns if the attribute needs to be accessed in a qualified manner or not, if it needs to be imported, then the
1838 * same needs to be done.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301839 *
1840 * @param importInfo import info for the current attribute being added
1841 * @return status of the qualified access to the attribute
1842 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301843 boolean getIsQualifiedAccessOrAddToImportList(
Shankara-Huaweib7564772016-08-02 18:13:13 +05301844 JavaQualifiedTypeInfoTranslator importInfo) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301845 return getJavaImportData().addImportInfo(importInfo, getGeneratedJavaClassName(),
1846 getJavaFileInfo().getPackage());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301847 }
1848
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301849 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301850 * Returns temp file handle for add to list interface.
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301851 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301852 * @return temp file handle for add to list interface
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301853 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301854 public File getAddToListInterfaceTempFileHandle() {
1855 return addToListInterfaceTempFileHandle;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301856 }
1857
1858 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301859 * Sets temp file handle for add to list interface.
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301860 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301861 * @param addToListInterfaceTempFileHandle temp file handle for add to list interface
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301862 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301863 private void setAddToListInterfaceTempFileHandle(File addToListInterfaceTempFileHandle) {
1864 this.addToListInterfaceTempFileHandle = addToListInterfaceTempFileHandle;
1865 }
1866
1867 /**
1868 * Returns temp file handle for add to list impl.
1869 *
1870 * @return temp file handle for add to list impl
1871 */
1872 public File getAddToListImplTempFileHandle() {
1873 return addToListImplTempFileHandle;
1874 }
1875
1876 /**
1877 * Sets temp file handle for add to list impl.
1878 *
1879 * @param addToListImplTempFileHandle temp file handle for add to list impl
1880 */
1881 private void setAddToListImplTempFileHandle(File addToListImplTempFileHandle) {
1882 this.addToListImplTempFileHandle = addToListImplTempFileHandle;
1883 }
1884
1885 /**
1886 * Returns temp file handle for leaf identifier attributes.
1887 *
1888 * @return temp file handle for leaf identifier attributes
1889 */
1890 public File getLeafIdAttributeTempFileHandle() {
1891 return leafIdAttributeTempFileHandle;
1892 }
1893
1894 /**
1895 * Sets temp file handle for leaf identifier attributes.
1896 *
Bharat saraswale50edca2016-08-05 01:58:25 +05301897 * @param leafIdAttributeTempFileHandle temp file handle for leaf identifier attributes
Bharat saraswal8beac342016-08-04 02:00:03 +05301898 */
1899 private void setLeafIdAttributeTempFileHandle(File leafIdAttributeTempFileHandle) {
1900 this.leafIdAttributeTempFileHandle = leafIdAttributeTempFileHandle;
1901 }
1902
1903 /**
1904 * Returns if root node is set.
1905 *
1906 * @return true if root node
1907 */
1908 private boolean isRooNode() {
1909 return isRooNode;
1910 }
1911
1912 /**
1913 * Sets true if root node.
1914 *
1915 * @param rooNode true if root node
1916 */
Bharat saraswale50edca2016-08-05 01:58:25 +05301917 void setRooNode(boolean rooNode) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301918 isRooNode = rooNode;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301919 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301920
1921 /**
1922 * Returns temp file for is content match.
1923 *
1924 * @return temp file for is content match
1925 */
1926 public File getIsContentMatchLeafTempFileHandle() {
1927 return isContentMatchLeafTempFileHandle;
1928 }
1929
1930 /**
1931 * Sets temp file handle for is content match.
1932 *
1933 * @param isContentMatchLeafTempFileHandle temp file handle for is content match
1934 */
1935 private void setIsContentMatchLeafTempFileHandle(File isContentMatchLeafTempFileHandle) {
1936 this.isContentMatchLeafTempFileHandle = isContentMatchLeafTempFileHandle;
1937 }
1938
1939 /**
1940 * Returns temp file for edit content file.
1941 *
1942 * @return temp file for edit content file
1943 */
1944 public File getEditContentTempFileHandle() {
1945 return editContentTempFileHandle;
1946 }
1947
1948 /**
1949 * Sets temp file for edit content file.
1950 *
1951 * @param editContentTempFileHandle temp file for edit content file
1952 */
1953 private void setEditContentTempFileHandle(File editContentTempFileHandle) {
1954 this.editContentTempFileHandle = editContentTempFileHandle;
1955 }
1956
1957 /**
1958 * Returns temp file for is content match.
1959 *
1960 * @return temp file for is content match
1961 */
1962 public File getIsContentMatchLeafListTempFileHandle() {
1963 return isContentMatchLeafListTempFileHandle;
1964 }
1965
1966 /**
1967 * Sets temp file handle for is content match.
1968 *
1969 * @param isContentMatchLeafListTempFileHandle temp file handle for is content match
1970 */
1971 private void setIsContentMatchLeafListTempFileHandle(File isContentMatchLeafListTempFileHandle) {
1972 this.isContentMatchLeafListTempFileHandle = isContentMatchLeafListTempFileHandle;
1973 }
1974
1975 /**
1976 * Returns temp file for is content match.
1977 *
1978 * @return temp file for is content match
1979 */
1980 public File getIsContentMatchNodeTempFileHandle() {
1981 return isContentMatchNodeTempFileHandle;
1982 }
1983
1984 /**
1985 * Sets temp file handle for is content match.
1986 *
1987 * @param isContentMatchNodeTempFileHandle temp file handle for is content match
1988 */
1989 private void setIsContentMatchNodeTempFileHandle(File isContentMatchNodeTempFileHandle) {
1990 this.isContentMatchNodeTempFileHandle = isContentMatchNodeTempFileHandle;
1991 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301992}