blob: 0e1c1328f154958c11b4cffc493e573e6659cf86 [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
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053018import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswale50edca2016-08-05 01:58:25 +053019import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswalaf413b82016-07-14 15:18:20 +053020import org.onosproject.yangutils.datamodel.YangAugmentableNode;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053021import org.onosproject.yangutils.datamodel.YangCase;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053022import org.onosproject.yangutils.datamodel.YangChoice;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053023import org.onosproject.yangutils.datamodel.YangLeaf;
24import org.onosproject.yangutils.datamodel.YangLeafList;
25import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Vidyashree Ramab3670472016-08-06 15:49:56 +053026import org.onosproject.yangutils.datamodel.YangList;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053027import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053028import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal94844d62016-10-13 13:28:03 +053029import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053030import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfo;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053031import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
33import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Shankara-Huaweib7564772016-08-02 18:13:13 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGroupingTranslator;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053035import org.onosproject.yangutils.translator.tojava.utils.BitsJavaInfoHandler;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053036import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053037import org.onosproject.yangutils.utils.io.YangPluginConfig;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053038
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053039import java.io.File;
40import java.io.IOException;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053041import java.util.ArrayList;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053042import java.util.List;
43
Bharat saraswalc2d3be12016-06-16 00:29:12 +053044import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053045import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
46import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053047import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.DEFAULT_CLASS_MASK;
Bharat saraswalaf413b82016-07-14 15:18:20 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053050import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053051import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053052import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_IMPL_MASK;
53import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_INTERFACE_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053054import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Bharat saraswale50edca2016-08-05 01:58:25 +053055import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EDIT_CONTENT_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053056import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswale50edca2016-08-05 01:58:25 +053057import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK;
58import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_LEAF_MASK;
59import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FILTER_CONTENT_MATCH_FOR_NODES_MASK;
Bharat saraswal250a7472016-05-12 13:16:57 +053060import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053061import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
62import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
63import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053064import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053065import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
66import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
67import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
68import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweib7564772016-08-02 18:13:13 +053069import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
70import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053071import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo;
Bharat saraswal8beac342016-08-04 02:00:03 +053072import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
73import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefinition;
Bharat saraswalaf413b82016-07-14 15:18:20 +053074import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.sortImports;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053075import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
76import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053077import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateDefaultClassFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053078import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswald14cbe82016-07-14 13:26:18 +053080import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053081import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddToListMethodImpl;
82import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddToListMethodInterface;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053083import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053084import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
85import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswal250a7472016-05-12 13:16:57 +053086import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053087import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
88import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
89import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
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 saraswal94844d62016-10-13 13:28:03 +053094import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.getImportString;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053095import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.getOverRideString;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053096import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getSubtreeFilteringForLeaf;
97import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getSubtreeFilteringForLeafList;
98import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getSubtreeFilteringForNode;
99import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_LEAF_HOLDER;
100import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_NODE;
101import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_PARENT_NODE;
102import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.MISSING_PARENT_NODE;
103import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getBeanFiles;
104import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsg;
Bharat saraswal8beac342016-08-04 02:00:03 +0530105import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST_IMPORT;
Bharat saraswal94844d62016-10-13 13:28:03 +0530106import static org.onosproject.yangutils.utils.UtilConstants.AUGMENT_MAP_TYPE;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530107import static org.onosproject.yangutils.utils.UtilConstants.BIT_SET;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530108import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530109import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530110import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530111import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_CAPS;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530112import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
113import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530114import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
Bharat saraswal8beac342016-08-04 02:00:03 +0530115import static org.onosproject.yangutils.utils.UtilConstants.INVOCATION_TARGET_EXCEPTION_IMPORT;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530116import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_PKG;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530117import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530118import static org.onosproject.yangutils.utils.UtilConstants.OPERATION_TYPE_ATTRIBUTE;
119import static org.onosproject.yangutils.utils.UtilConstants.OPERATION_TYPE_CLASS;
Bharat saraswal8beac342016-08-04 02:00:03 +0530120import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530121import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530122import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
123import static org.onosproject.yangutils.utils.UtilConstants.PROTECTED;
Bharat saraswal94844d62016-10-13 13:28:03 +0530124import static org.onosproject.yangutils.utils.UtilConstants.SELECT_LEAF;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530125import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530126import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswal94844d62016-10-13 13:28:03 +0530127import static org.onosproject.yangutils.utils.UtilConstants.VALUE_LEAF;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530128import static org.onosproject.yangutils.utils.UtilConstants.YANG;
Bharat saraswal94844d62016-10-13 13:28:03 +0530129import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_MAP;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530130import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530131import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswal8beac342016-08-04 02:00:03 +0530132import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ADD_TO_LIST;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530133import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530134import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530135import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530136import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
137import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
138import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530139import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
140import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530141import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530142
143/**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530144 * Represents implementation of java code fragments temporary implementations.
145 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530146 */
147public class TempJavaFragmentFiles {
Bharat saraswal250a7472016-05-12 13:16:57 +0530148
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530149 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530150 * File type extension for java classes.
151 */
152 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswal250a7472016-05-12 13:16:57 +0530153
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530154 /**
155 * File type extension for temporary classes.
156 */
157 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswal250a7472016-05-12 13:16:57 +0530158
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530159 /**
160 * Folder suffix for temporary files folder.
161 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530162 private static final String TEMP_FOLDER_NAME_SUFFIX = "-Temp";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530163
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530164 /**
165 * File name for getter method.
166 */
167 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530168
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530169 /**
170 * File name for setter method.
171 */
172 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530173
174 /**
175 * File name for getter method implementation.
176 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530177 private static final String GETTER_METHOD_IMPL_FILE_NAME =
178 "GetterMethodImpl";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530179
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530180 /**
181 * File name for setter method implementation.
182 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530183 private static final String SETTER_METHOD_IMPL_FILE_NAME =
184 "SetterMethodImpl";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530185
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530186 /**
187 * File name for attributes.
188 */
189 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswal250a7472016-05-12 13:16:57 +0530190
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530191 /**
192 * File name for to string method.
193 */
194 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswal250a7472016-05-12 13:16:57 +0530195
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530196 /**
197 * File name for hash code method.
198 */
199 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswal250a7472016-05-12 13:16:57 +0530200
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530201 /**
202 * File name for equals method.
203 */
204 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530205
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530206 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530207 * File name for from string method.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530208 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530209 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530210
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530211 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530212 * File name for from add to list interface method.
213 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530214 private static final String ADD_TO_LIST_INTERFACE_METHOD_FILE_NAME =
215 "addToList";
Bharat saraswal8beac342016-08-04 02:00:03 +0530216
217 /**
218 * File name for from add to list impl method.
219 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530220 private static final String ADD_TO_LIST_IMPL_METHOD_FILE_NAME =
221 "addToListImpl";
Bharat saraswal8beac342016-08-04 02:00:03 +0530222
223 /**
224 * File name for from leaf identifier attributes.
225 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530226 private static final String LEAF_IDENTIFIER_ATTRIBUTES_FILE_NAME =
227 "leafIdentifierAtr";
Bharat saraswal8beac342016-08-04 02:00:03 +0530228
229 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530230 * File name for is filter content leaf match.
231 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530232 private static final String FILTER_CONTENT_MATCH_LEAF_FILE_NAME =
233 "isFilterContentMatchLeafMask";
Bharat saraswale50edca2016-08-05 01:58:25 +0530234
235 /**
236 * File name for is filter content leaf-list match.
237 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530238 private static final String FILTER_CONTENT_MATCH_LEAF_LIST_FILE_NAME =
239 "isFilterContentMatchLeafListMask";
Bharat saraswale50edca2016-08-05 01:58:25 +0530240
241 /**
242 * File name for is filter content node match.
243 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530244 private static final String FILTER_CONTENT_MATCH_NODE_FILE_NAME =
245 "isFilterContentMatchNodeMask";
Bharat saraswale50edca2016-08-05 01:58:25 +0530246
247 /**
248 * File name for edit content file.
249 */
250 private static final String EDIT_CONTENT_FILE_NAME = "editContentFile";
251
252 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530253 * File name for interface java file name suffix.
254 */
255 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswal250a7472016-05-12 13:16:57 +0530256
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530257 /**
258 * File name for builder interface file name suffix.
259 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530260 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX =
261 BUILDER + INTERFACE;
Bharat saraswal250a7472016-05-12 13:16:57 +0530262
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530263 /**
264 * File name for builder class file name suffix.
265 */
266 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswal250a7472016-05-12 13:16:57 +0530267
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530268 /**
Bharat saraswal94844d62016-10-13 13:28:03 +0530269 *
270 */
271 private boolean isBinary;
272
273 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530274 * Information about the java files being generated.
275 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530276 private JavaFileInfoTranslator javaFileInfo;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530277
278 /**
279 * Imported class info.
280 */
281 private JavaImportData javaImportData;
282
283 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530284 * The variable which guides the types of temporary files generated using
285 * the temporary generated file types mask.
Bharat saraswalaf413b82016-07-14 15:18:20 +0530286 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530287 private int tempFilesFlagSet;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530288
289 /**
290 * Absolute path where the target java file needs to be generated.
291 */
292 private String absoluteDirPath;
293
294 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530295 * Contains all the interface(s)/class name which will be extended by
296 * generated files.
Bharat saraswalaf413b82016-07-14 15:18:20 +0530297 */
298 private JavaExtendsListHolder javaExtendsListHolder;
299
Bharat saraswald14cbe82016-07-14 13:26:18 +0530300 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530301 * Java file handle for interface file.
302 */
303 private File interfaceJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530304
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530305 /**
306 * Java file handle for builder interface file.
307 */
308 private File builderInterfaceJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530309
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530310 /**
311 * Java file handle for builder class file.
312 */
313 private File builderClassJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530314
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530315 /**
316 * Java file handle for impl class file.
317 */
318 private File implClassJavaFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530319
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530320 /**
321 * Temporary file handle for attribute.
322 */
323 private File attributesTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530324
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530325 /**
326 * Temporary file handle for getter of interface.
327 */
328 private File getterInterfaceTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530329
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530330 /**
331 * Temporary file handle for setter of interface.
332 */
333 private File setterInterfaceTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530334
335 /**
336 * Temporary file handle for getter of class.
337 */
338 private File getterImplTempFileHandle;
339
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530340 /**
341 * Temporary file handle for setter of class.
342 */
343 private File setterImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530344
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530345 /**
346 * Temporary file handle for hash code method of class.
347 */
348 private File hashCodeImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530349
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530350 /**
351 * Temporary file handle for equals method of class.
352 */
353 private File equalsImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530354
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530355 /**
356 * Temporary file handle for to string method of class.
357 */
358 private File toStringImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530359
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530360 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530361 * Temporary file handle for from string method of class.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530362 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530363 private File fromStringImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530364
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530365 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530366 * Temporary file handle for add to list interface method of class.
367 */
368 private File addToListInterfaceTempFileHandle;
369
370 /**
371 * Temporary file handle for add to list impl method of class.
372 */
373 private File addToListImplTempFileHandle;
374
375 /**
376 * Temporary file handle for leaf id attributes of enum.
377 */
378 private File leafIdAttributeTempFileHandle;
379
380 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530381 * Temporary file handle for is content match method for leaf-list.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530382 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530383 private File getSubtreeFilteringForListTempFileHandle;
Bharat saraswale50edca2016-08-05 01:58:25 +0530384
385 /**
386 * Temporary file handle for is content match method for node.
387 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530388 private File getSubtreeFilteringForChildNodeTempFileHandle;
Bharat saraswale50edca2016-08-05 01:58:25 +0530389
390 /**
391 * Temporary file handle for is content match method for leaf.
392 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530393 private File subtreeFilteringForLeafTempFileHandle;
Bharat saraswale50edca2016-08-05 01:58:25 +0530394
395 /**
396 * Temporary file handle for edit content file.
397 */
398 private File editContentTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530399
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530400 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530401 * Leaf count.
402 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530403 private int leafCount;
Bharat saraswal8beac342016-08-04 02:00:03 +0530404
405 /**
406 * If current node is root node.
407 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530408 private boolean rootNode;
Bharat saraswal8beac342016-08-04 02:00:03 +0530409
410 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530411 * Is attribute added.
412 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530413 private boolean isAttributePresent;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530414
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530415 /**
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530416 * List of bits attributes.
417 */
418 private List<BitsJavaInfoHandler> bitsHandler = new ArrayList<>();
419
420 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530421 * Creates an instance of temp JAVA fragment files.
422 */
Bharat saraswalaf413b82016-07-14 15:18:20 +0530423 TempJavaFragmentFiles() {
424 }
425
426 /**
427 * Creates an instance of temporary java code fragment.
428 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530429 * @param fileInfo generated java file information
Bharat saraswalaf413b82016-07-14 15:18:20 +0530430 * @throws IOException when fails to create new file handle
431 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530432 protected TempJavaFragmentFiles(JavaFileInfoTranslator fileInfo)
Bharat saraswalaf413b82016-07-14 15:18:20 +0530433 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530434 javaExtendsListHolder = new JavaExtendsListHolder();
435 javaImportData = new JavaImportData();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530436 javaFileInfo = fileInfo;
437 absoluteDirPath = getAbsolutePackagePath(fileInfo.getBaseCodeGenPath(),
438 fileInfo.getPackageFilePath());
Bharat saraswalaf413b82016-07-14 15:18:20 +0530439 /*
440 * Initialize getter when generation file type matches to interface
441 * mask.
442 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530443 if (javaFlagSet(INTERFACE_MASK)) {
444 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK |
445 ADD_TO_LIST_INTERFACE_MASK |
446 LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530447 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530448 /*
449 * Initialize getter and setter when generation file type matches to
450 * builder interface mask.
451 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530452 if (javaFlagSet(BUILDER_INTERFACE_MASK)) {
453 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK |
454 SETTER_FOR_INTERFACE_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530455 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530456 /*
457 * Initialize getterImpl, setterImpl and attributes when generation file
458 * type matches to builder class mask.
459 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530460 if (javaFlagSet(BUILDER_CLASS_MASK)) {
461 addGeneratedTempFile(ATTRIBUTES_MASK | GETTER_FOR_CLASS_MASK |
462 SETTER_FOR_CLASS_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530463 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530464 /*
465 * Initialize getterImpl, attributes, constructor, hash code, equals and
466 * to strings when generation file type matches to impl class mask.
467 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530468 if (javaFlagSet(DEFAULT_CLASS_MASK)) {
469 addGeneratedTempFile(
470 ATTRIBUTES_MASK | GETTER_FOR_CLASS_MASK |
471 HASH_CODE_IMPL_MASK | EQUALS_IMPL_MASK |
472 TO_STRING_IMPL_MASK | ADD_TO_LIST_IMPL_MASK |
473 FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK |
474 FILTER_CONTENT_MATCH_FOR_LEAF_MASK |
475 FILTER_CONTENT_MATCH_FOR_NODES_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530476 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530477 /*
478 * Initialize temp files to generate type class.
479 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530480 if (javaFlagSet(GENERATE_TYPE_CLASS)) {
481 addGeneratedTempFile(ATTRIBUTES_MASK | GETTER_FOR_CLASS_MASK |
482 HASH_CODE_IMPL_MASK | EQUALS_IMPL_MASK |
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530483 FROM_STRING_IMPL_MASK);
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530484
485 if (getGeneratedJavaFiles() != GENERATE_UNION_CLASS) {
486 addGeneratedTempFile(TO_STRING_IMPL_MASK);
487 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530488 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530489
Vidyashree Ramab3670472016-08-06 15:49:56 +0530490 //Set temporary file handles
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530491 if (tempFlagSet(ATTRIBUTES_MASK)) {
492 attributesTempFileHandle =
493 getTemporaryFileHandle(ATTRIBUTE_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530494 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530495 if (tempFlagSet(GETTER_FOR_INTERFACE_MASK)) {
496 getterInterfaceTempFileHandle =
497 getTemporaryFileHandle(GETTER_METHOD_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530498 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530499 if (tempFlagSet(SETTER_FOR_INTERFACE_MASK)) {
500 setterInterfaceTempFileHandle =
501 getTemporaryFileHandle(SETTER_METHOD_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530502 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530503 if (tempFlagSet(GETTER_FOR_CLASS_MASK)) {
504 getterImplTempFileHandle =
505 getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530506 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530507 if (tempFlagSet(SETTER_FOR_CLASS_MASK)) {
508 setterImplTempFileHandle =
509 getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530510 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530511 if (tempFlagSet(HASH_CODE_IMPL_MASK)) {
512 hashCodeImplTempFileHandle =
513 getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530514 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530515 if (tempFlagSet(EQUALS_IMPL_MASK)) {
516 equalsImplTempFileHandle =
517 getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530518 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530519 if (tempFlagSet(TO_STRING_IMPL_MASK)) {
520 toStringImplTempFileHandle =
521 getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530522 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530523 if (tempFlagSet(FROM_STRING_IMPL_MASK)) {
524 fromStringImplTempFileHandle =
525 getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530526 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530527 if (tempFlagSet(ADD_TO_LIST_INTERFACE_MASK)) {
528 addToListInterfaceTempFileHandle =
529 getTemporaryFileHandle(ADD_TO_LIST_INTERFACE_METHOD_FILE_NAME);
Bharat saraswal8beac342016-08-04 02:00:03 +0530530 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530531 if (tempFlagSet(ADD_TO_LIST_IMPL_MASK)) {
532 addToListImplTempFileHandle =
533 getTemporaryFileHandle(ADD_TO_LIST_IMPL_METHOD_FILE_NAME);
Bharat saraswal8beac342016-08-04 02:00:03 +0530534 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530535 if (tempFlagSet(LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK)) {
536 leafIdAttributeTempFileHandle =
537 getTemporaryFileHandle(LEAF_IDENTIFIER_ATTRIBUTES_FILE_NAME);
Bharat saraswal8beac342016-08-04 02:00:03 +0530538 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530539 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_LEAF_MASK)) {
540 subtreeFilteringForLeafTempFileHandle =
541 getTemporaryFileHandle(FILTER_CONTENT_MATCH_LEAF_FILE_NAME);
Bharat saraswale50edca2016-08-05 01:58:25 +0530542 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530543 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK)) {
544 getSubtreeFilteringForListTempFileHandle =
545 getTemporaryFileHandle(FILTER_CONTENT_MATCH_LEAF_LIST_FILE_NAME);
Bharat saraswale50edca2016-08-05 01:58:25 +0530546 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530547 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_NODES_MASK)) {
548 getSubtreeFilteringForChildNodeTempFileHandle =
549 getTemporaryFileHandle(FILTER_CONTENT_MATCH_NODE_FILE_NAME);
Bharat saraswale50edca2016-08-05 01:58:25 +0530550 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530551 if (tempFlagSet(EDIT_CONTENT_MASK)) {
552 editContentTempFileHandle =
553 getTemporaryFileHandle(EDIT_CONTENT_FILE_NAME);
Bharat saraswale50edca2016-08-05 01:58:25 +0530554 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530555 }
556
557 /**
558 * Adds current node info as and attribute to the parent generated file.
559 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530560 * @param curNode current node
561 * @param isList is list construct
562 * @param config plugin configurations
Bharat saraswalaf413b82016-07-14 15:18:20 +0530563 * @throws IOException IO operation exception
564 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530565 protected static void addCurNodeInfoInParentTempFile(YangNode curNode,
566 boolean isList,
567 YangPluginConfig config)
Bharat saraswalaf413b82016-07-14 15:18:20 +0530568 throws IOException {
569 YangNode parent = getParentNodeInGenCode(curNode);
570 if (!(parent instanceof JavaCodeGenerator)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530571 throw new TranslatorException(getErrorMsg(MISSING_PARENT_NODE,
572 curNode));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530573 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530574 if (parent instanceof YangJavaGroupingTranslator) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530575 /*
576 * In case of grouping, there is no need to add the information, it
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530577 * will be taken care in uses.
Bharat saraswalaf413b82016-07-14 15:18:20 +0530578 */
579 return;
580 }
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530581 addCurNodeInfoInParentTempFile(curNode, isList, config, parent);
582 }
583
584 /**
585 * Adds current node info as and attribute to a specified parent generated
586 * file. In case of grouping parent will be referred grouping node or
587 * referred node in grouping.
588 *
589 * @param curNode current node
590 * @param isList is list construct
591 * @param config plugin configurations
592 * @param parent parent node
593 * @throws IOException IO operation exception
594 */
595 protected static void addCurNodeInfoInParentTempFile(
596 YangNode curNode, boolean isList, YangPluginConfig config,
597 YangNode parent)
598 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530599 TempJavaBeanFragmentFiles tempFiles =
600 getBeanFiles((JavaCodeGeneratorInfo) parent);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530601 JavaAttributeInfo attr =
602 getCurNodeAsAttributeInTarget(curNode, parent, isList,
603 tempFiles);
604 tempFiles.addJavaSnippetInfoToApplicableTempFiles(attr, config);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530605 }
606
607 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530608 * Creates an attribute info object corresponding to a data model node
609 * and return it.
Bharat saraswalaf413b82016-07-14 15:18:20 +0530610 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530611 * @param curNode current node
612 * @param targetNode target node
613 * @param listNode flag indicating if a node is a list node
614 * @param tempFiles temp java fragment files
615 * @return java attribute info
Bharat saraswalaf413b82016-07-14 15:18:20 +0530616 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530617 public static JavaAttributeInfo
618 getCurNodeAsAttributeInTarget(YangNode curNode, YangNode targetNode,
619 boolean listNode,
620 TempJavaFragmentFiles tempFiles) {
621 JavaFileInfoTranslator translator =
622 ((JavaFileInfoContainer) curNode).getJavaFileInfo();
623 String curNodeName = translator.getJavaName();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530624 if (curNodeName == null) {
625 updateJavaFileInfo(curNode, null);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530626 curNodeName = translator.getJavaName();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530627 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530628 /*
629 * Get the import info corresponding to the attribute for import in
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530630 * generated java files or qualified access.
Bharat saraswalaf413b82016-07-14 15:18:20 +0530631 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530632 JavaQualifiedTypeInfoTranslator typeInfo =
633 getQualifiedTypeInfoOfCurNode(curNode,
634 getCapitalCase(curNodeName));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530635 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530636 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE,
637 curNode));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530638 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530639 JavaImportData parentImportData = tempFiles.getJavaImportData();
640 JavaFileInfoTranslator fileInfo =
641 ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530642
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530643 boolean qualified;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530644 if (tempFiles instanceof TempJavaServiceFragmentFiles &&
645 typeInfo.getClassInfo().contentEquals(SERVICE) ||
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530646 typeInfo.getClassInfo()
647 .contentEquals(getCapitalCase(fileInfo.getJavaName() +
648 SERVICE))) {
649 qualified = true;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530650 } else {
651 String className;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530652 if (tempFiles instanceof TempJavaServiceFragmentFiles) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530653 className = getCapitalCase(fileInfo.getJavaName()) + SERVICE;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530654 } else {
655 className = getCapitalCase(fileInfo.getJavaName());
656 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530657 qualified = parentImportData.addImportInfo(typeInfo, className,
658 fileInfo.getPackage());
Bharat saraswalaf413b82016-07-14 15:18:20 +0530659 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530660 boolean collectionSet = false;
Vidyashree Ramab3670472016-08-06 15:49:56 +0530661 if (curNode instanceof YangList) {
662 YangList yangList = (YangList) curNode;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530663 if (yangList.getCompilerAnnotation() != null &&
664 yangList.getCompilerAnnotation()
665 .getYangAppDataStructure() != null) {
666 switch (yangList.getCompilerAnnotation()
667 .getYangAppDataStructure().getDataStructure()) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530668 case QUEUE: {
669 parentImportData.setQueueToImport(true);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530670 collectionSet = true;
Vidyashree Ramab3670472016-08-06 15:49:56 +0530671 break;
672 }
673 case SET: {
674 parentImportData.setSetToImport(true);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530675 collectionSet = true;
Vidyashree Ramab3670472016-08-06 15:49:56 +0530676 break;
677 }
678 default: {
679 // TODO : to be implemented
680 }
681 }
682 }
683 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530684 if (listNode && !collectionSet) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530685 parentImportData.setIfListImported(true);
686 }
Vidyashree Ramab3670472016-08-06 15:49:56 +0530687 if (curNode instanceof YangList) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530688 return getAttributeInfoForTheData(typeInfo, curNodeName,
689 null, qualified, listNode,
690 ((YangList) curNode)
691 .getCompilerAnnotation());
Vidyashree Ramab3670472016-08-06 15:49:56 +0530692 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530693 return getAttributeInfoForTheData(typeInfo, curNodeName, null,
694 qualified, listNode);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530695 }
696
697 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530698 * Returns java attribute for leaf.
699 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530700 * @param tempFiles temporary generated file
701 * @param container JAVA leaf info container
702 * @param config plugin configurations
703 * @param leafList flag indicating if it's leaf list
704 * @return java attribute info
Bharat saraswal64e7e232016-07-14 23:33:55 +0530705 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530706 private static JavaAttributeInfo
707 getJavaAttributeOfLeaf(TempJavaFragmentFiles tempFiles,
708 JavaLeafInfoContainer container,
709 YangPluginConfig config, boolean leafList) {
710 if (leafList) {
711 tempFiles.getJavaImportData().setIfListImported(true);
712 return getAttributeOfLeafInfoContainer(tempFiles, container, config,
713 true);
714 }
715 return getAttributeOfLeafInfoContainer(tempFiles, container, config,
716 false);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530717 }
718
719 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530720 * Returns java attribute for leaf container.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530721 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530722 * @param tempFiles temporary generated file
723 * @param container JAVA leaf info container
724 * @param config plugin configurations
725 * @param listAttribute flag indicating if list attribute
726 * @return JAVA attribute information
Bharat saraswal64e7e232016-07-14 23:33:55 +0530727 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530728 private static JavaAttributeInfo
729 getAttributeOfLeafInfoContainer(TempJavaFragmentFiles tempFiles,
730 JavaLeafInfoContainer container,
731 YangPluginConfig config,
732 boolean listAttribute) {
733 container.setConflictResolveConfig(config.getConflictResolver());
734 container.updateJavaQualifiedInfo();
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530735 addImportForLeafInfo(tempFiles, container);
736 JavaAttributeInfo attr = getAttributeInfoForTheData(
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530737 container.getJavaQualifiedInfo(),
738 container.getJavaName(config.getConflictResolver()),
739 container.getDataType(),
740 tempFiles.getIsQualifiedAccessOrAddToImportList(
741 container.getJavaQualifiedInfo()), listAttribute);
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530742 if (container.getDataType().getDataType() == YangDataTypes.BITS) {
743 addBitsHandler(attr, container.getDataType(), tempFiles);
744 }
745 return attr;
746 }
747
748 /**
749 * Adds bits handler attribute for bits to string method.
750 *
751 * @param attr attribute
752 * @param type type
753 * @param tempFiles temp fragment file
754 */
755 static void addBitsHandler(JavaAttributeInfo attr, YangType type,
756 TempJavaFragmentFiles tempFiles) {
757 BitsJavaInfoHandler handler
758 = new BitsJavaInfoHandler(attr, type);
759 tempFiles.getBitsHandler().add(handler);
760 }
761
762 /**
763 * Adds attribute types import to leaf info container's parent.
764 *
765 * @param tempFiles temp java file
766 * @param container leaf info container
767 */
768 private static void addImportForLeafInfo(TempJavaFragmentFiles tempFiles,
769 JavaLeafInfoContainer container) {
770 String containedInCls = getCapitalCase(tempFiles.getJavaFileInfo()
771 .getJavaName());
772 String containedInPkg = tempFiles.getJavaFileInfo().getPackage();
Bharat saraswal94844d62016-10-13 13:28:03 +0530773 JavaQualifiedTypeInfoTranslator info;
774 if (container.getDataType().getDataType() == YangDataTypes.BINARY) {
775 tempFiles.setBinary(true);
776 } else if (container.getDataType().getDataType() == YangDataTypes.BITS) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530777 //Add bitset import for type and leaf value flags.
778 info = new JavaQualifiedTypeInfoTranslator();
779 info.setClassInfo(BIT_SET);
780 info.setPkgInfo(JAVA_UTIL_PKG);
781 tempFiles.getJavaImportData().addImportInfo(info, containedInCls,
782 containedInPkg);
783 }
784 tempFiles.getJavaImportData().addImportInfo(
785 (JavaQualifiedTypeInfoTranslator) container
786 .getJavaQualifiedInfo(), containedInCls, containedInPkg);
787
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530788 }
789
790 /**
791 * Sets absolute path where the file needs to be generated.
792 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530793 * @param absoluteDirPath absolute path where the file needs to be
794 * generated
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530795 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530796 protected void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530797 this.absoluteDirPath = absoluteDirPath;
798 }
799
800 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530801 * Returns the generated java file information.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530802 *
803 * @return generated java file information
804 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530805 protected JavaFileInfoTranslator getJavaFileInfo() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530806 return javaFileInfo;
807 }
808
809 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530810 * Sets the generated java file information.
811 *
812 * @param javaFileInfo generated java file information
813 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530814 protected void setJavaFileInfo(JavaFileInfoTranslator javaFileInfo) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530815 this.javaFileInfo = javaFileInfo;
816 }
817
818 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530819 * Returns the flag-set for generated temp files.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530820 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530821 * @return flag-set
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530822 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530823 protected int getGeneratedTempFiles() {
824 return tempFilesFlagSet;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530825 }
826
827 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530828 * Adds to the flag-set for generated temp files.
Bharat saraswalaf413b82016-07-14 15:18:20 +0530829 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530830 * @param flags generated temp files flag-set
Bharat saraswalaf413b82016-07-14 15:18:20 +0530831 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530832 protected void addGeneratedTempFile(int flags) {
833 tempFilesFlagSet |= flags;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530834 }
835
836 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530837 * Returns the generated Java files.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530838 *
839 * @return generated Java files
840 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530841 protected int getGeneratedJavaFiles() {
842 return javaFileInfo.getGeneratedFileTypes();
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530843 }
844
845 /**
846 * Retrieves the mapped Java class name.
847 *
848 * @return mapped Java class name
849 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530850 protected String getGeneratedJavaClassName() {
851 return getCapitalCase(javaFileInfo.getJavaName());
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530852 }
853
854 /**
855 * Retrieves the import data for the generated Java file.
856 *
857 * @return import data for the generated Java file
858 */
859 public JavaImportData getJavaImportData() {
860 return javaImportData;
861 }
862
863 /**
864 * Sets import data for the generated Java file.
865 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530866 * @param data import data for the generated Java file
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530867 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530868 protected void setJavaImportData(JavaImportData data) {
869 javaImportData = data;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530870 }
871
872 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530873 * Retrieves the status of any attributes added.
874 *
875 * @return status of any attributes added
876 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530877 protected boolean isAttributePresent() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530878 return isAttributePresent;
879 }
880
881 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530882 * Returns getter methods's temporary file handle.
883 *
884 * @return temporary file handle
885 */
886 public File getGetterInterfaceTempFileHandle() {
887 return getterInterfaceTempFileHandle;
888 }
889
890 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530891 * Returns setter method's temporary file handle.
892 *
893 * @return temporary file handle
894 */
895 public File getSetterInterfaceTempFileHandle() {
896 return setterInterfaceTempFileHandle;
897 }
898
899 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530900 * Returns setter method's impl's temporary file handle.
901 *
902 * @return temporary file handle
903 */
904 public File getSetterImplTempFileHandle() {
905 return setterImplTempFileHandle;
906 }
907
908 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530909 * Returns from string method's temporary file handle.
910 *
911 * @return from string method's temporary file handle
912 */
913 public File getFromStringImplTempFileHandle() {
914 return fromStringImplTempFileHandle;
915 }
916
917 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530918 * Returns attribute's temporary file handle.
919 *
920 * @return temporary file handle
921 */
922 public File getAttributesTempFileHandle() {
923 return attributesTempFileHandle;
924 }
925
926 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530927 * Returns getter method's impl's temporary file handle.
928 *
929 * @return temporary file handle
930 */
931 public File getGetterImplTempFileHandle() {
932 return getterImplTempFileHandle;
933 }
934
935 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530936 * Returns hash code method's temporary file handle.
937 *
938 * @return temporary file handle
939 */
940 public File getHashCodeImplTempFileHandle() {
941 return hashCodeImplTempFileHandle;
942 }
943
944 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530945 * Returns equals method's temporary file handle.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530946 *
947 * @return temporary file handle
948 */
949 public File getEqualsImplTempFileHandle() {
950 return equalsImplTempFileHandle;
951 }
952
953 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530954 * Returns to string method's temporary file handle.
955 *
956 * @return temporary file handle
957 */
958 public File getToStringImplTempFileHandle() {
959 return toStringImplTempFileHandle;
960 }
961
962 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530963 * Returns java extends list holder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530964 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530965 * @return java extends list holder
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530966 */
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530967 public JavaExtendsListHolder getJavaExtendsListHolder() {
968 return javaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530969 }
970
971 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530972 * Sets java extends list holder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530973 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530974 * @param holder java extends list holder
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530975 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530976 protected void setJavaExtendsListHolder(
977 JavaExtendsListHolder holder) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530978 javaExtendsListHolder = holder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530979 }
980
981 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530982 * Adds is filter content match for leaf.
983 *
984 * @param attr java attribute
985 * @throws IOException when fails to do IO operations
986 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530987 private void addSubTreeFilteringForLeaf(JavaAttributeInfo attr)
Bharat saraswale50edca2016-08-05 01:58:25 +0530988 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530989 appendToFile(subtreeFilteringForLeafTempFileHandle,
990 getSubtreeFilteringForLeaf(attr, attr.getAttributeType()) +
991 NEW_LINE);
Bharat saraswale50edca2016-08-05 01:58:25 +0530992 }
993
994 /**
995 * Adds is filter content match for leaf-list.
996 *
997 * @param attr java attribute
998 * @throws IOException when fails to do IO operations
999 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301000 private void addSubtreeFilteringForLeafList(JavaAttributeInfo attr)
Bharat saraswale50edca2016-08-05 01:58:25 +05301001 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301002 appendToFile(getSubtreeFilteringForListTempFileHandle,
Bharat saraswal1edde622016-09-06 10:18:04 +05301003 getSubtreeFilteringForLeafList(attr) + NEW_LINE);
Bharat saraswale50edca2016-08-05 01:58:25 +05301004 }
1005
1006 /**
1007 * Adds is filter content match for nodes.
1008 *
1009 * @param attr java attribute
1010 * @throws IOException when fails to do IO operations
1011 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301012 private void addSubtreeFilteringForChildNode(JavaAttributeInfo attr)
Bharat saraswale50edca2016-08-05 01:58:25 +05301013 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301014 appendToFile(getSubtreeFilteringForChildNodeTempFileHandle,
Bharat saraswal1edde622016-09-06 10:18:04 +05301015 getSubtreeFilteringForNode(attr) + NEW_LINE);
Bharat saraswale50edca2016-08-05 01:58:25 +05301016 }
1017
1018 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301019 * Adds attribute for class.
1020 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301021 * @param attr attribute info
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301022 * @throws IOException when fails to append to temporary file
1023 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301024 private void addAttribute(JavaAttributeInfo attr)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301025 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301026 appendToFile(attributesTempFileHandle, parseAttribute(attr));
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301027 }
1028
1029 /**
1030 * Adds getter for interface.
1031 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301032 * @param attr attribute info
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301033 * @throws IOException when fails to append to temporary file
1034 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301035 private void addGetterForInterface(JavaAttributeInfo attr)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301036 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301037 appendToFile(getterInterfaceTempFileHandle,
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301038 getGetterString(attr, getGeneratedJavaFiles()) +
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301039 NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301040 }
1041
1042 /**
1043 * Adds setter for interface.
1044 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301045 * @param attr attribute info
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301046 * @throws IOException when fails to append to temporary file
1047 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301048 private void addSetterForInterface(JavaAttributeInfo attr)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301049 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301050 appendToFile(setterInterfaceTempFileHandle,
1051 getSetterString(attr, getGeneratedJavaClassName(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301052 getGeneratedJavaFiles()) +
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301053 NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301054 }
1055
1056 /**
1057 * Adds setter's implementation for class.
1058 *
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301059 * @param attr attribute info
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301060 * @throws IOException when fails to append to temporary file
1061 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301062 private void addSetterImpl(JavaAttributeInfo attr)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301063 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301064 String setter = getSetterForClass(attr, getGeneratedJavaClassName(),
1065 getGeneratedJavaFiles());
Bharat saraswal94844d62016-10-13 13:28:03 +05301066 appendToFile(setterImplTempFileHandle, getOverRideString() + setter);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301067 }
1068
1069 /**
1070 * Adds getter method's impl for class.
1071 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301072 * @param attr attribute info
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301073 * @throws IOException when fails to append to temporary file
1074 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301075 protected void addGetterImpl(JavaAttributeInfo attr)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301076 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301077 String getter = getGetterForClass(attr, getGeneratedJavaFiles());
1078 if (javaFlagSet(BUILDER_CLASS_MASK)) {
Bharat saraswal94844d62016-10-13 13:28:03 +05301079 appendToFile(getterImplTempFileHandle, getOverRideString() +
1080 getter);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301081 } else {
Vidyashree Ramab3670472016-08-06 15:49:56 +05301082 String appDataStructure = null;
1083 if (attr.getCompilerAnnotation() != null) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301084 appDataStructure = attr.getCompilerAnnotation()
1085 .getYangAppDataStructure().getDataStructure().name();
Vidyashree Ramab3670472016-08-06 15:49:56 +05301086 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301087 appendToFile(getterImplTempFileHandle,
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301088 getJavaDoc(GETTER_METHOD, attr.getAttributeName(),
1089 false, appDataStructure) + getter);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301090 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301091 }
1092
1093 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301094 * Adds add to list interface method.
1095 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301096 * @param attr attribute
Bharat saraswal8beac342016-08-04 02:00:03 +05301097 * @throws IOException when fails to do IO operations
1098 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301099 private void addAddToListInterface(JavaAttributeInfo attr)
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301100 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301101 appendToFile(addToListInterfaceTempFileHandle,
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301102 getJavaDoc(ADD_TO_LIST, attr.getAttributeName(), false,
1103 null) + getAddToListMethodInterface(
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301104 attr, getGeneratedJavaClassName()) + NEW_LINE);
Bharat saraswal8beac342016-08-04 02:00:03 +05301105 }
1106
1107 /**
1108 * Adds add to list interface method.
1109 *
1110 * @param attr attribute
1111 * @throws IOException when fails to do IO operations
1112 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301113 private void addAddToListImpl(JavaAttributeInfo attr)
1114 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301115 appendToFile(addToListImplTempFileHandle,
Bharat saraswal94844d62016-10-13 13:28:03 +05301116 getAddToListMethodImpl(attr, getGeneratedJavaClassName()
1117 ) + NEW_LINE);
Bharat saraswal8beac342016-08-04 02:00:03 +05301118 }
1119
1120 /**
1121 * Adds leaf identifier enum attributes.
1122 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301123 * @param attr attribute
1124 * @param value value
Bharat saraswal8beac342016-08-04 02:00:03 +05301125 * @throws IOException when fails to do IO operations
1126 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301127 private void addLeafIdAttributes(JavaAttributeInfo attr, int value)
Bharat saraswal8beac342016-08-04 02:00:03 +05301128 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301129 appendToFile(leafIdAttributeTempFileHandle, FOUR_SPACE_INDENTATION +
1130 generateEnumAttributeString(attr.getAttributeName(), value));
Bharat saraswal8beac342016-08-04 02:00:03 +05301131 }
1132
1133 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301134 * Adds build method for interface.
1135 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301136 * @return build method for interface
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301137 * @throws IOException when fails to append to temporary file
1138 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301139 protected String addBuildMethodForInterface()
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301140 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301141 return parseBuilderInterfaceBuildMethodString(
1142 getGeneratedJavaClassName());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301143 }
1144
1145 /**
1146 * Adds build method's implementation for class.
1147 *
1148 * @return build method implementation for class
1149 * @throws IOException when fails to append to temporary file
1150 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301151 protected String addBuildMethodImpl()
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301152 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301153 return getBuildString(getGeneratedJavaClassName(), rootNode) + NEW_LINE;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301154 }
1155
1156 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301157 * Adds default constructor for class.
1158 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301159 * @param modifier modifier for constructor
1160 * @param toAppend string which need to be appended with the class name
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301161 * @param suffix is value need to be appended as suffix
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301162 * @return default constructor for class
1163 * @throws IOException when fails to append to file
1164 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301165 protected String addDefaultConstructor(String modifier, String toAppend,
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301166 boolean suffix)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301167 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301168 StringBuilder name = new StringBuilder();
1169 name.append(getGeneratedJavaClassName());
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301170 if (rootNode && !toAppend.equals(BUILDER)) {
1171 name.append(OP_PARAM);
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301172 return getDefaultConstructorString(name.toString(), modifier);
Bharat saraswal8beac342016-08-04 02:00:03 +05301173 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301174 if (suffix) {
1175 name.append(toAppend);
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301176 return getDefaultConstructorString(name.toString(), modifier);
Bharat saraswal8beac342016-08-04 02:00:03 +05301177 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301178 StringBuilder appended = new StringBuilder();
Bharat saraswal8beac342016-08-04 02:00:03 +05301179 if (toAppend.equals(DEFAULT)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301180 appended.append(getCapitalCase(toAppend));
Bharat saraswal8beac342016-08-04 02:00:03 +05301181 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301182 appended.append(toAppend);
Bharat saraswal8beac342016-08-04 02:00:03 +05301183 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301184 return NEW_LINE + getDefaultConstructorString(appended.append(
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301185 name).toString(), modifier);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301186 // TODO getDefaultConstructorString to handle new line.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301187 }
1188
1189 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301190 * Adds hash code method for class.
1191 *
1192 * @param attr attribute info
1193 * @throws IOException when fails to append to temporary file
1194 */
1195 private void addHashCodeMethod(JavaAttributeInfo attr)
1196 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301197 appendToFile(hashCodeImplTempFileHandle,
1198 getHashCodeMethod(attr) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301199 }
1200
1201 /**
1202 * Adds equals method for class.
1203 *
1204 * @param attr attribute info
1205 * @throws IOException when fails to append to temporary file
1206 */
1207 private void addEqualsMethod(JavaAttributeInfo attr)
1208 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301209 appendToFile(equalsImplTempFileHandle,
1210 getEqualsMethod(attr) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301211 }
1212
1213 /**
1214 * Adds ToString method for class.
1215 *
1216 * @param attr attribute info
1217 * @throws IOException when fails to append to temporary file
1218 */
1219 private void addToStringMethod(JavaAttributeInfo attr)
1220 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301221 appendToFile(toStringImplTempFileHandle,
1222 getToStringMethod(attr) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301223 }
1224
1225 /**
Bharat saraswal250a7472016-05-12 13:16:57 +05301226 * Adds from string method for union class.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301227 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301228 * @param attr type attribute info
1229 * @param fromStringAttr from string attribute info
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301230 * @param genClassName generated class name
Bharat saraswal250a7472016-05-12 13:16:57 +05301231 * @throws IOException when fails to append to temporary file
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301232 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301233 void addFromStringMethod(JavaAttributeInfo attr,
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301234 JavaAttributeInfo fromStringAttr, String genClassName)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301235 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301236 appendToFile(fromStringImplTempFileHandle,
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301237 getFromStringMethod(attr, fromStringAttr, genClassName) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301238 }
1239
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301240 /**
1241 * Returns a temporary file handle for the specific file type.
1242 *
1243 * @param fileName file name
1244 * @return temporary file handle
1245 * @throws IOException when fails to create new file handle
1246 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301247 protected File getTemporaryFileHandle(String fileName)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301248 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301249 String path = getTempDirPath(absoluteDirPath);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301250 File dir = new File(path);
Bharat saraswal8beac342016-08-04 02:00:03 +05301251 boolean isCreated;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301252 if (!dir.exists()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301253 isCreated = dir.mkdirs();
Shankara-Huaweib7564772016-08-02 18:13:13 +05301254 if (!isCreated) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301255 throw new IOException("failed to create temporary directory " +
1256 "for " + fileName);
Shankara-Huaweib7564772016-08-02 18:13:13 +05301257 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301258 }
1259 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1260 if (!file.exists()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301261 isCreated = file.createNewFile();
Shankara-Huaweib7564772016-08-02 18:13:13 +05301262 if (!isCreated) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301263 throw new IOException("failed to create temporary file for " +
1264 fileName);
Shankara-Huaweib7564772016-08-02 18:13:13 +05301265 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301266 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301267 throw new IOException(fileName + " is reused due to YANG naming. " +
1268 "probably your previous build " +
1269 "would have failed");
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301270 }
1271 return file;
1272 }
1273
1274 /**
1275 * Returns a temporary file handle for the specific file type.
1276 *
1277 * @param fileName file name
1278 * @return temporary file handle
1279 * @throws IOException when fails to create new file handle
1280 */
Bharat saraswal94844d62016-10-13 13:28:03 +05301281 public File getJavaFileHandle(String fileName)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301282 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301283 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION,
1284 javaFileInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301285 }
1286
1287 /**
1288 * Returns data from the temporary files.
1289 *
Bharat saraswalaf413b82016-07-14 15:18:20 +05301290 * @param file temporary file handle
Bharat saraswald14cbe82016-07-14 13:26:18 +05301291 * @param absolutePath absolute path
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301292 * @return stored data from temporary files
1293 * @throws IOException when failed to get data from the given file
1294 */
Bharat saraswald14cbe82016-07-14 13:26:18 +05301295 public String getTemporaryDataFromFileHandle(File file, String absolutePath)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301296 throws IOException {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301297 String path = getTempDirPath(absolutePath);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301298 if (new File(path + file.getName()).exists()) {
1299 return readAppendFile(path + file.getName(), EMPTY_STRING);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301300 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301301 throw new IOException("Unable to get data from the given " +
1302 file.getName() + " file for " +
1303 getGeneratedJavaClassName() + PERIOD);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301304 }
1305
1306 /**
1307 * Returns temporary directory path.
1308 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301309 * @param path absolute path
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301310 * @return directory path
1311 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301312 private String getTempDirPath(String path) {
1313 return getPackageDirPathFromJavaJPackage(path) + SLASH +
1314 getGeneratedJavaClassName() + TEMP_FOLDER_NAME_SUFFIX + SLASH;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301315 }
1316
1317 /**
1318 * Parses attribute to get the attribute string.
1319 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301320 * @param attr attribute info
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301321 * @return attribute string
1322 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301323 protected String parseAttribute(JavaAttributeInfo attr) {
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301324 /*
1325 * TODO: check if this utility needs to be called or move to the caller
1326 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301327 String attrName = attr.getAttributeName();
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301328 String attrAccessType = PRIVATE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301329 if ((getGeneratedJavaFiles() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301330 attrAccessType = PROTECTED;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301331 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301332 String pkg = null;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301333 if (attr.isQualifiedName()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301334 pkg = attr.getImportInfo().getPkgInfo();
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301335 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301336 return getJavaAttributeDefinition(
1337 pkg, attr.getImportInfo().getClassInfo(), attrName,
1338 attr.isListAttr(), attrAccessType, attr.getCompilerAnnotation());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301339 }
1340
1341 /**
1342 * Appends content to temporary file.
1343 *
1344 * @param file temporary file
1345 * @param data data to be appended
1346 * @throws IOException when fails to append to file
1347 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301348 protected void appendToFile(File file, String data)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301349 throws IOException {
1350 try {
1351 insertDataIntoJavaFile(file, data);
1352 } catch (IOException ex) {
1353 throw new IOException("failed to write in temp file.");
1354 }
1355 }
1356
1357 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301358 * Adds parent's info to current node import list.
1359 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301360 * @param curNode current node
1361 * @param config plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301362 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301363 protected void addParentInfoInCurNodeTempFile(YangNode curNode,
1364 YangPluginConfig config) {
1365 JavaQualifiedTypeInfoTranslator caseImportInfo =
1366 new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301367 YangNode parent = getParentNodeInGenCode(curNode);
Bharat saraswale50edca2016-08-05 01:58:25 +05301368 if (curNode instanceof YangCase && parent instanceof YangAugment) {
1369 return;
1370 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301371 if (!(parent instanceof JavaCodeGenerator)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301372 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE, curNode));
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301373 }
1374 if (!(curNode instanceof JavaFileInfoContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301375 throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode));
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301376 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301377 caseImportInfo.setClassInfo(
1378 getCapitalCase(getCamelCase(parent.getName(),
1379 config.getConflictResolver())));
1380 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo()
1381 .getPackage());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301382
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301383 JavaFileInfoTranslator fileInfo =
1384 ((JavaFileInfoContainer) curNode).getJavaFileInfo();
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301385
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301386 getBeanFiles(curNode).getJavaImportData().addImportInfo(
1387 caseImportInfo, getCapitalCase(fileInfo.getJavaName()),
1388 fileInfo.getPackage());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301389 }
1390
1391 /**
1392 * Adds leaf attributes in generated files.
1393 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301394 * @param entry JAVA leaf info container
1395 * @param config plugin config
1396 * @param curNode current node
1397 * @param leafList flag indicating whether leaf container is leafList
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301398 * @throws IOException IO operation fail
1399 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301400 private void addLeafInfoToTempFiles(JavaLeafInfoContainer entry,
1401 YangPluginConfig config,
1402 YangNode curNode,
1403 boolean leafList)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301404 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301405 if (entry != null) {
1406 if (curNode instanceof RpcNotificationContainer) {
1407 TempJavaBeanFragmentFiles tempFiles = getBeanFiles(curNode);
1408 addJavaSnippetInfoToApplicableTempFiles(
1409 getJavaAttributeOfLeaf(tempFiles, entry,
1410 config, leafList), config);
1411 } else {
1412 addJavaSnippetInfoToApplicableTempFiles(
1413 getJavaAttributeOfLeaf(this, entry,
1414 config, leafList), config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301415 }
1416 }
1417 }
1418
1419 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301420 * Adds all the leaves in the current data model node as part of the
1421 * generated temporary file.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301422 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301423 * @param curNode current node
1424 * @param config plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301425 * @throws IOException IO operation fail
1426 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301427 protected void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
1428 YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301429 throws IOException {
1430 if (!(curNode instanceof YangLeavesHolder)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301431 throw new TranslatorException(getErrorMsg(INVALID_LEAF_HOLDER,
1432 curNode));
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301433 }
1434 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301435
1436 for (YangLeaf leaf : leavesHolder.getListOfLeaf()) {
1437 addLeafInfoToTempFiles((JavaLeafInfoContainer) leaf, config,
1438 curNode, false);
1439 }
1440
1441 for (YangLeafList leafList : leavesHolder.getListOfLeafList()) {
1442 addLeafInfoToTempFiles((JavaLeafInfoContainer) leafList, config,
1443 curNode, true);
1444 }
1445 }
1446
1447 /**
1448 * Adds operation type to temp files.
1449 *
1450 * @param curNode current YANG node
1451 * @param config YANG plugin config
1452 * @throws IOException IO exception
1453 */
1454 protected void addOperationTypeToTempFiles(YangNode curNode,
1455 YangPluginConfig config)
1456 throws IOException {
1457 JavaQualifiedTypeInfoTranslator typeInfo =
1458 new JavaQualifiedTypeInfoTranslator();
1459 typeInfo.setClassInfo(OPERATION_TYPE_CLASS);
Bharat saraswal94844d62016-10-13 13:28:03 +05301460 JavaFileInfo curInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1461 JavaFileInfo info;
1462 if (curNode instanceof RpcNotificationContainer) {
1463 info = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1464 } else {
1465 info = ((JavaFileInfoContainer) getModuleNode(curNode))
1466 .getJavaFileInfo();
1467 }
1468
1469 typeInfo.setPkgInfo(info.getPackage().toLowerCase() + PERIOD +
1470 getCapitalCase(info.getJavaName()));
1471 String curNodeName = getCapitalCase(curInfo.getJavaName());
1472 if (!(curNode instanceof RpcNotificationContainer)) {
1473 javaImportData.addImportInfo(typeInfo, curNodeName, curInfo
1474 .getPackage());
1475 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301476 JavaAttributeInfo attributeInfo =
Bharat saraswal94844d62016-10-13 13:28:03 +05301477 getAttributeInfoForTheData(typeInfo, YANG + curNodeName +
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301478 OPERATION_TYPE_ATTRIBUTE, null, false, false);
1479 addJavaSnippetInfoToApplicableTempFiles(attributeInfo, config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301480 }
1481
Bharat saraswal94844d62016-10-13 13:28:03 +05301482 private YangNode getModuleNode(YangNode curNode) {
1483 YangNode tempNode = curNode.getParent();
1484 while (!(tempNode instanceof RpcNotificationContainer)) {
1485 tempNode = tempNode.getParent();
1486 }
1487 return tempNode;
1488 }
1489
1490 /**
1491 * Adds value leaf flag to temp files.
1492 *
1493 * @param config YANG plugin config
1494 * @param node YANG node
1495 * @throws IOException IO exception
1496 */
1497 protected void addValueLeafFlag(YangPluginConfig config, YangNode node)
1498 throws IOException {
1499 JavaFileInfo info = ((JavaFileInfoContainer) node).getJavaFileInfo();
1500 JavaQualifiedTypeInfoTranslator typeInfo =
1501 new JavaQualifiedTypeInfoTranslator();
1502 typeInfo.setClassInfo(BIT_SET);
1503 typeInfo.setPkgInfo(JAVA_UTIL_PKG);
1504 this.getJavaImportData().addImportInfo(typeInfo, info.getJavaName(),
1505 info.getPackage());
1506 JavaAttributeInfo attributeInfo =
1507 getAttributeInfoForTheData(typeInfo, VALUE_LEAF, null, false, false);
1508 addJavaSnippetInfoToApplicableTempFiles(attributeInfo, config);
1509 }
1510
1511 /**
1512 * Adds value leaf flag to temp files.
1513 *
1514 * @param config YANG plugin config
1515 * @throws IOException IO exception
1516 */
1517 protected void addSelectLeafFlag(YangPluginConfig config)
1518 throws IOException {
1519 JavaQualifiedTypeInfoTranslator typeInfo =
1520 new JavaQualifiedTypeInfoTranslator();
1521 typeInfo.setClassInfo(BIT_SET);
1522 typeInfo.setPkgInfo(JAVA_UTIL_PKG);
1523 JavaAttributeInfo attributeInfo =
1524 getAttributeInfoForTheData(typeInfo, SELECT_LEAF, null, false, false);
1525 addJavaSnippetInfoToApplicableTempFiles(attributeInfo, config);
1526 }
1527
1528 /**
1529 * Adds value leaf flag to temp files.
1530 *
1531 * @param config YANG plugin config
1532 * @throws IOException IO exception
1533 */
1534 protected void addYangAugmentedMap(YangPluginConfig config)
1535 throws IOException {
1536 JavaQualifiedTypeInfoTranslator typeInfo =
1537 new JavaQualifiedTypeInfoTranslator();
1538 typeInfo.setClassInfo(AUGMENT_MAP_TYPE);
1539 //Fix for add yangAugmentedInfo in equals/hashcode/and to string method.
1540 typeInfo.setPkgInfo(null);
1541 JavaAttributeInfo attributeInfo =
1542 getAttributeInfoForTheData(typeInfo, YANG_AUGMENTED_INFO_MAP,
1543 null, false, false);
1544 addJavaSnippetInfoToApplicableTempFiles(attributeInfo, config);
1545 }
1546
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301547 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301548 * Adds the new attribute info to the target generated temporary files.
1549 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301550 * @param newAttrInfo new attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301551 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301552 * @throws IOException IO operation fail
1553 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301554 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo,
1555 YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301556 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301557 isAttributePresent = true;
Bharat saraswal94844d62016-10-13 13:28:03 +05301558 String attrName = newAttrInfo.getAttributeName();
1559 boolean required = !attrName.equals(VALUE_LEAF) &&
1560 !attrName.equals(SELECT_LEAF) &&
1561 !attrName.equals(YANG_AUGMENTED_INFO_MAP);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301562 if (tempFlagSet(ATTRIBUTES_MASK)) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301563 addAttribute(newAttrInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301564 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301565 if (tempFlagSet(GETTER_FOR_INTERFACE_MASK)) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301566 addGetterForInterface(newAttrInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301567 }
Bharat saraswal94844d62016-10-13 13:28:03 +05301568 if (tempFlagSet(SETTER_FOR_INTERFACE_MASK) && required) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301569 addSetterForInterface(newAttrInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301570 }
Bharat saraswal94844d62016-10-13 13:28:03 +05301571 if (tempFlagSet(SETTER_FOR_CLASS_MASK) && required) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301572 addSetterImpl(newAttrInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301573 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301574 if (tempFlagSet(HASH_CODE_IMPL_MASK)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301575 addHashCodeMethod(newAttrInfo);
1576 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301577 if (tempFlagSet(EQUALS_IMPL_MASK)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301578 addEqualsMethod(newAttrInfo);
1579 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301580 if (tempFlagSet(TO_STRING_IMPL_MASK)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301581 addToStringMethod(newAttrInfo);
1582 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301583 if (tempFlagSet(EDIT_CONTENT_MASK)) {
Bharat saraswale50edca2016-08-05 01:58:25 +05301584 //TODO: add implementation for edit content match.
1585 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301586 boolean listAttr = newAttrInfo.isListAttr();
1587 if (tempFlagSet(ADD_TO_LIST_IMPL_MASK) && listAttr) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301588 addAddToListImpl(newAttrInfo);
1589 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301590 if (tempFlagSet(ADD_TO_LIST_INTERFACE_MASK) && listAttr) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301591 addAddToListInterface(newAttrInfo);
Bharat saraswal8beac342016-08-04 02:00:03 +05301592 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301593 YangType attrType = newAttrInfo.getAttributeType();
1594 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_NODES_MASK) &&
Bharat saraswal94844d62016-10-13 13:28:03 +05301595 attrType == null && !attrName
1596 .contains(OPERATION_TYPE_ATTRIBUTE) && required) {
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301597 addSubtreeFilteringForChildNode(newAttrInfo);
Bharat saraswale50edca2016-08-05 01:58:25 +05301598 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301599 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_LEAF_MASK) &&
1600 !listAttr && attrType != null) {
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301601 addSubTreeFilteringForLeaf(newAttrInfo);
Bharat saraswale50edca2016-08-05 01:58:25 +05301602 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301603 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK) &&
1604 listAttr && attrType != null) {
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301605 addSubtreeFilteringForLeafList(newAttrInfo);
Bharat saraswale50edca2016-08-05 01:58:25 +05301606 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301607 if (tempFlagSet(LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) &&
1608 !listAttr && attrType != null) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301609 leafCount++;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301610 addLeafIdAttributes(newAttrInfo, leafCount);
Bharat saraswal8beac342016-08-04 02:00:03 +05301611 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301612 if (!newAttrInfo.isIntConflict() && !newAttrInfo.isLongConflict() &&
1613 !newAttrInfo.isShortConflict()) {
1614 if (tempFlagSet(GETTER_FOR_CLASS_MASK)) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301615 addGetterImpl(newAttrInfo);
Bharat saraswal64e7e232016-07-14 23:33:55 +05301616 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301617 if (tempFlagSet(FROM_STRING_IMPL_MASK)) {
1618 JavaQualifiedTypeInfoTranslator typeInfo =
1619 getQualifiedInfoOfFromString(newAttrInfo,
1620 pluginConfig.getConflictResolver());
Bharat saraswal250a7472016-05-12 13:16:57 +05301621 /*
1622 * Create a new java attribute info with qualified information of
1623 * wrapper classes.
1624 */
Bharat saraswal748fc3c2016-09-06 16:38:20 +05301625 JavaAttributeInfo fromStringAttributeInfo =
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301626 getAttributeInfoForTheData(
1627 typeInfo, newAttrInfo.getAttributeName(),
1628 attrType, getIsQualifiedAccessOrAddToImportList(
1629 typeInfo), false);
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301630 addFromStringMethod(newAttrInfo, fromStringAttributeInfo,
1631 getGeneratedJavaClassName());
Bharat saraswal64e7e232016-07-14 23:33:55 +05301632 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301633 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301634 }
1635
1636 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301637 * Returns JAVA class name for non implementation classes.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301638 *
1639 * @param suffix for the class name based on the file type
1640 * @return java class name
1641 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301642 protected String getJavaClassName(String suffix) {
1643 return getCapitalCase(javaFileInfo.getJavaName()) + suffix;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301644 }
1645
1646 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301647 * Returns class name for implementation classes.
Bharat saraswald14cbe82016-07-14 13:26:18 +05301648 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301649 * @param node YANG node
Bharat saraswald14cbe82016-07-14 13:26:18 +05301650 * @return java class name
1651 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301652 private String getImplClassName(YangNode node) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301653 if (node instanceof RpcNotificationContainer) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301654 return getGeneratedJavaClassName() + OP_PARAM;
Bharat saraswal8beac342016-08-04 02:00:03 +05301655 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301656 return DEFAULT_CAPS + getGeneratedJavaClassName();
Bharat saraswald14cbe82016-07-14 13:26:18 +05301657 }
1658
1659 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301660 * Returns the directory path.
1661 *
1662 * @return directory path
1663 */
1664 private String getDirPath() {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301665 return javaFileInfo.getPackageFilePath();
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301666 }
1667
1668 /**
1669 * Constructs java code exit.
1670 *
1671 * @param fileType generated file type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301672 * @param curNode current YANG node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301673 * @throws IOException when fails to generate java files
1674 */
1675 public void generateJavaFile(int fileType, YangNode curNode)
1676 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301677 List<String> imports =
1678 getBeanFiles(curNode).getJavaImportData().getImports();
Bharat saraswal8beac342016-08-04 02:00:03 +05301679 if (curNode instanceof YangAugmentableNode) {
Gaurav Agrawal8a147522016-08-10 13:43:01 +05301680 addImportsForAugmentableClass(imports, true, true, curNode);
Bharat saraswal8beac342016-08-04 02:00:03 +05301681 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301682 createPackage(curNode);
Vidyashree Ramab3670472016-08-06 15:49:56 +05301683
1684 //Generate java code.
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301685 if ((fileType & INTERFACE_MASK) != 0 ||
1686 (fileType & BUILDER_INTERFACE_MASK) != 0) {
Vidyashree Ramab3670472016-08-06 15:49:56 +05301687
1688 //Create interface file.
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301689 interfaceJavaFileHandle =
1690 getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX));
1691 interfaceJavaFileHandle =
1692 generateInterfaceFile(interfaceJavaFileHandle, imports,
1693 curNode, isAttributePresent);
Vidyashree Ramab3670472016-08-06 15:49:56 +05301694
Bharat saraswal94844d62016-10-13 13:28:03 +05301695 //Create builder interface file.
1696 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1697 builderInterfaceJavaFileHandle =
1698 getJavaFileHandle(getJavaClassName(
1699 BUILDER_INTERFACE_FILE_NAME_SUFFIX));
1700 builderInterfaceJavaFileHandle =
1701 generateBuilderInterfaceFile(
1702 builderInterfaceJavaFileHandle,
1703 curNode, isAttributePresent);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301704 /*
1705 * Append builder interface file to interface file and
1706 * close it.
1707 */
Bharat saraswal94844d62016-10-13 13:28:03 +05301708 mergeJavaFiles(builderInterfaceJavaFileHandle,
1709 interfaceJavaFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301710 }
Bharat saraswal94844d62016-10-13 13:28:03 +05301711
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301712 insertDataIntoJavaFile(interfaceJavaFileHandle, CLOSE_CURLY_BRACKET);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301713 validateLineLength(interfaceJavaFileHandle);
Bharat saraswal8beac342016-08-04 02:00:03 +05301714 if (curNode instanceof YangAugmentableNode) {
Gaurav Agrawal8a147522016-08-10 13:43:01 +05301715 addImportsForAugmentableClass(imports, false, true, curNode);
Bharat saraswald14cbe82016-07-14 13:26:18 +05301716 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301717 }
Bharat saraswal94844d62016-10-13 13:28:03 +05301718
1719 if (!curNode.isOpTypeReq() && curNode instanceof YangCase) {
1720 removeCaseParentImport(curNode, imports);
1721 }
1722
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301723 if ((fileType & BUILDER_CLASS_MASK) != 0 ||
1724 (fileType & DEFAULT_CLASS_MASK) != 0) {
1725 if (isAttributePresent) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301726 addImportsToStringAndHasCodeMethods(imports, true);
Bharat saraswal8beac342016-08-04 02:00:03 +05301727 addArrayListImport(imports);
Bharat saraswalaf413b82016-07-14 15:18:20 +05301728 }
Gaurav Agrawal8a147522016-08-10 13:43:01 +05301729
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301730 addBitsAndBase64Imports(curNode, imports);
Bharat saraswalaf413b82016-07-14 15:18:20 +05301731 if (curNode instanceof YangAugmentableNode) {
Gaurav Agrawal8a147522016-08-10 13:43:01 +05301732 addImportsForAugmentableClass(imports, true, false, curNode);
Bharat saraswal94844d62016-10-13 13:28:03 +05301733 if (curNode.isOpTypeReq()) {
1734 addInvocationExceptionImport(imports);
1735 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301736 }
Bharat saraswal250a7472016-05-12 13:16:57 +05301737 sortImports(imports);
Vidyashree Ramab3670472016-08-06 15:49:56 +05301738
1739 //Create impl class file.
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301740 implClassJavaFileHandle =
1741 getJavaFileHandle(getImplClassName(curNode));
1742 implClassJavaFileHandle =
1743 generateDefaultClassFile(implClassJavaFileHandle,
1744 curNode, isAttributePresent,
1745 imports);
Vidyashree Ramab3670472016-08-06 15:49:56 +05301746
1747 //Create builder class file.
Bharat saraswald14cbe82016-07-14 13:26:18 +05301748 if ((fileType & BUILDER_CLASS_MASK) != 0) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301749 builderClassJavaFileHandle =
1750 getJavaFileHandle(getJavaClassName(
1751 BUILDER_CLASS_FILE_NAME_SUFFIX));
1752 builderClassJavaFileHandle =
1753 generateBuilderClassFile(builderClassJavaFileHandle,
1754 curNode,
1755 isAttributePresent);
Vidyashree Ramab3670472016-08-06 15:49:56 +05301756
1757 //Append impl class to builder class and close it.
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301758 mergeJavaFiles(builderClassJavaFileHandle,
1759 implClassJavaFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301760 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301761 insertDataIntoJavaFile(implClassJavaFileHandle, CLOSE_CURLY_BRACKET);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301762 validateLineLength(implClassJavaFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301763 }
Vidyashree Ramab3670472016-08-06 15:49:56 +05301764 //Close all the file handles.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301765 freeTemporaryResources(false);
1766 }
1767
Bharat saraswal94844d62016-10-13 13:28:03 +05301768 //Removes case's parent import.
1769 private void removeCaseParentImport(YangNode node, List<String> imports) {
1770 YangNode parent = node.getParent();
1771 JavaFileInfo info = ((JavaFileInfoContainer) parent).getJavaFileInfo();
1772 String impt = getImportString(info.getPackage(),
1773 getCapitalCase(info.getJavaName()));
1774 imports.remove(impt);
1775 }
1776
Vidyashree Ramab3670472016-08-06 15:49:56 +05301777 //Adds import for array list.
Bharat saraswal8beac342016-08-04 02:00:03 +05301778 private void addArrayListImport(List<String> imports) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301779 if (imports.contains(javaImportData.getImportForList())) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301780 imports.add(ARRAY_LIST_IMPORT);
1781 }
1782 }
1783
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301784 //Adds import for bitset and base64 list.
1785 private void addBitsAndBase64Imports(YangNode curNode, List<String> imports) {
1786 if (curNode instanceof YangLeavesHolder) {
1787 YangLeavesHolder holder = (YangLeavesHolder) curNode;
Bharat saraswal94844d62016-10-13 13:28:03 +05301788 String impt = getJavaImportData()
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301789 .getImportForToBitSet();
1790 if (!holder.getListOfLeaf().isEmpty() &&
1791 !imports.contains(impt)) {
1792 imports.add(impt);
1793 }
1794 }
Bharat saraswald14cbe82016-07-14 13:26:18 +05301795 }
1796
1797 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301798 * Adds imports for ToString and HashCodeMethod.
1799 *
Bharat saraswal039f59c2016-07-14 21:57:13 +05301800 * @param imports import list
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301801 * @param operation add or remove
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301802 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301803 protected void addImportsToStringAndHasCodeMethods(List<String> imports,
1804 boolean operation) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301805 if (operation) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301806 imports.add(javaImportData.getImportForHashAndEquals());
1807 imports.add(javaImportData.getImportForToString());
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301808 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301809 imports.remove(javaImportData.getImportForHashAndEquals());
1810 imports.remove(javaImportData.getImportForToString());
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301811 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301812 // TODO change boolean to OPERATION, in all related places.
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301813 }
1814
1815 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +05301816 * Adds import for map and hash map.
1817 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301818 * @param imports import list
1819 * @param operations true for adding and false for deletion
1820 * @param isInterfaceFile if need to add in interface file
Gaurav Agrawal8a147522016-08-10 13:43:01 +05301821 * @param curNode current node
Bharat saraswalaf413b82016-07-14 15:18:20 +05301822 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301823 private void addImportsForAugmentableClass(List<String> imports,
1824 boolean operations,
1825 boolean isInterfaceFile,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +05301826 YangNode curNode) {
Bharat saraswal039f59c2016-07-14 21:57:13 +05301827 if (operations) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301828 if (!isInterfaceFile) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301829 imports.add(javaImportData.getHashMapImport());
Bharat saraswal8beac342016-08-04 02:00:03 +05301830 }
Gaurav Agrawal8a147522016-08-10 13:43:01 +05301831 // Add import for hash map only if node is not a YANG choice.
1832 if (!(curNode instanceof YangChoice)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301833 imports.add(javaImportData.getMapImport());
Gaurav Agrawal8a147522016-08-10 13:43:01 +05301834 }
Bharat saraswal039f59c2016-07-14 21:57:13 +05301835 } else {
Bharat saraswal8beac342016-08-04 02:00:03 +05301836 if (!isInterfaceFile) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301837 imports.remove(javaImportData.getHashMapImport());
Bharat saraswal8beac342016-08-04 02:00:03 +05301838 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301839 imports.remove(javaImportData.getMapImport());
Bharat saraswald14cbe82016-07-14 13:26:18 +05301840 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301841 sortImports(imports);
Bharat saraswald14cbe82016-07-14 13:26:18 +05301842 }
1843
1844 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301845 * Adds invocation exception import.
Bharat saraswal039f59c2016-07-14 21:57:13 +05301846 *
1847 * @param imports list of imports
Bharat saraswal039f59c2016-07-14 21:57:13 +05301848 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301849 private void addInvocationExceptionImport(List<String> imports) {
1850 imports.add(INVOCATION_TARGET_EXCEPTION_IMPORT);
Bharat saraswal039f59c2016-07-14 21:57:13 +05301851 }
1852
1853 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301854 * Removes all temporary file handles.
1855 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301856 * @param errorOccurred flag indicating if error occurred
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301857 * @throws IOException when failed to delete the temporary files
1858 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301859 public void freeTemporaryResources(boolean errorOccurred)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301860 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301861 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301862 * Close all java file handles and when error occurs delete the files.
1863 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301864 if (javaFlagSet(INTERFACE_MASK)) {
1865 closeFile(interfaceJavaFileHandle, errorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301866 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301867 if (javaFlagSet(BUILDER_CLASS_MASK)) {
1868 closeFile(builderClassJavaFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301869 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301870 if (javaFlagSet(BUILDER_INTERFACE_MASK)) {
1871 closeFile(builderInterfaceJavaFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301872 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301873 if (javaFlagSet(DEFAULT_CLASS_MASK)) {
1874 closeFile(implClassJavaFileHandle, errorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301875 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301876 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301877 * Close all temporary file handles and delete the files.
1878 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301879 if (tempFlagSet(GETTER_FOR_CLASS_MASK)) {
1880 closeFile(getterImplTempFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301881 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301882 if (tempFlagSet(ATTRIBUTES_MASK)) {
1883 closeFile(attributesTempFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301884 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301885 if (tempFlagSet(HASH_CODE_IMPL_MASK)) {
1886 closeFile(hashCodeImplTempFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301887 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301888 if (tempFlagSet(TO_STRING_IMPL_MASK)) {
1889 closeFile(toStringImplTempFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301890 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301891 if (tempFlagSet(EQUALS_IMPL_MASK)) {
1892 closeFile(equalsImplTempFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301893 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301894 if (tempFlagSet(FROM_STRING_IMPL_MASK)) {
1895 closeFile(fromStringImplTempFileHandle);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301896 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301897 if (tempFlagSet(ADD_TO_LIST_IMPL_MASK)) {
1898 closeFile(addToListImplTempFileHandle);
Bharat saraswal8beac342016-08-04 02:00:03 +05301899 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301900 if (tempFlagSet(ADD_TO_LIST_INTERFACE_MASK)) {
1901 closeFile(addToListInterfaceTempFileHandle);
Bharat saraswal8beac342016-08-04 02:00:03 +05301902 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301903 if (tempFlagSet(LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK)) {
1904 closeFile(leafIdAttributeTempFileHandle);
Bharat saraswal8beac342016-08-04 02:00:03 +05301905 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301906 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_LEAF_MASK)) {
1907 closeFile(subtreeFilteringForLeafTempFileHandle);
Bharat saraswale50edca2016-08-05 01:58:25 +05301908 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301909 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_LEAF_LIST_MASK)) {
1910 closeFile(getSubtreeFilteringForListTempFileHandle);
Bharat saraswale50edca2016-08-05 01:58:25 +05301911 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301912 if (tempFlagSet(FILTER_CONTENT_MATCH_FOR_NODES_MASK)) {
1913 closeFile(getSubtreeFilteringForChildNodeTempFileHandle);
Bharat saraswale50edca2016-08-05 01:58:25 +05301914 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301915 if (tempFlagSet(EDIT_CONTENT_MASK)) {
1916 closeFile(editContentTempFileHandle);
Bharat saraswale50edca2016-08-05 01:58:25 +05301917 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301918 }
1919
1920 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301921 * Returns if the attribute needs to be accessed in a qualified manner or
1922 * not, if it needs to be imported, then the same needs to be done.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301923 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301924 * @param importInfo import info
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301925 * @return status of the qualified access to the attribute
1926 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301927 protected boolean getIsQualifiedAccessOrAddToImportList(
1928 JavaQualifiedTypeInfo importInfo) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301929 return javaImportData
1930 .addImportInfo((JavaQualifiedTypeInfoTranslator) importInfo,
1931 getGeneratedJavaClassName(),
1932 javaFileInfo.getPackage());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301933 }
1934
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301935 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301936 * Returns temp file handle for add to list interface.
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301937 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301938 * @return temp file handle for add to list interface
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301939 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301940 public File getAddToListInterfaceTempFileHandle() {
1941 return addToListInterfaceTempFileHandle;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301942 }
1943
1944 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301945 * Returns temp file handle for add to list impl.
1946 *
1947 * @return temp file handle for add to list impl
1948 */
1949 public File getAddToListImplTempFileHandle() {
1950 return addToListImplTempFileHandle;
1951 }
1952
1953 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301954 * Returns temp file handle for leaf identifier attributes.
1955 *
1956 * @return temp file handle for leaf identifier attributes
1957 */
1958 public File getLeafIdAttributeTempFileHandle() {
1959 return leafIdAttributeTempFileHandle;
1960 }
1961
1962 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301963 * Sets true if root node.
1964 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301965 * @param rootNode true if root node
Bharat saraswal8beac342016-08-04 02:00:03 +05301966 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301967 void setRootNode(boolean rootNode) {
1968 this.rootNode = rootNode;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301969 }
Bharat saraswale50edca2016-08-05 01:58:25 +05301970
1971 /**
1972 * Returns temp file for is content match.
1973 *
1974 * @return temp file for is content match
1975 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301976 public File getSubtreeFilteringForLeafTempFileHandle() {
1977 return subtreeFilteringForLeafTempFileHandle;
Bharat saraswale50edca2016-08-05 01:58:25 +05301978 }
1979
1980 /**
Bharat saraswale50edca2016-08-05 01:58:25 +05301981 * Returns temp file for edit content file.
1982 *
1983 * @return temp file for edit content file
1984 */
1985 public File getEditContentTempFileHandle() {
1986 return editContentTempFileHandle;
1987 }
1988
1989 /**
Bharat saraswale50edca2016-08-05 01:58:25 +05301990 * Returns temp file for is content match.
1991 *
1992 * @return temp file for is content match
1993 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05301994 public File getGetSubtreeFilteringForListTempFileHandle() {
1995 return getSubtreeFilteringForListTempFileHandle;
Bharat saraswale50edca2016-08-05 01:58:25 +05301996 }
1997
1998 /**
Bharat saraswale50edca2016-08-05 01:58:25 +05301999 * Returns temp file for is content match.
2000 *
2001 * @return temp file for is content match
2002 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +05302003 public File getGetSubtreeFilteringForChildNodeTempFileHandle() {
2004 return getSubtreeFilteringForChildNodeTempFileHandle;
Bharat saraswale50edca2016-08-05 01:58:25 +05302005 }
2006
2007 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05302008 * Checks if a given flag is set in generated java files.
2009 * Returns true if ANY flag is set in a bitwise-ORed argument, e.g.
2010 * <pre>
2011 * javaFlagSet(FOO | BAR)
2012 * </pre>
2013 * returns true if either FOO flag or BAR flag is set.
Bharat saraswale50edca2016-08-05 01:58:25 +05302014 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05302015 * @param flag input flag mask value
2016 * @return true if set, else false
Bharat saraswale50edca2016-08-05 01:58:25 +05302017 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05302018 private boolean javaFlagSet(int flag) {
2019 return (getGeneratedJavaFiles() & flag) != 0;
2020 }
2021
2022 /**
2023 * Checks if a given flag is set in temp files.
2024 *
2025 * @param flag input flag mask value
2026 * @return true if set, else false
2027 */
2028 private boolean tempFlagSet(int flag) {
2029 return (tempFilesFlagSet & flag) != 0;
Bharat saraswale50edca2016-08-05 01:58:25 +05302030 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +05302031
2032 /**
2033 * Returns list of bits attributes.
2034 *
2035 * @return list of bits attributes
2036 */
2037 public List<BitsJavaInfoHandler> getBitsHandler() {
2038 return bitsHandler;
2039 }
2040
Bharat saraswal94844d62016-10-13 13:28:03 +05302041 /**
2042 * Sets true if binary type is there for leaf/leaf-list.
2043 *
2044 * @param binary true if binary type is there for leaf/leaf-list
2045 */
2046 public void setBinary(boolean binary) {
2047 isBinary = binary;
2048 }
2049
2050 /**
2051 * Returns true if binary type is there for leaf/leaf-list.
2052 *
2053 * @return true if binary type is there for leaf/leaf-list
2054 */
2055 public boolean isBinary() {
2056 return isBinary;
2057 }
2058
Vinod Kumar S79a374b2016-04-30 21:09:15 +05302059}