blob: b652f48c4a7dd239666bcbc0807ad8f10edf3dc1 [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;
24import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
25import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfLeafAttribute;
26import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
27
28/**
Bharat saraswald9822e92016-04-05 15:13:44 +053029 * Represents the attribute info corresponding to class/interface generated.
Vinod Kumar S38046502016-03-23 15:30:27 +053030 */
31public final class JavaAttributeInfo {
32
33 /**
34 * The data type info of attribute.
35 */
36 private YangType<?> attrType;
37
38 /**
39 * Name of the attribute.
40 */
41 private String name;
42
43 /**
44 * If the added attribute is a list of info.
45 */
46 private boolean isListAttr = false;
47
48 /**
49 * If the added attribute has to be accessed in a fully qualified manner.
50 */
51 private boolean isQualifiedName = false;
52
53 /**
54 * The class info will be used to set the attribute type and package info
55 * will be use for qualified name.
56 */
57 private JavaQualifiedTypeInfo importInfo;
58
59 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Creates a java attribute info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053061 */
62 private JavaAttributeInfo() {
63 }
64
65 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053066 * Creates object of java attribute info.
Bharat saraswald6f12412016-03-28 15:50:13 +053067 *
68 * @param attrType YANG type
69 * @param name attribute name
70 * @param isListAttr is list attribute
71 * @param isQualifiedName is qualified name
72 */
73 public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) {
74 this.attrType = attrType;
75 this.name = name;
76 this.isListAttr = isListAttr;
77 this.isQualifiedName = isQualifiedName;
78 }
79
80 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053081 * Returns the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053082 *
83 * @return the data type info of attribute
84 */
85 public YangType<?> getAttributeType() {
86
87 if (attrType == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053088 throw new TranslatorException("Expected java attribute type is null");
Vinod Kumar S38046502016-03-23 15:30:27 +053089 }
90 return attrType;
91 }
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Sets the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053095 *
96 * @param type the data type info of attribute
97 */
98 public void setAttributeType(YangType<?> type) {
Vinod Kumar S38046502016-03-23 15:30:27 +053099 attrType = type;
100 }
101
102 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530103 * Returns name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530104 *
105 * @return name of the attribute
106 */
107 public String getAttributeName() {
108
109 if (name == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530110 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 }
112 return name;
113 }
114
115 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530116 * Sets name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 *
118 * @param attrName name of the attribute
119 */
120 public void setAttributeName(String attrName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 name = attrName;
122 }
123
124 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530125 * Returns if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530126 *
127 * @return the if the added attribute is a list of info
128 */
129 public boolean isListAttr() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530130 return isListAttr;
131 }
132
133 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530134 * Sets if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 *
136 * @param isList if the added attribute is a list of info
137 */
138 public void setListAttr(boolean isList) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530139 isListAttr = isList;
140 }
141
142 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530143 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530144 * manner.
145 *
146 * @return the if the added attribute has to be accessed in a fully
147 * qualified manner.
148 */
149 public boolean isQualifiedName() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530150 return isQualifiedName;
151 }
152
153 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530154 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 * manner.
156 *
157 * @param isQualified if the added attribute has to be accessed in a fully
158 * qualified manner
159 */
160 public void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530161 isQualifiedName = isQualified;
162 }
163
164 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530165 * Returns the import info for the attribute type. It will be null, if the type
Vinod Kumar S38046502016-03-23 15:30:27 +0530166 * is basic built-in java type.
167 *
168 * @return import info
169 */
170 public JavaQualifiedTypeInfo getImportInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530171 return importInfo;
172 }
173
174 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530175 * Sets the import info for the attribute type.
Vinod Kumar S38046502016-03-23 15:30:27 +0530176 *
177 * @param importInfo import info for the attribute type
178 */
179 public void setImportInfo(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530180 this.importInfo = importInfo;
181 }
182
183 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530184 * Creates an attribute info object corresponding to the passed leaf
Vinod Kumar S38046502016-03-23 15:30:27 +0530185 * information and return it.
186 *
187 * @param curNode current data model node for which the java file is being
188 * generated
189 * @param attributeType leaf data type
190 * @param attributeName leaf name
191 * @param isListAttribute is the current added attribute needs to be a list
192 * @return AttributeInfo attribute details required to add in temporary
193 * files
194 */
195 public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode,
196 YangType<?> attributeType, String attributeName,
197 boolean isListAttribute) {
198
Vinod Kumar S38046502016-03-23 15:30:27 +0530199 /*
200 * Get the import info corresponding to the attribute for import in
201 * generated java files or qualified access
202 */
203 JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
204 attributeType, attributeName, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530205
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530206 return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
Vinod Kumar S38046502016-03-23 15:30:27 +0530207 }
208
209 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530210 * Creates an attribute info object corresponding to a data model node and
Vinod Kumar S38046502016-03-23 15:30:27 +0530211 * return it.
212 *
213 * @param curNode current data model node for which the java code generation
214 * is being handled
215 * @param parentNode parent node in which the current node is an attribute
216 * @param isListNode is the current added attribute needs to be a list
217 * @return AttributeInfo attribute details required to add in temporary
218 * files
219 */
220 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
221 YangNode curNode, YangNode parentNode, boolean isListNode) {
222
Vinod Kumar S38046502016-03-23 15:30:27 +0530223 String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName();
224
225 /*
226 * Get the import info corresponding to the attribute for import in
227 * generated java files or qualified access
228 */
229 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
230 curNodeName, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530231
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530232 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode);
Vinod Kumar S38046502016-03-23 15:30:27 +0530233 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530234
235 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530236 * Creates an attribute info object corresponding to the passed type def attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530237 * information and return it.
238 *
239 * @param curNode current data model node for which the java file is being
240 * generated
241 * @param attributeType leaf data type
242 * @param attributeName leaf name
243 * @param isListAttribute is the current added attribute needs to be a list
244 * @return AttributeInfo attribute details required to add in temporary
245 * files
246 */
247 public static JavaAttributeInfo getAttributeInfoOfTypeDef(YangNode curNode,
248 YangType<?> attributeType, String attributeName,
249 boolean isListAttribute) {
250
Bharat saraswale2d51d62016-03-23 19:40:35 +0530251 /*
252 * Get the import info corresponding to the attribute for import in
253 * generated java files or qualified access
254 */
255 JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
256 attributeType, attributeName, isListAttribute);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530257
258 return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute);
259 }
260
261 /**
262 * Returns java attribute info.
263 *
264 * @param importInfo java qualified type info
265 * @param attributeName attribute name
266 * @param attributeType attribute type
267 * @param curNode current YANG node
268 * @param isListAttribute is list attribute
269 * @return java attribute info.
270 */
271 private static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName,
272 YangType<?> attributeType, YangNode curNode, boolean isListAttribute) {
273
274 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530275 newAttr.setImportInfo(importInfo);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530276 newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(curNode, importInfo));
janani bde4ffab2016-04-15 16:18:30 +0530277 newAttr.setAttributeName(getCamelCase(attributeName, null));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530278 newAttr.setListAttr(isListAttribute);
279 newAttr.setImportInfo(importInfo);
280 newAttr.setAttributeType(attributeType);
281
282 return newAttr;
283 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530284}