blob: 4ffd94204314d24445593897cd495849b9a9c178 [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;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053025import org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType;
Shankara-Huaweib7564772016-08-02 18:13:13 +053026import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053027import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053028
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053029import com.google.common.base.MoreObjects;
30
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +053031import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053032import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportClass;
33import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportPackage;
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +053034import static org.onosproject.yangutils.utils.UtilConstants.BASE64;
35import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053036
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053037/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053038 * Represents the information about individual imports in the generated file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053039 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053040public class JavaQualifiedTypeInfoTranslator extends JavaQualifiedTypeInfo
41 implements Comparable<JavaQualifiedTypeInfoTranslator>, Serializable {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053042 private static final long serialVersionUID = 806201634L;
43
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053044 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053045 * Creates a java qualified type info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053046 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053047 public JavaQualifiedTypeInfoTranslator() {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053048 super();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053049 }
50
51 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053052 * Returns the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053053 *
54 * @return the imported package info
55 */
56 public String getPkgInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053057 return pkgInfo;
58 }
59
60 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053061 * Sets the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053062 *
63 * @param pkgInfo the imported package info
64 */
65 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053066 this.pkgInfo = pkgInfo;
67 }
68
69 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053070 * Returns the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053071 *
72 * @return the imported class/interface info
73 */
74 public String getClassInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053075 return classInfo;
76 }
77
78 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053079 * Sets the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053080 *
81 * @param classInfo the imported class/interface info
82 */
83 public void setClassInfo(String classInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053084 this.classInfo = classInfo;
85 }
86
87 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053088 * Updates the leaf's java information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053089 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +053090 * @param leaf leaf whose java information is being updated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053091 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053092 public static void updateLeavesJavaQualifiedInfo(JavaLeafInfoContainer leaf) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053093
Bharat saraswal2da23bf2016-08-25 15:28:39 +053094 JavaQualifiedTypeInfoTranslator importInfo = (JavaQualifiedTypeInfoTranslator) leaf.getJavaQualifiedInfo();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095
Vinod Kumar S79a374b2016-04-30 21:09:15 +053096 if (leaf.getDataType() == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +053097 throw new TranslatorException("missing data type of leaf " + leaf.getName()
98 + " in " + leaf.getLineNumber() + " at" + leaf.getCharPosition() + " in " + leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053099 }
100
101 /*
102 * Current leaves holder is adding a leaf info as a attribute to the
103 * current class.
104 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530105 String className = getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
janani b3e357f62016-05-19 17:39:50 +0530106 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530107 if (className != null) {
108 /*
109 * Corresponding to the attribute type a class needs to be imported,
110 * since it can be a derived type or a usage of wrapper classes.
111 */
112 importInfo.setClassInfo(className);
Bharat saraswal8beac342016-08-04 02:00:03 +0530113 String classPkg = getJavaImportPackage(leaf.getDataType(),
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530114 leaf.isLeafList(), leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530115 if (classPkg == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530116 throw new TranslatorException("import package cannot be null when the class is used for "
117 + leaf.getName()
118 + " in " + leaf.getLineNumber() + " at" + leaf.getCharPosition() + " in " + leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530119 }
120 importInfo.setPkgInfo(classPkg);
121 } else {
122 /*
123 * The attribute does not need a class to be imported, for example
124 * built in java types.
125 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530126 String dataTypeName = AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530127 if (dataTypeName == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530128 throw new TranslatorException("not supported data type for "
129 + leaf.getName()
130 + " in " + leaf.getLineNumber() + " at" + leaf.getCharPosition() + " in " + leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530131 }
132 importInfo.setClassInfo(dataTypeName);
133 }
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530134
135 leaf.getJavaQualifiedInfo().setJavaAttributeName(leaf.getJavaName(null));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 }
137
138 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530139 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530140 * generation for import or for qualified access.
141 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530142 * @param curNode current data model node for which the java file is being
143 * generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530144 * @param attributeName name of the attribute being added, it will used in
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530145 * import info for child class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530146 * @return return the import info for this attribute
147 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530148 public static JavaQualifiedTypeInfoTranslator getQualifiedTypeInfoOfCurNode(YangNode curNode,
149 String attributeName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530150
Shankara-Huaweib7564772016-08-02 18:13:13 +0530151 JavaQualifiedTypeInfoTranslator importInfo = new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530152
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530153 if (!(curNode instanceof JavaFileInfoContainer)) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530154 throw new TranslatorException("missing java file information to get the package details "
Bharat saraswale3175d32016-08-31 17:50:11 +0530155 + "of attribute corresponding to child node " + curNode.getName() +
156 " in " + curNode.getLineNumber() + " at " + curNode.getCharPosition() + " in " +
157 curNode.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530158 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530159
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530160 importInfo.setClassInfo(attributeName);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530161 importInfo.setPkgInfo(((JavaFileInfoContainer) curNode)
162 .getJavaFileInfo().getPackage());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530163
164 return importInfo;
165 }
166
167 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530168 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530169 *
170 * @param referredTypesAttrInfo attribute of referred type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530171 * @param conflictResolver plugin configurations
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530172 * @return return the import info for this attribute
173 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530174 static JavaQualifiedTypeInfoTranslator getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo,
175 YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530176
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530177 /*
178 * Get the java qualified type information for the wrapper classes and
179 * set it in new java attribute information.
180 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530181 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = new JavaQualifiedTypeInfoTranslator();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530182
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530183 if (referredTypesAttrInfo.getAttributeType().getDataType() == BINARY) {
184 qualifiedInfoOfFromString.setClassInfo(BASE64);
185 qualifiedInfoOfFromString.setPkgInfo(COLLECTION_IMPORTS);
186 } else {
187 qualifiedInfoOfFromString.setClassInfo(
188 getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
189 qualifiedInfoOfFromString.setPkgInfo(
190 getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
191 }
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530192 return qualifiedInfoOfFromString;
193 }
194
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530195 @Override
196 public int hashCode() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530197 return Objects.hash(pkgInfo, classInfo);
198 }
199
200 @Override
201 public boolean equals(Object obj) {
202
203 if (this == obj) {
204 return true;
205 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530206 if (obj instanceof JavaQualifiedTypeInfoTranslator) {
207 JavaQualifiedTypeInfoTranslator other = (JavaQualifiedTypeInfoTranslator) obj;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530208 return Objects.equals(pkgInfo, other.pkgInfo) &&
209 Objects.equals(classInfo, other.classInfo);
210 }
211 return false;
212 }
213
214 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530215 * Checks if the import info matches.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530216 *
217 * @param importInfo matched import
218 * @return if equal or not
219 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530220 public boolean exactMatch(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530221 return equals(importInfo)
222 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
223 && Objects.equals(classInfo, importInfo.getClassInfo());
224 }
225
226 @Override
227 public String toString() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530228 return MoreObjects.toStringHelper(getClass())
229 .add("pkgInfo", pkgInfo)
230 .add("classInfo", classInfo).toString();
231 }
232
233 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530234 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530235 *
236 * @param other compared import info.
237 */
238 @Override
Shankara-Huaweib7564772016-08-02 18:13:13 +0530239 public int compareTo(JavaQualifiedTypeInfoTranslator other) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530240 return getClassInfo().compareTo(other.getClassInfo());
241 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530242}