blob: f50f84aeacdd5738c5d18c3cc87c29127265d8bd [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
19import org.onosproject.yangutils.datamodel.YangNode;
20import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053022
23import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053024import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
25import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfAttribute;
Vinod Kumar S38046502016-03-23 15:30:27 +053026import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Vinod Kumar S38046502016-03-23 15:30:27 +053027import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
28
29/**
Bharat saraswald9822e92016-04-05 15:13:44 +053030 * Represents the attribute info corresponding to class/interface generated.
Vinod Kumar S38046502016-03-23 15:30:27 +053031 */
32public final class JavaAttributeInfo {
33
34 /**
35 * The data type info of attribute.
36 */
37 private YangType<?> attrType;
38
39 /**
40 * Name of the attribute.
41 */
42 private String name;
43
44 /**
45 * If the added attribute is a list of info.
46 */
47 private boolean isListAttr = false;
48
49 /**
50 * If the added attribute has to be accessed in a fully qualified manner.
51 */
52 private boolean isQualifiedName = false;
53
54 /**
55 * The class info will be used to set the attribute type and package info
56 * will be use for qualified name.
57 */
58 private JavaQualifiedTypeInfo importInfo;
59
60 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053061 * Creates a java attribute info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053062 */
63 private JavaAttributeInfo() {
64 }
65
66 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053067 * Creates object of java attribute info.
Bharat saraswald6f12412016-03-28 15:50:13 +053068 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +053069 * @param attrType YANG type
70 * @param name attribute name
71 * @param isListAttr is list attribute
Bharat saraswald6f12412016-03-28 15:50:13 +053072 * @param isQualifiedName is qualified name
73 */
74 public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) {
75 this.attrType = attrType;
76 this.name = name;
77 this.isListAttr = isListAttr;
78 this.isQualifiedName = isQualifiedName;
79 }
80
81 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053082 * Creates an attribute info object corresponding to the passed type's attribute
83 * information and return it.
84 *
85 * @param curNode current data model node for which the java file is being generated
86 * @param referredTypesAttrInfo attribute of referred type
87 * @return JavaAttributeInfo attribute details required to add in temporary files
88 */
89 public static JavaAttributeInfo getFromStringAttributeInfo(YangNode curNode,
90 JavaAttributeInfo referredTypesAttrInfo) {
91
92 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(referredTypesAttrInfo);
93 /*
94 * Create a new java attribute info with qualified information of
95 * wrapper classes.
96 */
97 return getAttributeInfoForTheData(qualifiedInfoOfFromString, referredTypesAttrInfo.getAttributeName(),
98 referredTypesAttrInfo.getAttributeType(), curNode, false);
99 }
100
101 /**
102 * Creates an attribute info object corresponding to the passed type attribute
103 * information and return it.
104 *
105 * @param curNode current data model node for which the java file is being
106 * generated
107 * @param attributeType leaf data type
108 * @param attributeName leaf name
109 * @param isListAttribute is the current added attribute needs to be a list
110 * @return AttributeInfo attribute details required to add in temporary
111 * files
112 */
113 public static JavaAttributeInfo getAttributeInfoOfType(YangNode curNode,
114 YangType<?> attributeType, String attributeName,
115 boolean isListAttribute) {
116 /*
117 * Get the import info corresponding to the attribute for import in
118 * generated java files or qualified access
119 */
120 JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfAttribute(curNode,
121 attributeType, attributeName, isListAttribute);
122
123 return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
124 }
125
126 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530127 * Creates an attribute info object corresponding to the passed enumeration attribute
128 * information and return it.
129 *
Gaurav Agrawal56527662016-04-20 15:49:17 +0530130 * @param curNode current data model node for which the java file is being
131 * generated
Bharat saraswald72411a2016-04-19 01:00:16 +0530132 * @param attributeName attribute name
133 * @return AttributeInfo attribute details required to add in temporary
Gaurav Agrawal56527662016-04-20 15:49:17 +0530134 * files
Bharat saraswald72411a2016-04-19 01:00:16 +0530135 */
136 public static JavaAttributeInfo getAttributeInfoOfEnumAttribute(YangNode curNode, String attributeName) {
137
138 String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName();
139
140 /*
141 * Get the import info corresponding to the attribute for import in
142 * generated java files or qualified access
143 */
144 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
145 curNodeName, false);
146
147 return getAttributeInfoForTheData(qualifiedTypeInfo, attributeName, null, curNode, false);
148 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530149
Bharat saraswald72411a2016-04-19 01:00:16 +0530150 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Returns the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530152 *
153 * @return the data type info of attribute
154 */
155 public YangType<?> getAttributeType() {
156
157 if (attrType == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530158 throw new TranslatorException("Expected java attribute type is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530159 }
160 return attrType;
161 }
162
163 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530164 * Sets the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530165 *
166 * @param type the data type info of attribute
167 */
168 public void setAttributeType(YangType<?> type) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530169 attrType = type;
170 }
171
172 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530173 * Returns name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530174 *
175 * @return name of the attribute
176 */
177 public String getAttributeName() {
178
179 if (name == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530180 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530181 }
182 return name;
183 }
184
185 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530186 * Sets name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530187 *
188 * @param attrName name of the attribute
189 */
190 public void setAttributeName(String attrName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530191 name = attrName;
192 }
193
194 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530195 * Returns if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530196 *
197 * @return the if the added attribute is a list of info
198 */
199 public boolean isListAttr() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530200 return isListAttr;
201 }
202
203 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530204 * Sets if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530205 *
206 * @param isList if the added attribute is a list of info
207 */
208 public void setListAttr(boolean isList) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530209 isListAttr = isList;
210 }
211
212 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530213 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530214 * manner.
215 *
216 * @return the if the added attribute has to be accessed in a fully
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530217 * qualified manner.
Vinod Kumar S38046502016-03-23 15:30:27 +0530218 */
219 public boolean isQualifiedName() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530220 return isQualifiedName;
221 }
222
223 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530224 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530225 * manner.
226 *
227 * @param isQualified if the added attribute has to be accessed in a fully
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530228 * qualified manner
Vinod Kumar S38046502016-03-23 15:30:27 +0530229 */
230 public void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530231 isQualifiedName = isQualified;
232 }
233
234 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530235 * Returns the import info for the attribute type. It will be null, if the type
Vinod Kumar S38046502016-03-23 15:30:27 +0530236 * is basic built-in java type.
237 *
238 * @return import info
239 */
240 public JavaQualifiedTypeInfo getImportInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530241 return importInfo;
242 }
243
244 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530245 * Sets the import info for the attribute type.
Vinod Kumar S38046502016-03-23 15:30:27 +0530246 *
247 * @param importInfo import info for the attribute type
248 */
249 public void setImportInfo(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530250 this.importInfo = importInfo;
251 }
252
253 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530254 * Creates an attribute info object corresponding to the passed leaf
Vinod Kumar S38046502016-03-23 15:30:27 +0530255 * information and return it.
256 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530257 * @param curNode current data model node for which the java file is being
258 * generated
259 * @param attributeType leaf data type
260 * @param attributeName leaf name
Vinod Kumar S38046502016-03-23 15:30:27 +0530261 * @param isListAttribute is the current added attribute needs to be a list
262 * @return AttributeInfo attribute details required to add in temporary
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530263 * files
Vinod Kumar S38046502016-03-23 15:30:27 +0530264 */
265 public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530266 YangType<?> attributeType, String attributeName,
267 boolean isListAttribute) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530268
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 /*
270 * Get the import info corresponding to the attribute for import in
271 * generated java files or qualified access
272 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530273 JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfAttribute(curNode,
Vinod Kumar S38046502016-03-23 15:30:27 +0530274 attributeType, attributeName, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530275
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530276 return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530277 }
278
279 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530280 * Creates an attribute info object corresponding to a data model node and
Vinod Kumar S38046502016-03-23 15:30:27 +0530281 * return it.
282 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530283 * @param curNode current data model node for which the java code generation
284 * is being handled
Vinod Kumar S38046502016-03-23 15:30:27 +0530285 * @param parentNode parent node in which the current node is an attribute
286 * @param isListNode is the current added attribute needs to be a list
287 * @return AttributeInfo attribute details required to add in temporary
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530288 * files
Vinod Kumar S38046502016-03-23 15:30:27 +0530289 */
290 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
291 YangNode curNode, YangNode parentNode, boolean isListNode) {
292
Vinod Kumar S38046502016-03-23 15:30:27 +0530293 String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName();
294
295 /*
296 * Get the import info corresponding to the attribute for import in
297 * generated java files or qualified access
298 */
299 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
300 curNodeName, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530301
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530302 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530303 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530304
305 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530306 * Creates an attribute info object corresponding to a data model node and
307 * return it.
308 *
309 * @param parentNode parent node in which the current node is an attribute
310 * @param isListNode is the current added attribute needs to be a list
311 * @param curNodeName is the current added attribute needs to be a list
312 * @return AttributeInfo attribute details required to add in temporary
313 * files
314 */
315 public static JavaAttributeInfo getCurNodeAsAttributeInParent(YangNode parentNode, boolean isListNode,
316 String curNodeName) {
317
318 /*
319 * Get the import info corresponding to the attribute for import in
320 * generated java files or qualified access
321 */
322 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
323 curNodeName, isListNode);
324
325 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode);
326 }
327
328
329 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530330 * Returns java attribute info.
331 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530332 * @param importInfo java qualified type info
333 * @param attributeName attribute name
334 * @param attributeType attribute type
335 * @param curNode current YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530336 * @param isListAttribute is list attribute
337 * @return java attribute info.
338 */
339 private static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530340 YangType<?> attributeType, YangNode curNode,
341 boolean isListAttribute) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530342
343 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530344 newAttr.setImportInfo(importInfo);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530345 newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(curNode, importInfo));
janani bde4ffab2016-04-15 16:18:30 +0530346 newAttr.setAttributeName(getCamelCase(attributeName, null));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530347 newAttr.setListAttr(isListAttribute);
348 newAttr.setImportInfo(importInfo);
349 newAttr.setAttributeType(attributeType);
350
351 return newAttr;
352 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530353}