Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 17 | package org.onosproject.yangutils.translator.tojava; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 18 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangAtomicPath; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangAugment; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangCase; |
| 23 | import org.onosproject.yangutils.datamodel.YangChoice; |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangGrouping; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 26 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangTypeHolder; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.utils.DataModelUtils; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator; |
| 34 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator; |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaInputTranslator; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator; |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaOutputTranslator; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator; |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 40 | |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 41 | import java.io.File; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 42 | import java.io.IOException; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
| 45 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent; |
| 47 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 49 | import static org.onosproject.yangutils.translator.tojava.utils.IndentationType.FOUR_SPACE; |
| 50 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 52 | import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.methodClose; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_NODE; |
| 54 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_PARENT_NODE; |
| 55 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_TRANSLATION_NODE; |
| 56 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getBeanFiles; |
| 57 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsg; |
| 58 | import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsgForCodeGenerator; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 59 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 60 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 61 | import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN; |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.utils.UtilConstants.INPUT_KEYWORD; |
| 63 | import static org.onosproject.yangutils.utils.UtilConstants.OUTPUT_KEYWORD; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 65 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 66 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase; |
| 67 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 68 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 69 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 71 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Represents utility class for YANG java model. |
| 75 | */ |
| 76 | public final class YangJavaModelUtils { |
| 77 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 78 | // No instantiation. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 79 | private YangJavaModelUtils() { |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Updates YANG java file package information. |
| 84 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 85 | * @param info YANG java file info node |
| 86 | * @param config YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 87 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 88 | public static void updatePackageInfo(JavaCodeGeneratorInfo info, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 89 | YangPluginConfig config) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 90 | |
| 91 | JavaFileInfoTranslator translator = info.getJavaFileInfo(); |
| 92 | |
| 93 | if (info instanceof YangJavaAugmentTranslator) { |
| 94 | updatePackageForAugmentInfo(info, config); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 95 | } else { |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 96 | setNodeJavaName(info, config); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 97 | translator.setJavaAttributeName(info.getJavaFileInfo() |
| 98 | .getJavaName()); |
| 99 | translator.setPackage(getCurNodePackage((YangNode) info)); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 100 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 101 | updateCommonPackageInfo(translator, info, config); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 105 | * The java name for input, output is prefixed with rpc name and other |
| 106 | * nodes are set by taking its own name from YANG. |
| 107 | * |
| 108 | * @param info YANG java file info node |
| 109 | * @param config YANG plugin config |
| 110 | */ |
| 111 | private static void setNodeJavaName(JavaCodeGeneratorInfo info, |
| 112 | YangPluginConfig config) { |
| 113 | String javaGenName; |
| 114 | if (info instanceof YangJavaInputTranslator) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 115 | javaGenName = ((YangJavaInputTranslator) info).getParent().getName() + |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 116 | INPUT_KEYWORD; |
| 117 | } else if (info instanceof YangJavaOutputTranslator) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 118 | javaGenName = ((YangJavaOutputTranslator) info).getParent().getName() + |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 119 | OUTPUT_KEYWORD; |
| 120 | } else { |
| 121 | javaGenName = ((YangNode) info).getName(); |
| 122 | } |
| 123 | info.getJavaFileInfo().setJavaName(getCamelCase( |
| 124 | javaGenName, config.getConflictResolver())); |
| 125 | } |
| 126 | |
| 127 | /** |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 128 | * Updates YANG java file package information. |
| 129 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 130 | * @param info YANG java file info node |
| 131 | * @param config YANG plugin config |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 132 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 133 | private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo info, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 134 | YangPluginConfig config) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 135 | |
| 136 | JavaFileInfoTranslator translator = info.getJavaFileInfo(); |
| 137 | |
| 138 | translator.setJavaName(getAugmentClassName( |
| 139 | (YangJavaAugmentTranslator) info, config)); |
| 140 | translator.setPackage(getAugmentsNodePackage((YangNode) info, config)); |
| 141 | updateCommonPackageInfo(translator, info, config); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 145 | * Returns package for augment node. |
| 146 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 147 | * @param yangNode augment node |
| 148 | * @param config plugin configurations |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 149 | * @return package for augment node |
| 150 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 151 | private static String getAugmentsNodePackage(YangNode yangNode, |
| 152 | YangPluginConfig config) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 153 | YangAugment augment = (YangAugment) yangNode; |
| 154 | StringBuilder augmentPkg = new StringBuilder(); |
| 155 | augmentPkg.append(getCurNodePackage(augment)); |
| 156 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 157 | StringBuilder pkg = new StringBuilder(); |
| 158 | pkg.append(PERIOD); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 159 | for (YangAtomicPath atomicPath : augment.getTargetNode()) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 160 | pkg.append(getCamelCase(atomicPath.getNodeIdentifier().getName(), |
| 161 | config.getConflictResolver())) |
| 162 | .append(PERIOD); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 163 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 164 | augmentPkg.append(trimAtLast(pkg.toString(), PERIOD).toLowerCase()); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 165 | return augmentPkg.toString(); |
| 166 | } |
| 167 | |
| 168 | /** |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 169 | * Updates YANG java file package information for specified package. |
| 170 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 171 | * @param info YANG java file info node |
| 172 | * @param config YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 173 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 174 | private static void updatePackageInfo(JavaCodeGeneratorInfo info, |
| 175 | YangPluginConfig config, |
| 176 | String pkg) { |
| 177 | |
| 178 | JavaFileInfoTranslator translator = info.getJavaFileInfo(); |
| 179 | translator.setJavaName(getCamelCase(((YangNode) info).getName(), |
| 180 | config.getConflictResolver())); |
| 181 | translator.setPackage(pkg); |
| 182 | updateCommonPackageInfo(translator, info, config); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Updates common package information. |
| 187 | * |
| 188 | * @param translator JAVA file info translator |
| 189 | * @param info YANG java file info node |
| 190 | * @param config YANG plugin config |
| 191 | */ |
| 192 | private static void updateCommonPackageInfo(JavaFileInfoTranslator translator, |
| 193 | JavaCodeGeneratorInfo info, |
| 194 | YangPluginConfig config) { |
| 195 | translator.setPackageFilePath(getPackageDirPathFromJavaJPackage( |
| 196 | info.getJavaFileInfo().getPackage())); |
| 197 | translator.setBaseCodeGenPath(config.getCodeGenDir()); |
| 198 | translator.setPluginConfig(config); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Updates temporary java code fragment files. |
| 203 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 204 | * @param info YANG java file info node |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 205 | * @throws IOException IO operations fails |
| 206 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 207 | private static void createTempFragmentFile(JavaCodeGeneratorInfo info) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 208 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 209 | info.setTempJavaCodeFragmentFiles( |
| 210 | new TempJavaCodeFragmentFiles(info.getJavaFileInfo())); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Updates leaf information in temporary java code fragment files. |
| 215 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 216 | * @param info YANG java file info node |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 217 | * @throws IOException IO operations fails |
| 218 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 219 | private static void updateTempFragmentFiles(JavaCodeGeneratorInfo info, |
| 220 | YangPluginConfig config) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 221 | throws IOException { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 222 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 223 | TempJavaCodeFragmentFiles translator = |
| 224 | info.getTempJavaCodeFragmentFiles(); |
| 225 | |
| 226 | if (info instanceof RpcNotificationContainer) { |
| 227 | getBeanFiles(info).setRootNode(true); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 228 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 229 | * Module / sub module node code generation. |
| 230 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 231 | if (info instanceof YangJavaModuleTranslator) { |
| 232 | if (!((YangJavaModuleTranslator) info).getNotificationNodes() |
| 233 | .isEmpty()) { |
| 234 | updateNotificationNodeInfo(info, config); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 235 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 236 | } else if (info instanceof YangJavaSubModuleTranslator) { |
| 237 | if (!((YangJavaSubModuleTranslator) info).getNotificationNodes() |
| 238 | .isEmpty()) { |
| 239 | updateNotificationNodeInfo(info, config); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 240 | } |
| 241 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 242 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 243 | if (info instanceof YangLeavesHolder) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 244 | YangLeavesHolder holder = (YangLeavesHolder) info; |
| 245 | boolean isLeafPresent = holder.getListOfLeaf() != null && !holder |
| 246 | .getListOfLeaf().isEmpty(); |
| 247 | boolean isLeafListPresent = holder.getListOfLeafList() != null && |
| 248 | !holder.getListOfLeafList().isEmpty(); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 249 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 250 | * Container |
| 251 | * Case |
| 252 | * Grouping |
| 253 | * Input |
| 254 | * List |
| 255 | * Notification |
| 256 | * Output |
| 257 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 258 | if (isLeafPresent || isLeafListPresent) { |
| 259 | getBeanFiles(info).addCurNodeLeavesInfoToTempFiles((YangNode) info, |
| 260 | config); |
| 261 | } |
| 262 | //Add value leaf flag attribute to temp file. |
| 263 | if (isLeafPresent) { |
| 264 | getBeanFiles(info).addValueLeafFlag(config, (YangNode) info); |
| 265 | } |
| 266 | if (((YangNode) info).isOpTypeReq()) { |
| 267 | // Add operation type as an attribute. |
| 268 | getBeanFiles(info).addOperationTypeToTempFiles((YangNode) info, |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 269 | config); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 270 | if (isLeafPresent) { |
| 271 | //Add select leaf flag attribute to temp file. |
| 272 | getBeanFiles(info).addSelectLeafFlag(config); |
| 273 | } |
| 274 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 275 | } else if (info instanceof YangTypeHolder) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 276 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 277 | * Typedef |
| 278 | * Union |
| 279 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 280 | translator.addTypeInfoToTempFiles((YangTypeHolder) info, config); |
| 281 | } else if (info instanceof YangJavaEnumerationTranslator) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 282 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 283 | * Enumeration |
| 284 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 285 | translator.getEnumTempFiles() |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 286 | .addEnumAttributeToTempFiles((YangNode) info, config); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 287 | } else if (!(info instanceof YangChoice)) { |
| 288 | /*Do nothing, only the interface needs to be generated for choice*/ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 289 | throw new TranslatorException( |
| 290 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 291 | info)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Process generate code entry of YANG node. |
| 297 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 298 | * @param info YANG java file info node |
| 299 | * @param config plugin configurations |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 300 | * @throws IOException IO operations fails |
| 301 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 302 | private static void generateTempFiles(JavaCodeGeneratorInfo info, |
| 303 | YangPluginConfig config) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 304 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 305 | if (!(info instanceof YangNode)) { |
| 306 | throw new TranslatorException( |
| 307 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 308 | info)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 309 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 310 | createTempFragmentFile(info); |
| 311 | updateTempFragmentFiles(info, config); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 315 | * Updates notification node info in service temporary file. |
| 316 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 317 | * @param info java code generator info |
| 318 | * @param config plugin configurations |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 319 | * @throws IOException when fails to do IO operations |
| 320 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 321 | private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo info, |
| 322 | YangPluginConfig config) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 323 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 324 | TempJavaCodeFragmentFiles translator = |
| 325 | info.getTempJavaCodeFragmentFiles(); |
| 326 | if (info instanceof YangJavaModuleTranslator) { |
| 327 | for (YangNode notification : ((YangJavaModuleTranslator) info) |
| 328 | .getNotificationNodes()) { |
| 329 | translator.getEventFragmentFiles() |
| 330 | .addJavaSnippetOfEvent(notification, config); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 331 | } |
| 332 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 333 | if (info instanceof YangJavaSubModuleTranslator) { |
| 334 | for (YangNode notification : ((YangJavaSubModuleTranslator) info) |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 335 | .getNotificationNodes()) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 336 | translator.getEventFragmentFiles() |
| 337 | .addJavaSnippetOfEvent(notification, config); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 343 | * Generates code for the current ata model node and adds itself as an |
| 344 | * attribute in the parent. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 345 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 346 | * @param info YANG java file info node |
| 347 | * @param config YANG plugin config |
| 348 | * @param isMultiInstance flag to indicate whether it's a list |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 349 | * @throws IOException IO operations fails |
| 350 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 351 | public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo info, |
| 352 | YangPluginConfig config, |
| 353 | boolean isMultiInstance) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 354 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 355 | if (!(info instanceof YangNode)) { |
| 356 | throw new TranslatorException( |
| 357 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 358 | info)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 359 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 360 | |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame] | 361 | /* |
| 362 | * For second level and below cloned nodes code shouldn't be |
| 363 | * generated also they needn't be added in parent, since |
| 364 | * generated code will be under grouping, cloned node is only |
| 365 | * used for YANG namespace. |
| 366 | */ |
| 367 | YangNode n = (YangNode) info; |
| 368 | if (n.getReferredSchema() != null && |
| 369 | !(((YangNode) n.getReferredSchema()).getParent() instanceof |
| 370 | YangGrouping)) { |
| 371 | return; |
| 372 | } |
| 373 | /* |
| 374 | * If first level cloned node, then it needs to be imported in the |
| 375 | * generated code. In case uses under grouping, it would further have |
| 376 | * second level uses, hence needn't add in parent grouping. |
| 377 | */ |
| 378 | YangSchemaNode rn = getRefSchema(info); |
| 379 | if (rn != null) { |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 380 | YangNode parent = ((YangNode) info).getParent(); |
| 381 | if (!(parent instanceof YangGrouping)) { |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame] | 382 | addCurNodeInfoInParentTempFile((YangNode) rn, isMultiInstance, |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 383 | config, parent); |
| 384 | } |
| 385 | return; |
| 386 | } |
| 387 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 388 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 389 | * Generate the Java files corresponding to the current node. |
| 390 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 391 | generateCodeOfAugmentableNode(info, config); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 392 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 393 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 394 | * Update the current nodes info in its parent nodes generated files. |
| 395 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 396 | addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance, |
| 397 | config); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 401 | * Returns referred schema node in case of grouping uses. |
| 402 | * |
| 403 | * @param info YANG java file info node |
| 404 | * @return referred schema node |
| 405 | */ |
| 406 | private static YangSchemaNode getRefSchema(JavaCodeGeneratorInfo info) { |
| 407 | |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 408 | YangSchemaNode node = (YangSchemaNode) info; |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 409 | if (node.getReferredSchema() == null) { |
| 410 | return null; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Obtain last referred node in case grouping is embedded inside |
| 415 | * another grouping. |
| 416 | */ |
| 417 | while (node.getReferredSchema() != null) { |
| 418 | node = node.getReferredSchema(); |
| 419 | } |
| 420 | return node; |
| 421 | } |
| 422 | |
| 423 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 424 | * Generates code for the current data model node and adds support for it to |
| 425 | * be augmented. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 426 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 427 | * @param info YANG java file info node |
| 428 | * @param config YANG plugin config |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 429 | * @throws IOException IO operations fails |
| 430 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 431 | public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info, |
| 432 | YangPluginConfig config) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 433 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 434 | if (!(info instanceof YangNode)) { |
| 435 | throw new TranslatorException( |
| 436 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 437 | info)); |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 438 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 439 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 440 | generateCodeOfNode(info, config); |
| 441 | TempJavaCodeFragmentFiles tempFiles = |
| 442 | info.getTempJavaCodeFragmentFiles(); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 443 | if (!(info instanceof YangChoice)) { |
| 444 | getBeanFiles(info).addYangAugmentedMap(config); |
| 445 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 446 | if (info instanceof YangCase) { |
| 447 | YangNode parent = ((YangCase) info).getParent(); |
| 448 | JavaQualifiedTypeInfoTranslator typeInfo = |
| 449 | getQualifierInfoForCasesParent(parent, config); |
| 450 | getBeanFiles(info).getJavaExtendsListHolder() |
| 451 | .addToExtendsList(typeInfo, (YangNode) info, |
| 452 | tempFiles.getBeanTempFiles()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 453 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 454 | getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info, |
| 455 | config); |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 456 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 460 | * Returns cases parent's qualified info. |
| 461 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 462 | * @param parent parent node |
| 463 | * @param config plugin configuration |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 464 | * @return cases parent's qualified info |
| 465 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 466 | public static JavaQualifiedTypeInfoTranslator |
| 467 | getQualifierInfoForCasesParent(YangNode parent, |
| 468 | YangPluginConfig config) { |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 469 | String parentName; |
| 470 | String parentPkg; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 471 | JavaFileInfoTranslator parentInfo; |
| 472 | if (parent instanceof YangChoice) { |
| 473 | parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo(); |
| 474 | } else { |
| 475 | parent = ((YangAugment) parent).getAugmentedNode(); |
| 476 | parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo(); |
| 477 | } |
| 478 | if (parentInfo.getPackage() != null) { |
| 479 | parentName = getCapitalCase(parentInfo.getJavaName()); |
| 480 | parentPkg = parentInfo.getPackage(); |
| 481 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 482 | parentName = getCapitalCase(getCamelCase(parent.getName(), |
| 483 | config.getConflictResolver())); |
| 484 | parentPkg = getNodesPackage(parent, config); |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 485 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 486 | JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = |
| 487 | new JavaQualifiedTypeInfoTranslator(); |
| 488 | qualifiedTypeInfo.setClassInfo(parentName); |
| 489 | qualifiedTypeInfo.setPkgInfo(parentPkg); |
| 490 | return qualifiedTypeInfo; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 494 | * Generates code for the current data model node. |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 495 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 496 | * @param info YANG java file info node |
| 497 | * @param config YANG plugin config |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 498 | * @throws IOException IO operations fails |
| 499 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 500 | public static void generateCodeOfNode(JavaCodeGeneratorInfo info, |
| 501 | YangPluginConfig config) |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 502 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 503 | if (!(info instanceof YangNode)) { |
| 504 | throw new TranslatorException( |
| 505 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 506 | info)); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 507 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 508 | updatePackageInfo(info, config); |
| 509 | generateTempFiles(info, config); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 513 | * Generates code for the root module/sub-module node. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 514 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 515 | * @param info YANG java file info node |
| 516 | * @param config YANG plugin config |
| 517 | * @param rootPkg package of the root node |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 518 | * @throws IOException IO operations fails |
| 519 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 520 | public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info, |
| 521 | YangPluginConfig config, |
| 522 | String rootPkg) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 523 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 524 | if (!(info instanceof YangNode)) { |
| 525 | throw new TranslatorException( |
| 526 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 527 | info)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 528 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 529 | updatePackageInfo(info, config, rootPkg); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 530 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 531 | if (isRpcChildNodePresent((YangNode) info)) { |
| 532 | info.getJavaFileInfo() |
| 533 | .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 534 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 535 | generateTempFiles(info, config); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 536 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 537 | |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 538 | /** |
| 539 | * Returns the node package string. |
| 540 | * |
| 541 | * @param curNode current java node whose package string needs to be set |
| 542 | * @return returns the root package string |
| 543 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 544 | public static String getCurNodePackage(YangNode curNode) |
| 545 | throws TranslatorException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 546 | |
| 547 | String pkg; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 548 | if (!(curNode instanceof JavaFileInfoContainer) || |
| 549 | curNode.getParent() == null) { |
| 550 | throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode)); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode); |
| 554 | if (!(parentNode instanceof JavaFileInfoContainer)) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 555 | throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE, |
| 556 | curNode)); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 557 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 558 | JavaFileInfoTranslator handle = |
| 559 | ((JavaFileInfoContainer) parentNode).getJavaFileInfo(); |
| 560 | pkg = handle.getPackage() + PERIOD + handle.getJavaName(); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 561 | return pkg.toLowerCase(); |
| 562 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 563 | |
| 564 | /** |
| 565 | * Returns true if root node contains any data node. |
| 566 | * |
| 567 | * @param node root YANG node |
| 568 | * @return true if root node contains any data node |
| 569 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 570 | public static boolean isRootNodesCodeGenRequired(YangNode node) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 571 | return isNodeCodeGenRequired(node, true); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 575 | * Returns true if get/set method of root node are required. |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 576 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 577 | * @param node root node |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 578 | * @return true if get/set method of root node are required |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 579 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 580 | public static boolean isGetSetOfRootNodeRequired(YangNode node) { |
| 581 | return isNodeCodeGenRequired(node, false); |
| 582 | } |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 583 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 584 | /** |
| 585 | * Returns true if either get/set method of root node are required or root |
| 586 | * node contains any data node. This check is done depending on the |
| 587 | * boolean parameter indicating whether check to be performed for root |
| 588 | * node code generation or get/set method generation. |
| 589 | * |
| 590 | * @param node root node |
| 591 | * @param rootNodeGen flag indicating check type |
| 592 | * @return true if check pass, false otherwise |
| 593 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 594 | private static boolean isNodeCodeGenRequired(YangNode node, |
| 595 | boolean rootNodeGen) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 596 | YangLeavesHolder holder = (YangLeavesHolder) node; |
| 597 | |
| 598 | if (!holder.getListOfLeaf().isEmpty()) { |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 599 | return true; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 600 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 601 | if (!holder.getListOfLeafList().isEmpty()) { |
| 602 | return true; |
| 603 | } |
| 604 | node = node.getChild(); |
| 605 | if (node == null) { |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | if (rootNodeGen) { |
| 610 | while (node != null) { |
| 611 | if (!(node instanceof YangTranslatorOperatorNode)) { |
| 612 | return true; |
| 613 | } |
| 614 | node = node.getNextSibling(); |
| 615 | } |
| 616 | return false; |
| 617 | } |
| 618 | while (node != null) { |
| 619 | if (!(node instanceof YangAugment)) { |
| 620 | return true; |
| 621 | } |
| 622 | node = node.getNextSibling(); |
| 623 | } |
| 624 | return false; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /** |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 628 | * Returns nodes package. |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 629 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 630 | * @param node YANG node |
| 631 | * @param config plugin config |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 632 | * @return java package |
| 633 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 634 | public static String getNodesPackage(YangNode node, |
| 635 | YangPluginConfig config) { |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 636 | |
| 637 | List<String> clsInfo = new ArrayList<>(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 638 | while (node.getParent() != null) { |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 639 | if (node instanceof YangJavaAugmentTranslator) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 640 | clsInfo.add(getAugmentClassName((YangAugment) node, |
| 641 | config)); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 642 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 643 | clsInfo.add(getCamelCase(node.getName(), config |
| 644 | .getConflictResolver())); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 645 | } |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 646 | node = node.getParent(); |
| 647 | } |
| 648 | |
| 649 | StringBuilder pkg = new StringBuilder(); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 650 | if (node instanceof YangJavaModuleTranslator) { |
| 651 | YangJavaModuleTranslator module = (YangJavaModuleTranslator) node; |
Gaurav Agrawal | 493e0a8 | 2016-09-28 18:30:08 +0530 | [diff] [blame] | 652 | pkg.append(getRootPackage(module.getVersion(), |
| 653 | module.getModuleNamespace(), |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 654 | module.getRevision(), |
| 655 | config.getConflictResolver())); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 656 | } else if (node instanceof YangJavaSubModuleTranslator) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 657 | YangJavaSubModuleTranslator subModule = |
| 658 | (YangJavaSubModuleTranslator) node; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 659 | pkg.append(getRootPackage(subModule.getVersion(), |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 660 | subModule.getNameSpaceFromModule(), |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 661 | subModule.getRevision(), |
| 662 | config.getConflictResolver())); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 663 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 664 | pkg.append(EMPTY_STRING); |
| 665 | int size = clsInfo.size(); |
| 666 | for (int i = size - 1; i >= 0; i--) { |
| 667 | pkg.append(PERIOD).append(clsInfo.get(i)); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 668 | } |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 669 | return pkg.toString().toLowerCase(); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 670 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 671 | |
| 672 | /** |
| 673 | * Returns augment class name. |
| 674 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 675 | * @param augment YANG augment |
| 676 | * @param config plugin configurations |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 677 | * @return augment class name |
| 678 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 679 | private static String getAugmentClassName(YangAugment augment, |
| 680 | YangPluginConfig config) { |
| 681 | YangNodeIdentifier identifier = |
| 682 | augment.getTargetNode().get(augment.getTargetNode().size() - 1) |
| 683 | .getNodeIdentifier(); |
| 684 | String name = getCapitalCase(getCamelCase(identifier.getName(), |
| 685 | config.getConflictResolver())); |
| 686 | if (identifier.getPrefix() != null) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 687 | return getCapitalCase(getCamelCase(AUGMENTED + HYPHEN + identifier |
| 688 | .getPrefix(), |
| 689 | config.getConflictResolver())) + name; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 690 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 691 | return AUGMENTED + name; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 692 | } |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame] | 693 | |
| 694 | /** |
| 695 | * Generated java code during exit. |
| 696 | * |
| 697 | * @param type generated file type |
| 698 | * @param node current YANG node |
| 699 | * @throws IOException when fails to generate java files |
| 700 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 701 | public static void generateJava(int type, YangNode node) |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame] | 702 | throws IOException { |
| 703 | /* |
| 704 | * Call for file generation if node is not under uses. |
| 705 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 706 | if (node.getReferredSchema() == null) { |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame] | 707 | ((TempJavaCodeFragmentFilesContainer) node) |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 708 | .getTempJavaCodeFragmentFiles().generateJavaFile(type, node); |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame] | 709 | } |
| 710 | } |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame^] | 711 | |
| 712 | /** |
| 713 | * Generates interface file for those yang file which contains only any |
| 714 | * of these grouping, typedef and identity. |
| 715 | * |
| 716 | * @param rootNode root node |
| 717 | * @throws IOException when fails to do IO operations |
| 718 | */ |
| 719 | public static void generateInterfaceFileForNonDataNodes(YangNode rootNode) throws |
| 720 | IOException { |
| 721 | JavaCodeGeneratorInfo info = (JavaCodeGeneratorInfo) rootNode; |
| 722 | TempJavaCodeFragmentFiles tempFile = info |
| 723 | .getTempJavaCodeFragmentFiles(); |
| 724 | JavaFileInfoTranslator fileInfo = info.getJavaFileInfo(); |
| 725 | File filePath = new File(fileInfo.getBaseCodeGenPath() + fileInfo |
| 726 | .getPackageFilePath()); |
| 727 | String name = getCapitalCase(fileInfo.getJavaName()); |
| 728 | //Add package info file for this. |
| 729 | addPackageInfo(filePath, name, fileInfo.getPackage(), false); |
| 730 | //Generate file handle for this. |
| 731 | File interFace = tempFile.getBeanTempFiles().getJavaFileHandle( |
| 732 | name); |
| 733 | //generate java code for interface file. |
| 734 | validateLineLength(generateInterfaceFile(interFace, null, rootNode, |
| 735 | false)); |
| 736 | insertDataIntoJavaFile(interFace, methodClose(FOUR_SPACE)); |
| 737 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 738 | } |