Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 3804650 | 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 | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 20 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 26 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | import com.google.common.base.MoreObjects; |
| 28 | |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass; |
| 30 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage; |
| 31 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 32 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 33 | * Represents the information about individual imports in the generated file. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 34 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 35 | public class JavaQualifiedTypeInfo |
| 36 | implements Comparable<JavaQualifiedTypeInfo> { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 37 | /** |
| 38 | * Package location where the imported class/interface is defined. |
| 39 | */ |
| 40 | private String pkgInfo; |
| 41 | |
| 42 | /** |
| 43 | * Class/interface being referenced. |
| 44 | */ |
| 45 | private String classInfo; |
| 46 | |
| 47 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 48 | * Creates a java qualified type info object. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 49 | */ |
| 50 | public JavaQualifiedTypeInfo() { |
| 51 | } |
| 52 | |
| 53 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 54 | * Returns the imported package info. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 55 | * |
| 56 | * @return the imported package info |
| 57 | */ |
| 58 | public String getPkgInfo() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 59 | return pkgInfo; |
| 60 | } |
| 61 | |
| 62 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 63 | * Sets the imported package info. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 64 | * |
| 65 | * @param pkgInfo the imported package info |
| 66 | */ |
| 67 | public void setPkgInfo(String pkgInfo) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 68 | this.pkgInfo = pkgInfo; |
| 69 | } |
| 70 | |
| 71 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 72 | * Returns the imported class/interface info. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 73 | * |
| 74 | * @return the imported class/interface info |
| 75 | */ |
| 76 | public String getClassInfo() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 77 | return classInfo; |
| 78 | } |
| 79 | |
| 80 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 81 | * Sets the imported class/interface info. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 82 | * |
| 83 | * @param classInfo the imported class/interface info |
| 84 | */ |
| 85 | public void setClassInfo(String classInfo) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 86 | this.classInfo = classInfo; |
| 87 | } |
| 88 | |
| 89 | /** |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 90 | * Updates the leaf's java information. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 91 | * |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 92 | * @param leaf leaf whose java information is being updated |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 93 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 94 | public static void updateLeavesJavaQualifiedInfo(JavaLeafInfoContainer leaf) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 95 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 96 | JavaQualifiedTypeInfo importInfo = leaf.getJavaQualifiedInfo(); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 97 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 98 | if (leaf.getDataType() == null) { |
| 99 | throw new TranslatorException("missing data type of leaf " + leaf.getName()); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Current leaves holder is adding a leaf info as a attribute to the |
| 104 | * current class. |
| 105 | */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 106 | String className = |
| 107 | AttributesJavaDataType.getJavaImportClass(leaf.getDataType(), leaf.isLeafList(), |
| 108 | leaf.getConflictResolveConfig()); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 109 | if (className != null) { |
| 110 | /* |
| 111 | * Corresponding to the attribute type a class needs to be imported, |
| 112 | * since it can be a derived type or a usage of wrapper classes. |
| 113 | */ |
| 114 | importInfo.setClassInfo(className); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 115 | String classPkg = AttributesJavaDataType.getJavaImportPackage(leaf.getDataType(), |
| 116 | leaf.isLeafList(), className); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 117 | if (classPkg == null) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 118 | throw new TranslatorException("import package cannot be null when the class is used"); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 119 | } |
| 120 | importInfo.setPkgInfo(classPkg); |
| 121 | } else { |
| 122 | /* |
| 123 | * The attribute does not need a class to be imported, for example |
| 124 | * built in java types. |
| 125 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 126 | String dataTypeName = AttributesJavaDataType.getJavaDataType(leaf.getDataType()); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 127 | if (dataTypeName == null) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 128 | throw new TranslatorException("not supported data type"); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 129 | } |
| 130 | importInfo.setClassInfo(dataTypeName); |
| 131 | } |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 135 | * Returns the import info for an attribute, which needs to be used for code |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 136 | * generation for import or for qualified access. |
| 137 | * |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 138 | * @param curNode current data model node for which the java file is being |
| 139 | * generated |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 140 | * @param attributeName name of the attribute being added, it will used in |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 141 | * import info for child class |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 142 | * @return return the import info for this attribute |
| 143 | */ |
| 144 | public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode, |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 145 | String attributeName) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 146 | |
| 147 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
| 148 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 149 | if (!(curNode instanceof JavaFileInfoContainer)) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 150 | throw new TranslatorException("missing java file information to get the package details " |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 151 | + "of attribute corresponding to child node"); |
| 152 | } |
| 153 | /* |
| 154 | * The scenario when we need to add the child class as an attribute in |
| 155 | * the current class. The child class is in the package of the current |
| 156 | * classes package with current classes name. |
| 157 | */ |
| 158 | importInfo.setClassInfo(attributeName); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 159 | importInfo.setPkgInfo((((JavaFileInfoContainer) curNode).getJavaFileInfo().getPackage() + "." |
| 160 | + ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName()).toLowerCase()); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 161 | |
| 162 | return importInfo; |
| 163 | } |
| 164 | |
| 165 | /** |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 166 | * Returns the java qualified type information for the wrapper classes. |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 167 | * |
| 168 | * @param referredTypesAttrInfo attribute of referred type |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 169 | * @param confilictResolver plugin configurations |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 170 | * @return return the import info for this attribute |
| 171 | */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 172 | public static JavaQualifiedTypeInfo getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo, |
| 173 | YangToJavaNamingConflictUtil confilictResolver) { |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 174 | |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 175 | /* |
| 176 | * Get the java qualified type information for the wrapper classes and |
| 177 | * set it in new java attribute information. |
| 178 | */ |
| 179 | JavaQualifiedTypeInfo qualifiedInfoOfFromString = new JavaQualifiedTypeInfo(); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 180 | |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 181 | qualifiedInfoOfFromString.setClassInfo( |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 182 | getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true, confilictResolver)); |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 183 | qualifiedInfoOfFromString.setPkgInfo( |
| 184 | getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, null)); |
| 185 | return qualifiedInfoOfFromString; |
| 186 | } |
| 187 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 188 | @Override |
| 189 | public int hashCode() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 190 | return Objects.hash(pkgInfo, classInfo); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public boolean equals(Object obj) { |
| 195 | |
| 196 | if (this == obj) { |
| 197 | return true; |
| 198 | } |
| 199 | if (obj instanceof JavaQualifiedTypeInfo) { |
| 200 | JavaQualifiedTypeInfo other = (JavaQualifiedTypeInfo) obj; |
| 201 | return Objects.equals(pkgInfo, other.pkgInfo) && |
| 202 | Objects.equals(classInfo, other.classInfo); |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 208 | * Checks if the import info matches. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 209 | * |
| 210 | * @param importInfo matched import |
| 211 | * @return if equal or not |
| 212 | */ |
| 213 | public boolean exactMatch(JavaQualifiedTypeInfo importInfo) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 214 | return equals(importInfo) |
| 215 | && Objects.equals(pkgInfo, importInfo.getPkgInfo()) |
| 216 | && Objects.equals(classInfo, importInfo.getClassInfo()); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public String toString() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 221 | return MoreObjects.toStringHelper(getClass()) |
| 222 | .add("pkgInfo", pkgInfo) |
| 223 | .add("classInfo", classInfo).toString(); |
| 224 | } |
| 225 | |
| 226 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 227 | * Checks that there is no 2 objects with the same class name. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 228 | * |
| 229 | * @param other compared import info. |
| 230 | */ |
| 231 | @Override |
| 232 | public int compareTo(JavaQualifiedTypeInfo other) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 233 | return getClassInfo().compareTo(other.getClassInfo()); |
| 234 | } |
| 235 | |
| 236 | } |