blob: 35ee6d52c767ab13154eeec8e8c94b857ea53e86 [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
19import java.util.Objects;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053020
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053021import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal780eca32016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053023import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053025import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053026
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053027import com.google.common.base.MoreObjects;
28
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053029import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
30import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage;
31
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053032/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053033 * Represents the information about individual imports in the generated file.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053034 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053035public class JavaQualifiedTypeInfo
36 implements Comparable<JavaQualifiedTypeInfo> {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053037 /**
38 * Package location where the imported class/interface is defined.
39 */
40 private String pkgInfo;
41
42 /**
43 * Class/interface being referenced.
44 */
45 private String classInfo;
46
47 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053048 * Creates a java qualified type info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053049 */
50 public JavaQualifiedTypeInfo() {
51 }
52
53 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053054 * Returns the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053055 *
56 * @return the imported package info
57 */
58 public String getPkgInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053059 return pkgInfo;
60 }
61
62 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053063 * Sets the imported package info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053064 *
65 * @param pkgInfo the imported package info
66 */
67 public void setPkgInfo(String pkgInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053068 this.pkgInfo = pkgInfo;
69 }
70
71 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053072 * Returns the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053073 *
74 * @return the imported class/interface info
75 */
76 public String getClassInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 return classInfo;
78 }
79
80 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053081 * Sets the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053082 *
83 * @param classInfo the imported class/interface info
84 */
85 public void setClassInfo(String classInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 this.classInfo = classInfo;
87 }
88
89 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053090 * Updates the leaf's java information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053091 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +053092 * @param leaf leaf whose java information is being updated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053093 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053094 public static void updateLeavesJavaQualifiedInfo(JavaLeafInfoContainer leaf) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095
Vinod Kumar S79a374b2016-04-30 21:09:15 +053096 JavaQualifiedTypeInfo importInfo = leaf.getJavaQualifiedInfo();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053097
Vinod Kumar S79a374b2016-04-30 21:09:15 +053098 if (leaf.getDataType() == null) {
99 throw new TranslatorException("missing data type of leaf " + leaf.getName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530100 }
101
102 /*
103 * Current leaves holder is adding a leaf info as a attribute to the
104 * current class.
105 */
janani b3e357f62016-05-19 17:39:50 +0530106 String className = AttributesJavaDataType.getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
107 leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530108 if (className != null) {
109 /*
110 * Corresponding to the attribute type a class needs to be imported,
111 * since it can be a derived type or a usage of wrapper classes.
112 */
113 importInfo.setClassInfo(className);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530114 String classPkg = AttributesJavaDataType.getJavaImportPackage(leaf.getDataType(),
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530115 leaf.isLeafList(), leaf.getConflictResolveConfig());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530116 if (classPkg == null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530117 throw new TranslatorException("import package cannot be null when the class is used");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530118 }
119 importInfo.setPkgInfo(classPkg);
120 } else {
121 /*
122 * The attribute does not need a class to be imported, for example
123 * built in java types.
124 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530125 String dataTypeName = AttributesJavaDataType.getJavaDataType(leaf.getDataType());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530126 if (dataTypeName == null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530127 throw new TranslatorException("not supported data type");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530128 }
129 importInfo.setClassInfo(dataTypeName);
130 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530131 }
132
133 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530134 * Returns the import info for an attribute, which needs to be used for code
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530135 * generation for import or for qualified access.
136 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530137 * @param curNode current data model node for which the java file is being
138 * generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530139 * @param attributeName name of the attribute being added, it will used in
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530140 * import info for child class
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530141 * @return return the import info for this attribute
142 */
143 public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode,
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530144 String attributeName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530145
146 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
147
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530148 if (!(curNode instanceof JavaFileInfoContainer)) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530149 throw new TranslatorException("missing java file information to get the package details "
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530150 + "of attribute corresponding to child node");
151 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530152
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530153 importInfo.setClassInfo(attributeName);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530154 importInfo.setPkgInfo(((JavaFileInfoContainer) curNode)
155 .getJavaFileInfo().getPackage());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530156
157 return importInfo;
158 }
159
160 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530161 * Returns the java qualified type information for the wrapper classes.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530162 *
163 * @param referredTypesAttrInfo attribute of referred type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530164 * @param conflictResolver plugin configurations
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530165 * @return return the import info for this attribute
166 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530167 public static JavaQualifiedTypeInfo getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo,
janani b3e357f62016-05-19 17:39:50 +0530168 YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530169
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530170 /*
171 * Get the java qualified type information for the wrapper classes and
172 * set it in new java attribute information.
173 */
174 JavaQualifiedTypeInfo qualifiedInfoOfFromString = new JavaQualifiedTypeInfo();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530175
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530176 qualifiedInfoOfFromString.setClassInfo(
janani b3e357f62016-05-19 17:39:50 +0530177 getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530178 qualifiedInfoOfFromString.setPkgInfo(
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530179 getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530180 return qualifiedInfoOfFromString;
181 }
182
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530183 @Override
184 public int hashCode() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530185 return Objects.hash(pkgInfo, classInfo);
186 }
187
188 @Override
189 public boolean equals(Object obj) {
190
191 if (this == obj) {
192 return true;
193 }
194 if (obj instanceof JavaQualifiedTypeInfo) {
195 JavaQualifiedTypeInfo other = (JavaQualifiedTypeInfo) obj;
196 return Objects.equals(pkgInfo, other.pkgInfo) &&
197 Objects.equals(classInfo, other.classInfo);
198 }
199 return false;
200 }
201
202 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530203 * Checks if the import info matches.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530204 *
205 * @param importInfo matched import
206 * @return if equal or not
207 */
208 public boolean exactMatch(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530209 return equals(importInfo)
210 && Objects.equals(pkgInfo, importInfo.getPkgInfo())
211 && Objects.equals(classInfo, importInfo.getClassInfo());
212 }
213
214 @Override
215 public String toString() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530216 return MoreObjects.toStringHelper(getClass())
217 .add("pkgInfo", pkgInfo)
218 .add("classInfo", classInfo).toString();
219 }
220
221 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530222 * Checks that there is no 2 objects with the same class name.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530223 *
224 * @param other compared import info.
225 */
226 @Override
227 public int compareTo(JavaQualifiedTypeInfo other) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530228 return getClassInfo().compareTo(other.getClassInfo());
229 }
230
231}