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 | |
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) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 107 | javaGenName = ((YangJavaInputTranslator) info).getParent().getName() + |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 108 | INPUT_KEYWORD; |
| 109 | } else if (info instanceof YangJavaOutputTranslator) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 110 | javaGenName = ((YangJavaOutputTranslator) info).getParent().getName() + |
janani b | 3c396f0 | 2016-09-27 18:45:23 +0530 | [diff] [blame] | 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 | if (info instanceof YangJavaModuleTranslator) { |
| 224 | if (!((YangJavaModuleTranslator) info).getNotificationNodes() |
| 225 | .isEmpty()) { |
| 226 | updateNotificationNodeInfo(info, config); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 227 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 228 | } else if (info instanceof YangJavaSubModuleTranslator) { |
| 229 | if (!((YangJavaSubModuleTranslator) info).getNotificationNodes() |
| 230 | .isEmpty()) { |
| 231 | updateNotificationNodeInfo(info, config); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 232 | } |
| 233 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 234 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 235 | if (info instanceof YangLeavesHolder) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 236 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 237 | * Container |
| 238 | * Case |
| 239 | * Grouping |
| 240 | * Input |
| 241 | * List |
| 242 | * Notification |
| 243 | * Output |
| 244 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 245 | getBeanFiles(info).addCurNodeLeavesInfoToTempFiles((YangNode) info, |
| 246 | config); |
| 247 | |
| 248 | // Add operation type as an attribute. |
| 249 | getBeanFiles(info).addOperationTypeToTempFiles((YangNode) info, |
| 250 | config); |
| 251 | } else if (info instanceof YangTypeHolder) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 252 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 253 | * Typedef |
| 254 | * Union |
| 255 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 256 | translator.addTypeInfoToTempFiles((YangTypeHolder) info, config); |
| 257 | } else if (info instanceof YangJavaEnumerationTranslator) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 258 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 259 | * Enumeration |
| 260 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 261 | translator.getEnumTempFiles() |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 262 | .addEnumAttributeToTempFiles((YangNode) info, config); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 263 | } else if (!(info instanceof YangChoice)) { |
| 264 | /*Do nothing, only the interface needs to be generated for choice*/ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 265 | throw new TranslatorException( |
| 266 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 267 | info)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Process generate code entry of YANG node. |
| 273 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 274 | * @param info YANG java file info node |
| 275 | * @param config plugin configurations |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 276 | * @throws IOException IO operations fails |
| 277 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 278 | private static void generateTempFiles(JavaCodeGeneratorInfo info, |
| 279 | YangPluginConfig config) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 280 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 281 | if (!(info instanceof YangNode)) { |
| 282 | throw new TranslatorException( |
| 283 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 284 | info)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 285 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 286 | createTempFragmentFile(info); |
| 287 | updateTempFragmentFiles(info, config); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 291 | * Updates notification node info in service temporary file. |
| 292 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 293 | * @param info java code generator info |
| 294 | * @param config plugin configurations |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 295 | * @throws IOException when fails to do IO operations |
| 296 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 297 | private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo info, |
| 298 | YangPluginConfig config) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 299 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 300 | TempJavaCodeFragmentFiles translator = |
| 301 | info.getTempJavaCodeFragmentFiles(); |
| 302 | if (info instanceof YangJavaModuleTranslator) { |
| 303 | for (YangNode notification : ((YangJavaModuleTranslator) info) |
| 304 | .getNotificationNodes()) { |
| 305 | translator.getEventFragmentFiles() |
| 306 | .addJavaSnippetOfEvent(notification, config); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 307 | } |
| 308 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 309 | if (info instanceof YangJavaSubModuleTranslator) { |
| 310 | for (YangNode notification : ((YangJavaSubModuleTranslator) info) |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 311 | .getNotificationNodes()) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 312 | translator.getEventFragmentFiles() |
| 313 | .addJavaSnippetOfEvent(notification, config); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 319 | * Generates code for the current ata model node and adds itself as an |
| 320 | * attribute in the parent. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 321 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 322 | * @param info YANG java file info node |
| 323 | * @param config YANG plugin config |
| 324 | * @param isMultiInstance flag to indicate whether it's a list |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 325 | * @throws IOException IO operations fails |
| 326 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 327 | public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo info, |
| 328 | YangPluginConfig config, |
| 329 | boolean isMultiInstance) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 330 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 331 | if (!(info instanceof YangNode)) { |
| 332 | throw new TranslatorException( |
| 333 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 334 | info)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 335 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 336 | |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame^] | 337 | /* |
| 338 | * For second level and below cloned nodes code shouldn't be |
| 339 | * generated also they needn't be added in parent, since |
| 340 | * generated code will be under grouping, cloned node is only |
| 341 | * used for YANG namespace. |
| 342 | */ |
| 343 | YangNode n = (YangNode) info; |
| 344 | if (n.getReferredSchema() != null && |
| 345 | !(((YangNode) n.getReferredSchema()).getParent() instanceof |
| 346 | YangGrouping)) { |
| 347 | return; |
| 348 | } |
| 349 | /* |
| 350 | * If first level cloned node, then it needs to be imported in the |
| 351 | * generated code. In case uses under grouping, it would further have |
| 352 | * second level uses, hence needn't add in parent grouping. |
| 353 | */ |
| 354 | YangSchemaNode rn = getRefSchema(info); |
| 355 | if (rn != null) { |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 356 | YangNode parent = ((YangNode) info).getParent(); |
| 357 | if (!(parent instanceof YangGrouping)) { |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame^] | 358 | addCurNodeInfoInParentTempFile((YangNode) rn, isMultiInstance, |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 359 | config, parent); |
| 360 | } |
| 361 | return; |
| 362 | } |
| 363 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 364 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 365 | * Generate the Java files corresponding to the current node. |
| 366 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 367 | generateCodeOfAugmentableNode(info, config); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 368 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 369 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 370 | * Update the current nodes info in its parent nodes generated files. |
| 371 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 372 | addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance, |
| 373 | config); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
Gaurav Agrawal | 9381ebb | 2016-09-29 19:00:33 +0530 | [diff] [blame] | 377 | * Returns referred schema node in case of grouping uses. |
| 378 | * |
| 379 | * @param info YANG java file info node |
| 380 | * @return referred schema node |
| 381 | */ |
| 382 | private static YangSchemaNode getRefSchema(JavaCodeGeneratorInfo info) { |
| 383 | |
| 384 | YangSchemaNode node = ((YangSchemaNode) info); |
| 385 | if (node.getReferredSchema() == null) { |
| 386 | return null; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Obtain last referred node in case grouping is embedded inside |
| 391 | * another grouping. |
| 392 | */ |
| 393 | while (node.getReferredSchema() != null) { |
| 394 | node = node.getReferredSchema(); |
| 395 | } |
| 396 | return node; |
| 397 | } |
| 398 | |
| 399 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 400 | * Generates code for the current data model node and adds support for it to |
| 401 | * be augmented. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 402 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 403 | * @param info YANG java file info node |
| 404 | * @param config YANG plugin config |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 405 | * @throws IOException IO operations fails |
| 406 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 407 | public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info, |
| 408 | YangPluginConfig config) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 409 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 410 | if (!(info instanceof YangNode)) { |
| 411 | throw new TranslatorException( |
| 412 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 413 | info)); |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 414 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 415 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 416 | generateCodeOfNode(info, config); |
| 417 | TempJavaCodeFragmentFiles tempFiles = |
| 418 | info.getTempJavaCodeFragmentFiles(); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 419 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 420 | if (info instanceof YangCase) { |
| 421 | YangNode parent = ((YangCase) info).getParent(); |
| 422 | JavaQualifiedTypeInfoTranslator typeInfo = |
| 423 | getQualifierInfoForCasesParent(parent, config); |
| 424 | getBeanFiles(info).getJavaExtendsListHolder() |
| 425 | .addToExtendsList(typeInfo, (YangNode) info, |
| 426 | tempFiles.getBeanTempFiles()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 427 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 428 | getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info, |
| 429 | config); |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 430 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 434 | * Returns cases parent's qualified info. |
| 435 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 436 | * @param parent parent node |
| 437 | * @param config plugin configuration |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 438 | * @return cases parent's qualified info |
| 439 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 440 | public static JavaQualifiedTypeInfoTranslator |
| 441 | getQualifierInfoForCasesParent(YangNode parent, |
| 442 | YangPluginConfig config) { |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 443 | String parentName; |
| 444 | String parentPkg; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 445 | JavaFileInfoTranslator parentInfo; |
| 446 | if (parent instanceof YangChoice) { |
| 447 | parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo(); |
| 448 | } else { |
| 449 | parent = ((YangAugment) parent).getAugmentedNode(); |
| 450 | parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo(); |
| 451 | } |
| 452 | if (parentInfo.getPackage() != null) { |
| 453 | parentName = getCapitalCase(parentInfo.getJavaName()); |
| 454 | parentPkg = parentInfo.getPackage(); |
| 455 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 456 | parentName = getCapitalCase(getCamelCase(parent.getName(), |
| 457 | config.getConflictResolver())); |
| 458 | parentPkg = getNodesPackage(parent, config); |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 459 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 460 | JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = |
| 461 | new JavaQualifiedTypeInfoTranslator(); |
| 462 | qualifiedTypeInfo.setClassInfo(parentName); |
| 463 | qualifiedTypeInfo.setPkgInfo(parentPkg); |
| 464 | return qualifiedTypeInfo; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 468 | * Generates code for the current data model node. |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 469 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 470 | * @param info YANG java file info node |
| 471 | * @param config YANG plugin config |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 472 | * @throws IOException IO operations fails |
| 473 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 474 | public static void generateCodeOfNode(JavaCodeGeneratorInfo info, |
| 475 | YangPluginConfig config) |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 476 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 477 | if (!(info instanceof YangNode)) { |
| 478 | throw new TranslatorException( |
| 479 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 480 | info)); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 481 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 482 | updatePackageInfo(info, config); |
| 483 | generateTempFiles(info, config); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 487 | * Generates code for the root module/sub-module node. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 488 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 489 | * @param info YANG java file info node |
| 490 | * @param config YANG plugin config |
| 491 | * @param rootPkg package of the root node |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 492 | * @throws IOException IO operations fails |
| 493 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 494 | public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info, |
| 495 | YangPluginConfig config, |
| 496 | String rootPkg) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 497 | throws IOException { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 498 | if (!(info instanceof YangNode)) { |
| 499 | throw new TranslatorException( |
| 500 | getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE, |
| 501 | info)); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 502 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 503 | updatePackageInfo(info, config, rootPkg); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 504 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 505 | if (isRpcChildNodePresent((YangNode) info)) { |
| 506 | info.getJavaFileInfo() |
| 507 | .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 508 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 509 | generateTempFiles(info, config); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 510 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 511 | |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 512 | /** |
| 513 | * Returns the node package string. |
| 514 | * |
| 515 | * @param curNode current java node whose package string needs to be set |
| 516 | * @return returns the root package string |
| 517 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 518 | public static String getCurNodePackage(YangNode curNode) |
| 519 | throws TranslatorException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 520 | |
| 521 | String pkg; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 522 | if (!(curNode instanceof JavaFileInfoContainer) || |
| 523 | curNode.getParent() == null) { |
| 524 | throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode)); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode); |
| 528 | if (!(parentNode instanceof JavaFileInfoContainer)) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 529 | throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE, |
| 530 | curNode)); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 531 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 532 | JavaFileInfoTranslator handle = |
| 533 | ((JavaFileInfoContainer) parentNode).getJavaFileInfo(); |
| 534 | pkg = handle.getPackage() + PERIOD + handle.getJavaName(); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 535 | return pkg.toLowerCase(); |
| 536 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 537 | |
| 538 | /** |
| 539 | * Returns true if root node contains any data node. |
| 540 | * |
| 541 | * @param node root YANG node |
| 542 | * @return true if root node contains any data node |
| 543 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 544 | public static boolean isRootNodesCodeGenRequired(YangNode node) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 545 | return isNodeCodeGenRequired(node, true); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 549 | * Returns true if get/set method of root node are required. |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 550 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 551 | * @param node root node |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 552 | * @return true if get/set method of root node are required |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 553 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 554 | public static boolean isGetSetOfRootNodeRequired(YangNode node) { |
| 555 | return isNodeCodeGenRequired(node, false); |
| 556 | } |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 557 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 558 | /** |
| 559 | * Returns true if either get/set method of root node are required or root |
| 560 | * node contains any data node. This check is done depending on the |
| 561 | * boolean parameter indicating whether check to be performed for root |
| 562 | * node code generation or get/set method generation. |
| 563 | * |
| 564 | * @param node root node |
| 565 | * @param rootNodeGen flag indicating check type |
| 566 | * @return true if check pass, false otherwise |
| 567 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 568 | private static boolean isNodeCodeGenRequired(YangNode node, |
| 569 | boolean rootNodeGen) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 570 | YangLeavesHolder holder = (YangLeavesHolder) node; |
| 571 | |
| 572 | if (!holder.getListOfLeaf().isEmpty()) { |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 573 | return true; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 574 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 575 | if (!holder.getListOfLeafList().isEmpty()) { |
| 576 | return true; |
| 577 | } |
| 578 | node = node.getChild(); |
| 579 | if (node == null) { |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | if (rootNodeGen) { |
| 584 | while (node != null) { |
| 585 | if (!(node instanceof YangTranslatorOperatorNode)) { |
| 586 | return true; |
| 587 | } |
| 588 | node = node.getNextSibling(); |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | while (node != null) { |
| 593 | if (!(node instanceof YangAugment)) { |
| 594 | return true; |
| 595 | } |
| 596 | node = node.getNextSibling(); |
| 597 | } |
| 598 | return false; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /** |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 602 | * Returns nodes package. |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 603 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 604 | * @param node YANG node |
| 605 | * @param config plugin config |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 606 | * @return java package |
| 607 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 608 | public static String getNodesPackage(YangNode node, |
| 609 | YangPluginConfig config) { |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 610 | |
| 611 | List<String> clsInfo = new ArrayList<>(); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 612 | while (node.getParent() != null) { |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 613 | if (node instanceof YangJavaAugmentTranslator) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 614 | clsInfo.add(getAugmentClassName((YangAugment) node, |
| 615 | config)); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 616 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 617 | clsInfo.add(getCamelCase(node.getName(), config |
| 618 | .getConflictResolver())); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 619 | } |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 620 | node = node.getParent(); |
| 621 | } |
| 622 | |
| 623 | StringBuilder pkg = new StringBuilder(); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 624 | if (node instanceof YangJavaModuleTranslator) { |
| 625 | YangJavaModuleTranslator module = (YangJavaModuleTranslator) node; |
Gaurav Agrawal | 493e0a8 | 2016-09-28 18:30:08 +0530 | [diff] [blame] | 626 | pkg.append(getRootPackage(module.getVersion(), |
| 627 | module.getModuleNamespace(), |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 628 | module.getRevision(), |
| 629 | config.getConflictResolver())); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 630 | } else if (node instanceof YangJavaSubModuleTranslator) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 631 | YangJavaSubModuleTranslator subModule = |
| 632 | (YangJavaSubModuleTranslator) node; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 633 | pkg.append(getRootPackage(subModule.getVersion(), |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 634 | subModule.getNameSpaceFromModule(), |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 635 | subModule.getRevision(), |
| 636 | config.getConflictResolver())); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 637 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 638 | pkg.append(EMPTY_STRING); |
| 639 | int size = clsInfo.size(); |
| 640 | for (int i = size - 1; i >= 0; i--) { |
| 641 | pkg.append(PERIOD).append(clsInfo.get(i)); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 642 | } |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 643 | return pkg.toString().toLowerCase(); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 644 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 645 | |
| 646 | /** |
| 647 | * Returns augment class name. |
| 648 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 649 | * @param augment YANG augment |
| 650 | * @param config plugin configurations |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 651 | * @return augment class name |
| 652 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 653 | private static String getAugmentClassName(YangAugment augment, |
| 654 | YangPluginConfig config) { |
| 655 | YangNodeIdentifier identifier = |
| 656 | augment.getTargetNode().get(augment.getTargetNode().size() - 1) |
| 657 | .getNodeIdentifier(); |
| 658 | String name = getCapitalCase(getCamelCase(identifier.getName(), |
| 659 | config.getConflictResolver())); |
| 660 | if (identifier.getPrefix() != null) { |
| 661 | return AUGMENTED + getCapitalCase( |
| 662 | getCamelCase(identifier.getPrefix(), |
| 663 | config.getConflictResolver())) + name; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 664 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 665 | return AUGMENTED + name; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 666 | } |
Gaurav Agrawal | d30f79b | 2016-10-06 00:58:57 +0530 | [diff] [blame^] | 667 | |
| 668 | /** |
| 669 | * Generated java code during exit. |
| 670 | * |
| 671 | * @param type generated file type |
| 672 | * @param node current YANG node |
| 673 | * @throws IOException when fails to generate java files |
| 674 | */ |
| 675 | public static void generateJava(int type, YangNode node) |
| 676 | throws IOException { |
| 677 | /* |
| 678 | * Call for file generation if node is not under uses. |
| 679 | */ |
| 680 | if(node.getReferredSchema() == null) { |
| 681 | ((TempJavaCodeFragmentFilesContainer) node) |
| 682 | .getTempJavaCodeFragmentFiles().generateJavaFile(type, node); |
| 683 | } |
| 684 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 685 | } |