Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 3804650 | 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 | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | |
| 23 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList; |
Gaurav Agrawal | 338735b | 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 | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 28 | |
| 29 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 30 | * Represents the attribute info corresponding to class/interface generated. |
Vinod Kumar S | 3804650 | 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 | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 61 | * Creates a java attribute info object. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 62 | */ |
| 63 | private JavaAttributeInfo() { |
| 64 | } |
| 65 | |
| 66 | /** |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 67 | * Creates object of java attribute info. |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 68 | * |
Gaurav Agrawal | 338735b | 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 | d6f1241 | 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 | 338735b | 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 | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 127 | * Returns the data type info of attribute. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 128 | * |
| 129 | * @return the data type info of attribute |
| 130 | */ |
| 131 | public YangType<?> getAttributeType() { |
| 132 | |
| 133 | if (attrType == null) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 134 | throw new TranslatorException("Expected java attribute type is null"); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 135 | } |
| 136 | return attrType; |
| 137 | } |
| 138 | |
| 139 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 140 | * Sets the data type info of attribute. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 141 | * |
| 142 | * @param type the data type info of attribute |
| 143 | */ |
| 144 | public void setAttributeType(YangType<?> type) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 145 | attrType = type; |
| 146 | } |
| 147 | |
| 148 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 149 | * Returns name of the attribute. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 150 | * |
| 151 | * @return name of the attribute |
| 152 | */ |
| 153 | public String getAttributeName() { |
| 154 | |
| 155 | if (name == null) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 156 | throw new TranslatorException("Expected java attribute name is null"); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 157 | } |
| 158 | return name; |
| 159 | } |
| 160 | |
| 161 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 162 | * Sets name of the attribute. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 163 | * |
| 164 | * @param attrName name of the attribute |
| 165 | */ |
| 166 | public void setAttributeName(String attrName) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 167 | name = attrName; |
| 168 | } |
| 169 | |
| 170 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 171 | * Returns if the added attribute is a list of info. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 172 | * |
| 173 | * @return the if the added attribute is a list of info |
| 174 | */ |
| 175 | public boolean isListAttr() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 176 | return isListAttr; |
| 177 | } |
| 178 | |
| 179 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 180 | * Sets if the added attribute is a list of info. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 181 | * |
| 182 | * @param isList if the added attribute is a list of info |
| 183 | */ |
| 184 | public void setListAttr(boolean isList) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 185 | isListAttr = isList; |
| 186 | } |
| 187 | |
| 188 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 189 | * Returns if the added attribute has to be accessed in a fully qualified |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 190 | * manner. |
| 191 | * |
| 192 | * @return the if the added attribute has to be accessed in a fully |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 193 | * qualified manner. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 194 | */ |
| 195 | public boolean isQualifiedName() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 196 | return isQualifiedName; |
| 197 | } |
| 198 | |
| 199 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 200 | * Sets if the added attribute has to be accessed in a fully qualified |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 201 | * manner. |
| 202 | * |
| 203 | * @param isQualified if the added attribute has to be accessed in a fully |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 204 | * qualified manner |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 205 | */ |
| 206 | public void setIsQualifiedAccess(boolean isQualified) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 207 | isQualifiedName = isQualified; |
| 208 | } |
| 209 | |
| 210 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 211 | * Returns the import info for the attribute type. It will be null, if the type |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 212 | * is basic built-in java type. |
| 213 | * |
| 214 | * @return import info |
| 215 | */ |
| 216 | public JavaQualifiedTypeInfo getImportInfo() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 217 | return importInfo; |
| 218 | } |
| 219 | |
| 220 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 221 | * Sets the import info for the attribute type. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 222 | * |
| 223 | * @param importInfo import info for the attribute type |
| 224 | */ |
| 225 | public void setImportInfo(JavaQualifiedTypeInfo importInfo) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 226 | this.importInfo = importInfo; |
| 227 | } |
| 228 | |
| 229 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 230 | * Creates an attribute info object corresponding to the passed leaf |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 231 | * information and return it. |
| 232 | * |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 233 | * @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 S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 237 | * @param isListAttribute is the current added attribute needs to be a list |
| 238 | * @return AttributeInfo attribute details required to add in temporary |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 239 | * files |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 240 | */ |
| 241 | public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode, |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 242 | YangType<?> attributeType, String attributeName, |
| 243 | boolean isListAttribute) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 244 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 245 | /* |
| 246 | * Get the import info corresponding to the attribute for import in |
| 247 | * generated java files or qualified access |
| 248 | */ |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 249 | JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfAttribute(curNode, |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 250 | attributeType, attributeName, isListAttribute); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 251 | |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 252 | return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 256 | * Creates an attribute info object corresponding to a data model node and |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 257 | * return it. |
| 258 | * |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 259 | * @param curNode current data model node for which the java code generation |
| 260 | * is being handled |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 261 | * @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 Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 264 | * files |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 265 | */ |
| 266 | public static JavaAttributeInfo getCurNodeAsAttributeInParent( |
| 267 | YangNode curNode, YangNode parentNode, boolean isListNode) { |
| 268 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 269 | 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 S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 277 | |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 278 | return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 279 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 280 | |
| 281 | /** |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 282 | * Returns java attribute info. |
| 283 | * |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 284 | * @param importInfo java qualified type info |
| 285 | * @param attributeName attribute name |
| 286 | * @param attributeType attribute type |
| 287 | * @param curNode current YANG node |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 288 | * @param isListAttribute is list attribute |
| 289 | * @return java attribute info. |
| 290 | */ |
| 291 | private static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName, |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame^] | 292 | YangType<?> attributeType, YangNode curNode, |
| 293 | boolean isListAttribute) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 294 | |
| 295 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 296 | newAttr.setImportInfo(importInfo); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 297 | newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(curNode, importInfo)); |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 298 | newAttr.setAttributeName(getCamelCase(attributeName, null)); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 299 | newAttr.setListAttr(isListAttribute); |
| 300 | newAttr.setImportInfo(importInfo); |
| 301 | newAttr.setAttributeType(attributeType); |
| 302 | |
| 303 | return newAttr; |
| 304 | } |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 305 | } |