blob: 44e8ab2712a7e28bdacfbceb7fcea56643cf67a6 [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 *
130 * @param curNode current data model node for which the java file is being
131 * generated
132 * @param attributeName attribute name
133 * @return AttributeInfo attribute details required to add in temporary
134 * files
135 */
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 }
149 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530150 * Returns the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530151 *
152 * @return the data type info of attribute
153 */
154 public YangType<?> getAttributeType() {
155
156 if (attrType == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530157 throw new TranslatorException("Expected java attribute type is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530158 }
159 return attrType;
160 }
161
162 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530163 * Sets the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530164 *
165 * @param type the data type info of attribute
166 */
167 public void setAttributeType(YangType<?> type) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530168 attrType = type;
169 }
170
171 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530172 * Returns name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530173 *
174 * @return name of the attribute
175 */
176 public String getAttributeName() {
177
178 if (name == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530179 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530180 }
181 return name;
182 }
183
184 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530185 * Sets name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530186 *
187 * @param attrName name of the attribute
188 */
189 public void setAttributeName(String attrName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530190 name = attrName;
191 }
192
193 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530194 * Returns if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530195 *
196 * @return the if the added attribute is a list of info
197 */
198 public boolean isListAttr() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530199 return isListAttr;
200 }
201
202 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530203 * Sets if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530204 *
205 * @param isList if the added attribute is a list of info
206 */
207 public void setListAttr(boolean isList) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530208 isListAttr = isList;
209 }
210
211 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530212 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530213 * manner.
214 *
215 * @return the if the added attribute has to be accessed in a fully
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530216 * qualified manner.
Vinod Kumar S38046502016-03-23 15:30:27 +0530217 */
218 public boolean isQualifiedName() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530219 return isQualifiedName;
220 }
221
222 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530223 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530224 * manner.
225 *
226 * @param isQualified if the added attribute has to be accessed in a fully
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530227 * qualified manner
Vinod Kumar S38046502016-03-23 15:30:27 +0530228 */
229 public void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530230 isQualifiedName = isQualified;
231 }
232
233 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530234 * Returns the import info for the attribute type. It will be null, if the type
Vinod Kumar S38046502016-03-23 15:30:27 +0530235 * is basic built-in java type.
236 *
237 * @return import info
238 */
239 public JavaQualifiedTypeInfo getImportInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530240 return importInfo;
241 }
242
243 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530244 * Sets the import info for the attribute type.
Vinod Kumar S38046502016-03-23 15:30:27 +0530245 *
246 * @param importInfo import info for the attribute type
247 */
248 public void setImportInfo(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530249 this.importInfo = importInfo;
250 }
251
252 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530253 * Creates an attribute info object corresponding to the passed leaf
Vinod Kumar S38046502016-03-23 15:30:27 +0530254 * information and return it.
255 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530256 * @param curNode current data model node for which the java file is being
257 * generated
258 * @param attributeType leaf data type
259 * @param attributeName leaf name
Vinod Kumar S38046502016-03-23 15:30:27 +0530260 * @param isListAttribute is the current added attribute needs to be a list
261 * @return AttributeInfo attribute details required to add in temporary
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530262 * files
Vinod Kumar S38046502016-03-23 15:30:27 +0530263 */
264 public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530265 YangType<?> attributeType, String attributeName,
266 boolean isListAttribute) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530267
Vinod Kumar S38046502016-03-23 15:30:27 +0530268 /*
269 * Get the import info corresponding to the attribute for import in
270 * generated java files or qualified access
271 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530272 JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfAttribute(curNode,
Vinod Kumar S38046502016-03-23 15:30:27 +0530273 attributeType, attributeName, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530274
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530275 return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530276 }
277
278 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530279 * Creates an attribute info object corresponding to a data model node and
Vinod Kumar S38046502016-03-23 15:30:27 +0530280 * return it.
281 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530282 * @param curNode current data model node for which the java code generation
283 * is being handled
Vinod Kumar S38046502016-03-23 15:30:27 +0530284 * @param parentNode parent node in which the current node is an attribute
285 * @param isListNode is the current added attribute needs to be a list
286 * @return AttributeInfo attribute details required to add in temporary
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530287 * files
Vinod Kumar S38046502016-03-23 15:30:27 +0530288 */
289 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
290 YangNode curNode, YangNode parentNode, boolean isListNode) {
291
Vinod Kumar S38046502016-03-23 15:30:27 +0530292 String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName();
293
294 /*
295 * Get the import info corresponding to the attribute for import in
296 * generated java files or qualified access
297 */
298 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
299 curNodeName, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530300
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530301 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530302 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530303
304 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530305 * Returns java attribute info.
306 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530307 * @param importInfo java qualified type info
308 * @param attributeName attribute name
309 * @param attributeType attribute type
310 * @param curNode current YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530311 * @param isListAttribute is list attribute
312 * @return java attribute info.
313 */
314 private static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530315 YangType<?> attributeType, YangNode curNode,
316 boolean isListAttribute) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530317
318 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530319 newAttr.setImportInfo(importInfo);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530320 newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(curNode, importInfo));
janani bde4ffab2016-04-15 16:18:30 +0530321 newAttr.setAttributeName(getCamelCase(attributeName, null));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530322 newAttr.setListAttr(isListAttribute);
323 newAttr.setImportInfo(importInfo);
324 newAttr.setAttributeType(attributeType);
325
326 return newAttr;
327 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530328}