blob: a5a644a5bbc2fbc376254f6b83bb12a05b74ec17 [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
Vidyashree Ramab3670472016-08-06 15:49:56 +053019import org.onosproject.yangutils.datamodel.YangCompilerAnnotation;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053020import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053021import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfo;
Bharat saraswal780eca32016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053023
janani bb3be1332016-08-03 16:40:01 +053024import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeLeafref;
25import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeNameLeafref;
26
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053027/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053028 * Represents the attribute info corresponding to class/interface generated.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053029 */
30public final class JavaAttributeInfo {
31
32 /**
33 * The data type info of attribute.
34 */
35 private YangType<?> attrType;
36
37 /**
38 * Name of the attribute.
39 */
40 private String name;
41
42 /**
43 * If the added attribute is a list of info.
44 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053045 private boolean isListAttr;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053046
47 /**
48 * If the added attribute has to be accessed in a fully qualified manner.
49 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053050 private boolean isQualifiedName;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053051
52 /**
53 * The class info will be used to set the attribute type and package info
54 * will be use for qualified name.
55 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053056 private JavaQualifiedTypeInfoTranslator importInfo;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053057
58 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +053059 * Compiler annotation attribute info.
60 */
61 private YangCompilerAnnotation compilerAnnotation;
62
63 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053064 * If conflict occurs.
65 */
66 private boolean isIntConflict;
67
68 /**
69 * If conflict occurs.
70 */
71 private boolean isLongConflict;
72
73 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +053074 * If conflict occurs.
75 */
76 private boolean isShortConflict;
77
78 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053079 * Creates a java attribute info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053080 */
81 private JavaAttributeInfo() {
82 }
83
84 /**
Bharat saraswal780eca32016-04-05 12:45:45 +053085 * Creates object of java attribute info.
Bharat saraswalc34b9442016-03-28 15:50:13 +053086 *
Shankara-Huaweib7564772016-08-02 18:13:13 +053087 * @param attrType YANG type
88 * @param name attribute name
89 * @param isListAttr is list attribute
Bharat saraswalc34b9442016-03-28 15:50:13 +053090 * @param isQualifiedName is qualified name
91 */
92 public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) {
93 this.attrType = attrType;
94 this.name = name;
95 this.isListAttr = isListAttr;
96 this.isQualifiedName = isQualifiedName;
97 }
98
99 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530100 * Returns the data type info of attribute.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530101 *
102 * @return the data type info of attribute
103 */
104 public YangType<?> getAttributeType() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530105 return attrType;
106 }
107
108 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530109 * Sets the data type info of attribute.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530110 *
111 * @param type the data type info of attribute
112 */
113 public void setAttributeType(YangType<?> type) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530114 attrType = type;
115 }
116
117 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530118 * Returns name of the attribute.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530119 *
120 * @return name of the attribute
121 */
122 public String getAttributeName() {
123
124 if (name == null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530125 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530126 }
127 return name;
128 }
129
130 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530131 * Sets name of the attribute.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530132 *
133 * @param attrName name of the attribute
134 */
135 public void setAttributeName(String attrName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 name = attrName;
137 }
138
139 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530140 * Returns if the added attribute is a list of info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530141 *
142 * @return the if the added attribute is a list of info
143 */
144 public boolean isListAttr() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530145 return isListAttr;
146 }
147
148 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530149 * Sets if the added attribute is a list of info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530150 *
151 * @param isList if the added attribute is a list of info
152 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530153 private void setListAttr(boolean isList) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530154 isListAttr = isList;
155 }
156
157 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530158 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530159 * manner.
160 *
161 * @return the if the added attribute has to be accessed in a fully
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530162 * qualified manner.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530163 */
164 public boolean isQualifiedName() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530165 return isQualifiedName;
166 }
167
168 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530169 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530170 * manner.
171 *
172 * @param isQualified if the added attribute has to be accessed in a fully
Shankara-Huaweib7564772016-08-02 18:13:13 +0530173 * qualified manner
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530174 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530175 private void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530176 isQualifiedName = isQualified;
177 }
178
179 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530180 * Returns the import info for the attribute type. It will be null, if the type
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530181 * is basic built-in java type.
182 *
183 * @return import info
184 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530185 public JavaQualifiedTypeInfoTranslator getImportInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530186 return importInfo;
187 }
188
189 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530190 * Sets the import info for the attribute type.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530191 *
192 * @param importInfo import info for the attribute type
193 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530194 public void setImportInfo(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530195 this.importInfo = importInfo;
196 }
197
198 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +0530199 * Returns the compiler annotation.
200 *
201 * @return compiler annotation info
202 */
203 public YangCompilerAnnotation getCompilerAnnotation() {
204 return compilerAnnotation;
205 }
206
207 /**
208 * Sets the compiler annotation.
209 *
210 * @param compilerAnnotation the compiler annotation to set
211 */
212 public void setCompilerAnnotation(YangCompilerAnnotation compilerAnnotation) {
213 this.compilerAnnotation = compilerAnnotation;
214 }
215
216 /**
217 * Returns true if conflict between int and uint.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530218 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530219 * @return true if conflict between int and uInt
Bharat saraswal64e7e232016-07-14 23:33:55 +0530220 */
221 public boolean isIntConflict() {
222 return isIntConflict;
223 }
224
225 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530226 * Sets true if conflict between int and uInt.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530227 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530228 * @param intConflict true if conflict between int and uInt
Bharat saraswal64e7e232016-07-14 23:33:55 +0530229 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530230 void setIntConflict(boolean intConflict) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530231 isIntConflict = intConflict;
232 }
233
234 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530235 * Returns true if conflict between long and uLong.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530236 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530237 * @return true if conflict between long and uLong
Bharat saraswal64e7e232016-07-14 23:33:55 +0530238 */
239 public boolean isLongConflict() {
240 return isLongConflict;
241 }
242
243 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530244 * Sets true if conflict between long and uLong.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530245 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530246 * @param longConflict true if conflict between long and uLong
Bharat saraswal64e7e232016-07-14 23:33:55 +0530247 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530248 void setLongConflict(boolean longConflict) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530249 isLongConflict = longConflict;
250 }
251
252 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530253 * Returns true if conflict between short and uint8.
254 *
255 * @return true if conflict between short and uint8
256 */
257 public boolean isShortConflict() {
258 return isShortConflict;
259 }
260
261 /**
262 * Sets true if conflict between short and uint8.
263 *
264 * @param shortConflict true if conflict between short and uint8
265 */
266 public void setShortConflict(boolean shortConflict) {
267 isShortConflict = shortConflict;
268 }
269
270 /**
Bharat saraswal780eca32016-04-05 12:45:45 +0530271 * Returns java attribute info.
272 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530273 * @param importInfo java qualified type info
274 * @param attributeName attribute name
275 * @param attributeType attribute type
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530276 * @param isQualifiedAccess is the attribute a qualified access
Shankara-Huaweib7564772016-08-02 18:13:13 +0530277 * @param isListAttribute is list attribute
Bharat saraswal780eca32016-04-05 12:45:45 +0530278 * @return java attribute info.
279 */
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530280 public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo,
Shankara-Huaweib7564772016-08-02 18:13:13 +0530281 String attributeName,
282 YangType<?> attributeType, boolean isQualifiedAccess,
283 boolean isListAttribute) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530284
janani bb3be1332016-08-03 16:40:01 +0530285 if (attributeType != null) {
286 attributeType = isTypeLeafref(attributeType);
287 }
288 attributeName = isTypeNameLeafref(attributeName, attributeType);
Bharat saraswal780eca32016-04-05 12:45:45 +0530289 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530290 newAttr.setImportInfo((JavaQualifiedTypeInfoTranslator) importInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530291 newAttr.setAttributeName(attributeName);
Bharat saraswal84366c52016-03-23 19:40:35 +0530292 newAttr.setAttributeType(attributeType);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530293 newAttr.setIsQualifiedAccess(isQualifiedAccess);
294 newAttr.setListAttr(isListAttribute);
Bharat saraswal84366c52016-03-23 19:40:35 +0530295
296 return newAttr;
297 }
Vidyashree Ramab3670472016-08-06 15:49:56 +0530298
299 /**
300 * Returns java attribute info.
301 *
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530302 * @param importInfo java qualified type info
303 * @param attributeName attribute name
304 * @param attributeType attribute type
305 * @param isQualifiedAccess is the attribute a qualified access
306 * @param isListAttribute is list attribute
Vidyashree Ramab3670472016-08-06 15:49:56 +0530307 * @param compilerAnnotation compiler annotation
308 * @return java attribute info.
309 */
310 public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfoTranslator importInfo,
311 String attributeName, YangType<?> attributeType,
312 boolean isQualifiedAccess, boolean isListAttribute,
313 YangCompilerAnnotation compilerAnnotation) {
314 JavaAttributeInfo newAttr = getAttributeInfoForTheData(importInfo, attributeName, attributeType,
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530315 isQualifiedAccess, isListAttribute);
Vidyashree Ramab3670472016-08-06 15:49:56 +0530316
317 newAttr.setCompilerAnnotation(compilerAnnotation);
318
319 return newAttr;
320 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530321}