blob: f09b98ff06cdb2b9b3167ee0c344ce82354f2b7f [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;
Bharat saraswale2d51d62016-03-23 19:40:35 +053022import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
Vinod Kumar S38046502016-03-23 15:30:27 +053023
24import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList;
25import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
26import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfLeafAttribute;
27import 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 *
69 * @param attrType YANG type
70 * @param name attribute name
71 * @param isListAttr is list attribute
72 * @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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053082 * Returns the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053083 *
84 * @return the data type info of attribute
85 */
86 public YangType<?> getAttributeType() {
87
88 if (attrType == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053089 throw new TranslatorException("Expected java attribute type is null");
Vinod Kumar S38046502016-03-23 15:30:27 +053090 }
91 return attrType;
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Sets the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053096 *
97 * @param type the data type info of attribute
98 */
99 public void setAttributeType(YangType<?> type) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 attrType = type;
101 }
102
103 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 * Returns name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 *
106 * @return name of the attribute
107 */
108 public String getAttributeName() {
109
110 if (name == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530111 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 }
113 return name;
114 }
115
116 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530117 * Sets name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 *
119 * @param attrName name of the attribute
120 */
121 public void setAttributeName(String attrName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 name = attrName;
123 }
124
125 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530126 * Returns if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530127 *
128 * @return the if the added attribute is a list of info
129 */
130 public boolean isListAttr() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530131 return isListAttr;
132 }
133
134 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530135 * Sets if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530136 *
137 * @param isList if the added attribute is a list of info
138 */
139 public void setListAttr(boolean isList) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530140 isListAttr = isList;
141 }
142
143 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530144 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530145 * manner.
146 *
147 * @return the if the added attribute has to be accessed in a fully
148 * qualified manner.
149 */
150 public boolean isQualifiedName() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530151 return isQualifiedName;
152 }
153
154 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530155 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530156 * manner.
157 *
158 * @param isQualified if the added attribute has to be accessed in a fully
159 * qualified manner
160 */
161 public void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530162 isQualifiedName = isQualified;
163 }
164
165 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530166 * Returns the import info for the attribute type. It will be null, of the type
Vinod Kumar S38046502016-03-23 15:30:27 +0530167 * is basic built-in java type.
168 *
169 * @return import info
170 */
171 public JavaQualifiedTypeInfo getImportInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530172 return importInfo;
173 }
174
175 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530176 * Sets the import info for the attribute type.
Vinod Kumar S38046502016-03-23 15:30:27 +0530177 *
178 * @param importInfo import info for the attribute type
179 */
180 public void setImportInfo(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530181 this.importInfo = importInfo;
182 }
183
184 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530185 * Creates an attribute info object corresponding to the passed leaf
Vinod Kumar S38046502016-03-23 15:30:27 +0530186 * information and return it.
187 *
188 * @param curNode current data model node for which the java file is being
189 * generated
190 * @param attributeType leaf data type
191 * @param attributeName leaf name
192 * @param isListAttribute is the current added attribute needs to be a list
193 * @return AttributeInfo attribute details required to add in temporary
194 * files
195 */
196 public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode,
197 YangType<?> attributeType, String attributeName,
198 boolean isListAttribute) {
199
Vinod Kumar S38046502016-03-23 15:30:27 +0530200 /*
201 * Get the import info corresponding to the attribute for import in
202 * generated java files or qualified access
203 */
204 JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
205 attributeType, attributeName, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530206
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530207 return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530208 }
209
210 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530211 * Creates an attribute info object corresponding to a data model node and
Vinod Kumar S38046502016-03-23 15:30:27 +0530212 * return it.
213 *
214 * @param curNode current data model node for which the java code generation
215 * is being handled
216 * @param parentNode parent node in which the current node is an attribute
217 * @param isListNode is the current added attribute needs to be a list
218 * @return AttributeInfo attribute details required to add in temporary
219 * files
220 */
221 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
222 YangNode curNode, YangNode parentNode, boolean isListNode) {
223
Vinod Kumar S38046502016-03-23 15:30:27 +0530224 String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName();
225
226 /*
227 * Get the import info corresponding to the attribute for import in
228 * generated java files or qualified access
229 */
230 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
231 curNodeName, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530232
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530233 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530234 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530235
236 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530237 * Creates an attribute info object corresponding to the passed type def attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530238 * information and return it.
239 *
240 * @param curNode current data model node for which the java file is being
241 * generated
242 * @param attributeType leaf data type
243 * @param attributeName leaf name
244 * @param isListAttribute is the current added attribute needs to be a list
245 * @return AttributeInfo attribute details required to add in temporary
246 * files
247 */
248 public static JavaAttributeInfo getAttributeInfoOfTypeDef(YangNode curNode,
249 YangType<?> attributeType, String attributeName,
250 boolean isListAttribute) {
251
Bharat saraswale2d51d62016-03-23 19:40:35 +0530252 /*
253 * Get the import info corresponding to the attribute for import in
254 * generated java files or qualified access
255 */
256 JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
257 attributeType, attributeName, isListAttribute);
258 AttributesJavaDataType.addImportInfo(importInfo);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530259
260 return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
261 }
262
263 /**
264 * Returns java attribute info.
265 *
266 * @param importInfo java qualified type info
267 * @param attributeName attribute name
268 * @param attributeType attribute type
269 * @param curNode current YANG node
270 * @param isListAttribute is list attribute
271 * @return java attribute info.
272 */
273 private static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName,
274 YangType<?> attributeType, YangNode curNode, boolean isListAttribute) {
275
276 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530277 newAttr.setImportInfo(importInfo);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530278 newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(curNode, importInfo));
janani bde4ffab2016-04-15 16:18:30 +0530279 newAttr.setAttributeName(getCamelCase(attributeName, null));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530280 newAttr.setListAttr(isListAttribute);
281 newAttr.setImportInfo(importInfo);
282 newAttr.setAttributeType(attributeType);
283
284 return newAttr;
285 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530286}