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