blob: 5c675c6d1786d56aaebe5a9f5a0d034ca12628a5 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-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 saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Vinod Kumar S38046502016-03-23 15:30:27 +053020import java.util.Objects;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053021
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.YangNode;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053023import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfo;
24import org.onosproject.yangutils.datamodel.javadatamodel.YangToJavaNamingConflictUtil;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053025import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053026import org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053027import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053028
Vinod Kumar S38046502016-03-23 15:30:27 +053029import com.google.common.base.MoreObjects;
30
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053031import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportClass;
32import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportPackage;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053033
Vinod Kumar S38046502016-03-23 15:30:27 +053034/**
Bharat saraswald9822e92016-04-05 15:13:44 +053035 * Represents the information about individual imports in the generated file.
Vinod Kumar S38046502016-03-23 15:30:27 +053036 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053037public class JavaQualifiedTypeInfoTranslator extends JavaQualifiedTypeInfo
38 implements Comparable<JavaQualifiedTypeInfoTranslator>, Serializable {
Bharat saraswal96dfef02016-06-16 00:29:12 +053039 private static final long serialVersionUID = 806201634L;
40
Vinod Kumar S38046502016-03-23 15:30:27 +053041 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053042 * Creates a java qualified type info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053043 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053044 public JavaQualifiedTypeInfoTranslator() {
Vinod Kumar S38046502016-03-23 15:30:27 +053045 }
46
47 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053048 * Returns the imported package info.
Vinod Kumar S38046502016-03-23 15:30:27 +053049 *
50 * @return the imported package info
51 */
52 public String getPkgInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053053 return pkgInfo;
54 }
55
56 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Sets the imported package info.
Vinod Kumar S38046502016-03-23 15:30:27 +053058 *
59 * @param pkgInfo the imported package info
60 */
61 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053062 this.pkgInfo = pkgInfo;
63 }
64
65 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Returns the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053067 *
68 * @return the imported class/interface info
69 */
70 public String getClassInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053071 return classInfo;
72 }
73
74 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053075 * Sets the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053076 *
77 * @param classInfo the imported class/interface info
78 */
79 public void setClassInfo(String classInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053080 this.classInfo = classInfo;
81 }
82
83 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053084 * Updates the leaf's java information.
Vinod Kumar S38046502016-03-23 15:30:27 +053085 *
Bharat saraswal33dfa012016-05-17 19:59:16 +053086 * @param leaf leaf whose java information is being updated
Vinod Kumar S38046502016-03-23 15:30:27 +053087 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053088 public static void updateLeavesJavaQualifiedInfo(JavaLeafInfoContainer leaf) {
Vinod Kumar S38046502016-03-23 15:30:27 +053089
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053090 JavaQualifiedTypeInfoTranslator importInfo = leaf.getJavaQualifiedInfo();
Vinod Kumar S38046502016-03-23 15:30:27 +053091
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092 if (leaf.getDataType() == null) {
93 throw new TranslatorException("missing data type of leaf " + leaf.getName());
Vinod Kumar S38046502016-03-23 15:30:27 +053094 }
95
96 /*
97 * Current leaves holder is adding a leaf info as a attribute to the
98 * current class.
99 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530100 String className = getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
janani bdd1314f2016-05-19 17:39:50 +0530101 leaf.getConflictResolveConfig());
Vinod Kumar S38046502016-03-23 15:30:27 +0530102 if (className != null) {
103 /*
104 * Corresponding to the attribute type a class needs to be imported,
105 * since it can be a derived type or a usage of wrapper classes.
106 */
107 importInfo.setClassInfo(className);
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530108 String classPkg = getJavaImportPackage(leaf.getDataType(),
Bharat saraswalcad0e652016-05-26 23:48:38 +0530109 leaf.isLeafList(), leaf.getConflictResolveConfig());
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 if (classPkg == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530111 throw new TranslatorException("import package cannot be null when the class is used");
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 }
113 importInfo.setPkgInfo(classPkg);
114 } else {
115 /*
116 * The attribute does not need a class to be imported, for example
117 * built in java types.
118 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530119 String dataTypeName = AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 if (dataTypeName == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530121 throw new TranslatorException("not supported data type");
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 }
123 importInfo.setClassInfo(dataTypeName);
124 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530125 }
126
127 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530128 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S38046502016-03-23 15:30:27 +0530129 * generation for import or for qualified access.
130 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530131 * @param curNode current data model node for which the java file is being
132 * generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 * @param attributeName name of the attribute being added, it will used in
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530134 * import info for child class
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 * @return return the import info for this attribute
136 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530137 public static JavaQualifiedTypeInfoTranslator getQualifiedTypeInfoOfCurNode(YangNode curNode,
138 String attributeName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530139
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530140 JavaQualifiedTypeInfoTranslator importInfo = new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S38046502016-03-23 15:30:27 +0530141
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 if (!(curNode instanceof JavaFileInfoContainer)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530143 throw new TranslatorException("missing java file information to get the package details "
Vinod Kumar S38046502016-03-23 15:30:27 +0530144 + "of attribute corresponding to child node");
145 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530146
Vinod Kumar S38046502016-03-23 15:30:27 +0530147 importInfo.setClassInfo(attributeName);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530148 importInfo.setPkgInfo(((JavaFileInfoContainer) curNode)
149 .getJavaFileInfo().getPackage());
Vinod Kumar S38046502016-03-23 15:30:27 +0530150
151 return importInfo;
152 }
153
154 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530155 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530156 *
157 * @param referredTypesAttrInfo attribute of referred type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530158 * @param conflictResolver plugin configurations
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530159 * @return return the import info for this attribute
160 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530161 static JavaQualifiedTypeInfoTranslator getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo,
162 YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530163
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530164 /*
165 * Get the java qualified type information for the wrapper classes and
166 * set it in new java attribute information.
167 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530168 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = new JavaQualifiedTypeInfoTranslator();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530169
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530170 qualifiedInfoOfFromString.setClassInfo(
janani bdd1314f2016-05-19 17:39:50 +0530171 getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530172 qualifiedInfoOfFromString.setPkgInfo(
Bharat saraswalcad0e652016-05-26 23:48:38 +0530173 getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530174 return qualifiedInfoOfFromString;
175 }
176
Vinod Kumar S38046502016-03-23 15:30:27 +0530177 @Override
178 public int hashCode() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530179 return Objects.hash(pkgInfo, classInfo);
180 }
181
182 @Override
183 public boolean equals(Object obj) {
184
185 if (this == obj) {
186 return true;
187 }
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530188 if (obj instanceof JavaQualifiedTypeInfoTranslator) {
189 JavaQualifiedTypeInfoTranslator other = (JavaQualifiedTypeInfoTranslator) obj;
Vinod Kumar S38046502016-03-23 15:30:27 +0530190 return Objects.equals(pkgInfo, other.pkgInfo) &&
191 Objects.equals(classInfo, other.classInfo);
192 }
193 return false;
194 }
195
196 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530197 * Checks if the import info matches.
Vinod Kumar S38046502016-03-23 15:30:27 +0530198 *
199 * @param importInfo matched import
200 * @return if equal or not
201 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530202 public boolean exactMatch(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530203 return equals(importInfo)
204 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
205 && Objects.equals(classInfo, importInfo.getClassInfo());
206 }
207
208 @Override
209 public String toString() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530210 return MoreObjects.toStringHelper(getClass())
211 .add("pkgInfo", pkgInfo)
212 .add("classInfo", classInfo).toString();
213 }
214
215 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530216 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530217 *
218 * @param other compared import info.
219 */
220 @Override
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530221 public int compareTo(JavaQualifiedTypeInfoTranslator other) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530222 return getClassInfo().compareTo(other.getClassInfo());
223 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530224}