Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 17 | package org.onosproject.yangutils.translator.tojava.javamodel; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 18 | |
| 19 | import java.io.IOException; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangCase; |
| 22 | import org.onosproject.yangutils.datamodel.YangChoice; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangTypeHolder; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.utils.DataModelUtils; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
| 29 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 34 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS; |
| 37 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE; |
| 38 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 42 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase; |
| 43 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 44 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Represents utility class for YANG java model. |
| 48 | */ |
| 49 | public final class YangJavaModelUtils { |
| 50 | |
| 51 | /** |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 52 | * Creates an instance of YANG java model utility. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 53 | */ |
| 54 | private YangJavaModelUtils() { |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Updates YANG java file package information. |
| 59 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 60 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 61 | * @param yangPluginConfig YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 62 | * @throws IOException IO operations fails |
| 63 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 64 | public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 65 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 66 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 67 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 68 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 69 | .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), |
| 70 | yangPluginConfig.getConflictResolver())); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 71 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo)); |
| 72 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 73 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 74 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir()); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 75 | javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Updates YANG java file package information for specified package. |
| 80 | * |
| 81 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 82 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 83 | * @throws IOException IO operations fails |
| 84 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 85 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 86 | String pkg) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 87 | throws IOException { |
| 88 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 89 | .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), |
| 90 | yangPlugin.getConflictResolver())); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 91 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg); |
| 92 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 93 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 94 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 95 | javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Updates temporary java code fragment files. |
| 100 | * |
| 101 | * @param javaCodeGeneratorInfo YANG java file info node |
| 102 | * @throws IOException IO operations fails |
| 103 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 104 | private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) |
| 105 | throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 106 | javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles( |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 107 | new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo())); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Updates leaf information in temporary java code fragment files. |
| 112 | * |
| 113 | * @param javaCodeGeneratorInfo YANG java file info node |
| 114 | * @throws IOException IO operations fails |
| 115 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 116 | private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 117 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 118 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 119 | if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 120 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 121 | * Module / sub module node code generation. |
| 122 | */ |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 123 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 124 | .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles( |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 125 | (YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 126 | if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) { |
| 127 | if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) { |
| 128 | updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 129 | } |
| 130 | } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) { |
| 131 | if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) { |
| 132 | updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 133 | } |
| 134 | } |
| 135 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 136 | } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 137 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 138 | * Container |
| 139 | * Case |
| 140 | * Grouping |
| 141 | * Input |
| 142 | * List |
| 143 | * Notification |
| 144 | * Output |
| 145 | */ |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 146 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 147 | .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles( |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 148 | (YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 149 | } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 150 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 151 | * Typedef |
| 152 | * Union |
| 153 | */ |
| 154 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 155 | .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 156 | } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 157 | /* |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 158 | * Enumeration |
| 159 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 160 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles() |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 161 | .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig); |
| 162 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 163 | } else if (javaCodeGeneratorInfo instanceof YangChoice) { |
| 164 | /*Do nothing, only the interface needs to be generated*/ |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 165 | } else { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 166 | throw new TranslatorException("Unsupported Node Translation"); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Process generate code entry of YANG node. |
| 172 | * |
| 173 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 174 | * @throws IOException IO operations fails |
| 175 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 176 | private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 177 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 178 | throws IOException { |
| 179 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 180 | throw new TranslatorException("translation is not supported for the node"); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 181 | } |
| 182 | createTempFragmentFile(javaCodeGeneratorInfo); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 183 | updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 184 | |
| 185 | } |
| 186 | |
| 187 | /** |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 188 | * Updates notification node info in service temporary file. |
| 189 | * |
| 190 | * @param javaCodeGeneratorInfo java code generator info |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 191 | * @param yangPluginConfig plugin configurations |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 192 | * @throws IOException when fails to do IO operations |
| 193 | */ |
| 194 | private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 195 | YangPluginConfig yangPluginConfig) |
| 196 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 197 | if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) { |
| 198 | for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) { |
| 199 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 200 | .getServiceTempFiles() |
| 201 | .addJavaSnippetOfEvent(notificaiton, yangPluginConfig); |
| 202 | } |
| 203 | } |
| 204 | if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) { |
| 205 | for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo) |
| 206 | .getNotificationNodes()) { |
| 207 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 208 | .getServiceTempFiles() |
| 209 | .addJavaSnippetOfEvent(notificaiton, yangPluginConfig); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 215 | * Generates code for the current ata model node and adds itself as an attribute in the parent. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 216 | * |
| 217 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 218 | * @param yangPlugin YANG plugin config |
| 219 | * @param isMultiInstance flag to indicate whether it's a list |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 220 | * @throws IOException IO operations fails |
| 221 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 222 | public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 223 | YangPluginConfig yangPlugin, boolean isMultiInstance) |
| 224 | throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 225 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 226 | throw new TranslatorException("Invalid node for translation"); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 227 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 228 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 229 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 230 | * Generate the Java files corresponding to the current node. |
| 231 | */ |
| 232 | generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin); |
| 233 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 234 | /* |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 235 | * Update the current nodes info in its parent nodes generated files. |
| 236 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 237 | addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 241 | * Generates code for the current data model node and adds support for it to be augmented. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 242 | * |
| 243 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 244 | * @param yangPlugin YANG plugin config |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 245 | * @throws IOException IO operations fails |
| 246 | */ |
| 247 | public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 248 | YangPluginConfig yangPlugin) |
| 249 | throws IOException { |
| 250 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 251 | throw new TranslatorException("invalid node for translation"); |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 252 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 253 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 254 | generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin); |
| 255 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 256 | /* |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 257 | TODO: Need to use this, when augmentation is added in YMS |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 258 | * For augmentation of nodes. |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 259 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 260 | if (javaCodeGeneratorInfo instanceof YangAugmentationHolder) { |
| 261 | JavaQualifiedTypeInfo augmentationHoldersInfo = new JavaQualifiedTypeInfo(); |
| 262 | augmentationHoldersInfo.setClassInfo(AUGMENTATION_HOLDER); |
| 263 | augmentationHoldersInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG); |
| 264 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder() |
| 265 | .addToExtendsList(augmentationHoldersInfo, (YangNode) javaCodeGeneratorInfo); |
| 266 | |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 267 | } else if (javaCodeGeneratorInfo instanceof YangAugment) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 268 | JavaQualifiedTypeInfo augmentedInfo = new JavaQualifiedTypeInfo(); |
| 269 | augmentedInfo.setClassInfo(AUGMENTED_INFO); |
| 270 | augmentedInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG); |
| 271 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder() |
| 272 | .addToExtendsList(augmentedInfo, (YangNode) javaCodeGeneratorInfo); |
| 273 | |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 274 | } |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 275 | */ |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 276 | if (javaCodeGeneratorInfo instanceof YangCase) { |
| 277 | YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent(); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 278 | JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo(); |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 279 | String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 280 | String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage(); |
| 281 | parentsInfo.setClassInfo(parentName); |
| 282 | parentsInfo.setPkgInfo(parentPkg); |
| 283 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder() |
| 284 | .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo); |
| 285 | |
| 286 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles() |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 287 | .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 288 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 289 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 293 | * Generates code for the current data model node. |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 294 | * |
| 295 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 296 | * @param yangPluginConfig YANG plugin config |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 297 | * @throws IOException IO operations fails |
| 298 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 299 | public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
| 300 | YangPluginConfig yangPluginConfig) |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 301 | throws IOException { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 302 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
sangyun-han | a60bf4c | 2016-05-30 20:13:13 +0900 | [diff] [blame] | 303 | throw new TranslatorException("invalid node for translation"); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 304 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 305 | updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig); |
| 306 | generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 310 | * Generates code for the root module/sub-module node. |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 311 | * |
| 312 | * @param javaCodeGeneratorInfo YANG java file info node |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 313 | * @param yangPluginConfig YANG plugin config |
| 314 | * @param rootPkg package of the root node |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 315 | * @throws IOException IO operations fails |
| 316 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 317 | public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 318 | YangPluginConfig yangPluginConfig, String rootPkg) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 319 | throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 320 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
sangyun-han | a60bf4c | 2016-05-30 20:13:13 +0900 | [diff] [blame] | 321 | throw new TranslatorException("invalid node for translation"); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 322 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 323 | updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg); |
| 324 | |
| 325 | if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) { |
| 326 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER); |
| 327 | } |
| 328 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 329 | if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) { |
| 330 | if (!((YangJavaModule) javaCodeGeneratorInfo) |
| 331 | .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) { |
| 332 | updateCodeGenInfoForEvent(javaCodeGeneratorInfo); |
| 333 | } |
| 334 | } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) { |
| 335 | if (!((YangJavaSubModule) javaCodeGeneratorInfo) |
| 336 | .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) { |
| 337 | updateCodeGenInfoForEvent(javaCodeGeneratorInfo); |
| 338 | } |
| 339 | } |
| 340 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 341 | generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 342 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 343 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 344 | /*Updates java code generator info with events info.*/ |
| 345 | private static void updateCodeGenInfoForEvent(JavaCodeGeneratorInfo javaCodeGeneratorInfo) { |
| 346 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_SUBJECT_CLASS); |
| 347 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_CLASS); |
| 348 | javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_LISTENER_INTERFACE); |
| 349 | } |
| 350 | |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 351 | /** |
| 352 | * Returns the node package string. |
| 353 | * |
| 354 | * @param curNode current java node whose package string needs to be set |
| 355 | * @return returns the root package string |
| 356 | */ |
| 357 | public static String getCurNodePackage(YangNode curNode) { |
| 358 | |
| 359 | String pkg; |
| 360 | if (!(curNode instanceof JavaFileInfoContainer) |
| 361 | || curNode.getParent() == null) { |
| 362 | throw new TranslatorException("missing parent node to get current node's package"); |
| 363 | } |
| 364 | |
| 365 | YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode); |
| 366 | if (!(parentNode instanceof JavaFileInfoContainer)) { |
| 367 | throw new TranslatorException("missing parent java node to get current node's package"); |
| 368 | } |
| 369 | JavaFileInfo parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo(); |
| 370 | pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName(); |
| 371 | return pkg.toLowerCase(); |
| 372 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 373 | } |