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