blob: e695b58fd3256956534694ed69161a29b54120aa [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;
20
21import org.onosproject.yangutils.datamodel.YangNode;
22import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053023import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
25
26import com.google.common.base.MoreObjects;
27
28/**
Bharat saraswald9822e92016-04-05 15:13:44 +053029 * Represents the information about individual imports in the generated file.
Vinod Kumar S38046502016-03-23 15:30:27 +053030 */
31public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo> {
32
33 /**
34 * Package location where the imported class/interface is defined.
35 */
36 private String pkgInfo;
37
38 /**
39 * Class/interface being referenced.
40 */
41 private String classInfo;
42
43 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053044 * Creates a java qualified type info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053045 */
46 public JavaQualifiedTypeInfo() {
47 }
48
49 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053050 * Returns the imported package info.
Vinod Kumar S38046502016-03-23 15:30:27 +053051 *
52 * @return the imported package info
53 */
54 public String getPkgInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053055 return pkgInfo;
56 }
57
58 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053059 * Sets the imported package info.
Vinod Kumar S38046502016-03-23 15:30:27 +053060 *
61 * @param pkgInfo the imported package info
62 */
63 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053064 this.pkgInfo = pkgInfo;
65 }
66
67 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053068 * Returns the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053069 *
70 * @return the imported class/interface info
71 */
72 public String getClassInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053073 return classInfo;
74 }
75
76 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053077 * Sets the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053078 *
79 * @param classInfo the imported class/interface info
80 */
81 public void setClassInfo(String classInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053082 this.classInfo = classInfo;
83 }
84
85 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053086 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S38046502016-03-23 15:30:27 +053087 * generation for import or for qualified access.
88 *
89 * @param curNode current data model node for which the java file is being
90 * generated.
91 * @param attrType type of attribute being added, it will be null, when the
92 * child class is added as an attribute
93 * @param attributeName name of the attribute being added, it will used in
94 * import info for child class.
95 * @param isListAttr is the added attribute going to be used as a list
96 * @return return the import info for this attribute
97 */
98 public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfLeafAttribute(YangNode curNode,
99 YangType<?> attrType, String attributeName,
100 boolean isListAttr) {
101
102 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
103
104 if (attrType == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530105 throw new TranslatorException("missing data type of leaf " + attributeName);
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 }
107
108 /*
109 * Current leaves holder is adding a leaf info as a attribute to the
110 * current class.
111 */
112 String className = AttributesJavaDataType.getJavaImportClass(attrType, isListAttr);
113 if (className != null) {
114 /*
115 * Corresponding to the attribute type a class needs to be imported,
116 * since it can be a derived type or a usage of wrapper classes.
117 */
118 importInfo.setClassInfo(className);
119 String classPkg = AttributesJavaDataType.getJavaImportPackage(attrType, isListAttr, className);
120 if (classPkg == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530121 throw new TranslatorException("import package cannot be null when the class is used");
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 }
123 importInfo.setPkgInfo(classPkg);
124 } else {
125 /*
126 * The attribute does not need a class to be imported, for example
127 * built in java types.
128 */
129 String dataTypeName = AttributesJavaDataType.getJavaDataType(attrType);
130 if (dataTypeName == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530131 throw new TranslatorException("not supported data type");
Vinod Kumar S38046502016-03-23 15:30:27 +0530132 }
133 importInfo.setClassInfo(dataTypeName);
134 }
135 return importInfo;
136 }
137
138 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530139 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S38046502016-03-23 15:30:27 +0530140 * generation for import or for qualified access.
141 *
142 * @param curNode current data model node for which the java file is being
143 * generated.
144 * @param attributeName name of the attribute being added, it will used in
145 * import info for child class.
146 * @param isListAttr is the added attribute going to be used as a list
147 * @return return the import info for this attribute
148 */
149 public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode,
150 String attributeName, boolean isListAttr) {
151
152 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
153
154 if (!(curNode instanceof HasJavaFileInfo)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530155 throw new TranslatorException("missing java file information to get the package details "
Vinod Kumar S38046502016-03-23 15:30:27 +0530156 + "of attribute corresponding to child node");
157 }
158 /*
159 * The scenario when we need to add the child class as an attribute in
160 * the current class. The child class is in the package of the current
161 * classes package with current classes name.
162 */
163 importInfo.setClassInfo(attributeName);
164 importInfo.setPkgInfo((((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage() + "."
165 + ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName()).toLowerCase());
166
167 return importInfo;
168 }
169
170 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530171 * Returns if the attribute needs to be accessed in a qualified manner or not,
Vinod Kumar S38046502016-03-23 15:30:27 +0530172 * if it needs to be imported, then the same needs to be done.
173 *
174 * @param curNode current cache of the data model node for which java file
175 * is bing generated
176 * @param importInfo import info for the current attribute being added
177 * @return status of the qualified access to the attribute
178 */
179 public static boolean getIsQualifiedAccessOrAddToImportList(YangNode curNode,
180 JavaQualifiedTypeInfo importInfo) {
181
182 boolean isImportPkgEqualCurNodePkg;
183 if (!(curNode instanceof HasJavaFileInfo)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530184 throw new TranslatorException("missing java file info for getting the qualified access");
Vinod Kumar S38046502016-03-23 15:30:27 +0530185 }
186 if (importInfo.getClassInfo().contentEquals(
187 ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName())) {
188 /*
189 * if the current class name is same as the attribute class name,
190 * then the attribute must be accessed in a qualified manner.
191 */
192 return true;
193 } else if (importInfo.getPkgInfo() != null) {
194 /*
195 * If the attribute type is having the package info, it is contender
196 * for import list and also need to check if it needs to be a
197 * qualified access.
198 */
199 isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(curNode, importInfo);
200 if (!isImportPkgEqualCurNodePkg) {
201 /*
202 * If the package of the attribute added is not same as the
203 * current class package, then it must either be imported for
204 * access or it must be a qualified access.
205 */
206 if (!(curNode instanceof HasJavaImportData)) {
207 /*
208 * If the current data model node is not supposed to import
209 * data, then this is a usage issue and needs to be fixed.
210 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530211 throw new TranslatorException("Current node needs to support Imports");
Vinod Kumar S38046502016-03-23 15:30:27 +0530212 }
213
214 boolean isImportAdded = ((HasJavaImportData) curNode).getJavaImportData()
215 .addImportInfo(curNode, importInfo);
216 if (!isImportAdded) {
217 /*
218 * If the attribute type info is not imported, then it must
219 * be a qualified access.
220 */
221 return true;
222 }
223 }
224 }
225 return false;
226 }
227
228 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530229 * Checks if the import info is same as the package of the current generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530230 * java file.
231 *
232 * @param curNode Java identifier of the current data model node
233 * @param importInfo import info for an attribute
234 * @return true if the import info is same as the current nodes package
235 * false otherwise
236 */
237 public static boolean isImportPkgEqualCurNodePkg(
238 YangNode curNode, JavaQualifiedTypeInfo importInfo) {
239
240 if (!(curNode instanceof HasJavaFileInfo)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530241 throw new TranslatorException("missing java file info for the data model node");
Vinod Kumar S38046502016-03-23 15:30:27 +0530242 }
243 return ((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage()
244 .contentEquals(importInfo.getPkgInfo()
245 + "." + importInfo.getClassInfo());
246 }
247
248 @Override
249 public int hashCode() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530250 return Objects.hash(pkgInfo, classInfo);
251 }
252
253 @Override
254 public boolean equals(Object obj) {
255
256 if (this == obj) {
257 return true;
258 }
259 if (obj instanceof JavaQualifiedTypeInfo) {
260 JavaQualifiedTypeInfo other = (JavaQualifiedTypeInfo) obj;
261 return Objects.equals(pkgInfo, other.pkgInfo) &&
262 Objects.equals(classInfo, other.classInfo);
263 }
264 return false;
265 }
266
267 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530268 * checks if the import info matches.
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 *
270 * @param importInfo matched import
271 * @return if equal or not
272 */
273 public boolean exactMatch(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530274 return equals(importInfo)
275 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
276 && Objects.equals(classInfo, importInfo.getClassInfo());
277 }
278
279 @Override
280 public String toString() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530281 return MoreObjects.toStringHelper(getClass())
282 .add("pkgInfo", pkgInfo)
283 .add("classInfo", classInfo).toString();
284 }
285
286 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530287 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530288 *
289 * @param other compared import info.
290 */
291 @Override
292 public int compareTo(JavaQualifiedTypeInfo other) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530293 return getClassInfo().compareTo(other.getClassInfo());
294 }
295
296}