blob: 8570b6c0e9b4338eab13e1f8356f2a3956800c8b [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 saraswalc2d3be12016-06-16 00:29:12 +053019import java.io.Serializable;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053020import java.util.Objects;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053021
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.YangNode;
Shankara-Huaweib7564772016-08-02 18:13:13 +053023import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfo;
Bharat saraswal780eca32016-04-05 12:45:45 +053024import org.onosproject.yangutils.translator.exception.TranslatorException;
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053025import org.onosproject.yangutils.translator.tojava.javamodel
26 .AttributesJavaDataType;
27import org.onosproject.yangutils.translator.tojava.javamodel
28 .JavaLeafInfoContainer;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053029import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053030
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053031import com.google.common.base.MoreObjects;
32
Mahesh Poojary Huawei1025a172016-09-27 16:06:07 +053033import static org.onosproject.yangutils.utils.UtilConstants.BASE64;
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053034import static org.onosproject.yangutils.datamodel.utils.builtindatatype
35 .YangDataTypes.BINARY;
Mahesh Poojary Huawei1025a172016-09-27 16:06:07 +053036import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BITS;
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053037import static org.onosproject.yangutils.translator.tojava.javamodel
38 .AttributesJavaDataType.getJavaImportClass;
39import static org.onosproject.yangutils.translator.tojava.javamodel
40 .AttributesJavaDataType.getJavaImportPackage;
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +053041import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
Mahesh Poojary Huawei1025a172016-09-27 16:06:07 +053042import static org.onosproject.yangutils.utils.UtilConstants.REGEX_IMPORTS;
43import static org.onosproject.yangutils.utils.UtilConstants.PATTERN;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053044
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053045/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053046 * Represents the information about individual imports in the generated file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053047 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053048public class JavaQualifiedTypeInfoTranslator
49 extends JavaQualifiedTypeInfo
Shankara-Huaweib7564772016-08-02 18:13:13 +053050 implements Comparable<JavaQualifiedTypeInfoTranslator>, Serializable {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053051 private static final long serialVersionUID = 806201634L;
52
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053053 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053054 * Creates a java qualified type info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053055 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053056 public JavaQualifiedTypeInfoTranslator() {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053057 super();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053058 }
59
60 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053061 * Returns the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053062 *
63 * @return the imported package info
64 */
65 public String getPkgInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053066 return pkgInfo;
67 }
68
69 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053070 * Sets the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053071 *
72 * @param pkgInfo the imported package info
73 */
74 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053075 this.pkgInfo = pkgInfo;
76 }
77
78 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053079 * Returns the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053080 *
81 * @return the imported class/interface info
82 */
83 public String getClassInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053084 return classInfo;
85 }
86
87 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053088 * Sets the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053089 *
90 * @param classInfo the imported class/interface info
91 */
92 public void setClassInfo(String classInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053093 this.classInfo = classInfo;
94 }
95
96 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053097 * Updates the leaf's java information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053098 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +053099 * @param leaf leaf whose java information is being updated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530100 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530101 public static void updateLeavesJavaQualifiedInfo(
102 JavaLeafInfoContainer leaf) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530103
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530104 JavaQualifiedTypeInfoTranslator importInfo =
105 (JavaQualifiedTypeInfoTranslator) leaf.getJavaQualifiedInfo();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530106
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530107 if (leaf.getDataType() == null) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530108 throw new TranslatorException(
109 "missing data type of leaf " + leaf.getName()
110 + " in " + leaf.getLineNumber() + " at" +
111 leaf.getCharPosition() + " in " +
112 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530113 }
114
115 /*
116 * Current leaves holder is adding a leaf info as a attribute to the
117 * current class.
118 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530119 String className =
120 getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
121 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530122 if (className != null) {
123 /*
124 * Corresponding to the attribute type a class needs to be imported,
125 * since it can be a derived type or a usage of wrapper classes.
126 */
127 importInfo.setClassInfo(className);
Bharat saraswal8beac342016-08-04 02:00:03 +0530128 String classPkg = getJavaImportPackage(leaf.getDataType(),
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530129 leaf.isLeafList(),
130 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530131 if (classPkg == null) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530132 throw new TranslatorException(
133 "import package cannot be null when the class is used" +
134 " for "
135 + leaf.getName()
136 + " in " + leaf.getLineNumber() + " at" +
137 leaf.getCharPosition() + " in " +
138 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530139 }
140 importInfo.setPkgInfo(classPkg);
141 } else {
142 /*
143 * The attribute does not need a class to be imported, for example
144 * built in java types.
145 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530146 String dataTypeName =
147 AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530148 if (dataTypeName == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530149 throw new TranslatorException("not supported data type for "
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530150 + leaf.getName()
151 + " in " +
152 leaf.getLineNumber() +
153 " at" +
154 leaf.getCharPosition() +
155 " in " +
156 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530157 }
158 importInfo.setClassInfo(dataTypeName);
159 }
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530160
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530161 leaf.getJavaQualifiedInfo().setJavaAttributeName(leaf.getJavaName(
162 leaf.getConflictResolveConfig()));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530163 }
164
165 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530166 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530167 * generation for import or for qualified access.
168 *
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530169 * @param curNode current data model node for which the java file
170 * is being
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530171 * generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530172 * @param attributeName name of the attribute being added, it will used in
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530173 * import info for child class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530174 * @return return the import info for this attribute
175 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530176 public static JavaQualifiedTypeInfoTranslator getQualifiedTypeInfoOfCurNode(
177 YangNode curNode,
178 String attributeName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530179
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530180 JavaQualifiedTypeInfoTranslator importInfo =
181 new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530182
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530183 if (!(curNode instanceof JavaFileInfoContainer)) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530184 throw new TranslatorException(
185 "missing java file information to get the package details "
186 + "of attribute corresponding to child node " +
187 curNode.getName() +
188 " in " + curNode.getLineNumber() + " at " +
189 curNode.getCharPosition() + " in " +
190 curNode.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530191 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530192
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530193 importInfo.setClassInfo(attributeName);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530194 importInfo.setPkgInfo(((JavaFileInfoContainer) curNode)
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530195 .getJavaFileInfo().getPackage());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530196
197 return importInfo;
198 }
199
200 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530201 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530202 *
203 * @param referredTypesAttrInfo attribute of referred type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530204 * @param conflictResolver plugin configurations
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530205 * @return return the import info for this attribute
206 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530207 static JavaQualifiedTypeInfoTranslator getQualifiedInfoOfFromString(
208 JavaAttributeInfo referredTypesAttrInfo,
209 YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530210
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530211 /*
212 * Get the java qualified type information for the wrapper classes and
213 * set it in new java attribute information.
214 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530215 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString =
216 new JavaQualifiedTypeInfoTranslator();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530217
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530218 if (referredTypesAttrInfo.getAttributeType().getDataType() == BINARY) {
219 qualifiedInfoOfFromString.setClassInfo(BASE64);
220 qualifiedInfoOfFromString.setPkgInfo(COLLECTION_IMPORTS);
Mahesh Poojary Huawei1025a172016-09-27 16:06:07 +0530221 } else if (referredTypesAttrInfo.getAttributeType().getDataType() == BITS) {
222 qualifiedInfoOfFromString.setClassInfo(PATTERN);
223 qualifiedInfoOfFromString.setPkgInfo(REGEX_IMPORTS);
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530224 } else {
225 qualifiedInfoOfFromString.setClassInfo(
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530226 getJavaImportClass(referredTypesAttrInfo.getAttributeType(),
227 true, conflictResolver));
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530228 qualifiedInfoOfFromString.setPkgInfo(
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530229 getJavaImportPackage(
230 referredTypesAttrInfo.getAttributeType(), true,
231 conflictResolver));
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530232 }
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530233 return qualifiedInfoOfFromString;
234 }
235
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530236 @Override
237 public int hashCode() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530238 return Objects.hash(pkgInfo, classInfo);
239 }
240
241 @Override
242 public boolean equals(Object obj) {
243
244 if (this == obj) {
245 return true;
246 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530247 if (obj instanceof JavaQualifiedTypeInfoTranslator) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530248 JavaQualifiedTypeInfoTranslator other =
249 (JavaQualifiedTypeInfoTranslator) obj;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530250 return Objects.equals(pkgInfo, other.pkgInfo) &&
251 Objects.equals(classInfo, other.classInfo);
252 }
253 return false;
254 }
255
256 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530257 * Checks if the import info matches.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530258 *
259 * @param importInfo matched import
260 * @return if equal or not
261 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530262 public boolean exactMatch(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530263 return equals(importInfo)
264 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
265 && Objects.equals(classInfo, importInfo.getClassInfo());
266 }
267
268 @Override
269 public String toString() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530270 return MoreObjects.toStringHelper(getClass())
271 .add("pkgInfo", pkgInfo)
272 .add("classInfo", classInfo).toString();
273 }
274
275 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530276 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530277 *
278 * @param other compared import info.
279 */
280 @Override
Shankara-Huaweib7564772016-08-02 18:13:13 +0530281 public int compareTo(JavaQualifiedTypeInfoTranslator other) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530282 return getClassInfo().compareTo(other.getClassInfo());
283 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530284}