blob: d4d2dead6f6cd597109e95cf47acdbc349b93dd6 [file] [log] [blame]
Vinod Kumar S9f26ae52016-03-23 15:30:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S9f26ae52016-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
Bharat saraswal0663aff2016-10-18 23:16:14 +053019import com.google.common.base.MoreObjects;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053020import org.onosproject.yangutils.datamodel.YangNode;
Shankara-Huaweib7564772016-08-02 18:13:13 +053021import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfo;
Bharat saraswal780eca32016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswal0663aff2016-10-18 23:16:14 +053023import org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType;
24import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053025import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053026
Bharat saraswal0663aff2016-10-18 23:16:14 +053027import java.io.Serializable;
28import java.util.Objects;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053029
Bharat saraswal0663aff2016-10-18 23:16:14 +053030import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY;
31import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportClass;
32import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportPackage;
Mahesh Poojary Huawei1025a172016-09-27 16:06:07 +053033import static org.onosproject.yangutils.utils.UtilConstants.BASE64;
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +053034import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053035
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053036/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053037 * Represents the information about individual imports in the generated file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053038 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053039public class JavaQualifiedTypeInfoTranslator
40 extends JavaQualifiedTypeInfo
Shankara-Huaweib7564772016-08-02 18:13:13 +053041 implements Comparable<JavaQualifiedTypeInfoTranslator>, Serializable {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053042 private static final long serialVersionUID = 806201634L;
Bharat saraswal0663aff2016-10-18 23:16:14 +053043 private boolean isQualified;
Bharat saraswal51c5d672016-11-13 09:03:48 +053044 private boolean isForInterface = true;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053045
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053046 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053047 * Creates a java qualified type info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053048 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053049 public JavaQualifiedTypeInfoTranslator() {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053050 super();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053051 }
52
53 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053054 * Returns the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053055 *
56 * @return the imported package info
57 */
58 public String getPkgInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053059 return pkgInfo;
60 }
61
62 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053063 * Sets the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053064 *
65 * @param pkgInfo the imported package info
66 */
67 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053068 this.pkgInfo = pkgInfo;
69 }
70
71 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053072 * Returns the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053073 *
74 * @return the imported class/interface info
75 */
76 public String getClassInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 return classInfo;
78 }
79
80 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053081 * Sets the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053082 *
83 * @param classInfo the imported class/interface info
84 */
85 public void setClassInfo(String classInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 this.classInfo = classInfo;
87 }
88
89 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053090 * Updates the leaf's java information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053091 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +053092 * @param leaf leaf whose java information is being updated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053093 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053094 public static void updateLeavesJavaQualifiedInfo(
95 JavaLeafInfoContainer leaf) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053096
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053097 JavaQualifiedTypeInfoTranslator importInfo =
98 (JavaQualifiedTypeInfoTranslator) leaf.getJavaQualifiedInfo();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053099
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530100 if (leaf.getDataType() == null) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530101 throw new TranslatorException(
102 "missing data type of leaf " + leaf.getName()
103 + " in " + leaf.getLineNumber() + " at" +
104 leaf.getCharPosition() + " in " +
105 leaf.getFileName());
Vinod Kumar S9f26ae52016-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 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530112 String className =
113 getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
114 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530115 if (className != null) {
116 /*
117 * Corresponding to the attribute type a class needs to be imported,
118 * since it can be a derived type or a usage of wrapper classes.
119 */
120 importInfo.setClassInfo(className);
Bharat saraswal8beac342016-08-04 02:00:03 +0530121 String classPkg = getJavaImportPackage(leaf.getDataType(),
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530122 leaf.isLeafList(),
123 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530124 if (classPkg == null) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530125 throw new TranslatorException(
126 "import package cannot be null when the class is used" +
127 " for "
128 + leaf.getName()
129 + " in " + leaf.getLineNumber() + " at" +
130 leaf.getCharPosition() + " in " +
131 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530132 }
133 importInfo.setPkgInfo(classPkg);
134 } else {
135 /*
136 * The attribute does not need a class to be imported, for example
137 * built in java types.
138 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530139 String dataTypeName =
140 AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530141 if (dataTypeName == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530142 throw new TranslatorException("not supported data type for "
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530143 + leaf.getName()
144 + " in " +
145 leaf.getLineNumber() +
146 " at" +
147 leaf.getCharPosition() +
148 " in " +
149 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530150 }
151 importInfo.setClassInfo(dataTypeName);
152 }
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530153
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530154 leaf.getJavaQualifiedInfo().setJavaAttributeName(leaf.getJavaName(
155 leaf.getConflictResolveConfig()));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530156 }
157
158 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530159 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530160 * generation for import or for qualified access.
161 *
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530162 * @param curNode current data model node for which the java file
163 * is being
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530164 * generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530165 * @param attributeName name of the attribute being added, it will used in
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530166 * import info for child class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530167 * @return return the import info for this attribute
168 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530169 public static JavaQualifiedTypeInfoTranslator getQualifiedTypeInfoOfCurNode(
170 YangNode curNode,
171 String attributeName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530172
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530173 JavaQualifiedTypeInfoTranslator importInfo =
174 new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530175
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530176 if (!(curNode instanceof JavaFileInfoContainer)) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530177 throw new TranslatorException(
178 "missing java file information to get the package details "
179 + "of attribute corresponding to child node " +
180 curNode.getName() +
181 " in " + curNode.getLineNumber() + " at " +
182 curNode.getCharPosition() + " in " +
183 curNode.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530184 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530185
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530186 importInfo.setClassInfo(attributeName);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530187 importInfo.setPkgInfo(((JavaFileInfoContainer) curNode)
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530188 .getJavaFileInfo().getPackage());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530189
190 return importInfo;
191 }
192
193 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530194 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530195 *
196 * @param referredTypesAttrInfo attribute of referred type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530197 * @param conflictResolver plugin configurations
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530198 * @return return the import info for this attribute
199 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530200 static JavaQualifiedTypeInfoTranslator getQualifiedInfoOfFromString(
201 JavaAttributeInfo referredTypesAttrInfo,
202 YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530203
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530204 /*
205 * Get the java qualified type information for the wrapper classes and
206 * set it in new java attribute information.
207 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530208 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString =
209 new JavaQualifiedTypeInfoTranslator();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530210
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530211 if (referredTypesAttrInfo.getAttributeType().getDataType() == BINARY) {
212 qualifiedInfoOfFromString.setClassInfo(BASE64);
213 qualifiedInfoOfFromString.setPkgInfo(COLLECTION_IMPORTS);
214 } else {
215 qualifiedInfoOfFromString.setClassInfo(
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530216 getJavaImportClass(referredTypesAttrInfo.getAttributeType(),
217 true, conflictResolver));
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530218 qualifiedInfoOfFromString.setPkgInfo(
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530219 getJavaImportPackage(
220 referredTypesAttrInfo.getAttributeType(), true,
221 conflictResolver));
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530222 }
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530223 return qualifiedInfoOfFromString;
224 }
225
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530226 @Override
227 public int hashCode() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530228 return Objects.hash(pkgInfo, classInfo);
229 }
230
231 @Override
232 public boolean equals(Object obj) {
233
234 if (this == obj) {
235 return true;
236 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530237 if (obj instanceof JavaQualifiedTypeInfoTranslator) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530238 JavaQualifiedTypeInfoTranslator other =
239 (JavaQualifiedTypeInfoTranslator) obj;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530240 return Objects.equals(pkgInfo, other.pkgInfo) &&
241 Objects.equals(classInfo, other.classInfo);
242 }
243 return false;
244 }
245
246 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530247 * Checks if the import info matches.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530248 *
249 * @param importInfo matched import
250 * @return if equal or not
251 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530252 public boolean exactMatch(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530253 return equals(importInfo)
254 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
255 && Objects.equals(classInfo, importInfo.getClassInfo());
256 }
257
258 @Override
259 public String toString() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530260 return MoreObjects.toStringHelper(getClass())
261 .add("pkgInfo", pkgInfo)
262 .add("classInfo", classInfo).toString();
263 }
264
265 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530266 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530267 *
268 * @param other compared import info.
269 */
270 @Override
Shankara-Huaweib7564772016-08-02 18:13:13 +0530271 public int compareTo(JavaQualifiedTypeInfoTranslator other) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530272 return getClassInfo().compareTo(other.getClassInfo());
273 }
Bharat saraswal0663aff2016-10-18 23:16:14 +0530274
275 /**
276 * Returns true if import is qualified.
277 *
278 * @return true if import is qualified
279 */
280 public boolean isQualified() {
281 return isQualified;
282 }
283
284 /**
285 * Sets true if import is qualified.
286 *
287 * @param qualified true if import is qualified
288 */
289 public void setQualified(boolean qualified) {
290 isQualified = qualified;
291 }
Bharat saraswal51c5d672016-11-13 09:03:48 +0530292
293 /**
294 * Returns true if import should be added to interface.
295 *
296 * @return true if import should be added to interface
297 */
298 boolean isForInterface() {
299 return isForInterface;
300 }
301
302 /**
303 * Sets true if import should be added to interface.
304 *
305 * @param forInterface true if import should be added to interface
306 */
307 void setForInterface(boolean forInterface) {
308 isForInterface = forInterface;
309 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530310}