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