blob: a0e7b93dbfee6cc4e26c3b0becd40035b75aece4 [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
Gaurav Agrawalbfce9342016-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 Agrawal97a5e1c2016-04-18 18:53:11 +053033
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053034/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053035 * Represents the information about individual imports in the generated file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053036 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053037public class JavaQualifiedTypeInfoTranslator extends JavaQualifiedTypeInfo
38 implements Comparable<JavaQualifiedTypeInfoTranslator>, Serializable {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053039 private static final long serialVersionUID = 806201634L;
40
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053041 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053042 * Creates a java qualified type info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053043 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053044 public JavaQualifiedTypeInfoTranslator() {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053045 super();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053046 }
47
48 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053049 * Returns the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053050 *
51 * @return the imported package info
52 */
53 public String getPkgInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053054 return pkgInfo;
55 }
56
57 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053058 * Sets the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053059 *
60 * @param pkgInfo the imported package info
61 */
62 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053063 this.pkgInfo = pkgInfo;
64 }
65
66 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053067 * Returns the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053068 *
69 * @return the imported class/interface info
70 */
71 public String getClassInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053072 return classInfo;
73 }
74
75 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053076 * Sets the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 *
78 * @param classInfo the imported class/interface info
79 */
80 public void setClassInfo(String classInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053081 this.classInfo = classInfo;
82 }
83
84 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053085 * Updates the leaf's java information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +053087 * @param leaf leaf whose java information is being updated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053088 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053089 public static void updateLeavesJavaQualifiedInfo(JavaLeafInfoContainer leaf) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053090
Bharat saraswal2da23bf2016-08-25 15:28:39 +053091 JavaQualifiedTypeInfoTranslator importInfo = (JavaQualifiedTypeInfoTranslator) leaf.getJavaQualifiedInfo();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053092
Vinod Kumar S79a374b2016-04-30 21:09:15 +053093 if (leaf.getDataType() == null) {
94 throw new TranslatorException("missing data type of leaf " + leaf.getName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095 }
96
97 /*
98 * Current leaves holder is adding a leaf info as a attribute to the
99 * current class.
100 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530101 String className = getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
janani b3e357f62016-05-19 17:39:50 +0530102 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530103 if (className != null) {
104 /*
105 * Corresponding to the attribute type a class needs to be imported,
106 * since it can be a derived type or a usage of wrapper classes.
107 */
108 importInfo.setClassInfo(className);
Bharat saraswal8beac342016-08-04 02:00:03 +0530109 String classPkg = getJavaImportPackage(leaf.getDataType(),
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530110 leaf.isLeafList(), leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530111 if (classPkg == null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530112 throw new TranslatorException("import package cannot be null when the class is used");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530113 }
114 importInfo.setPkgInfo(classPkg);
115 } else {
116 /*
117 * The attribute does not need a class to be imported, for example
118 * built in java types.
119 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530120 String dataTypeName = AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530121 if (dataTypeName == null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530122 throw new TranslatorException("not supported data type");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530123 }
124 importInfo.setClassInfo(dataTypeName);
125 }
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530126
127 leaf.getJavaQualifiedInfo().setJavaAttributeName(leaf.getJavaName(null));
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530128 }
129
130 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530131 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530132 * generation for import or for qualified access.
133 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530134 * @param curNode current data model node for which the java file is being
135 * generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 * @param attributeName name of the attribute being added, it will used in
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530137 * import info for child class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530138 * @return return the import info for this attribute
139 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530140 public static JavaQualifiedTypeInfoTranslator getQualifiedTypeInfoOfCurNode(YangNode curNode,
141 String attributeName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530142
Shankara-Huaweib7564772016-08-02 18:13:13 +0530143 JavaQualifiedTypeInfoTranslator importInfo = new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530144
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530145 if (!(curNode instanceof JavaFileInfoContainer)) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530146 throw new TranslatorException("missing java file information to get the package details "
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530147 + "of attribute corresponding to child node");
148 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530149
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530150 importInfo.setClassInfo(attributeName);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530151 importInfo.setPkgInfo(((JavaFileInfoContainer) curNode)
152 .getJavaFileInfo().getPackage());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530153
154 return importInfo;
155 }
156
157 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530158 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530159 *
160 * @param referredTypesAttrInfo attribute of referred type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530161 * @param conflictResolver plugin configurations
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530162 * @return return the import info for this attribute
163 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530164 static JavaQualifiedTypeInfoTranslator getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo,
165 YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530166
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530167 /*
168 * Get the java qualified type information for the wrapper classes and
169 * set it in new java attribute information.
170 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530171 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = new JavaQualifiedTypeInfoTranslator();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530172
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530173 qualifiedInfoOfFromString.setClassInfo(
janani b3e357f62016-05-19 17:39:50 +0530174 getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530175 qualifiedInfoOfFromString.setPkgInfo(
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530176 getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530177 return qualifiedInfoOfFromString;
178 }
179
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530180 @Override
181 public int hashCode() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530182 return Objects.hash(pkgInfo, classInfo);
183 }
184
185 @Override
186 public boolean equals(Object obj) {
187
188 if (this == obj) {
189 return true;
190 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530191 if (obj instanceof JavaQualifiedTypeInfoTranslator) {
192 JavaQualifiedTypeInfoTranslator other = (JavaQualifiedTypeInfoTranslator) obj;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530193 return Objects.equals(pkgInfo, other.pkgInfo) &&
194 Objects.equals(classInfo, other.classInfo);
195 }
196 return false;
197 }
198
199 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530200 * Checks if the import info matches.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530201 *
202 * @param importInfo matched import
203 * @return if equal or not
204 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530205 public boolean exactMatch(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530206 return equals(importInfo)
207 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
208 && Objects.equals(classInfo, importInfo.getClassInfo());
209 }
210
211 @Override
212 public String toString() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530213 return MoreObjects.toStringHelper(getClass())
214 .add("pkgInfo", pkgInfo)
215 .add("classInfo", classInfo).toString();
216 }
217
218 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530219 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530220 *
221 * @param other compared import info.
222 */
223 @Override
Shankara-Huaweib7564772016-08-02 18:13:13 +0530224 public int compareTo(JavaQualifiedTypeInfoTranslator other) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530225 return getClassInfo().compareTo(other.getClassInfo());
226 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530227}