Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Mahesh Poojary Huawei | 41547ad | 2016-07-14 17:49:50 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangAugmentableNode; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangCase; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangChoice; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangDerivedInfo; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangNode; |
Mahesh Poojary Huawei | 41547ad | 2016-07-14 17:49:50 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangType; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangUnion; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfo; |
Mahesh Poojary Huawei | 41547ad | 2016-07-14 17:49:50 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.translator.tojava.JavaImportData; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.translator.tojava.TempJavaEnumerationFragmentFiles; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.translator.tojava.TempJavaEventFragmentFiles; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 40 | import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 41 | import org.onosproject.yangutils.translator.tojava.TempJavaTypeFragmentFiles; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 42 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 43 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 44 | import java.io.File; |
| 45 | import java.io.IOException; |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.List; |
| 48 | |
| 49 | import static java.util.Collections.sort; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED; |
| 51 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.IDENTITYREF; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 53 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
| 54 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.DEFAULT_CLASS_MASK; |
| 55 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS; |
| 56 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS; |
| 57 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE; |
| 58 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS; |
| 59 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
| 60 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
| 61 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS; |
| 62 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
| 63 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_IMPL_MASK; |
| 64 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_INTERFACE_MASK; |
| 65 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK; |
| 66 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK; |
| 67 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK; |
| 68 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK; |
| 69 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK; |
| 70 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_ENUM_MASK; |
| 71 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_METHOD_MASK; |
| 72 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_ATTRIBUTE_MASK; |
| 73 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_GETTER_MASK; |
| 74 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_SETTER_MASK; |
| 75 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK; |
| 76 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK; |
| 77 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK; |
| 78 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK; |
| 79 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK; |
| 80 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK; |
| 81 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK; |
| 82 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK; |
| 83 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK; |
| 84 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 85 | import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.getCurNodeAsAttributeInTarget; |
| 86 | import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.getQualifierInfoForCasesParent; |
| 87 | import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isGetSetOfRootNodeRequired; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 88 | import static org.onosproject.yangutils.translator.tojava.utils.BitsJavaInfoHandler.generateBitsFile; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 89 | import static org.onosproject.yangutils.translator.tojava.utils.IndentationType.FOUR_SPACE; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 90 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getEnumsValueAttribute; |
| 91 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getEventEnumTypeStart; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 92 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getOperationTypeEnum; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 93 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getSetValueParaForUnionClass; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 94 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getDataFromTempFileHandle; |
| 95 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.initiateJavaFileGeneration; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 96 | import static org.onosproject.yangutils.translator.tojava.utils.MethodBodyTypes.ENUM_METHOD_INT_VALUE; |
| 97 | import static org.onosproject.yangutils.translator.tojava.utils.MethodBodyTypes.ENUM_METHOD_STRING_VALUE; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 98 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.builderMethod; |
| 99 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddAugmentInfoMethodImpl; |
| 100 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddAugmentInfoMethodInterface; |
| 101 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAugmentsDataMethodForService; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 102 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBitSetEnumClassFromString; |
| 103 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBitSetEnumClassToString; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 104 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 105 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEnumsConstructor; |
| 106 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEnumsOfValueMethod; |
| 107 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodClose; |
| 108 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodOpen; |
| 109 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethodClose; |
| 110 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethodSignature; |
| 111 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetter; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 112 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 113 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodClose; |
| 114 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodOpen; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 115 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getInterfaceLeafIdEnumSignature; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 116 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRangeValidatorMethodForUnion; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 117 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForSelectLeaf; |
| 118 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 119 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringForEnumClass; |
| 120 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringForType; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 121 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodClose; |
| 122 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodOpen; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 123 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getUnionToStringMethod; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 124 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getYangAugmentInfoImpl; |
| 125 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getYangAugmentInfoInterface; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 126 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.isLeafValueSetInterface; |
| 127 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.isSelectLeafSetInterface; |
| 128 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.processSubtreeFilteringInterface; |
| 129 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.setSelectLeafSetInterface; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 130 | import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.getInterfaceLeafIdEnumMethods; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 131 | import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.getIsSelectLeafSet; |
| 132 | import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.getIsValueLeafSet; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 133 | import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.methodClose; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 134 | import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.signatureClose; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 135 | import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getAugmentableSubTreeFiltering; |
| 136 | import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getProcessChildNodeSubtreeFiltering; |
| 137 | import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getProcessLeafListSubtreeFiltering; |
| 138 | import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getProcessLeafSubtreeFiltering; |
| 139 | import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getProcessSubTreeFilteringEnd; |
| 140 | import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getProcessSubtreeFilteringStart; |
| 141 | import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getProcessSubtreeFunctionBody; |
| 142 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.addDefaultConstructor; |
| 143 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getBeanFiles; |
| 144 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getTypeFiles; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 145 | import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 146 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 147 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_CLASS; |
| 148 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_INTERFACE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 149 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 150 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 151 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 152 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_CAPS; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 153 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 154 | import static org.onosproject.yangutils.utils.UtilConstants.ENUM_CLASS; |
| 155 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_CLASS; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 156 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 157 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 158 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_SUBJECT_NAME_SUFFIX; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 159 | import static org.onosproject.yangutils.utils.UtilConstants.IMPL_CLASS; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 160 | import static org.onosproject.yangutils.utils.UtilConstants.INT; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 161 | import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 162 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 163 | import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 164 | import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE; |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 165 | import static org.onosproject.yangutils.utils.UtilConstants.PROTECTED; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 166 | import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 167 | import static org.onosproject.yangutils.utils.UtilConstants.RPC_CLASS; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 168 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLON; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 169 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE_METHOD_STRING; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 170 | import static org.onosproject.yangutils.utils.UtilConstants.TYPEDEF_CLASS; |
| 171 | import static org.onosproject.yangutils.utils.UtilConstants.UNION_CLASS; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 172 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 173 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 174 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 175 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 176 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 177 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.replaceLast; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 178 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 179 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength; |
| 180 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 181 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 182 | * Representation of java file generator. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 183 | */ |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 184 | public final class JavaFileGenerator { |
| 185 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 186 | // No instantiation |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 187 | private JavaFileGenerator() { |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns generated interface file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 192 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 193 | * @param file file |
| 194 | * @param imports imports for the file |
| 195 | * @param curNode current YANG node |
| 196 | * @param attrPresent if any attribute is present or not |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 197 | * @return interface file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 198 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 199 | */ |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 200 | public static File generateInterfaceFile(File file, List<String> imports, |
| 201 | YangNode curNode, |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 202 | boolean attrPresent) |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 203 | throws IOException { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 204 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 205 | JavaFileInfoTranslator fileInfo = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 206 | ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 207 | |
| 208 | String path; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 209 | if (curNode instanceof RpcNotificationContainer) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 210 | path = fileInfo.getPluginConfig().getCodeGenDir() + |
| 211 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 212 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 213 | path = fileInfo.getBaseCodeGenPath() + |
| 214 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 215 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 216 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 217 | String className = getCapitalCase(fileInfo.getJavaName()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 218 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 219 | boolean leavesPresent; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 220 | YangLeavesHolder leavesHolder; |
| 221 | if (curNode instanceof YangLeavesHolder) { |
| 222 | leavesHolder = (YangLeavesHolder) curNode; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 223 | leavesPresent = leavesPresent(leavesHolder); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 224 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 225 | leavesPresent = false; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 226 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 227 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 228 | initiateJavaFileGeneration(file, INTERFACE_MASK, imports, curNode, |
| 229 | className); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 230 | |
| 231 | // Generate ENUM interface |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 232 | if (curNode instanceof RpcNotificationContainer) { |
| 233 | insertDataIntoJavaFile(file, getOperationTypeEnum()); |
| 234 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 235 | List<String> methods = new ArrayList<>(); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 236 | if (attrPresent) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 237 | // Add getter methods to interface file. |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 238 | try { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 239 | //Leaf identifier enum. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 240 | if (leavesPresent) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 241 | insertDataIntoJavaFile(file, getInterfaceLeafIdEnumSignature( |
| 242 | className) + trimAtLast(replaceLast( |
| 243 | getDataFromTempFileHandle( |
| 244 | LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK, |
| 245 | getBeanFiles(curNode), path), |
| 246 | COMMA, SEMI_COLON), NEW_LINE) + |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 247 | getInterfaceLeafIdEnumMethods()); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 248 | } |
| 249 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 250 | insertDataIntoJavaFile(file, NEW_LINE); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 251 | //Getter methods. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 252 | insertDataIntoJavaFile(file, getDataFromTempFileHandle( |
| 253 | GETTER_FOR_INTERFACE_MASK, getBeanFiles(curNode), path)); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 254 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 255 | throw new IOException(getErrorMsg(className, INTERFACE)); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 256 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 257 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 258 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 259 | if (curNode instanceof YangAugmentableNode && |
| 260 | !(curNode instanceof YangChoice)) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 261 | methods.add(getYangAugmentInfoInterface()); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 262 | } |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 263 | if (curNode.isOpTypeReq()) { |
| 264 | if (curNode instanceof YangCase) { |
| 265 | YangNode caseParent = curNode.getParent(); |
| 266 | JavaQualifiedTypeInfo qualifiedTypeInfo = |
| 267 | getQualifierInfoForCasesParent(caseParent, |
| 268 | fileInfo.getPluginConfig()); |
| 269 | methods.add(processSubtreeFilteringInterface( |
| 270 | qualifiedTypeInfo.getClassInfo())); |
| 271 | } else { |
| 272 | methods.add(processSubtreeFilteringInterface(className)); |
| 273 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 274 | } |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 275 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 276 | if (leavesPresent) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 277 | methods.add(isLeafValueSetInterface()); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 278 | if (curNode.isOpTypeReq()) { |
| 279 | methods.add(isSelectLeafSetInterface()); |
| 280 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 281 | } |
| 282 | for (String method : methods) { |
| 283 | insertDataIntoJavaFile(file, method); |
| 284 | } |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame] | 285 | return file; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 289 | * Returns generated builder interface file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 290 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 291 | * @param file file |
| 292 | * @param curNode current YANG node |
| 293 | * @param attrPresent if any attribute is present or not |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 294 | * @return builder interface file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 295 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 296 | */ |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 297 | public static File generateBuilderInterfaceFile(File file, YangNode curNode, |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 298 | boolean attrPresent) |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 299 | throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 300 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 301 | JavaFileInfoTranslator fileInfo = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 302 | ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 303 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 304 | boolean leavesPresent; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 305 | YangLeavesHolder leavesHolder; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 306 | if (curNode instanceof YangLeavesHolder && |
| 307 | curNode.isOpTypeReq()) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 308 | leavesHolder = (YangLeavesHolder) curNode; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 309 | leavesPresent = leavesPresent(leavesHolder); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 310 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 311 | leavesPresent = false; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 312 | } |
| 313 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 314 | String className = getCapitalCase(fileInfo.getJavaName()); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 315 | String path; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 316 | if (curNode instanceof RpcNotificationContainer) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 317 | path = fileInfo.getPluginConfig().getCodeGenDir() + |
| 318 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 319 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 320 | path = fileInfo.getBaseCodeGenPath() + |
| 321 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 322 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 323 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 324 | initiateJavaFileGeneration(file, BUILDER_INTERFACE_MASK, null, curNode, |
| 325 | className); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 326 | List<String> methods = new ArrayList<>(); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 327 | if (attrPresent) { |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 328 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 329 | //Getter methods. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 330 | methods.add(getDataFromTempFileHandle( |
| 331 | GETTER_FOR_INTERFACE_MASK, |
| 332 | getBeanFiles(curNode), path)); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 333 | |
| 334 | //Setter methods. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 335 | methods.add(getDataFromTempFileHandle( |
| 336 | SETTER_FOR_INTERFACE_MASK, |
| 337 | getBeanFiles(curNode), path)); |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 338 | |
| 339 | //Add to list method. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 340 | insertDataIntoJavaFile(file, getDataFromTempFileHandle( |
| 341 | ADD_TO_LIST_INTERFACE_MASK, getBeanFiles(curNode), path)); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 342 | } catch (IOException e) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 343 | throw new IOException(getErrorMsg(className, BUILDER_INTERFACE)); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 344 | } |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 345 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 346 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 347 | if (curNode instanceof YangAugmentableNode && |
| 348 | !(curNode instanceof YangChoice)) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 349 | methods.add(getAddAugmentInfoMethodInterface(className + BUILDER)); |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 350 | methods.add(getYangAugmentInfoInterface()); |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 351 | } |
| 352 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 353 | if (leavesPresent) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 354 | methods.add(setSelectLeafSetInterface(className)); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 355 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 356 | //Add build method to builder interface file. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 357 | methods.add(((TempJavaCodeFragmentFilesContainer) curNode) |
| 358 | .getTempJavaCodeFragmentFiles() |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 359 | .addBuildMethodForInterface()); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 360 | |
| 361 | //Add getters and setters in builder interface. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 362 | for (String method : methods) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 363 | insertDataIntoJavaFile(file, method); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 364 | } |
| 365 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 366 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame] | 367 | return file; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Returns generated builder class file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 372 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 373 | * @param file file |
| 374 | * @param curNode current YANG node |
| 375 | * @param attrPresent if any attribute is present or not |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 376 | * @return builder class file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 377 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 378 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 379 | public static File generateBuilderClassFile(File file, YangNode curNode, |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 380 | boolean attrPresent) |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 381 | throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 382 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 383 | JavaFileInfoTranslator fileInfo = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 384 | ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 385 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 386 | boolean leavesPresent; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 387 | YangLeavesHolder leavesHolder; |
| 388 | if (curNode instanceof YangLeavesHolder) { |
| 389 | leavesHolder = (YangLeavesHolder) curNode; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 390 | leavesPresent = leavesPresent(leavesHolder); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 391 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 392 | leavesPresent = false; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 393 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 394 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 395 | String className = getCapitalCase(fileInfo.getJavaName()); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 396 | String path; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 397 | if (curNode instanceof RpcNotificationContainer) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 398 | path = fileInfo.getPluginConfig().getCodeGenDir() + |
| 399 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 400 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 401 | path = fileInfo.getBaseCodeGenPath() + |
| 402 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 403 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 404 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 405 | initiateJavaFileGeneration(file, BUILDER_CLASS_MASK, null, curNode, |
| 406 | className); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 407 | List<String> methods = new ArrayList<>(); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 408 | if (attrPresent) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 409 | |
| 410 | //Add attribute strings. |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 411 | try { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 412 | insertDataIntoJavaFile(file, getDataFromTempFileHandle( |
| 413 | ATTRIBUTES_MASK, getBeanFiles(curNode), path)); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 414 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 415 | throw new IOException(getErrorMsg(className, BUILDER_CLASS)); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 416 | } |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 417 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 418 | //Getter methods. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 419 | methods.add(getDataFromTempFileHandle( |
| 420 | GETTER_FOR_CLASS_MASK, getBeanFiles(curNode), path)); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 421 | // Setter methods. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 422 | methods.add(getDataFromTempFileHandle( |
| 423 | SETTER_FOR_CLASS_MASK, getBeanFiles(curNode), path)); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 424 | |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 425 | //Add to list impl method. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 426 | methods.add(getDataFromTempFileHandle( |
| 427 | ADD_TO_LIST_IMPL_MASK, getBeanFiles(curNode), path)); |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 428 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 429 | insertDataIntoJavaFile(file, NEW_LINE); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 430 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 431 | //Add operation attribute methods. |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 432 | if (leavesPresent && curNode.isOpTypeReq()) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 433 | insertDataIntoJavaFile(file, NEW_LINE); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 434 | methods.add(getSetterForSelectLeaf(className)); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 435 | } |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 436 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 437 | throw new IOException(getErrorMsg(className, BUILDER_CLASS)); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 438 | } |
| 439 | } else { |
| 440 | insertDataIntoJavaFile(file, NEW_LINE); |
| 441 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 442 | |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 443 | if (curNode instanceof YangAugmentableNode) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 444 | methods.add(getAddAugmentInfoMethodImpl(className + BUILDER)); |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 445 | methods.add(getYangAugmentInfoImpl()); |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 446 | } |
| 447 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 448 | // Add default constructor and build method impl. |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 449 | methods.add(((TempJavaCodeFragmentFilesContainer) curNode) |
| 450 | .getTempJavaCodeFragmentFiles() |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 451 | .addBuildMethodImpl()); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 452 | methods.add(addDefaultConstructor(curNode, PUBLIC, BUILDER)); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 453 | |
| 454 | //Add methods in builder class. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 455 | for (String method : methods) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 456 | insertDataIntoJavaFile(file, method); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 457 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 458 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET); |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame] | 459 | return file; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /** |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 463 | * Returns generated default class file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 464 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 465 | * @param file file |
| 466 | * @param curNode current YANG node |
| 467 | * @param attrPresent if any attribute is present or not |
| 468 | * @param imports list of imports |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 469 | * @return impl class file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 470 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 471 | */ |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 472 | public static File generateDefaultClassFile(File file, YangNode curNode, |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 473 | boolean attrPresent, |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 474 | List<String> imports) |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 475 | throws IOException { |
| 476 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 477 | JavaFileInfoTranslator fileInfo = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 478 | ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 479 | YangPluginConfig config = fileInfo.getPluginConfig(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 480 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 481 | boolean leavesPresent; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 482 | YangLeavesHolder leavesHolder; |
| 483 | if (curNode instanceof YangLeavesHolder) { |
| 484 | leavesHolder = (YangLeavesHolder) curNode; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 485 | leavesPresent = leavesPresent(leavesHolder); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 486 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 487 | leavesPresent = false; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 488 | } |
| 489 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 490 | boolean rootNode = false; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 491 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 492 | String className = getCapitalCase(fileInfo.getJavaName()); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 493 | String opParamClassName = className; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 494 | String path; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 495 | if (curNode instanceof RpcNotificationContainer) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 496 | opParamClassName = className + OP_PARAM; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 497 | rootNode = true; |
| 498 | path = fileInfo.getPluginConfig().getCodeGenDir() + |
| 499 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 500 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 501 | path = fileInfo.getBaseCodeGenPath() + |
| 502 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 503 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 504 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 505 | initiateJavaFileGeneration(file, DEFAULT_CLASS_MASK, imports, curNode, |
| 506 | className); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 507 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 508 | List<String> methods = new ArrayList<>(); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 509 | if (attrPresent) { |
| 510 | addDefaultClassAttributeInfo(file, curNode, className, |
| 511 | opParamClassName, path, methods, |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 512 | rootNode); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 513 | } else { |
| 514 | insertDataIntoJavaFile(file, NEW_LINE); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 515 | } |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 516 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 517 | if (curNode instanceof YangAugmentableNode) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 518 | methods.add(getYangAugmentInfoImpl()); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 519 | } |
| 520 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 521 | //Constructor. |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 522 | String constructor = getConstructorStart(className, rootNode) + |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 523 | getDataFromTempFileHandle( |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 524 | CONSTRUCTOR_IMPL_MASK, getBeanFiles(curNode), path) |
| 525 | + methodClose(FOUR_SPACE); |
| 526 | methods.add(constructor); |
| 527 | if (curNode.isOpTypeReq()) { |
| 528 | String augmentableSubTreeFiltering = EMPTY_STRING; |
| 529 | if (curNode instanceof YangAugmentableNode) { |
| 530 | // add is filter content match. |
| 531 | augmentableSubTreeFiltering = getAugmentableSubTreeFiltering(); |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 532 | } |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 533 | methods.add(getProcessSubtreeFilteringStart(curNode, config) + |
| 534 | getProcessSubtreeFunctionBody(curNode) + |
| 535 | augmentableSubTreeFiltering + |
| 536 | getProcessSubTreeFilteringEnd()); |
| 537 | |
| 538 | if (curNode instanceof YangLeavesHolder) { |
| 539 | if (((YangLeavesHolder) curNode).getListOfLeaf() != null && |
| 540 | !((YangLeavesHolder) curNode).getListOfLeaf().isEmpty()) { |
| 541 | methods.add(getProcessLeafSubtreeFiltering(curNode, config, |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 542 | path)); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 543 | } |
| 544 | if (((YangLeavesHolder) curNode).getListOfLeafList() != null && |
| 545 | !((YangLeavesHolder) curNode).getListOfLeafList().isEmpty()) { |
| 546 | methods.add(getProcessLeafListSubtreeFiltering(curNode, config, |
| 547 | path)); |
| 548 | } |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 549 | } |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 550 | |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 551 | if (curNode.getChild() != null) { |
| 552 | methods.add(getProcessChildNodeSubtreeFiltering(curNode, config, |
| 553 | path)); |
| 554 | } |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 555 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 556 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 557 | throw new IOException(getErrorMsg(className, IMPL_CLASS)); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 558 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 559 | |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 560 | methods.add(addDefaultConstructor(curNode, PROTECTED, DEFAULT)); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 561 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 562 | methods.add(builderMethod(className)); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 563 | if (leavesPresent) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 564 | methods.add(getIsValueLeafSet()); |
| 565 | if (curNode.isOpTypeReq()) { |
| 566 | methods.add(getIsSelectLeafSet()); |
| 567 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 568 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 569 | // Add methods in impl class. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 570 | for (String method : methods) { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 571 | insertDataIntoJavaFile(file, method); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 572 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 573 | |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame] | 574 | return file; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 578 | * Add methods related to attributes. |
| 579 | * |
| 580 | * @param file file |
| 581 | * @param curNode current YANG node |
| 582 | * @param className name of the class |
| 583 | * @param opParamClassName op param class name |
| 584 | * @param path file path |
| 585 | * @param methods list of methods string |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 586 | * @param rootNode flag indicating whether node is root node |
| 587 | * @throws IOException a violation in IO rule |
| 588 | */ |
| 589 | private static void addDefaultClassAttributeInfo(File file, YangNode curNode, |
| 590 | String className, |
| 591 | String opParamClassName, |
| 592 | String path, List<String> methods, |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 593 | boolean rootNode) |
| 594 | throws IOException { |
| 595 | |
| 596 | //Add attribute strings. |
| 597 | try { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 598 | insertDataIntoJavaFile(file, getDataFromTempFileHandle( |
| 599 | ATTRIBUTES_MASK, getBeanFiles(curNode), path)); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 600 | } catch (IOException e) { |
| 601 | throw new IOException(getErrorMsg(className, IMPL_CLASS)); |
| 602 | } |
| 603 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 604 | try { |
| 605 | //Getter methods. |
| 606 | methods.add(getDataFromTempFileHandle( |
| 607 | GETTER_FOR_CLASS_MASK, getBeanFiles(curNode), path)); |
| 608 | |
| 609 | // Hash code method. |
| 610 | methods.add(getHashCodeMethodClose( |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 611 | getHashCodeMethodOpen() + getDataFromTempFileHandle( |
| 612 | HASH_CODE_IMPL_MASK, getBeanFiles(curNode), path) |
| 613 | .replace(NEW_LINE, EMPTY_STRING))); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 614 | |
| 615 | //Equals method. |
| 616 | if (rootNode) { |
| 617 | methods.add(getEqualsMethodClose( |
| 618 | getEqualsMethodOpen(opParamClassName) + |
| 619 | getDataFromTempFileHandle( |
| 620 | EQUALS_IMPL_MASK, |
| 621 | getBeanFiles(curNode), path))); |
| 622 | } else { |
| 623 | methods.add(getEqualsMethodClose( |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 624 | getEqualsMethodOpen(DEFAULT_CAPS + className) + |
| 625 | getDataFromTempFileHandle(EQUALS_IMPL_MASK, |
| 626 | getBeanFiles(curNode), |
| 627 | path))); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 628 | } |
| 629 | // To string method. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 630 | methods.add(getToStringMethodOpen() + getDataFromTempFileHandle( |
| 631 | TO_STRING_IMPL_MASK, getBeanFiles(curNode), path) + |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 632 | getToStringMethodClose()); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 633 | |
| 634 | for (BitsJavaInfoHandler handler : getBeanFiles(curNode) |
| 635 | .getBitsHandler()) { |
| 636 | generateBitsFile(handler.getAttr(), handler.getYangType(), curNode); |
| 637 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 638 | } catch (IOException e) { |
| 639 | throw new IOException(getErrorMsg(className, IMPL_CLASS)); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /** |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 644 | * Generates class file for type def. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 645 | * |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 646 | * @param file generated file |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 647 | * @param curNode current YANG node |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 648 | * @param imports imports for file |
| 649 | * @return type def class file |
| 650 | * @throws IOException when fails to generate class file |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 651 | */ |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 652 | public static File generateTypeDefClassFile(File file, YangNode curNode, |
| 653 | List<String> imports) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 654 | throws IOException { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 655 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 656 | JavaFileInfoTranslator fileInfo = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 657 | ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
Mahesh Poojary Huawei | 41547ad | 2016-07-14 17:49:50 +0530 | [diff] [blame] | 658 | // import |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 659 | String className = getCapitalCase(fileInfo.getJavaName()); |
| 660 | String path = fileInfo.getBaseCodeGenPath() + |
| 661 | fileInfo.getPackageFilePath(); |
Mahesh Poojary Huawei | 41547ad | 2016-07-14 17:49:50 +0530 | [diff] [blame] | 662 | YangTypeDef typeDef = (YangTypeDef) curNode; |
| 663 | List<YangType<?>> types = typeDef.getTypeList(); |
| 664 | YangType type = types.get(0); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 665 | YangDataTypes yangDataTypes = type.getDataType(); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 666 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 667 | initiateJavaFileGeneration(file, className, GENERATE_TYPEDEF_CLASS, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 668 | imports, path); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 669 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 670 | List<String> methods = new ArrayList<>(); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 671 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 672 | //Add attribute strings. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 673 | try { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 674 | insertDataIntoJavaFile(file, getDataFromTempFileHandle( |
| 675 | ATTRIBUTES_MASK, getTypeFiles(curNode), path)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 676 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 677 | throw new IOException(getErrorMsg(className, TYPEDEF_CLASS)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 678 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 679 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 680 | //Default constructor. |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 681 | methods.add(addDefaultConstructor(curNode, PRIVATE, EMPTY_STRING)); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 682 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 683 | try { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 684 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 685 | //Type constructor. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 686 | methods.add(getDataFromTempFileHandle( |
| 687 | CONSTRUCTOR_FOR_TYPE_MASK, getTypeFiles(curNode), path)); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 688 | |
| 689 | //Of method. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 690 | methods.add(getDataFromTempFileHandle( |
| 691 | OF_STRING_IMPL_MASK, getTypeFiles(curNode), path)); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 692 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 693 | //Getter methods. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 694 | methods.add(getDataFromTempFileHandle( |
| 695 | GETTER_FOR_CLASS_MASK, getTypeFiles(curNode), path)); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 696 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 697 | // Hash code method. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 698 | methods.add(getHashCodeMethodClose( |
| 699 | getHashCodeMethodOpen() + getDataFromTempFileHandle( |
| 700 | HASH_CODE_IMPL_MASK, getTypeFiles(curNode), path) |
| 701 | .replace(NEW_LINE, EMPTY_STRING))); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 702 | |
| 703 | //Equals method. |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 704 | methods.add(getEqualsMethodClose( |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 705 | getEqualsMethodOpen(className + EMPTY_STRING) + |
| 706 | getDataFromTempFileHandle(EQUALS_IMPL_MASK, |
| 707 | getTypeFiles(curNode), path))); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 708 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 709 | // To string method. |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 710 | addTypedefToString(curNode, methods, path); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 711 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 712 | JavaCodeGeneratorInfo javaGenInfo = (JavaCodeGeneratorInfo) curNode; |
| 713 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 714 | //From string method. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 715 | if (type.getDataType() == DERIVED && |
| 716 | ((YangDerivedInfo) type.getDataTypeExtendedInfo()) |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 717 | .getEffectiveBuiltInType() |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 718 | == IDENTITYREF) { |
| 719 | yangDataTypes = IDENTITYREF; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 720 | } |
| 721 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 722 | if (type.getDataType() == IDENTITYREF) { |
| 723 | yangDataTypes = IDENTITYREF; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 724 | } |
| 725 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 726 | if (yangDataTypes != IDENTITYREF) { |
| 727 | methods.add(getFromStringMethodSignature(className) + |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 728 | getDataFromTempFileHandle( |
| 729 | FROM_STRING_IMPL_MASK, |
| 730 | javaGenInfo.getTempJavaCodeFragmentFiles() |
| 731 | .getTypeTempFiles(), path) + |
| 732 | getFromStringMethodClose()); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 733 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 734 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 735 | throw new IOException(getErrorMsg(className, TYPEDEF_CLASS)); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 736 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 737 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 738 | for (String method : methods) { |
| 739 | insertDataIntoJavaFile(file, method); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 740 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 741 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 742 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 743 | return validateLineLength(file); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 744 | } |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 745 | |
| 746 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 747 | * Adds typedef to string method. |
| 748 | * |
| 749 | * @param curNode current node |
| 750 | * @param methods list of methods string |
| 751 | * @param path file path |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 752 | * @throws IOException a violation in IO rule |
| 753 | */ |
| 754 | private static void addTypedefToString(YangNode curNode, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 755 | List<String> methods, String path) |
| 756 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 757 | //To string method. |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 758 | |
| 759 | List<YangType<?>> types = ((YangTypeDef) curNode).getTypeList(); |
| 760 | YangType type = types.get(0); |
| 761 | String className = ((JavaFileInfoContainer) curNode).getJavaFileInfo() |
| 762 | .getJavaName(); |
| 763 | methods.add(getToStringForType(getCamelCase(type.getDataTypeName(), |
| 764 | null), type, getCapitalCase(className))); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 765 | for (BitsJavaInfoHandler handler : getTypeFiles(curNode) |
| 766 | .getBitsHandler()) { |
| 767 | generateBitsFile(handler.getAttr(), handler.getYangType(), curNode); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 768 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 769 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /** |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 773 | * Generates class file for union type. |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 774 | * |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 775 | * @param file generated file |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 776 | * @param curNode current YANG node |
| 777 | * @param imports imports for file |
| 778 | * @return type def class file |
| 779 | * @throws IOException when fails to generate class file |
| 780 | */ |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 781 | public static File generateUnionClassFile(File file, YangNode curNode, |
| 782 | List<String> imports) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 783 | throws IOException { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 784 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 785 | JavaFileInfoTranslator fileInfo = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 786 | ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 787 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 788 | String className = getCapitalCase(fileInfo.getJavaName()); |
| 789 | String path = fileInfo.getBaseCodeGenPath() + |
| 790 | fileInfo.getPackageFilePath(); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 791 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 792 | TempJavaTypeFragmentFiles tempFiles = |
| 793 | ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles() |
| 794 | .getTypeTempFiles(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 795 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 796 | boolean intConflict = false; |
| 797 | boolean longConflict = false; |
| 798 | boolean shortConflict = false; |
| 799 | JavaAttributeInfo intAttr = tempFiles.getIntAttribute(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 800 | if (intAttr == null) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 801 | intAttr = tempFiles.getUIntAttribute(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 802 | } |
| 803 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 804 | JavaAttributeInfo longAttr = |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 805 | tempFiles.getLongAttribute(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 806 | if (longAttr == null) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 807 | longAttr = tempFiles.getULongAttribute(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 808 | } |
| 809 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 810 | JavaAttributeInfo shortAttr = |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 811 | tempFiles.getShortAttribute(); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 812 | if (shortAttr == null) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 813 | shortAttr = tempFiles.getUInt8Attribute(); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 814 | } |
| 815 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 816 | if (intAttr != null) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 817 | intConflict = intAttr.isIntConflict(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 818 | } |
| 819 | if (longAttr != null) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 820 | longConflict = longAttr.isLongConflict(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 821 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 822 | if (shortAttr != null) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 823 | shortConflict = shortAttr.isShortConflict(); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 824 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 825 | if (longConflict) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 826 | String impt = tempFiles.getJavaImportData().getBigIntegerImport(); |
| 827 | if (!imports.contains(impt)) { |
| 828 | imports.add(impt); |
| 829 | sort(imports); |
| 830 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 831 | } |
| 832 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 833 | initiateJavaFileGeneration(file, className, GENERATE_UNION_CLASS, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 834 | imports, path); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 835 | |
| 836 | List<String> methods = new ArrayList<>(); |
| 837 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 838 | // Add attribute strings. |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 839 | try { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 840 | addUnionClassAttributeInfo(file, curNode, intConflict, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 841 | longConflict, shortConflict, path, tempFiles); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 842 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 843 | throw new IOException(getErrorMsg(className, UNION_CLASS)); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 844 | } |
| 845 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 846 | //Default constructor. |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 847 | methods.add(addDefaultConstructor(curNode, PRIVATE, EMPTY_STRING)); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 848 | |
| 849 | try { |
| 850 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 851 | //Type constructor. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 852 | methods.add(getDataFromTempFileHandle( |
| 853 | CONSTRUCTOR_FOR_TYPE_MASK, getTypeFiles(curNode), path)); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 854 | |
| 855 | // Of string method. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 856 | methods.add(getDataFromTempFileHandle( |
| 857 | OF_STRING_IMPL_MASK, getTypeFiles(curNode), path)); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 858 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 859 | //Getter methods. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 860 | methods.add(getDataFromTempFileHandle( |
| 861 | GETTER_FOR_CLASS_MASK, getTypeFiles(curNode), path)); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 862 | |
| 863 | //Hash code method. |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 864 | methods.add(getHashCodeMethodClose( |
| 865 | getHashCodeMethodOpen() + |
| 866 | getDataFromTempFileHandle( |
| 867 | HASH_CODE_IMPL_MASK, getTypeFiles(curNode), |
| 868 | path).replace(NEW_LINE, EMPTY_STRING))); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 869 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 870 | //Equals method. |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 871 | methods.add(getEqualsMethodClose( |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 872 | getEqualsMethodOpen(className) + |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 873 | getDataFromTempFileHandle( |
| 874 | EQUALS_IMPL_MASK, getTypeFiles(curNode), |
| 875 | path))); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 876 | |
| 877 | //To string method. |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 878 | methods.add(getUnionToStringMethod( |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 879 | ((YangUnion) curNode).getTypeList(), getCapitalCase(className))); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 880 | |
| 881 | for (BitsJavaInfoHandler handler : getTypeFiles(curNode) |
| 882 | .getBitsHandler()) { |
| 883 | generateBitsFile(handler.getAttr(), handler.getYangType(), curNode); |
| 884 | } |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 885 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 886 | //From string method. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 887 | methods.add(getFromStringMethodSignature(className) + |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 888 | getDataFromTempFileHandle( |
| 889 | FROM_STRING_IMPL_MASK, |
| 890 | getTypeFiles(curNode), path) + |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 891 | getFromStringMethodClose()); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 892 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 893 | if (intConflict) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 894 | methods.add(getRangeValidatorMethodForUnion(INT)); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 895 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 896 | if (longConflict) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 897 | methods.add(getRangeValidatorMethodForUnion(BIG_INTEGER)); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 898 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 899 | if (shortConflict && !intConflict) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 900 | methods.add(getRangeValidatorMethodForUnion(INT)); |
| 901 | } |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 902 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 903 | throw new IOException(getErrorMsg(className, UNION_CLASS)); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | for (String method : methods) { |
| 907 | insertDataIntoJavaFile(file, method); |
| 908 | } |
| 909 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
| 910 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 911 | return validateLineLength(file); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 912 | } |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 913 | |
| 914 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 915 | * Adds union class attribute information. |
| 916 | * |
| 917 | * @param file generated file |
| 918 | * @param curNode current YANG node |
| 919 | * @param intConflict flag to indicate if there is a conflict in int |
| 920 | * @param longConflict flag to indicate if there is a conflict in long |
| 921 | * @param shortConflict flag to indicate if there is a conflict in short |
| 922 | * @param path file path |
| 923 | * @param tempFiles temp java type fragment files |
| 924 | * @throws IOException a violation in IO rule |
| 925 | */ |
| 926 | private static void addUnionClassAttributeInfo(File file, |
| 927 | YangNode curNode, |
| 928 | boolean intConflict, |
| 929 | boolean longConflict, |
| 930 | boolean shortConflict, |
| 931 | String path, |
| 932 | TempJavaTypeFragmentFiles tempFiles) |
| 933 | throws IOException { |
| 934 | if (intConflict) { |
| 935 | insertDataIntoJavaFile(file, JavaCodeSnippetGen |
| 936 | .addStaticAttributeIntRange(PRIVATE, |
| 937 | tempFiles.getIntIndex() < |
| 938 | tempFiles.getUIntIndex())); |
| 939 | } |
| 940 | |
| 941 | if (longConflict) { |
| 942 | insertDataIntoJavaFile(file, JavaCodeSnippetGen |
| 943 | .addStaticAttributeLongRange(PRIVATE, |
| 944 | tempFiles.getLongIndex() < |
| 945 | tempFiles.getULongIndex())); |
| 946 | } |
| 947 | |
| 948 | if (shortConflict) { |
| 949 | insertDataIntoJavaFile(file, JavaCodeSnippetGen |
| 950 | .addStaticAttributeShortRange(PRIVATE, |
| 951 | tempFiles.getShortIndex() < |
| 952 | tempFiles.getUInt8Index())); |
| 953 | } |
| 954 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 955 | insertDataIntoJavaFile(file, getDataFromTempFileHandle( |
| 956 | ATTRIBUTES_MASK, getTypeFiles(curNode), path)); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 957 | insertDataIntoJavaFile(file, getSetValueParaForUnionClass()); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /** |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 961 | * Generates class file for type enum. |
| 962 | * |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 963 | * @param file generated file |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 964 | * @param curNode current YANG node |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 965 | * @param imports import list |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 966 | * @return class file for type enum |
| 967 | * @throws IOException when fails to generate class file |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 968 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 969 | public static File generateEnumClassFile(File file, YangNode curNode, List<String> imports) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 970 | throws IOException { |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 971 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 972 | JavaFileInfoTranslator fileInfo = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 973 | ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 974 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 975 | String className = fileInfo.getJavaName(); |
| 976 | String path = fileInfo.getBaseCodeGenPath() + |
| 977 | fileInfo.getPackageFilePath(); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 978 | TempJavaEnumerationFragmentFiles enumFile = ((JavaCodeGeneratorInfo) |
| 979 | curNode).getTempJavaCodeFragmentFiles().getEnumTempFiles(); |
| 980 | JavaImportData data = enumFile.getJavaImportData(); |
| 981 | if (!enumFile.isEnumClass()) { |
| 982 | imports.add(data.getImportForToBitSet()); |
| 983 | imports.add(data.getImportForPattern()); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 984 | } |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 985 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 986 | initiateJavaFileGeneration(file, getCapitalCase(className), |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 987 | GENERATE_ENUM_CLASS, imports, path); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 988 | |
| 989 | //Add attribute strings. |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 990 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 991 | JavaCodeGeneratorInfo javaGenInfo = (JavaCodeGeneratorInfo) curNode; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 992 | String[] remove = {COMMA, NEW_LINE}; |
| 993 | insertDataIntoJavaFile(file, trimAtLast(getDataFromTempFileHandle( |
| 994 | ENUM_IMPL_MASK, javaGenInfo.getTempJavaCodeFragmentFiles() |
| 995 | .getEnumTempFiles(), path), remove) + |
| 996 | signatureClose()); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 997 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 998 | throw new IOException(getErrorMsg(getCapitalCase(className), |
| 999 | ENUM_CLASS)); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 1000 | } |
| 1001 | |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 1002 | insertDataIntoJavaFile(file, NEW_LINE); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 1003 | // Add an attribute to get the enum's values. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1004 | insertDataIntoJavaFile(file, getEnumsValueAttribute(className)); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 1005 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 1006 | // Add a constructor for enum. |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1007 | //TODO: generate javadoc for method. |
| 1008 | insertDataIntoJavaFile(file, getEnumsConstructor(getCapitalCase(className)) + |
| 1009 | NEW_LINE); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1010 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1011 | insertDataIntoJavaFile(file, |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1012 | getEnumsOfValueMethod(className, |
| 1013 | (YangEnumeration) curNode, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1014 | ENUM_METHOD_INT_VALUE)); |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 1015 | insertDataIntoJavaFile(file, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1016 | getEnumsOfValueMethod(className, |
| 1017 | (YangEnumeration) curNode, |
| 1018 | ENUM_METHOD_STRING_VALUE)); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 1019 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 1020 | // Add a getter method for enum. |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1021 | insertDataIntoJavaFile(file, getJavaDoc(GETTER_METHOD, className, false, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1022 | null) + |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1023 | getGetter(INT, className, GENERATE_ENUM_CLASS) + NEW_LINE); |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 1024 | |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 1025 | if (!enumFile.isEnumClass()) { |
| 1026 | insertDataIntoJavaFile(file, getBitSetEnumClassFromString |
| 1027 | (getCapitalCase(className))); |
| 1028 | insertDataIntoJavaFile(file, getBitSetEnumClassToString( |
| 1029 | getCapitalCase(className), (YangEnumeration) curNode)); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 1030 | } else { |
| 1031 | insertDataIntoJavaFile(file, getToStringForEnumClass()); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1032 | } |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 1033 | |
| 1034 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
| 1035 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1036 | return validateLineLength(file); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 1037 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1038 | |
| 1039 | /** |
| 1040 | * Generates interface file for rpc. |
| 1041 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 1042 | * @param file generated file |
| 1043 | * @param curNode current YANG node |
| 1044 | * @param imports imports for file |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1045 | * @return rpc class file |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1046 | * @throws IOException when fails to generate class file |
| 1047 | */ |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1048 | public static File generateServiceInterfaceFile(File file, YangNode curNode, |
| 1049 | List<String> imports) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1050 | throws IOException { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1051 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1052 | JavaFileInfoTranslator fileInfo = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1053 | ((JavaFileInfoContainer) curNode).getJavaFileInfo(); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1054 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1055 | TempJavaServiceFragmentFiles tempFiles = |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1056 | ((JavaCodeGeneratorInfo) curNode) |
| 1057 | .getTempJavaCodeFragmentFiles().getServiceTempFiles(); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1058 | String className = getCapitalCase(fileInfo.getJavaName()) + |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1059 | SERVICE_METHOD_STRING; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1060 | String path = fileInfo.getBaseCodeGenPath() + |
| 1061 | fileInfo.getPackageFilePath(); |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1062 | initiateJavaFileGeneration(file, GENERATE_SERVICE_AND_MANAGER, imports, |
| 1063 | curNode, className); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1064 | |
| 1065 | List<String> methods = new ArrayList<>(); |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1066 | JavaAttributeInfo rootAttribute = |
| 1067 | getCurNodeAsAttributeInTarget(curNode, curNode, false, |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1068 | tempFiles); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1069 | |
| 1070 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 1071 | |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 1072 | if (isGetSetOfRootNodeRequired(curNode)) { |
| 1073 | //Getter methods. |
| 1074 | methods.add(getGetterString(rootAttribute, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 1075 | GENERATE_SERVICE_AND_MANAGER) + |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 1076 | NEW_LINE); |
| 1077 | // Setter methods. |
| 1078 | methods.add(getSetterString(rootAttribute, className, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 1079 | GENERATE_SERVICE_AND_MANAGER) + |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 1080 | NEW_LINE); |
| 1081 | } |
| 1082 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1083 | methods.add(getAugmentsDataMethodForService(curNode)); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 1084 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1085 | if (((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles() |
| 1086 | .getServiceTempFiles() != null) { |
| 1087 | JavaCodeGeneratorInfo javaGenInfo = |
| 1088 | (JavaCodeGeneratorInfo) curNode; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 1089 | |
| 1090 | // Rpc methods |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1091 | methods.add(getDataFromTempFileHandle( |
| 1092 | RPC_INTERFACE_MASK, |
| 1093 | javaGenInfo.getTempJavaCodeFragmentFiles() |
| 1094 | .getServiceTempFiles(), path)); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 1095 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1096 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1097 | throw new IOException(getErrorMsg(className, RPC_CLASS)); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | for (String method : methods) { |
| 1101 | insertDataIntoJavaFile(file, method); |
| 1102 | } |
| 1103 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
| 1104 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1105 | return validateLineLength(file); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 1106 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1107 | |
| 1108 | /** |
| 1109 | * Generates event file. |
| 1110 | * |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1111 | * @param file generated file |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1112 | * @param curNode current YANG node |
| 1113 | * @param imports imports for file |
| 1114 | * @throws IOException when fails to generate class file |
| 1115 | */ |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1116 | public static void generateEventFile(File file, YangNode curNode, |
| 1117 | List<String> imports) |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 1118 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1119 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1120 | String className = |
| 1121 | getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo() |
| 1122 | .getJavaName()) + EVENT_STRING; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1123 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1124 | TempJavaEventFragmentFiles tempFiles = |
| 1125 | ((TempJavaCodeFragmentFilesContainer) curNode) |
| 1126 | .getTempJavaCodeFragmentFiles().getEventFragmentFiles(); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1127 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1128 | String path = ((JavaFileInfoContainer) curNode).getJavaFileInfo() |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1129 | .getBaseCodeGenPath() + |
| 1130 | ((JavaFileInfoContainer) curNode).getJavaFileInfo() |
| 1131 | .getPackageFilePath(); |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1132 | initiateJavaFileGeneration(file, GENERATE_EVENT_CLASS, imports, curNode, |
| 1133 | className); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1134 | try { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1135 | insertDataIntoJavaFile(file, getEventEnumTypeStart() + |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1136 | trimAtLast(getDataFromTempFileHandle(EVENT_ENUM_MASK, |
| 1137 | tempFiles, path), |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1138 | COMMA) + methodClose(FOUR_SPACE)); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1139 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1140 | insertDataIntoJavaFile(file, |
| 1141 | getDataFromTempFileHandle(EVENT_METHOD_MASK, |
| 1142 | tempFiles, path)); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1143 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1144 | throw new IOException(getErrorMsg(className, EVENT_CLASS)); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
| 1148 | validateLineLength(file); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * Generates event listener file. |
| 1153 | * |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1154 | * @param file generated file |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1155 | * @param curNode current YANG node |
| 1156 | * @param imports imports for file |
| 1157 | * @throws IOException when fails to generate class file |
| 1158 | */ |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1159 | public static void generateEventListenerFile(File file, YangNode curNode, |
| 1160 | List<String> imports) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1161 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1162 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1163 | String className = |
| 1164 | getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo() |
| 1165 | .getJavaName()) + EVENT_LISTENER_STRING; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1166 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1167 | initiateJavaFileGeneration(file, GENERATE_EVENT_LISTENER_INTERFACE, |
| 1168 | imports, curNode, className); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1169 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1170 | validateLineLength(file); |
| 1171 | } |
| 1172 | |
| 1173 | /** |
| 1174 | * Generates event subject's file. |
| 1175 | * |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 1176 | * @param file file handle |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1177 | * @param curNode current YANG node |
| 1178 | * @throws IOException when fails to do IO exceptions |
| 1179 | */ |
| 1180 | public static void generateEventSubjectFile(File file, YangNode curNode) |
| 1181 | throws IOException { |
| 1182 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1183 | String className = |
| 1184 | getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo() |
| 1185 | .getJavaName()) + EVENT_SUBJECT_NAME_SUFFIX; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1186 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1187 | initiateJavaFileGeneration(file, GENERATE_EVENT_SUBJECT_CLASS, null, |
| 1188 | curNode, className); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1189 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1190 | String path = ((JavaFileInfoContainer) curNode).getJavaFileInfo() |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1191 | .getBaseCodeGenPath() + |
| 1192 | ((JavaFileInfoContainer) curNode).getJavaFileInfo() |
| 1193 | .getPackageFilePath(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 1194 | |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 1195 | TempJavaEventFragmentFiles tempFiles = |
| 1196 | ((TempJavaCodeFragmentFilesContainer) curNode) |
| 1197 | .getTempJavaCodeFragmentFiles().getEventFragmentFiles(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1198 | |
| 1199 | insertDataIntoJavaFile(file, NEW_LINE); |
| 1200 | try { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1201 | insertDataIntoJavaFile( |
| 1202 | file, getDataFromTempFileHandle(EVENT_SUBJECT_ATTRIBUTE_MASK, |
| 1203 | tempFiles, path)); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1204 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1205 | insertDataIntoJavaFile( |
| 1206 | file, getDataFromTempFileHandle(EVENT_SUBJECT_GETTER_MASK, |
| 1207 | tempFiles, path)); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1208 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 1209 | insertDataIntoJavaFile( |
| 1210 | file, getDataFromTempFileHandle(EVENT_SUBJECT_SETTER_MASK, |
| 1211 | tempFiles, path)); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1212 | } catch (IOException e) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1213 | throw new IOException(getErrorMsg(className, EVENT_CLASS)); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE); |
| 1217 | validateLineLength(file); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1218 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1219 | |
| 1220 | /** |
| 1221 | * Returns error message string. |
| 1222 | * |
| 1223 | * @param className name of the class |
| 1224 | * @param fileType type of file |
| 1225 | * @return error message string |
| 1226 | */ |
| 1227 | private static String getErrorMsg(String className, String fileType) { |
| 1228 | return "No data found in temporary java code fragment files for " + |
| 1229 | className + " while " + fileType + " file generation."; |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * Checks whether leaf is present in YANG leaves holder. |
| 1234 | * |
| 1235 | * @param holder holder of YANG leaves |
| 1236 | * @return true if leaves are present, false otherwise |
| 1237 | */ |
| 1238 | private static boolean leavesPresent(YangLeavesHolder holder) { |
| 1239 | return holder.getListOfLeaf() != null && |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 1240 | !holder.getListOfLeaf().isEmpty(); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1241 | } |
Mahesh Poojary Huawei | 1025a17 | 2016-09-27 16:06:07 +0530 | [diff] [blame] | 1242 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 1243 | } |