Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 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; |
| 18 | |
| 19 | import java.util.Objects; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangNode; |
| 21 | import org.onosproject.yangutils.datamodel.YangType; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 24 | import com.google.common.base.MoreObjects; |
| 25 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass; |
| 27 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage; |
| 28 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 29 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 30 | * Represents the information about individual imports in the generated file. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 31 | */ |
| 32 | public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo> { |
| 33 | |
| 34 | /** |
| 35 | * Package location where the imported class/interface is defined. |
| 36 | */ |
| 37 | private String pkgInfo; |
| 38 | |
| 39 | /** |
| 40 | * Class/interface being referenced. |
| 41 | */ |
| 42 | private String classInfo; |
| 43 | |
| 44 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 45 | * Creates a java qualified type info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 46 | */ |
| 47 | public JavaQualifiedTypeInfo() { |
| 48 | } |
| 49 | |
| 50 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 51 | * Returns the imported package info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 52 | * |
| 53 | * @return the imported package info |
| 54 | */ |
| 55 | public String getPkgInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 56 | return pkgInfo; |
| 57 | } |
| 58 | |
| 59 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 60 | * Sets the imported package info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 61 | * |
| 62 | * @param pkgInfo the imported package info |
| 63 | */ |
| 64 | public void setPkgInfo(String pkgInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 65 | this.pkgInfo = pkgInfo; |
| 66 | } |
| 67 | |
| 68 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 69 | * Returns the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 70 | * |
| 71 | * @return the imported class/interface info |
| 72 | */ |
| 73 | public String getClassInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 74 | return classInfo; |
| 75 | } |
| 76 | |
| 77 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 78 | * Sets the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 79 | * |
| 80 | * @param classInfo the imported class/interface info |
| 81 | */ |
| 82 | public void setClassInfo(String classInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 83 | this.classInfo = classInfo; |
| 84 | } |
| 85 | |
| 86 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 87 | * Returns the import info for an attribute, which needs to be used for code |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 88 | * generation for import or for qualified access. |
| 89 | * |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 90 | * @param curNode current data model node for which the java file is being |
| 91 | * generated |
| 92 | * @param attrType type of attribute being added, it will be null, when the |
| 93 | * child class is added as an attribute |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 94 | * @param attributeName name of the attribute being added, it will used in |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 95 | * import info for child class |
| 96 | * @param isListAttr is the added attribute going to be used as a list |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 97 | * @return return the import info for this attribute |
| 98 | */ |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 99 | public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfAttribute(YangNode curNode, |
| 100 | YangType<?> attrType, String attributeName, |
| 101 | boolean isListAttr) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 102 | |
| 103 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
| 104 | |
| 105 | if (attrType == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 106 | throw new TranslatorException("missing data type of leaf " + attributeName); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Current leaves holder is adding a leaf info as a attribute to the |
| 111 | * current class. |
| 112 | */ |
| 113 | String className = AttributesJavaDataType.getJavaImportClass(attrType, isListAttr); |
| 114 | if (className != null) { |
| 115 | /* |
| 116 | * Corresponding to the attribute type a class needs to be imported, |
| 117 | * since it can be a derived type or a usage of wrapper classes. |
| 118 | */ |
| 119 | importInfo.setClassInfo(className); |
| 120 | String classPkg = AttributesJavaDataType.getJavaImportPackage(attrType, isListAttr, className); |
| 121 | if (classPkg == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 122 | throw new TranslatorException("import package cannot be null when the class is used"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 123 | } |
| 124 | importInfo.setPkgInfo(classPkg); |
| 125 | } else { |
| 126 | /* |
| 127 | * The attribute does not need a class to be imported, for example |
| 128 | * built in java types. |
| 129 | */ |
| 130 | String dataTypeName = AttributesJavaDataType.getJavaDataType(attrType); |
| 131 | if (dataTypeName == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 132 | throw new TranslatorException("not supported data type"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 133 | } |
| 134 | importInfo.setClassInfo(dataTypeName); |
| 135 | } |
| 136 | return importInfo; |
| 137 | } |
| 138 | |
| 139 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 140 | * Returns the import info for an attribute, which needs to be used for code |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 141 | * generation for import or for qualified access. |
| 142 | * |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 143 | * @param curNode current data model node for which the java file is being |
| 144 | * generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 145 | * @param attributeName name of the attribute being added, it will used in |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 146 | * import info for child class |
| 147 | * @param isListAttr is the added attribute going to be used as a list |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 148 | * @return return the import info for this attribute |
| 149 | */ |
| 150 | public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 151 | String attributeName, boolean isListAttr) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 152 | |
| 153 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
| 154 | |
| 155 | if (!(curNode instanceof HasJavaFileInfo)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 156 | throw new TranslatorException("missing java file information to get the package details " |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 157 | + "of attribute corresponding to child node"); |
| 158 | } |
| 159 | /* |
| 160 | * The scenario when we need to add the child class as an attribute in |
| 161 | * the current class. The child class is in the package of the current |
| 162 | * classes package with current classes name. |
| 163 | */ |
| 164 | importInfo.setClassInfo(attributeName); |
| 165 | importInfo.setPkgInfo((((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage() + "." |
| 166 | + ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName()).toLowerCase()); |
| 167 | |
| 168 | return importInfo; |
| 169 | } |
| 170 | |
| 171 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 172 | * Get the java qualified type information for the wrapper classes. |
| 173 | * |
| 174 | * @param referredTypesAttrInfo attribute of referred type |
| 175 | * @return return the import info for this attribute |
| 176 | */ |
| 177 | public static JavaQualifiedTypeInfo getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo) { |
| 178 | /* |
| 179 | * Get the java qualified type information for the wrapper classes and |
| 180 | * set it in new java attribute information. |
| 181 | */ |
| 182 | JavaQualifiedTypeInfo qualifiedInfoOfFromString = new JavaQualifiedTypeInfo(); |
| 183 | qualifiedInfoOfFromString.setClassInfo( |
| 184 | getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true)); |
| 185 | qualifiedInfoOfFromString.setPkgInfo( |
| 186 | getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, null)); |
| 187 | return qualifiedInfoOfFromString; |
| 188 | } |
| 189 | |
| 190 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 191 | * Returns if the attribute needs to be accessed in a qualified manner or not, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 192 | * if it needs to be imported, then the same needs to be done. |
| 193 | * |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 194 | * @param curNode current cache of the data model node for which java file |
| 195 | * is bing generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 196 | * @param importInfo import info for the current attribute being added |
| 197 | * @return status of the qualified access to the attribute |
| 198 | */ |
| 199 | public static boolean getIsQualifiedAccessOrAddToImportList(YangNode curNode, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 200 | JavaQualifiedTypeInfo importInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 201 | |
| 202 | boolean isImportPkgEqualCurNodePkg; |
| 203 | if (!(curNode instanceof HasJavaFileInfo)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 204 | throw new TranslatorException("missing java file info for getting the qualified access"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 205 | } |
| 206 | if (importInfo.getClassInfo().contentEquals( |
| 207 | ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName())) { |
| 208 | /* |
| 209 | * if the current class name is same as the attribute class name, |
| 210 | * then the attribute must be accessed in a qualified manner. |
| 211 | */ |
| 212 | return true; |
| 213 | } else if (importInfo.getPkgInfo() != null) { |
| 214 | /* |
| 215 | * If the attribute type is having the package info, it is contender |
| 216 | * for import list and also need to check if it needs to be a |
| 217 | * qualified access. |
| 218 | */ |
| 219 | isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(curNode, importInfo); |
| 220 | if (!isImportPkgEqualCurNodePkg) { |
| 221 | /* |
| 222 | * If the package of the attribute added is not same as the |
| 223 | * current class package, then it must either be imported for |
| 224 | * access or it must be a qualified access. |
| 225 | */ |
| 226 | if (!(curNode instanceof HasJavaImportData)) { |
| 227 | /* |
| 228 | * If the current data model node is not supposed to import |
| 229 | * data, then this is a usage issue and needs to be fixed. |
| 230 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 231 | throw new TranslatorException("Current node needs to support Imports"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | boolean isImportAdded = ((HasJavaImportData) curNode).getJavaImportData() |
| 235 | .addImportInfo(curNode, importInfo); |
| 236 | if (!isImportAdded) { |
| 237 | /* |
| 238 | * If the attribute type info is not imported, then it must |
| 239 | * be a qualified access. |
| 240 | */ |
| 241 | return true; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 249 | * Checks if the import info is same as the package of the current generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 250 | * java file. |
| 251 | * |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 252 | * @param curNode Java identifier of the current data model node |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 253 | * @param importInfo import info for an attribute |
| 254 | * @return true if the import info is same as the current nodes package |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 255 | * false otherwise |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 256 | */ |
| 257 | public static boolean isImportPkgEqualCurNodePkg( |
| 258 | YangNode curNode, JavaQualifiedTypeInfo importInfo) { |
| 259 | |
| 260 | if (!(curNode instanceof HasJavaFileInfo)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 261 | throw new TranslatorException("missing java file info for the data model node"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 262 | } |
| 263 | return ((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage() |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 264 | .contentEquals(importInfo.getPkgInfo()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public int hashCode() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 269 | return Objects.hash(pkgInfo, classInfo); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public boolean equals(Object obj) { |
| 274 | |
| 275 | if (this == obj) { |
| 276 | return true; |
| 277 | } |
| 278 | if (obj instanceof JavaQualifiedTypeInfo) { |
| 279 | JavaQualifiedTypeInfo other = (JavaQualifiedTypeInfo) obj; |
| 280 | return Objects.equals(pkgInfo, other.pkgInfo) && |
| 281 | Objects.equals(classInfo, other.classInfo); |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 287 | * Checks if the import info matches. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 288 | * |
| 289 | * @param importInfo matched import |
| 290 | * @return if equal or not |
| 291 | */ |
| 292 | public boolean exactMatch(JavaQualifiedTypeInfo importInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 293 | return equals(importInfo) |
| 294 | && Objects.equals(pkgInfo, importInfo.getPkgInfo()) |
| 295 | && Objects.equals(classInfo, importInfo.getClassInfo()); |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public String toString() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 300 | return MoreObjects.toStringHelper(getClass()) |
| 301 | .add("pkgInfo", pkgInfo) |
| 302 | .add("classInfo", classInfo).toString(); |
| 303 | } |
| 304 | |
| 305 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 306 | * Checks that there is no 2 objects with the same class name. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 307 | * |
| 308 | * @param other compared import info. |
| 309 | */ |
| 310 | @Override |
| 311 | public int compareTo(JavaQualifiedTypeInfo other) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 312 | return getClassInfo().compareTo(other.getClassInfo()); |
| 313 | } |
| 314 | |
| 315 | } |