Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.YangNode; |
| 20 | import org.onosproject.yangutils.datamodel.YangType; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | |
| 23 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 24 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString; |
| 25 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfAttribute; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 28 | |
| 29 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 30 | * Represents the attribute info corresponding to class/interface generated. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 31 | */ |
| 32 | public 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 saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 61 | * Creates a java attribute info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 62 | */ |
| 63 | private JavaAttributeInfo() { |
| 64 | } |
| 65 | |
| 66 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 67 | * Creates object of java attribute info. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 68 | * |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 69 | * @param attrType YANG type |
| 70 | * @param name attribute name |
| 71 | * @param isListAttr is list attribute |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 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 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 82 | * 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 saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 127 | * 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 saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 150 | * Returns the data type info of attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 151 | * |
| 152 | * @return the data type info of attribute |
| 153 | */ |
| 154 | public YangType<?> getAttributeType() { |
| 155 | |
| 156 | if (attrType == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 157 | throw new TranslatorException("Expected java attribute type is null"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 158 | } |
| 159 | return attrType; |
| 160 | } |
| 161 | |
| 162 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 163 | * Sets the data type info of attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 164 | * |
| 165 | * @param type the data type info of attribute |
| 166 | */ |
| 167 | public void setAttributeType(YangType<?> type) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 168 | attrType = type; |
| 169 | } |
| 170 | |
| 171 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 172 | * Returns name of the attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 173 | * |
| 174 | * @return name of the attribute |
| 175 | */ |
| 176 | public String getAttributeName() { |
| 177 | |
| 178 | if (name == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 179 | throw new TranslatorException("Expected java attribute name is null"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 180 | } |
| 181 | return name; |
| 182 | } |
| 183 | |
| 184 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 185 | * Sets name of the attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 186 | * |
| 187 | * @param attrName name of the attribute |
| 188 | */ |
| 189 | public void setAttributeName(String attrName) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 190 | name = attrName; |
| 191 | } |
| 192 | |
| 193 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 194 | * Returns if the added attribute is a list of info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 195 | * |
| 196 | * @return the if the added attribute is a list of info |
| 197 | */ |
| 198 | public boolean isListAttr() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 199 | return isListAttr; |
| 200 | } |
| 201 | |
| 202 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 203 | * Sets if the added attribute is a list of info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 204 | * |
| 205 | * @param isList if the added attribute is a list of info |
| 206 | */ |
| 207 | public void setListAttr(boolean isList) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 208 | isListAttr = isList; |
| 209 | } |
| 210 | |
| 211 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 212 | * Returns if the added attribute has to be accessed in a fully qualified |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 213 | * manner. |
| 214 | * |
| 215 | * @return the if the added attribute has to be accessed in a fully |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 216 | * qualified manner. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 217 | */ |
| 218 | public boolean isQualifiedName() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 219 | return isQualifiedName; |
| 220 | } |
| 221 | |
| 222 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 223 | * Sets if the added attribute has to be accessed in a fully qualified |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 224 | * manner. |
| 225 | * |
| 226 | * @param isQualified if the added attribute has to be accessed in a fully |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 227 | * qualified manner |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 228 | */ |
| 229 | public void setIsQualifiedAccess(boolean isQualified) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 230 | isQualifiedName = isQualified; |
| 231 | } |
| 232 | |
| 233 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 234 | * Returns the import info for the attribute type. It will be null, if the type |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 235 | * is basic built-in java type. |
| 236 | * |
| 237 | * @return import info |
| 238 | */ |
| 239 | public JavaQualifiedTypeInfo getImportInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 240 | return importInfo; |
| 241 | } |
| 242 | |
| 243 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 244 | * Sets the import info for the attribute type. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 245 | * |
| 246 | * @param importInfo import info for the attribute type |
| 247 | */ |
| 248 | public void setImportInfo(JavaQualifiedTypeInfo importInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 249 | this.importInfo = importInfo; |
| 250 | } |
| 251 | |
| 252 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 253 | * Creates an attribute info object corresponding to the passed leaf |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 254 | * information and return it. |
| 255 | * |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 256 | * @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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 260 | * @param isListAttribute is the current added attribute needs to be a list |
| 261 | * @return AttributeInfo attribute details required to add in temporary |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 262 | * files |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 263 | */ |
| 264 | public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 265 | YangType<?> attributeType, String attributeName, |
| 266 | boolean isListAttribute) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 267 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 268 | /* |
| 269 | * Get the import info corresponding to the attribute for import in |
| 270 | * generated java files or qualified access |
| 271 | */ |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 272 | JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfAttribute(curNode, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 273 | attributeType, attributeName, isListAttribute); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 274 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 275 | return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 279 | * Creates an attribute info object corresponding to a data model node and |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 280 | * return it. |
| 281 | * |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 282 | * @param curNode current data model node for which the java code generation |
| 283 | * is being handled |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 284 | * @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 Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 287 | * files |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 288 | */ |
| 289 | public static JavaAttributeInfo getCurNodeAsAttributeInParent( |
| 290 | YangNode curNode, YangNode parentNode, boolean isListNode) { |
| 291 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 292 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 300 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 301 | return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 302 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 303 | |
| 304 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 305 | * Returns java attribute info. |
| 306 | * |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 307 | * @param importInfo java qualified type info |
| 308 | * @param attributeName attribute name |
| 309 | * @param attributeType attribute type |
| 310 | * @param curNode current YANG node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 311 | * @param isListAttribute is list attribute |
| 312 | * @return java attribute info. |
| 313 | */ |
| 314 | private static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 315 | YangType<?> attributeType, YangNode curNode, |
| 316 | boolean isListAttribute) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 317 | |
| 318 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 319 | newAttr.setImportInfo(importInfo); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 320 | newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(curNode, importInfo)); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 321 | newAttr.setAttributeName(getCamelCase(attributeName, null)); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 322 | newAttr.setListAttr(isListAttribute); |
| 323 | newAttr.setImportInfo(importInfo); |
| 324 | newAttr.setAttributeType(attributeType); |
| 325 | |
| 326 | return newAttr; |
| 327 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 328 | } |