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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import java.io.IOException; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 20 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangAugment; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangAugmentationHolder; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangCase; |
| 25 | import org.onosproject.yangutils.datamodel.YangChoice; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 27 | import org.onosproject.yangutils.datamodel.YangNode; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangTypeHolder; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 30 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 33 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 35 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule; |
| 36 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 37 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS; |
| 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE; |
| 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 42 | 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] | 43 | import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage; |
| 46 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.UtilConstants.PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG; |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Represents utility class for YANG java model. |
| 53 | */ |
| 54 | public final class YangJavaModelUtils { |
| 55 | |
| 56 | /** |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 57 | * Creates an instance of YANG java model utility. |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 58 | */ |
| 59 | private YangJavaModelUtils() { |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Updates YANG java file package information. |
| 64 | * |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 65 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 66 | * @param yangPluginConfig YANG plugin config |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 67 | * @throws IOException IO operations fails |
| 68 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 69 | public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 70 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 71 | throws IOException { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 72 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 73 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 4a6711a | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 74 | .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), |
| 75 | yangPluginConfig.getConflictResolver())); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 76 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo)); |
| 77 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 78 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 79 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir()); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 80 | javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Updates YANG java file package information for specified package. |
| 85 | * |
| 86 | * @param javaCodeGeneratorInfo YANG java file info node |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 87 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 88 | * @throws IOException IO operations fails |
| 89 | */ |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 90 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 91 | String pkg) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 92 | throws IOException { |
| 93 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 4a6711a | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 94 | .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), |
| 95 | yangPlugin.getConflictResolver())); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 96 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg); |
| 97 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 98 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 99 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 100 | javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Updates temporary java code fragment files. |
| 105 | * |
| 106 | * @param javaCodeGeneratorInfo YANG java file info node |
| 107 | * @throws IOException IO operations fails |
| 108 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 109 | private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) |
| 110 | throws IOException { |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 111 | javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles( |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 112 | new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo())); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Updates leaf information in temporary java code fragment files. |
| 117 | * |
| 118 | * @param javaCodeGeneratorInfo YANG java file info node |
| 119 | * @throws IOException IO operations fails |
| 120 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 121 | private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 122 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 123 | throws IOException { |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 124 | if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 125 | /* |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 126 | * Module / sub module node code generation. |
| 127 | */ |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 128 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 129 | .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles( |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 130 | (YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 131 | if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) { |
| 132 | if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) { |
| 133 | updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 134 | } |
| 135 | } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) { |
| 136 | if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) { |
| 137 | updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 138 | } |
| 139 | } |
| 140 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 141 | } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 142 | /* |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 143 | * Container |
| 144 | * Case |
| 145 | * Grouping |
| 146 | * Input |
| 147 | * List |
| 148 | * Notification |
| 149 | * Output |
| 150 | */ |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 151 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 152 | .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles( |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 153 | (YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 154 | } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 155 | /* |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 156 | * Typedef |
| 157 | * Union |
| 158 | */ |
| 159 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 160 | .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig); |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 161 | } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 162 | /* |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 163 | * Enumeration |
| 164 | */ |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 165 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles() |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 166 | .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
| 167 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 168 | } else if (javaCodeGeneratorInfo instanceof YangChoice) { |
| 169 | /*Do nothing, only the interface needs to be generated*/ |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 170 | } else { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 171 | throw new TranslatorException("Unsupported Node Translation"); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Process generate code entry of YANG node. |
| 177 | * |
| 178 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 179 | * @throws IOException IO operations fails |
| 180 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 181 | private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 182 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 183 | throws IOException { |
| 184 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 185 | throw new TranslatorException("translation is not supported for the node"); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 186 | } |
| 187 | createTempFragmentFile(javaCodeGeneratorInfo); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 188 | updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 189 | |
| 190 | } |
| 191 | |
| 192 | /** |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 193 | * Updates notification node info in service temporary file. |
| 194 | * |
| 195 | * @param javaCodeGeneratorInfo java code generator info |
| 196 | * @param yangPluginConfig plugin configurations |
| 197 | * @throws IOException when fails to do IO operations |
| 198 | */ |
| 199 | private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 200 | YangPluginConfig yangPluginConfig) throws IOException { |
| 201 | if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) { |
| 202 | for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) { |
| 203 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 204 | .getServiceTempFiles() |
| 205 | .addJavaSnippetOfEvent(notificaiton, yangPluginConfig); |
| 206 | } |
| 207 | } |
| 208 | if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) { |
| 209 | for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo) |
| 210 | .getNotificationNodes()) { |
| 211 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 212 | .getServiceTempFiles() |
| 213 | .addJavaSnippetOfEvent(notificaiton, yangPluginConfig); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 219 | * 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] | 220 | * |
| 221 | * @param javaCodeGeneratorInfo YANG java file info node |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 222 | * @param yangPlugin YANG plugin config |
| 223 | * @param isMultiInstance flag to indicate whether it's a list |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 224 | * @throws IOException IO operations fails |
| 225 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 226 | public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 227 | YangPluginConfig yangPlugin, boolean isMultiInstance) |
| 228 | throws IOException { |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 229 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 230 | throw new TranslatorException("Invalid node for translation"); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 231 | } |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 232 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 233 | /* |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 234 | * Generate the Java files corresponding to the current node. |
| 235 | */ |
| 236 | generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin); |
| 237 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 238 | /* |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 239 | * Update the current nodes info in its parent nodes generated files. |
| 240 | */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 241 | addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 245 | * 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] | 246 | * |
| 247 | * @param javaCodeGeneratorInfo YANG java file info node |
| 248 | * @param yangPlugin YANG plugin config |
| 249 | * @throws IOException IO operations fails |
| 250 | */ |
| 251 | public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 252 | YangPluginConfig yangPlugin) |
| 253 | throws IOException { |
| 254 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 255 | throw new TranslatorException("invalid node for translation"); |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 256 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 257 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 258 | generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin); |
| 259 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 260 | /* |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 261 | * For augmentation of nodes. |
| 262 | */ |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 263 | if (javaCodeGeneratorInfo instanceof YangAugmentationHolder) { |
| 264 | JavaQualifiedTypeInfo augmentationHoldersInfo = new JavaQualifiedTypeInfo(); |
| 265 | augmentationHoldersInfo.setClassInfo(AUGMENTATION_HOLDER); |
| 266 | augmentationHoldersInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG); |
| 267 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder() |
| 268 | .addToExtendsList(augmentationHoldersInfo, (YangNode) javaCodeGeneratorInfo); |
| 269 | |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 270 | } else if (javaCodeGeneratorInfo instanceof YangAugment) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 271 | JavaQualifiedTypeInfo augmentedInfo = new JavaQualifiedTypeInfo(); |
| 272 | augmentedInfo.setClassInfo(AUGMENTED_INFO); |
| 273 | augmentedInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG); |
| 274 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder() |
| 275 | .addToExtendsList(augmentedInfo, (YangNode) javaCodeGeneratorInfo); |
| 276 | |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 277 | } |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 278 | |
| 279 | if (javaCodeGeneratorInfo instanceof YangCase) { |
| 280 | YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent(); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 281 | JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo(); |
| 282 | String parentName = ((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName(); |
| 283 | String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage(); |
| 284 | parentsInfo.setClassInfo(parentName); |
| 285 | parentsInfo.setPkgInfo(parentPkg); |
| 286 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder() |
| 287 | .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo); |
| 288 | |
| 289 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles() |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 290 | .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 291 | |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 292 | } |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 296 | * Generates code for the current data model node. |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 297 | * |
| 298 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 299 | * @param yangPluginConfig YANG plugin config |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 300 | * @throws IOException IO operations fails |
| 301 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 302 | public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 303 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 304 | throws IOException { |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 305 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 306 | // TODO:throw exception |
| 307 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 308 | updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 309 | generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 313 | * Generates code for the root module/sub-module node. |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 314 | * |
| 315 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 316 | * @param yangPluginConfig YANG plugin config |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 317 | * @param rootPkg package of the root node |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 318 | * @throws IOException IO operations fails |
| 319 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 320 | public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 321 | YangPluginConfig yangPluginConfig, String rootPkg) |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 322 | throws IOException { |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 323 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 324 | // TODO:throw exception |
| 325 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 326 | updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg); |
| 327 | |
| 328 | if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) { |
| 329 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER); |
| 330 | } |
| 331 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 332 | if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) { |
| 333 | if (!((YangJavaModule) javaCodeGeneratorInfo) |
| 334 | .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) { |
| 335 | updateCodeGenInfoForEvent(javaCodeGeneratorInfo); |
| 336 | } |
| 337 | } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) { |
| 338 | if (!((YangJavaSubModule) javaCodeGeneratorInfo) |
| 339 | .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) { |
| 340 | updateCodeGenInfoForEvent(javaCodeGeneratorInfo); |
| 341 | } |
| 342 | } |
| 343 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 344 | generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 345 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 346 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 347 | /*Updates java code generator info with events info.*/ |
| 348 | private static void updateCodeGenInfoForEvent(JavaCodeGeneratorInfo javaCodeGeneratorInfo) { |
| 349 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_SUBJECT_CLASS); |
| 350 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_CLASS); |
| 351 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_LISTENER_INTERFACE); |
| 352 | } |
| 353 | |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 354 | } |