blob: 9b0ccca80035abca8ed0cd54cdf8d878b5008a57 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +05303 *
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
17package org.onosproject.yangutils.translator.tojava;
18
19import java.util.Objects;
Vinod Kumar S38046502016-03-23 15:30:27 +053020import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053023import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import com.google.common.base.MoreObjects;
25
Gaurav Agrawal338735b2016-04-18 18:53:11 +053026import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
27import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage;
28
Vinod Kumar S38046502016-03-23 15:30:27 +053029/**
Bharat saraswald9822e92016-04-05 15:13:44 +053030 * Represents the information about individual imports in the generated file.
Vinod Kumar S38046502016-03-23 15:30:27 +053031 */
32public 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 saraswald9822e92016-04-05 15:13:44 +053045 * Creates a java qualified type info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053046 */
47 public JavaQualifiedTypeInfo() {
48 }
49
50 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Returns the imported package info.
Vinod Kumar S38046502016-03-23 15:30:27 +053052 *
53 * @return the imported package info
54 */
55 public String getPkgInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053056 return pkgInfo;
57 }
58
59 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Sets the imported package info.
Vinod Kumar S38046502016-03-23 15:30:27 +053061 *
62 * @param pkgInfo the imported package info
63 */
64 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053065 this.pkgInfo = pkgInfo;
66 }
67
68 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053069 * Returns the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053070 *
71 * @return the imported class/interface info
72 */
73 public String getClassInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053074 return classInfo;
75 }
76
77 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053078 * Sets the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053079 *
80 * @param classInfo the imported class/interface info
81 */
82 public void setClassInfo(String classInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053083 this.classInfo = classInfo;
84 }
85
86 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053087 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S38046502016-03-23 15:30:27 +053088 * generation for import or for qualified access.
89 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +053090 * @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 S38046502016-03-23 15:30:27 +053094 * @param attributeName name of the attribute being added, it will used in
Gaurav Agrawal338735b2016-04-18 18:53:11 +053095 * import info for child class
96 * @param isListAttr is the added attribute going to be used as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053097 * @return return the import info for this attribute
98 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +053099 public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfAttribute(YangNode curNode,
100 YangType<?> attrType, String attributeName,
101 boolean isListAttr) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530102
103 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
104
105 if (attrType == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530106 throw new TranslatorException("missing data type of leaf " + attributeName);
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 }
108
109 /*
110 * Current leaves holder is adding a leaf info as a attribute to the
111 * current class.
112 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530113 String className = getJavaImportClass(attrType, isListAttr);
Vinod Kumar S38046502016-03-23 15:30:27 +0530114 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);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530120 String classPkg = getJavaImportPackage(attrType, isListAttr, className);
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 if (classPkg == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530122 throw new TranslatorException("import package cannot be null when the class is used");
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 }
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 saraswal6ef0b762016-04-05 12:45:45 +0530132 throw new TranslatorException("not supported data type");
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 }
134 importInfo.setClassInfo(dataTypeName);
135 }
136 return importInfo;
137 }
138
139 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530140 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S38046502016-03-23 15:30:27 +0530141 * generation for import or for qualified access.
142 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530143 * @param curNode current data model node for which the java file is being
144 * generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530145 * @param attributeName name of the attribute being added, it will used in
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530146 * import info for child class
147 * @param isListAttr is the added attribute going to be used as a list
Vinod Kumar S38046502016-03-23 15:30:27 +0530148 * @return return the import info for this attribute
149 */
150 public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530151 String attributeName, boolean isListAttr) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530152
153 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
154
155 if (!(curNode instanceof HasJavaFileInfo)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530156 throw new TranslatorException("missing java file information to get the package details "
Vinod Kumar S38046502016-03-23 15:30:27 +0530157 + "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 Agrawal56527662016-04-20 15:49:17 +0530172 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530173 *
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 saraswald9822e92016-04-05 15:13:44 +0530191 * Returns if the attribute needs to be accessed in a qualified manner or not,
Vinod Kumar S38046502016-03-23 15:30:27 +0530192 * if it needs to be imported, then the same needs to be done.
193 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530194 * @param curNode current cache of the data model node for which java file
195 * is bing generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530196 * @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 Agrawal338735b2016-04-18 18:53:11 +0530200 JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530201
202 boolean isImportPkgEqualCurNodePkg;
203 if (!(curNode instanceof HasJavaFileInfo)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530204 throw new TranslatorException("missing java file info for getting the qualified access");
Vinod Kumar S38046502016-03-23 15:30:27 +0530205 }
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 saraswal6ef0b762016-04-05 12:45:45 +0530231 throw new TranslatorException("Current node needs to support Imports");
Vinod Kumar S38046502016-03-23 15:30:27 +0530232 }
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 saraswald9822e92016-04-05 15:13:44 +0530249 * Checks if the import info is same as the package of the current generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530250 * java file.
251 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530252 * @param curNode Java identifier of the current data model node
Vinod Kumar S38046502016-03-23 15:30:27 +0530253 * @param importInfo import info for an attribute
254 * @return true if the import info is same as the current nodes package
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530255 * false otherwise
Vinod Kumar S38046502016-03-23 15:30:27 +0530256 */
257 public static boolean isImportPkgEqualCurNodePkg(
258 YangNode curNode, JavaQualifiedTypeInfo importInfo) {
259
260 if (!(curNode instanceof HasJavaFileInfo)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530261 throw new TranslatorException("missing java file info for the data model node");
Vinod Kumar S38046502016-03-23 15:30:27 +0530262 }
263 return ((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage()
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530264 .contentEquals(importInfo.getPkgInfo());
Vinod Kumar S38046502016-03-23 15:30:27 +0530265 }
266
267 @Override
268 public int hashCode() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 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 saraswalcc1cdab2016-04-16 02:28:25 +0530287 * Checks if the import info matches.
Vinod Kumar S38046502016-03-23 15:30:27 +0530288 *
289 * @param importInfo matched import
290 * @return if equal or not
291 */
292 public boolean exactMatch(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530293 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 S38046502016-03-23 15:30:27 +0530300 return MoreObjects.toStringHelper(getClass())
301 .add("pkgInfo", pkgInfo)
302 .add("classInfo", classInfo).toString();
303 }
304
305 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530306 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530307 *
308 * @param other compared import info.
309 */
310 @Override
311 public int compareTo(JavaQualifiedTypeInfo other) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530312 return getClassInfo().compareTo(other.getClassInfo());
313 }
314
315}