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