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