blob: b02b4e2afcb4cd600c236cd84cb265b4b5e42d91 [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 saraswald9822e92016-04-05 15:13:44 +0530127 * Returns the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530128 *
129 * @return the data type info of attribute
130 */
131 public YangType<?> getAttributeType() {
132
133 if (attrType == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530134 throw new TranslatorException("Expected java attribute type is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 }
136 return attrType;
137 }
138
139 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530140 * Sets the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530141 *
142 * @param type the data type info of attribute
143 */
144 public void setAttributeType(YangType<?> type) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530145 attrType = type;
146 }
147
148 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530149 * Returns name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530150 *
151 * @return name of the attribute
152 */
153 public String getAttributeName() {
154
155 if (name == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530156 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530157 }
158 return name;
159 }
160
161 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530162 * Sets name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530163 *
164 * @param attrName name of the attribute
165 */
166 public void setAttributeName(String attrName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530167 name = attrName;
168 }
169
170 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530171 * Returns if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530172 *
173 * @return the if the added attribute is a list of info
174 */
175 public boolean isListAttr() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530176 return isListAttr;
177 }
178
179 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530180 * Sets if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530181 *
182 * @param isList if the added attribute is a list of info
183 */
184 public void setListAttr(boolean isList) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530185 isListAttr = isList;
186 }
187
188 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530189 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530190 * manner.
191 *
192 * @return the if the added attribute has to be accessed in a fully
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530193 * qualified manner.
Vinod Kumar S38046502016-03-23 15:30:27 +0530194 */
195 public boolean isQualifiedName() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530196 return isQualifiedName;
197 }
198
199 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530200 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530201 * manner.
202 *
203 * @param isQualified if the added attribute has to be accessed in a fully
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530204 * qualified manner
Vinod Kumar S38046502016-03-23 15:30:27 +0530205 */
206 public void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530207 isQualifiedName = isQualified;
208 }
209
210 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530211 * Returns the import info for the attribute type. It will be null, if the type
Vinod Kumar S38046502016-03-23 15:30:27 +0530212 * is basic built-in java type.
213 *
214 * @return import info
215 */
216 public JavaQualifiedTypeInfo getImportInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530217 return importInfo;
218 }
219
220 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530221 * Sets the import info for the attribute type.
Vinod Kumar S38046502016-03-23 15:30:27 +0530222 *
223 * @param importInfo import info for the attribute type
224 */
225 public void setImportInfo(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530226 this.importInfo = importInfo;
227 }
228
229 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530230 * Creates an attribute info object corresponding to the passed leaf
Vinod Kumar S38046502016-03-23 15:30:27 +0530231 * information and return it.
232 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530233 * @param curNode current data model node for which the java file is being
234 * generated
235 * @param attributeType leaf data type
236 * @param attributeName leaf name
Vinod Kumar S38046502016-03-23 15:30:27 +0530237 * @param isListAttribute is the current added attribute needs to be a list
238 * @return AttributeInfo attribute details required to add in temporary
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530239 * files
Vinod Kumar S38046502016-03-23 15:30:27 +0530240 */
241 public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530242 YangType<?> attributeType, String attributeName,
243 boolean isListAttribute) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530244
Vinod Kumar S38046502016-03-23 15:30:27 +0530245 /*
246 * Get the import info corresponding to the attribute for import in
247 * generated java files or qualified access
248 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530249 JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfAttribute(curNode,
Vinod Kumar S38046502016-03-23 15:30:27 +0530250 attributeType, attributeName, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530251
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530252 return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530253 }
254
255 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530256 * Creates an attribute info object corresponding to a data model node and
Vinod Kumar S38046502016-03-23 15:30:27 +0530257 * return it.
258 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530259 * @param curNode current data model node for which the java code generation
260 * is being handled
Vinod Kumar S38046502016-03-23 15:30:27 +0530261 * @param parentNode parent node in which the current node is an attribute
262 * @param isListNode is the current added attribute needs to be a list
263 * @return AttributeInfo attribute details required to add in temporary
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530264 * files
Vinod Kumar S38046502016-03-23 15:30:27 +0530265 */
266 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
267 YangNode curNode, YangNode parentNode, boolean isListNode) {
268
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName();
270
271 /*
272 * Get the import info corresponding to the attribute for import in
273 * generated java files or qualified access
274 */
275 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
276 curNodeName, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530277
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530278 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530279 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530280
281 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530282 * Returns java attribute info.
283 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530284 * @param importInfo java qualified type info
285 * @param attributeName attribute name
286 * @param attributeType attribute type
287 * @param curNode current YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530288 * @param isListAttribute is list attribute
289 * @return java attribute info.
290 */
291 private static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530292 YangType<?> attributeType, YangNode curNode,
293 boolean isListAttribute) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530294
295 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530296 newAttr.setImportInfo(importInfo);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530297 newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(curNode, importInfo));
janani bde4ffab2016-04-15 16:18:30 +0530298 newAttr.setAttributeName(getCamelCase(attributeName, null));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530299 newAttr.setListAttr(isListAttribute);
300 newAttr.setImportInfo(importInfo);
301 newAttr.setAttributeType(attributeType);
302
303 return newAttr;
304 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530305}