Gaurav Agrawal | 1c8f80c | 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 | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 17 | package org.onosproject.yangutils.translator.tojava; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 18 | |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 19 | import java.io.File; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 20 | import java.io.IOException; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangAugment; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangCase; |
| 27 | import org.onosproject.yangutils.datamodel.YangChoice; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangModule; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangSubModule; |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.datamodel.YangTypeHolder; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.datamodel.utils.DataModelUtils; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment; |
| 38 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration; |
| 39 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule; |
| 40 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 41 | import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 42 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent; |
| 44 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage; |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.UtilConstants.MANAGER; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO; |
| 51 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase; |
| 53 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 54 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Represents utility class for YANG java model. |
| 58 | */ |
| 59 | public final class YangJavaModelUtils { |
| 60 | |
| 61 | /** |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 62 | * Creates an instance of YANG java model utility. |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 63 | */ |
| 64 | private YangJavaModelUtils() { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Updates YANG java file package information. |
| 69 | * |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 70 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 71 | * @param yangPluginConfig YANG plugin config |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 72 | * @throws IOException IO operations fails |
| 73 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 74 | public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 75 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 76 | throws IOException { |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 77 | if (javaCodeGeneratorInfo instanceof YangJavaAugment) { |
| 78 | javaCodeGeneratorInfo.getJavaFileInfo() |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 79 | .setJavaName(getAugmentClassName((YangJavaAugment) javaCodeGeneratorInfo, |
| 80 | yangPluginConfig)); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 81 | } else { |
| 82 | javaCodeGeneratorInfo.getJavaFileInfo() |
| 83 | .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), |
| 84 | yangPluginConfig.getConflictResolver())); |
| 85 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 86 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo)); |
| 87 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 88 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 89 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir()); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 90 | javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Updates YANG java file package information for specified package. |
| 95 | * |
| 96 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 97 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 98 | * @throws IOException IO operations fails |
| 99 | */ |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 100 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 101 | String pkg) throws IOException { |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 102 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 4a6711a | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 103 | .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), |
| 104 | yangPlugin.getConflictResolver())); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 105 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg); |
| 106 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 107 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 108 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getManagerCodeGenDir()); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 109 | javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Updates temporary java code fragment files. |
| 114 | * |
| 115 | * @param javaCodeGeneratorInfo YANG java file info node |
| 116 | * @throws IOException IO operations fails |
| 117 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 118 | private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) |
| 119 | throws IOException { |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 120 | javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles( |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 121 | new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo())); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Updates leaf information in temporary java code fragment files. |
| 126 | * |
| 127 | * @param javaCodeGeneratorInfo YANG java file info node |
| 128 | * @throws IOException IO operations fails |
| 129 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 130 | private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 131 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 132 | throws IOException { |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 133 | if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 134 | /* |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 135 | * Module / sub module node code generation. |
| 136 | */ |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 137 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 138 | .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles( |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 139 | (YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 140 | if (javaCodeGeneratorInfo instanceof YangJavaModule) { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 141 | if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) { |
| 142 | updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 143 | } |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 144 | } else if (javaCodeGeneratorInfo instanceof YangJavaSubModule) { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 145 | if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) { |
| 146 | updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 147 | } |
| 148 | } |
| 149 | |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame^] | 150 | } |
| 151 | if (javaCodeGeneratorInfo instanceof YangLeavesHolder) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 152 | /* |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 153 | * Container |
| 154 | * Case |
| 155 | * Grouping |
| 156 | * Input |
| 157 | * List |
| 158 | * Notification |
| 159 | * Output |
| 160 | */ |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 161 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 162 | .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles( |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 163 | (YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 164 | } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 165 | /* |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 166 | * Typedef |
| 167 | * Union |
| 168 | */ |
| 169 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 170 | .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig); |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 171 | } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 172 | /* |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 173 | * Enumeration |
| 174 | */ |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 175 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles() |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 176 | .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
| 177 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 178 | } else if (javaCodeGeneratorInfo instanceof YangChoice) { |
| 179 | /*Do nothing, only the interface needs to be generated*/ |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 180 | } else { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 181 | throw new TranslatorException("Unsupported Node Translation"); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Process generate code entry of YANG node. |
| 187 | * |
| 188 | * @param javaCodeGeneratorInfo YANG java file info node |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 189 | * @param yangPluginConfig plugin configurations |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 190 | * @throws IOException IO operations fails |
| 191 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 192 | private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame^] | 193 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 194 | throws IOException { |
| 195 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 196 | throw new TranslatorException("translation is not supported for the node"); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 197 | } |
| 198 | createTempFragmentFile(javaCodeGeneratorInfo); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 199 | updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 200 | |
| 201 | } |
| 202 | |
| 203 | /** |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 204 | * Updates notification node info in service temporary file. |
| 205 | * |
| 206 | * @param javaCodeGeneratorInfo java code generator info |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 207 | * @param yangPluginConfig plugin configurations |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 208 | * @throws IOException when fails to do IO operations |
| 209 | */ |
| 210 | private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 211 | YangPluginConfig yangPluginConfig) |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 212 | throws IOException { |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 213 | if (javaCodeGeneratorInfo instanceof YangJavaModule) { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 214 | for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) { |
| 215 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 216 | .getEventFragmentFiles() |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 217 | .addJavaSnippetOfEvent(notificaiton, yangPluginConfig); |
| 218 | } |
| 219 | } |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 220 | if (javaCodeGeneratorInfo instanceof YangJavaSubModule) { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 221 | for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo) |
| 222 | .getNotificationNodes()) { |
| 223 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 224 | .getEventFragmentFiles() |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 225 | .addJavaSnippetOfEvent(notificaiton, yangPluginConfig); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 231 | * Generates code for the current ata model node and adds itself as an attribute in the parent. |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 232 | * |
| 233 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 234 | * @param yangPlugin YANG plugin config |
| 235 | * @param isMultiInstance flag to indicate whether it's a list |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 236 | * @throws IOException IO operations fails |
| 237 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 238 | public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 239 | YangPluginConfig yangPlugin, boolean isMultiInstance) |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 240 | throws IOException { |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 241 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 242 | throw new TranslatorException("Invalid node for translation"); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 243 | } |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 244 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 245 | /* |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 246 | * Generate the Java files corresponding to the current node. |
| 247 | */ |
| 248 | generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin); |
| 249 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 250 | /* |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 251 | * Update the current nodes info in its parent nodes generated files. |
| 252 | */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 253 | addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 257 | * Generates code for the current data model node and adds support for it to be augmented. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 258 | * |
| 259 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 260 | * @param yangPlugin YANG plugin config |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 261 | * @throws IOException IO operations fails |
| 262 | */ |
| 263 | public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 264 | YangPluginConfig yangPlugin) |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 265 | throws IOException { |
| 266 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 267 | throw new TranslatorException("invalid node for translation"); |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 268 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 269 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 270 | generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 271 | TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles(); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 272 | if (javaCodeGeneratorInfo instanceof YangJavaAugment) { |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 273 | |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 274 | JavaQualifiedTypeInfo yangAugmentedInfo = new JavaQualifiedTypeInfo(); |
| 275 | yangAugmentedInfo.setClassInfo(YANG_AUGMENTED_INFO); |
| 276 | yangAugmentedInfo.setPkgInfo(YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG); |
| 277 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder() |
| 278 | .addToExtendsList(yangAugmentedInfo, (YangNode) javaCodeGeneratorInfo, |
| 279 | tempJavaCodeFragmentFiles.getBeanTempFiles()); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 280 | } |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 281 | if (javaCodeGeneratorInfo instanceof YangCase) { |
| 282 | YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent(); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 283 | JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo(); |
Vidyashree Rama | 210c01d | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 284 | String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName()); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 285 | String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage(); |
| 286 | parentsInfo.setClassInfo(parentName); |
| 287 | parentsInfo.setPkgInfo(parentPkg); |
| 288 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder() |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 289 | .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo, |
| 290 | tempJavaCodeFragmentFiles.getBeanTempFiles()); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 291 | |
| 292 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles() |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 293 | .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 294 | |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 295 | } |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 299 | * Generates code for the current data model node. |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 300 | * |
| 301 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 302 | * @param yangPluginConfig YANG plugin config |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 303 | * @throws IOException IO operations fails |
| 304 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 305 | public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 306 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 307 | throws IOException { |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 308 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
sangyun-han | d45d42c | 2016-05-30 20:13:13 +0900 | [diff] [blame] | 309 | throw new TranslatorException("invalid node for translation"); |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 310 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 311 | updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 312 | generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 316 | * Generates code for the root module/sub-module node. |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 317 | * |
| 318 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 319 | * @param yangPluginConfig YANG plugin config |
| 320 | * @param rootPkg package of the root node |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 321 | * @throws IOException IO operations fails |
| 322 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 323 | public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 324 | YangPluginConfig yangPluginConfig, String rootPkg) |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 325 | throws IOException { |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 326 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
sangyun-han | d45d42c | 2016-05-30 20:13:13 +0900 | [diff] [blame] | 327 | throw new TranslatorException("invalid node for translation"); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 328 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 329 | updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg); |
| 330 | |
| 331 | if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) { |
| 332 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER); |
| 333 | } |
| 334 | |
| 335 | generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 336 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 337 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 338 | /** |
| 339 | * Returns the node package string. |
| 340 | * |
| 341 | * @param curNode current java node whose package string needs to be set |
| 342 | * @return returns the root package string |
| 343 | */ |
| 344 | public static String getCurNodePackage(YangNode curNode) { |
| 345 | |
| 346 | String pkg; |
| 347 | if (!(curNode instanceof JavaFileInfoContainer) |
| 348 | || curNode.getParent() == null) { |
| 349 | throw new TranslatorException("missing parent node to get current node's package"); |
| 350 | } |
| 351 | |
| 352 | YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode); |
| 353 | if (!(parentNode instanceof JavaFileInfoContainer)) { |
| 354 | throw new TranslatorException("missing parent java node to get current node's package"); |
| 355 | } |
| 356 | JavaFileInfo parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo(); |
| 357 | pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName(); |
| 358 | return pkg.toLowerCase(); |
| 359 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Returns true if root node contains any data node. |
| 363 | * |
| 364 | * @param node root YANG node |
| 365 | * @return true if root node contains any data node |
| 366 | */ |
| 367 | public static boolean isManagerCodeGenRequired(YangNode node) { |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 368 | |
| 369 | List<YangNode> childNodes = new ArrayList<>(); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 370 | node = node.getChild(); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 371 | while (node != null) { |
| 372 | childNodes.add(node); |
| 373 | node = node.getNextSibling(); |
| 374 | } |
| 375 | |
| 376 | if (childNodes.size() == 0) { |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 377 | return false; |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 378 | } else if (childNodes.size() == 1) { |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 379 | return !(childNodes.get(0) instanceof YangTranslatorOperatorNode); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 380 | } |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 381 | List<Boolean> booleanData = new ArrayList<>(); |
| 382 | for (YangNode child : childNodes) { |
| 383 | if (child instanceof YangTranslatorOperatorNode) { |
| 384 | booleanData.add(false); |
| 385 | } else { |
| 386 | booleanData.add(true); |
| 387 | } |
| 388 | } |
| 389 | return booleanData.contains(true); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Return false if files are already present. |
| 394 | * |
| 395 | * @param info java file info |
| 396 | * @return false if files already present |
| 397 | */ |
| 398 | public static boolean isGenerationOfCodeReq(JavaFileInfo info) { |
| 399 | File codeGenDir = new File(info.getBaseCodeGenPath() |
| 400 | + info.getPackageFilePath()); |
| 401 | File[] files = codeGenDir.listFiles(); |
| 402 | if (files.length >= 1) { |
| 403 | for (File file : files) { |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 404 | if (file.getName().contentEquals(getCapitalCase(info.getJavaName() + MANAGER + ".java"))) { |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 405 | return false; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | return true; |
| 410 | } |
| 411 | |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 412 | /** |
| 413 | * Returns augmented nodes package. |
| 414 | * |
| 415 | * @param node augmented node |
| 416 | * @param yangPluginConfig plugin config |
| 417 | * @return java package |
| 418 | */ |
| 419 | public static String getAugmentedNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) { |
| 420 | |
| 421 | List<String> clsInfo = new ArrayList<>(); |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 422 | while (node.getParent() != null) { |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 423 | if (!(node instanceof YangModule) |
| 424 | || !(node instanceof YangSubModule)) { |
| 425 | if (node instanceof YangJavaAugment) { |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 426 | clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig)); |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 427 | } else { |
| 428 | clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver())); |
| 429 | } |
| 430 | } |
| 431 | if (node instanceof YangJavaModule |
| 432 | || node instanceof YangJavaSubModule) { |
| 433 | break; |
| 434 | } |
| 435 | node = node.getParent(); |
| 436 | } |
| 437 | |
| 438 | StringBuilder pkg = new StringBuilder(); |
| 439 | if (node instanceof YangJavaModule) { |
| 440 | YangJavaModule module = (YangJavaModule) node; |
| 441 | pkg.append(getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module |
| 442 | .getRevision().getRevDate(), yangPluginConfig.getConflictResolver())); |
| 443 | } else if (node instanceof YangJavaSubModule) { |
| 444 | YangJavaSubModule submodule = (YangJavaSubModule) node; |
| 445 | pkg.append(getRootPackage(submodule.getVersion(), |
| 446 | submodule.getNameSpaceFromModule(submodule.getBelongsTo()), |
| 447 | submodule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver())); |
| 448 | } |
| 449 | for (int i = 1; i <= clsInfo.size(); i++) { |
| 450 | pkg.append("." + clsInfo.get(clsInfo.size() - i)); |
| 451 | } |
| 452 | |
| 453 | return pkg.toString().toLowerCase(); |
| 454 | |
| 455 | } |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * Returns augment class name. |
| 459 | * |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame^] | 460 | * @param augment YANG augment |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 461 | * @param yangPluginConfig plugin configurations |
| 462 | * @return augment class name |
| 463 | */ |
| 464 | public static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) { |
| 465 | YangNodeIdentifier nodeId = augment.getTargetNode().get(augment.getTargetNode().size() - 1).getNodeIdentifier(); |
| 466 | String name = getCapitalCase(getCamelCase(nodeId.getName(), yangPluginConfig.getConflictResolver())); |
| 467 | if (nodeId.getPrefix() != null) { |
| 468 | return AUGMENTED + getCapitalCase(nodeId.getPrefix()) + name; |
| 469 | } else { |
| 470 | return AUGMENTED + name; |
| 471 | } |
| 472 | } |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 473 | } |