Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +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.File; |
| 20 | import java.io.IOException; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
| 25 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 29 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
| 30 | import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles; |
| 31 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment; |
| 32 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 33 | |
| 34 | import static java.util.Collections.sort; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 35 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase; |
| 37 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase; |
| 38 | import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION; |
| 40 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS; |
| 44 | import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION; |
| 45 | import static org.onosproject.yangutils.utils.UtilConstants.EQUAL; |
| 46 | import static org.onosproject.yangutils.utils.UtilConstants.IMPL; |
| 47 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 48 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
| 49 | import static org.onosproject.yangutils.utils.UtilConstants.NEW; |
| 50 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 51 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS; |
| 52 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 53 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 54 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
| 55 | import static org.onosproject.yangutils.utils.UtilConstants.THIS; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
| 57 | |
| 58 | /** |
| 59 | * Represents utilities for temporary java code fragments. |
| 60 | */ |
| 61 | public final class TempJavaCodeFragmentFilesUtils { |
| 62 | |
| 63 | /** |
| 64 | * Creates a private instance of temporary java code fragment utils. |
| 65 | */ |
| 66 | private TempJavaCodeFragmentFilesUtils() { |
| 67 | } |
| 68 | |
| 69 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 70 | * Adds import for AugmentationHolders class. |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 71 | * |
| 72 | * @param curNode current YANG node |
| 73 | * @param imports list of imports |
| 74 | * @param operation add or delete import |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 75 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 76 | public static void addAugmentationHoldersImport(YangNode curNode, List<String> imports, boolean operation) { |
| 77 | String thisImport = getTempJavaFragement(curNode).getJavaImportData().getAugmentationHolderImport(); |
| 78 | performOperationOnImports(imports, thisImport, operation); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Adds import for AugmentedInfo class. |
| 83 | * |
| 84 | * @param curNode current YANG node |
| 85 | * @param imports list of imports |
| 86 | * @param operation add or delete import |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 87 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 88 | public static void addAugmentedInfoImport(YangNode curNode, List<String> imports, boolean operation) { |
| 89 | String thisImport = getTempJavaFragement(curNode).getJavaImportData().getAugmentedInfoImport(); |
| 90 | performOperationOnImports(imports, thisImport, operation); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns temp java fragment. |
| 95 | * |
| 96 | * @param curNode current YANG node |
| 97 | * @return temp java fragments |
| 98 | */ |
| 99 | public static TempJavaFragmentFiles getTempJavaFragement(YangNode curNode) { |
| 100 | TempJavaCodeFragmentFiles container = ((TempJavaCodeFragmentFilesContainer) curNode) |
| 101 | .getTempJavaCodeFragmentFiles(); |
| 102 | if (container.getBeanTempFiles() != null) { |
| 103 | return container.getBeanTempFiles(); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 104 | } |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 105 | if (container.getEventTempFiles() != null) { |
| 106 | return container.getEventTempFiles(); |
| 107 | } |
| 108 | if (container.getEventListenerTempFiles() != null) { |
| 109 | return container.getEventListenerTempFiles(); |
| 110 | } |
| 111 | if (container.getServiceTempFiles() != null) { |
| 112 | return container.getServiceTempFiles(); |
| 113 | } |
| 114 | |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Updated imports with augmented nodes import. |
| 120 | * |
| 121 | * @param curNode current YANG node |
| 122 | * @param imports list of imports |
| 123 | * @param operation to add or to delete |
| 124 | */ |
| 125 | public static void addAugmentedNodesImport(YangNode curNode, List<String> imports, boolean operation) { |
| 126 | |
| 127 | String nodesImport = ""; |
| 128 | |
| 129 | if (!(curNode instanceof YangJavaAugment)) { |
| 130 | throw new TranslatorException("current node should be of type augment node."); |
| 131 | } |
| 132 | YangJavaAugment augment = (YangJavaAugment) curNode; |
| 133 | List<YangNodeIdentifier> targetNodes = augment.getTargetNode(); |
| 134 | YangNode parent = curNode.getParent(); |
| 135 | if (parent instanceof YangJavaModule) { |
| 136 | // Add impl class import. |
| 137 | nodesImport = getAugmendtedNodesImports(parent, targetNodes, true) + SEMI_COLAN + NEW_LINE; |
| 138 | performOperationOnImports(imports, nodesImport, operation); |
| 139 | // Add builder class import. |
| 140 | if (targetNodes.size() > 2) { |
| 141 | nodesImport = getAugmendtedNodesImports(parent, targetNodes, false) + SEMI_COLAN + NEW_LINE; |
| 142 | performOperationOnImports(imports, nodesImport, operation); |
| 143 | } |
| 144 | } |
| 145 | // TODO: add functionality for submodule and uses. |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns imports for augmented node. |
| 150 | * |
| 151 | * @param parent parent YANG node |
| 152 | * @param targetNodes list of target nodes |
| 153 | * @param isImplClass if impl class's import required |
| 154 | * @return imports for augmented node |
| 155 | */ |
| 156 | private static String getAugmendtedNodesImports(YangNode parent, List<YangNodeIdentifier> targetNodes, |
| 157 | boolean isImplClass) { |
| 158 | String pkgInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage(); |
| 159 | |
| 160 | for (int i = 0; i < targetNodes.size() - 1; i++) { |
| 161 | pkgInfo = pkgInfo + PERIOD + targetNodes.get(i).getName(); |
| 162 | } |
| 163 | String classInfo = targetNodes.get(targetNodes.size() - 1).getName(); |
| 164 | if (!isImplClass) { |
| 165 | return IMPORT + pkgInfo.toLowerCase() + PERIOD + getCapitalCase(classInfo) + BUILDER; |
| 166 | } |
| 167 | return IMPORT + pkgInfo.toLowerCase() + PERIOD + getCapitalCase(classInfo) + BUILDER + PERIOD |
| 168 | + getCapitalCase(classInfo) + IMPL; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Provides string to be added in augment node's constructor. |
| 173 | * |
| 174 | * @param curNode current YANG node |
| 175 | * @return constructors string |
| 176 | */ |
| 177 | public static String getAugmentsAddToAugmentedClassString(YangNode curNode) { |
| 178 | |
| 179 | if (!(curNode instanceof YangJavaAugment)) { |
| 180 | throw new TranslatorException("current node should be of type augment node."); |
| 181 | } |
| 182 | YangJavaAugment augment = (YangJavaAugment) curNode; |
| 183 | List<YangNodeIdentifier> targetNodes = augment.getTargetNode(); |
| 184 | |
| 185 | String name = targetNodes.get(targetNodes.size() - 1).getName(); |
| 186 | String captialCase = getCapitalCase(name); |
| 187 | String smallCase = getSmallCase(captialCase); |
| 188 | return EIGHT_SPACE_INDENTATION + captialCase + IMPL + SPACE + smallCase + IMPL + SPACE + EQUAL + SPACE + NEW |
| 189 | + SPACE + captialCase + BUILDER + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + PERIOD + NEW + SPACE |
| 190 | + captialCase + IMPL + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE |
| 191 | + EIGHT_SPACE_INDENTATION + smallCase + IMPL + PERIOD + ADD_STRING + AUGMENTATION + OPEN_PARENTHESIS |
| 192 | + THIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE; |
| 193 | |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Adds import for array list. |
| 198 | * |
| 199 | * @param curNode current YANG node |
| 200 | * @param imports list of imports |
| 201 | * @param operation add or delete import |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 202 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 203 | public static void addArrayListImport(YangNode curNode, List<String> imports, boolean operation) { |
| 204 | String arrayListImport = getTempJavaFragement(curNode).getJavaImportData().getImportForArrayList(); |
| 205 | String listImport = getTempJavaFragement(curNode).getJavaImportData().getImportForList(); |
| 206 | performOperationOnImports(imports, arrayListImport, operation); |
| 207 | if (!imports.contains(listImport)) { |
| 208 | /** |
| 209 | * List can be there because of attribute also , so no need to remove it and operation will |
| 210 | * always be add(true). |
| 211 | */ |
| 212 | performOperationOnImports(imports, listImport, true); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 213 | } |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 214 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 215 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 216 | /** |
| 217 | * Adds listener's imports. |
| 218 | * |
| 219 | * @param curNode currentYangNode. |
| 220 | * @param imports import list |
| 221 | * @param operation add or remove |
| 222 | * @param classInfo class info to be added to import list |
| 223 | */ |
| 224 | public static void addListnersImport(YangNode curNode, List<String> imports, boolean operation, |
| 225 | String classInfo) { |
| 226 | String thisImport = ""; |
| 227 | if (classInfo.equals(LISTENER_SERVICE)) { |
| 228 | thisImport = getTempJavaFragement(curNode).getJavaImportData().getListenerServiceImport(); |
| 229 | performOperationOnImports(imports, thisImport, operation); |
| 230 | } else { |
| 231 | thisImport = getTempJavaFragement(curNode).getJavaImportData().getListenerRegistryImport(); |
| 232 | performOperationOnImports(imports, thisImport, operation); |
| 233 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Performs given operations on import list. |
| 238 | * |
| 239 | * @param imports list of imports |
| 240 | * @param curImport current import |
| 241 | * @param operation add or remove |
| 242 | * @return import list |
| 243 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 244 | private static List<String> performOperationOnImports(List<String> imports, String curImport, |
| 245 | boolean operation) { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 246 | if (operation) { |
| 247 | imports.add(curImport); |
| 248 | } else { |
| 249 | imports.remove(curImport); |
| 250 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 251 | sortImports(imports); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 252 | return imports; |
| 253 | } |
| 254 | |
| 255 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 256 | * Returns true if AugmentationHolder class needs to be extended. |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 257 | * |
| 258 | * @param extendsList list of classes need to be extended |
| 259 | * @return true or false |
| 260 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 261 | public static boolean isAugmentationHolderExtended(List<JavaQualifiedTypeInfo> extendsList) { |
| 262 | for (JavaQualifiedTypeInfo info : extendsList) { |
| 263 | return info.getClassInfo().equals(AUGMENTATION_HOLDER); |
| 264 | } |
| 265 | return false; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns true if AugmentedInfo class needs to be extended. |
| 270 | * |
| 271 | * @param extendsList list of classes need to be extended |
| 272 | * @return true or false |
| 273 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 274 | public static boolean isAugmentedInfoExtended(List<JavaQualifiedTypeInfo> extendsList) { |
| 275 | for (JavaQualifiedTypeInfo info : extendsList) { |
| 276 | return info.getClassInfo().equals(AUGMENTED_INFO); |
| 277 | } |
| 278 | return false; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Closes the file handle for temporary file. |
| 283 | * |
| 284 | * @param file file to be closed |
| 285 | * @param toBeDeleted flag to indicate if file needs to be deleted |
| 286 | * @throws IOException when failed to close the file handle |
| 287 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 288 | public static void closeFile(File file, boolean toBeDeleted) |
| 289 | throws IOException { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 290 | |
| 291 | if (file != null) { |
| 292 | updateFileHandle(file, null, true); |
| 293 | if (toBeDeleted) { |
| 294 | file.delete(); |
| 295 | } |
| 296 | } |
| 297 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 298 | |
| 299 | /** |
| 300 | * Detects collision between parent and child node which have same name. |
| 301 | * When parent and child node both have the same name in that case child node should be used with |
| 302 | * qualified name. |
| 303 | * |
| 304 | * @param curNode current YANG node |
| 305 | * @param qualifiedTypeInfo current node's qualified info |
| 306 | * @return true if collision is detected |
| 307 | */ |
| 308 | public static boolean detectCollisionBwParentAndChildForImport(YangNode curNode, |
| 309 | JavaQualifiedTypeInfo qualifiedTypeInfo) { |
| 310 | |
| 311 | YangNode parent = curNode.getParent(); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 312 | String parentsClassInfo = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName()); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 313 | String childsClassInfo = qualifiedTypeInfo.getClassInfo(); |
| 314 | if (childsClassInfo.equals(parentsClassInfo)) { |
| 315 | return true; |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Returns sorted import list. |
| 322 | * |
| 323 | * @param imports import list |
| 324 | * @return sorted import list |
| 325 | */ |
| 326 | public static List<String> sortImports(List<String> imports) { |
| 327 | sort(imports); |
| 328 | return imports; |
| 329 | } |
| 330 | |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 331 | } |