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