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 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 19 | import java.io.Serializable; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 20 | import java.util.Objects; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 21 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangNode; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfo; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 28 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 29 | import com.google.common.base.MoreObjects; |
| 30 | |
Mahesh Poojary Huawei | 9a16ea0 | 2016-09-02 22:59:00 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportClass; |
| 33 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportPackage; |
Mahesh Poojary Huawei | 9a16ea0 | 2016-09-02 22:59:00 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.UtilConstants.BASE64; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 36 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 37 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 38 | * Represents the information about individual imports in the generated file. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 39 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 40 | public class JavaQualifiedTypeInfoTranslator extends JavaQualifiedTypeInfo |
| 41 | implements Comparable<JavaQualifiedTypeInfoTranslator>, Serializable { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 42 | private static final long serialVersionUID = 806201634L; |
| 43 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 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 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 47 | public JavaQualifiedTypeInfoTranslator() { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 48 | super(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 52 | * Returns the imported package info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 53 | * |
| 54 | * @return the imported package info |
| 55 | */ |
| 56 | public String getPkgInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 57 | return pkgInfo; |
| 58 | } |
| 59 | |
| 60 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 61 | * Sets the imported package info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 62 | * |
| 63 | * @param pkgInfo the imported package info |
| 64 | */ |
| 65 | public void setPkgInfo(String pkgInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 66 | this.pkgInfo = pkgInfo; |
| 67 | } |
| 68 | |
| 69 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 70 | * Returns the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 71 | * |
| 72 | * @return the imported class/interface info |
| 73 | */ |
| 74 | public String getClassInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 75 | return classInfo; |
| 76 | } |
| 77 | |
| 78 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 79 | * Sets the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 80 | * |
| 81 | * @param classInfo the imported class/interface info |
| 82 | */ |
| 83 | public void setClassInfo(String classInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 84 | this.classInfo = classInfo; |
| 85 | } |
| 86 | |
| 87 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 88 | * Updates the leaf's java information. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | * |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 90 | * @param leaf leaf whose java information is being updated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 91 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 92 | public static void updateLeavesJavaQualifiedInfo(JavaLeafInfoContainer leaf) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 93 | |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 94 | JavaQualifiedTypeInfoTranslator importInfo = (JavaQualifiedTypeInfoTranslator) leaf.getJavaQualifiedInfo(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 95 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 96 | if (leaf.getDataType() == null) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 97 | throw new TranslatorException("missing data type of leaf " + leaf.getName() |
| 98 | + " in " + leaf.getLineNumber() + " at" + leaf.getCharPosition() + " in " + leaf.getFileName()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Current leaves holder is adding a leaf info as a attribute to the |
| 103 | * current class. |
| 104 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 105 | String className = getJavaImportClass(leaf.getDataType(), leaf.isLeafList(), |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 106 | leaf.getConflictResolveConfig()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 107 | if (className != null) { |
| 108 | /* |
| 109 | * Corresponding to the attribute type a class needs to be imported, |
| 110 | * since it can be a derived type or a usage of wrapper classes. |
| 111 | */ |
| 112 | importInfo.setClassInfo(className); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 113 | String classPkg = getJavaImportPackage(leaf.getDataType(), |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 114 | leaf.isLeafList(), leaf.getConflictResolveConfig()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 115 | if (classPkg == null) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 116 | throw new TranslatorException("import package cannot be null when the class is used for " |
| 117 | + leaf.getName() |
| 118 | + " in " + leaf.getLineNumber() + " at" + leaf.getCharPosition() + " in " + leaf.getFileName()); |
Vinod Kumar S | 9f26ae5 | 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 | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 126 | String dataTypeName = AttributesJavaDataType.getJavaDataType(leaf.getDataType()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 127 | if (dataTypeName == null) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 128 | throw new TranslatorException("not supported data type for " |
| 129 | + leaf.getName() |
| 130 | + " in " + leaf.getLineNumber() + " at" + leaf.getCharPosition() + " in " + leaf.getFileName()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 131 | } |
| 132 | importInfo.setClassInfo(dataTypeName); |
| 133 | } |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 134 | |
| 135 | leaf.getJavaQualifiedInfo().setJavaAttributeName(leaf.getJavaName(null)); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 139 | * 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] | 140 | * generation for import or for qualified access. |
| 141 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 142 | * @param curNode current data model node for which the java file is being |
| 143 | * generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 144 | * @param attributeName name of the attribute being added, it will used in |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 145 | * import info for child class |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 146 | * @return return the import info for this attribute |
| 147 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 148 | public static JavaQualifiedTypeInfoTranslator getQualifiedTypeInfoOfCurNode(YangNode curNode, |
| 149 | String attributeName) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 150 | |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 151 | JavaQualifiedTypeInfoTranslator importInfo = new JavaQualifiedTypeInfoTranslator(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 152 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 153 | if (!(curNode instanceof JavaFileInfoContainer)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 154 | throw new TranslatorException("missing java file information to get the package details " |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 155 | + "of attribute corresponding to child node " + curNode.getName() + |
| 156 | " in " + curNode.getLineNumber() + " at " + curNode.getCharPosition() + " in " + |
| 157 | curNode.getFileName()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 158 | } |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 159 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 160 | importInfo.setClassInfo(attributeName); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 161 | importInfo.setPkgInfo(((JavaFileInfoContainer) curNode) |
| 162 | .getJavaFileInfo().getPackage()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 163 | |
| 164 | return importInfo; |
| 165 | } |
| 166 | |
| 167 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 168 | * Returns the java qualified type information for the wrapper classes. |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 169 | * |
| 170 | * @param referredTypesAttrInfo attribute of referred type |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 171 | * @param conflictResolver plugin configurations |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 172 | * @return return the import info for this attribute |
| 173 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 174 | static JavaQualifiedTypeInfoTranslator getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo, |
| 175 | YangToJavaNamingConflictUtil conflictResolver) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 176 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 177 | /* |
| 178 | * Get the java qualified type information for the wrapper classes and |
| 179 | * set it in new java attribute information. |
| 180 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 181 | JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = new JavaQualifiedTypeInfoTranslator(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 182 | |
Mahesh Poojary Huawei | 9a16ea0 | 2016-09-02 22:59:00 +0530 | [diff] [blame] | 183 | if (referredTypesAttrInfo.getAttributeType().getDataType() == BINARY) { |
| 184 | qualifiedInfoOfFromString.setClassInfo(BASE64); |
| 185 | qualifiedInfoOfFromString.setPkgInfo(COLLECTION_IMPORTS); |
| 186 | } else { |
| 187 | qualifiedInfoOfFromString.setClassInfo( |
| 188 | getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true, conflictResolver)); |
| 189 | qualifiedInfoOfFromString.setPkgInfo( |
| 190 | getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, conflictResolver)); |
| 191 | } |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 192 | return qualifiedInfoOfFromString; |
| 193 | } |
| 194 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 195 | @Override |
| 196 | public int hashCode() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 197 | return Objects.hash(pkgInfo, classInfo); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean equals(Object obj) { |
| 202 | |
| 203 | if (this == obj) { |
| 204 | return true; |
| 205 | } |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 206 | if (obj instanceof JavaQualifiedTypeInfoTranslator) { |
| 207 | JavaQualifiedTypeInfoTranslator other = (JavaQualifiedTypeInfoTranslator) obj; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 208 | return Objects.equals(pkgInfo, other.pkgInfo) && |
| 209 | Objects.equals(classInfo, other.classInfo); |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 215 | * Checks if the import info matches. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 216 | * |
| 217 | * @param importInfo matched import |
| 218 | * @return if equal or not |
| 219 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 220 | public boolean exactMatch(JavaQualifiedTypeInfoTranslator importInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 221 | return equals(importInfo) |
| 222 | && Objects.equals(pkgInfo, importInfo.getPkgInfo()) |
| 223 | && Objects.equals(classInfo, importInfo.getClassInfo()); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public String toString() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 228 | return MoreObjects.toStringHelper(getClass()) |
| 229 | .add("pkgInfo", pkgInfo) |
| 230 | .add("classInfo", classInfo).toString(); |
| 231 | } |
| 232 | |
| 233 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 234 | * 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] | 235 | * |
| 236 | * @param other compared import info. |
| 237 | */ |
| 238 | @Override |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 239 | public int compareTo(JavaQualifiedTypeInfoTranslator other) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 240 | return getClassInfo().compareTo(other.getClassInfo()); |
| 241 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 242 | } |