blob: cfe507b9431b730167aebefe7f261a63d408edca [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;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053042
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053043/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053044 * Represents the information about individual imports in the generated file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053045 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053046public class JavaQualifiedTypeInfoTranslator
47 extends JavaQualifiedTypeInfo
Shankara-Huaweib7564772016-08-02 18:13:13 +053048 implements Comparable<JavaQualifiedTypeInfoTranslator>, Serializable {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053049 private static final long serialVersionUID = 806201634L;
50
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053051 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053052 * Creates a java qualified type info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053053 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053054 public JavaQualifiedTypeInfoTranslator() {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053055 super();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053056 }
57
58 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053059 * Returns the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053060 *
61 * @return the imported package info
62 */
63 public String getPkgInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053064 return pkgInfo;
65 }
66
67 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053068 * Sets the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053069 *
70 * @param pkgInfo the imported package info
71 */
72 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053073 this.pkgInfo = pkgInfo;
74 }
75
76 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053077 * Returns the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053078 *
79 * @return the imported class/interface info
80 */
81 public String getClassInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053082 return classInfo;
83 }
84
85 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053086 * Sets the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053087 *
88 * @param classInfo the imported class/interface info
89 */
90 public void setClassInfo(String classInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053091 this.classInfo = classInfo;
92 }
93
94 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053095 * Updates the leaf's java information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053096 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +053097 * @param leaf leaf whose java information is being updated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053098 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053099 public static void updateLeavesJavaQualifiedInfo(
100 JavaLeafInfoContainer leaf) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530101
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530102 JavaQualifiedTypeInfoTranslator importInfo =
103 (JavaQualifiedTypeInfoTranslator) leaf.getJavaQualifiedInfo();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530104
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530105 if (leaf.getDataType() == null) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530106 throw new TranslatorException(
107 "missing data type of leaf " + leaf.getName()
108 + " in " + leaf.getLineNumber() + " at" +
109 leaf.getCharPosition() + " in " +
110 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530111 }
112
113 /*
114 * Current leaves holder is adding a leaf info as a attribute to the
115 * current class.
116 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530117 String className =
118 getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
119 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530120 if (className != null) {
121 /*
122 * Corresponding to the attribute type a class needs to be imported,
123 * since it can be a derived type or a usage of wrapper classes.
124 */
125 importInfo.setClassInfo(className);
Bharat saraswal8beac342016-08-04 02:00:03 +0530126 String classPkg = getJavaImportPackage(leaf.getDataType(),
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530127 leaf.isLeafList(),
128 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530129 if (classPkg == null) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530130 throw new TranslatorException(
131 "import package cannot be null when the class is used" +
132 " for "
133 + leaf.getName()
134 + " in " + leaf.getLineNumber() + " at" +
135 leaf.getCharPosition() + " in " +
136 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530137 }
138 importInfo.setPkgInfo(classPkg);
139 } else {
140 /*
141 * The attribute does not need a class to be imported, for example
142 * built in java types.
143 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530144 String dataTypeName =
145 AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530146 if (dataTypeName == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530147 throw new TranslatorException("not supported data type for "
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530148 + leaf.getName()
149 + " in " +
150 leaf.getLineNumber() +
151 " at" +
152 leaf.getCharPosition() +
153 " in " +
154 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530155 }
156 importInfo.setClassInfo(dataTypeName);
157 }
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530158
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530159 leaf.getJavaQualifiedInfo().setJavaAttributeName(leaf.getJavaName(
160 leaf.getConflictResolveConfig()));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530161 }
162
163 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530164 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530165 * generation for import or for qualified access.
166 *
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530167 * @param curNode current data model node for which the java file
168 * is being
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530169 * generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530170 * @param attributeName name of the attribute being added, it will used in
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530171 * import info for child class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530172 * @return return the import info for this attribute
173 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530174 public static JavaQualifiedTypeInfoTranslator getQualifiedTypeInfoOfCurNode(
175 YangNode curNode,
176 String attributeName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530177
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530178 JavaQualifiedTypeInfoTranslator importInfo =
179 new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530180
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530181 if (!(curNode instanceof JavaFileInfoContainer)) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530182 throw new TranslatorException(
183 "missing java file information to get the package details "
184 + "of attribute corresponding to child node " +
185 curNode.getName() +
186 " in " + curNode.getLineNumber() + " at " +
187 curNode.getCharPosition() + " in " +
188 curNode.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530189 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530190
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530191 importInfo.setClassInfo(attributeName);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530192 importInfo.setPkgInfo(((JavaFileInfoContainer) curNode)
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530193 .getJavaFileInfo().getPackage());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530194
195 return importInfo;
196 }
197
198 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530199 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530200 *
201 * @param referredTypesAttrInfo attribute of referred type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530202 * @param conflictResolver plugin configurations
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530203 * @return return the import info for this attribute
204 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530205 static JavaQualifiedTypeInfoTranslator getQualifiedInfoOfFromString(
206 JavaAttributeInfo referredTypesAttrInfo,
207 YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530208
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530209 /*
210 * Get the java qualified type information for the wrapper classes and
211 * set it in new java attribute information.
212 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530213 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString =
214 new JavaQualifiedTypeInfoTranslator();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530215
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530216 if (referredTypesAttrInfo.getAttributeType().getDataType() == BINARY) {
217 qualifiedInfoOfFromString.setClassInfo(BASE64);
218 qualifiedInfoOfFromString.setPkgInfo(COLLECTION_IMPORTS);
219 } else {
220 qualifiedInfoOfFromString.setClassInfo(
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530221 getJavaImportClass(referredTypesAttrInfo.getAttributeType(),
222 true, conflictResolver));
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530223 qualifiedInfoOfFromString.setPkgInfo(
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530224 getJavaImportPackage(
225 referredTypesAttrInfo.getAttributeType(), true,
226 conflictResolver));
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530227 }
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530228 return qualifiedInfoOfFromString;
229 }
230
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530231 @Override
232 public int hashCode() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530233 return Objects.hash(pkgInfo, classInfo);
234 }
235
236 @Override
237 public boolean equals(Object obj) {
238
239 if (this == obj) {
240 return true;
241 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530242 if (obj instanceof JavaQualifiedTypeInfoTranslator) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530243 JavaQualifiedTypeInfoTranslator other =
244 (JavaQualifiedTypeInfoTranslator) obj;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530245 return Objects.equals(pkgInfo, other.pkgInfo) &&
246 Objects.equals(classInfo, other.classInfo);
247 }
248 return false;
249 }
250
251 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530252 * Checks if the import info matches.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530253 *
254 * @param importInfo matched import
255 * @return if equal or not
256 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530257 public boolean exactMatch(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530258 return equals(importInfo)
259 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
260 && Objects.equals(classInfo, importInfo.getClassInfo());
261 }
262
263 @Override
264 public String toString() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530265 return MoreObjects.toStringHelper(getClass())
266 .add("pkgInfo", pkgInfo)
267 .add("classInfo", classInfo).toString();
268 }
269
270 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530271 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530272 *
273 * @param other compared import info.
274 */
275 @Override
Shankara-Huaweib7564772016-08-02 18:13:13 +0530276 public int compareTo(JavaQualifiedTypeInfoTranslator other) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530277 return getClassInfo().compareTo(other.getClassInfo());
278 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530279}