blob: 25535e255caa27c47d88c899a51ad5435df23b23 [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
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053033import static org.onosproject.yangutils.datamodel.utils.builtindatatype
34 .YangDataTypes.BINARY;
35import static org.onosproject.yangutils.translator.tojava.javamodel
36 .AttributesJavaDataType.getJavaImportClass;
37import static org.onosproject.yangutils.translator.tojava.javamodel
38 .AttributesJavaDataType.getJavaImportPackage;
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +053039import static org.onosproject.yangutils.utils.UtilConstants.BASE64;
40import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053041
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053042/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053043 * Represents the information about individual imports in the generated file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053044 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053045public class JavaQualifiedTypeInfoTranslator
46 extends JavaQualifiedTypeInfo
Shankara-Huaweib7564772016-08-02 18:13:13 +053047 implements Comparable<JavaQualifiedTypeInfoTranslator>, Serializable {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053048 private static final long serialVersionUID = 806201634L;
49
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053050 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053051 * Creates a java qualified type info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053052 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053053 public JavaQualifiedTypeInfoTranslator() {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053054 super();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053055 }
56
57 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053058 * Returns the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053059 *
60 * @return the imported package info
61 */
62 public String getPkgInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053063 return pkgInfo;
64 }
65
66 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053067 * Sets the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053068 *
69 * @param pkgInfo the imported package info
70 */
71 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053072 this.pkgInfo = pkgInfo;
73 }
74
75 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053076 * Returns the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 *
78 * @return the imported class/interface info
79 */
80 public String getClassInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053081 return classInfo;
82 }
83
84 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053085 * Sets the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 *
87 * @param classInfo the imported class/interface info
88 */
89 public void setClassInfo(String classInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053090 this.classInfo = classInfo;
91 }
92
93 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053094 * Updates the leaf's java information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +053096 * @param leaf leaf whose java information is being updated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053097 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +053098 public static void updateLeavesJavaQualifiedInfo(
99 JavaLeafInfoContainer leaf) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530100
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530101 JavaQualifiedTypeInfoTranslator importInfo =
102 (JavaQualifiedTypeInfoTranslator) leaf.getJavaQualifiedInfo();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530103
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530104 if (leaf.getDataType() == null) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530105 throw new TranslatorException(
106 "missing data type of leaf " + leaf.getName()
107 + " in " + leaf.getLineNumber() + " at" +
108 leaf.getCharPosition() + " in " +
109 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530110 }
111
112 /*
113 * Current leaves holder is adding a leaf info as a attribute to the
114 * current class.
115 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530116 String className =
117 getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
118 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530119 if (className != null) {
120 /*
121 * Corresponding to the attribute type a class needs to be imported,
122 * since it can be a derived type or a usage of wrapper classes.
123 */
124 importInfo.setClassInfo(className);
Bharat saraswal8beac342016-08-04 02:00:03 +0530125 String classPkg = getJavaImportPackage(leaf.getDataType(),
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530126 leaf.isLeafList(),
127 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530128 if (classPkg == null) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530129 throw new TranslatorException(
130 "import package cannot be null when the class is used" +
131 " for "
132 + leaf.getName()
133 + " in " + leaf.getLineNumber() + " at" +
134 leaf.getCharPosition() + " in " +
135 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 }
137 importInfo.setPkgInfo(classPkg);
138 } else {
139 /*
140 * The attribute does not need a class to be imported, for example
141 * built in java types.
142 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530143 String dataTypeName =
144 AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530145 if (dataTypeName == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530146 throw new TranslatorException("not supported data type for "
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530147 + leaf.getName()
148 + " in " +
149 leaf.getLineNumber() +
150 " at" +
151 leaf.getCharPosition() +
152 " in " +
153 leaf.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530154 }
155 importInfo.setClassInfo(dataTypeName);
156 }
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530157
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530158 leaf.getJavaQualifiedInfo().setJavaAttributeName(leaf.getJavaName(
159 leaf.getConflictResolveConfig()));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530160 }
161
162 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530163 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530164 * generation for import or for qualified access.
165 *
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530166 * @param curNode current data model node for which the java file
167 * is being
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530168 * generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530169 * @param attributeName name of the attribute being added, it will used in
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530170 * import info for child class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530171 * @return return the import info for this attribute
172 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530173 public static JavaQualifiedTypeInfoTranslator getQualifiedTypeInfoOfCurNode(
174 YangNode curNode,
175 String attributeName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530176
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530177 JavaQualifiedTypeInfoTranslator importInfo =
178 new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530179
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530180 if (!(curNode instanceof JavaFileInfoContainer)) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530181 throw new TranslatorException(
182 "missing java file information to get the package details "
183 + "of attribute corresponding to child node " +
184 curNode.getName() +
185 " in " + curNode.getLineNumber() + " at " +
186 curNode.getCharPosition() + " in " +
187 curNode.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530188 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530189
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530190 importInfo.setClassInfo(attributeName);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530191 importInfo.setPkgInfo(((JavaFileInfoContainer) curNode)
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530192 .getJavaFileInfo().getPackage());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530193
194 return importInfo;
195 }
196
197 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530198 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530199 *
200 * @param referredTypesAttrInfo attribute of referred type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530201 * @param conflictResolver plugin configurations
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530202 * @return return the import info for this attribute
203 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530204 static JavaQualifiedTypeInfoTranslator getQualifiedInfoOfFromString(
205 JavaAttributeInfo referredTypesAttrInfo,
206 YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530207
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530208 /*
209 * Get the java qualified type information for the wrapper classes and
210 * set it in new java attribute information.
211 */
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530212 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString =
213 new JavaQualifiedTypeInfoTranslator();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530214
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530215 if (referredTypesAttrInfo.getAttributeType().getDataType() == BINARY) {
216 qualifiedInfoOfFromString.setClassInfo(BASE64);
217 qualifiedInfoOfFromString.setPkgInfo(COLLECTION_IMPORTS);
218 } else {
219 qualifiedInfoOfFromString.setClassInfo(
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530220 getJavaImportClass(referredTypesAttrInfo.getAttributeType(),
221 true, conflictResolver));
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530222 qualifiedInfoOfFromString.setPkgInfo(
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530223 getJavaImportPackage(
224 referredTypesAttrInfo.getAttributeType(), true,
225 conflictResolver));
Mahesh Poojary Huawei9a16ea02016-09-02 22:59:00 +0530226 }
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530227 return qualifiedInfoOfFromString;
228 }
229
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530230 @Override
231 public int hashCode() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530232 return Objects.hash(pkgInfo, classInfo);
233 }
234
235 @Override
236 public boolean equals(Object obj) {
237
238 if (this == obj) {
239 return true;
240 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530241 if (obj instanceof JavaQualifiedTypeInfoTranslator) {
VinodKumarS-Huawei32889102016-09-06 10:54:37 +0530242 JavaQualifiedTypeInfoTranslator other =
243 (JavaQualifiedTypeInfoTranslator) obj;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530244 return Objects.equals(pkgInfo, other.pkgInfo) &&
245 Objects.equals(classInfo, other.classInfo);
246 }
247 return false;
248 }
249
250 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530251 * Checks if the import info matches.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530252 *
253 * @param importInfo matched import
254 * @return if equal or not
255 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530256 public boolean exactMatch(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530257 return equals(importInfo)
258 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
259 && Objects.equals(classInfo, importInfo.getClassInfo());
260 }
261
262 @Override
263 public String toString() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530264 return MoreObjects.toStringHelper(getClass())
265 .add("pkgInfo", pkgInfo)
266 .add("classInfo", classInfo).toString();
267 }
268
269 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530270 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530271 *
272 * @param other compared import info.
273 */
274 @Override
Shankara-Huaweib7564772016-08-02 18:13:13 +0530275 public int compareTo(JavaQualifiedTypeInfoTranslator other) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530276 return getClassInfo().compareTo(other.getClassInfo());
277 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530278}