blob: ffa7b21bd8be86a27755e8f6ccc0ea7b38be9810 [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 saraswal780eca32016-04-05 12:45:45 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053022
janani bb3be1332016-08-03 16:40:01 +053023import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeLeafref;
24import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeNameLeafref;
25
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053026/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053027 * Represents the attribute info corresponding to class/interface generated.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053028 */
29public final class JavaAttributeInfo {
30
31 /**
32 * The data type info of attribute.
33 */
34 private YangType<?> attrType;
35
36 /**
37 * Name of the attribute.
38 */
39 private String name;
40
41 /**
42 * If the added attribute is a list of info.
43 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053044 private boolean isListAttr;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053045
46 /**
47 * If the added attribute has to be accessed in a fully qualified manner.
48 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053049 private boolean isQualifiedName;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053050
51 /**
52 * The class info will be used to set the attribute type and package info
53 * will be use for qualified name.
54 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053055 private JavaQualifiedTypeInfoTranslator importInfo;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053056
57 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +053058 * Compiler annotation attribute info.
59 */
60 private YangCompilerAnnotation compilerAnnotation;
61
62 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053063 * If conflict occurs.
64 */
65 private boolean isIntConflict;
66
67 /**
68 * If conflict occurs.
69 */
70 private boolean isLongConflict;
71
72 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053073 * Creates a java attribute info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053074 */
75 private JavaAttributeInfo() {
76 }
77
78 /**
Bharat saraswal780eca32016-04-05 12:45:45 +053079 * Creates object of java attribute info.
Bharat saraswalc34b9442016-03-28 15:50:13 +053080 *
Shankara-Huaweib7564772016-08-02 18:13:13 +053081 * @param attrType YANG type
82 * @param name attribute name
83 * @param isListAttr is list attribute
Bharat saraswalc34b9442016-03-28 15:50:13 +053084 * @param isQualifiedName is qualified name
85 */
86 public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) {
87 this.attrType = attrType;
88 this.name = name;
89 this.isListAttr = isListAttr;
90 this.isQualifiedName = isQualifiedName;
91 }
92
93 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053094 * Returns the data type info of attribute.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095 *
96 * @return the data type info of attribute
97 */
98 public YangType<?> getAttributeType() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053099 return attrType;
100 }
101
102 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530103 * Sets the data type info of attribute.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530104 *
105 * @param type the data type info of attribute
106 */
107 public void setAttributeType(YangType<?> type) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530108 attrType = type;
109 }
110
111 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530112 * Returns name of the attribute.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530113 *
114 * @return name of the attribute
115 */
116 public String getAttributeName() {
117
118 if (name == null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530119 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530120 }
121 return name;
122 }
123
124 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530125 * Sets name of the attribute.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530126 *
127 * @param attrName name of the attribute
128 */
129 public void setAttributeName(String attrName) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530130 name = attrName;
131 }
132
133 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530134 * Returns if the added attribute is a list of info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530135 *
136 * @return the if the added attribute is a list of info
137 */
138 public boolean isListAttr() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530139 return isListAttr;
140 }
141
142 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530143 * Sets if the added attribute is a list of info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530144 *
145 * @param isList if the added attribute is a list of info
146 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530147 private void setListAttr(boolean isList) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530148 isListAttr = isList;
149 }
150
151 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530152 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530153 * manner.
154 *
155 * @return the if the added attribute has to be accessed in a fully
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530156 * qualified manner.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530157 */
158 public boolean isQualifiedName() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530159 return isQualifiedName;
160 }
161
162 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530163 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530164 * manner.
165 *
166 * @param isQualified if the added attribute has to be accessed in a fully
Shankara-Huaweib7564772016-08-02 18:13:13 +0530167 * qualified manner
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530168 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530169 private void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530170 isQualifiedName = isQualified;
171 }
172
173 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530174 * Returns the import info for the attribute type. It will be null, if the type
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530175 * is basic built-in java type.
176 *
177 * @return import info
178 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530179 public JavaQualifiedTypeInfoTranslator getImportInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530180 return importInfo;
181 }
182
183 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530184 * Sets the import info for the attribute type.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530185 *
186 * @param importInfo import info for the attribute type
187 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530188 public void setImportInfo(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530189 this.importInfo = importInfo;
190 }
191
192 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +0530193 * Returns the compiler annotation.
194 *
195 * @return compiler annotation info
196 */
197 public YangCompilerAnnotation getCompilerAnnotation() {
198 return compilerAnnotation;
199 }
200
201 /**
202 * Sets the compiler annotation.
203 *
204 * @param compilerAnnotation the compiler annotation to set
205 */
206 public void setCompilerAnnotation(YangCompilerAnnotation compilerAnnotation) {
207 this.compilerAnnotation = compilerAnnotation;
208 }
209
210 /**
211 * Returns true if conflict between int and uint.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530212 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530213 * @return true if conflict between int and uInt
Bharat saraswal64e7e232016-07-14 23:33:55 +0530214 */
215 public boolean isIntConflict() {
216 return isIntConflict;
217 }
218
219 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530220 * Sets true if conflict between int and uInt.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530221 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530222 * @param intConflict true if conflict between int and uInt
Bharat saraswal64e7e232016-07-14 23:33:55 +0530223 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530224 void setIntConflict(boolean intConflict) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530225 isIntConflict = intConflict;
226 }
227
228 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530229 * Returns true if conflict between long and uLong.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530230 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530231 * @return true if conflict between long and uLong
Bharat saraswal64e7e232016-07-14 23:33:55 +0530232 */
233 public boolean isLongConflict() {
234 return isLongConflict;
235 }
236
237 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530238 * Sets true if conflict between long and uLong.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530239 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530240 * @param longConflict true if conflict between long and uLong
Bharat saraswal64e7e232016-07-14 23:33:55 +0530241 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530242 void setLongConflict(boolean longConflict) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530243 isLongConflict = longConflict;
244 }
245
246 /**
Bharat saraswal780eca32016-04-05 12:45:45 +0530247 * Returns java attribute info.
248 *
Shankara-Huaweib7564772016-08-02 18:13:13 +0530249 * @param importInfo java qualified type info
250 * @param attributeName attribute name
251 * @param attributeType attribute type
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530252 * @param isQualifiedAccess is the attribute a qualified access
Shankara-Huaweib7564772016-08-02 18:13:13 +0530253 * @param isListAttribute is list attribute
Bharat saraswal780eca32016-04-05 12:45:45 +0530254 * @return java attribute info.
255 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530256 public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfoTranslator importInfo,
257 String attributeName,
258 YangType<?> attributeType, boolean isQualifiedAccess,
259 boolean isListAttribute) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530260
janani bb3be1332016-08-03 16:40:01 +0530261 if (attributeType != null) {
262 attributeType = isTypeLeafref(attributeType);
263 }
264 attributeName = isTypeNameLeafref(attributeName, attributeType);
Bharat saraswal780eca32016-04-05 12:45:45 +0530265 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswal84366c52016-03-23 19:40:35 +0530266 newAttr.setImportInfo(importInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530267 newAttr.setAttributeName(attributeName);
Bharat saraswal84366c52016-03-23 19:40:35 +0530268 newAttr.setAttributeType(attributeType);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530269 newAttr.setIsQualifiedAccess(isQualifiedAccess);
270 newAttr.setListAttr(isListAttribute);
Bharat saraswal84366c52016-03-23 19:40:35 +0530271
272 return newAttr;
273 }
Vidyashree Ramab3670472016-08-06 15:49:56 +0530274
275 /**
276 * Returns java attribute info.
277 *
278 * @param importInfo java qualified type info
279 * @param attributeName attribute name
280 * @param attributeType attribute type
281 * @param isQualifiedAccess is the attribute a qualified access
282 * @param isListAttribute is list attribute
283 * @param compilerAnnotation compiler annotation
284 * @return java attribute info.
285 */
286 public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfoTranslator importInfo,
287 String attributeName, YangType<?> attributeType,
288 boolean isQualifiedAccess, boolean isListAttribute,
289 YangCompilerAnnotation compilerAnnotation) {
290 JavaAttributeInfo newAttr = getAttributeInfoForTheData(importInfo, attributeName, attributeType,
291 isQualifiedAccess, isListAttribute);
292
293 newAttr.setCompilerAnnotation(compilerAnnotation);
294
295 return newAttr;
296 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530297}