Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | |
| 23 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList; |
| 24 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode; |
| 25 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfLeafAttribute; |
| 26 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 27 | |
| 28 | /** |
| 29 | * Maintains the attribute info corresponding to class/interface generated. |
| 30 | */ |
| 31 | public 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 | /** |
| 60 | * Default constructor. |
| 61 | */ |
| 62 | private JavaAttributeInfo() { |
| 63 | } |
| 64 | |
| 65 | /** |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 66 | * Construct object of java attribute info. |
| 67 | * |
| 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 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 81 | * Get the data type info of attribute. |
| 82 | * |
| 83 | * @return the data type info of attribute |
| 84 | */ |
| 85 | public YangType<?> getAttributeType() { |
| 86 | |
| 87 | if (attrType == null) { |
| 88 | throw new RuntimeException("Expected java attribute type is null"); |
| 89 | } |
| 90 | return attrType; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Set the data type info of attribute. |
| 95 | * |
| 96 | * @param type the data type info of attribute |
| 97 | */ |
| 98 | public void setAttributeType(YangType<?> type) { |
| 99 | |
| 100 | attrType = type; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get name of the attribute. |
| 105 | * |
| 106 | * @return name of the attribute |
| 107 | */ |
| 108 | public String getAttributeName() { |
| 109 | |
| 110 | if (name == null) { |
| 111 | throw new RuntimeException("Expected java attribute name is null"); |
| 112 | } |
| 113 | return name; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set name of the attribute. |
| 118 | * |
| 119 | * @param attrName name of the attribute |
| 120 | */ |
| 121 | public void setAttributeName(String attrName) { |
| 122 | |
| 123 | name = attrName; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get if the added attribute is a list of info. |
| 128 | * |
| 129 | * @return the if the added attribute is a list of info |
| 130 | */ |
| 131 | public boolean isListAttr() { |
| 132 | |
| 133 | return isListAttr; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Set if the added attribute is a list of info. |
| 138 | * |
| 139 | * @param isList if the added attribute is a list of info |
| 140 | */ |
| 141 | public void setListAttr(boolean isList) { |
| 142 | |
| 143 | isListAttr = isList; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get if the added attribute has to be accessed in a fully qualified |
| 148 | * manner. |
| 149 | * |
| 150 | * @return the if the added attribute has to be accessed in a fully |
| 151 | * qualified manner. |
| 152 | */ |
| 153 | public boolean isQualifiedName() { |
| 154 | |
| 155 | return isQualifiedName; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Set if the added attribute has to be accessed in a fully qualified |
| 160 | * manner. |
| 161 | * |
| 162 | * @param isQualified if the added attribute has to be accessed in a fully |
| 163 | * qualified manner |
| 164 | */ |
| 165 | public void setIsQualifiedAccess(boolean isQualified) { |
| 166 | |
| 167 | isQualifiedName = isQualified; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get the import info for the attribute type. It will be null, of the type |
| 172 | * is basic built-in java type. |
| 173 | * |
| 174 | * @return import info |
| 175 | */ |
| 176 | public JavaQualifiedTypeInfo getImportInfo() { |
| 177 | |
| 178 | return importInfo; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Set the import info for the attribute type. |
| 183 | * |
| 184 | * @param importInfo import info for the attribute type |
| 185 | */ |
| 186 | public void setImportInfo(JavaQualifiedTypeInfo importInfo) { |
| 187 | |
| 188 | this.importInfo = importInfo; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Create an attribute info object corresponding to the passed leaf |
| 193 | * information and return it. |
| 194 | * |
| 195 | * @param curNode current data model node for which the java file is being |
| 196 | * generated |
| 197 | * @param attributeType leaf data type |
| 198 | * @param attributeName leaf name |
| 199 | * @param isListAttribute is the current added attribute needs to be a list |
| 200 | * @return AttributeInfo attribute details required to add in temporary |
| 201 | * files |
| 202 | */ |
| 203 | public static JavaAttributeInfo getAttributeInfoOfLeaf(YangNode curNode, |
| 204 | YangType<?> attributeType, String attributeName, |
| 205 | boolean isListAttribute) { |
| 206 | |
| 207 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
| 208 | |
| 209 | /* |
| 210 | * Get the import info corresponding to the attribute for import in |
| 211 | * generated java files or qualified access |
| 212 | */ |
| 213 | JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode, |
| 214 | attributeType, attributeName, isListAttribute); |
| 215 | newAttr.setImportInfo(importInfo); |
| 216 | newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList( |
| 217 | curNode, importInfo)); |
| 218 | newAttr.setAttributeName(getCamelCase(attributeName)); |
| 219 | newAttr.setListAttr(isListAttribute); |
| 220 | newAttr.setImportInfo(importInfo); |
| 221 | newAttr.setAttributeType(attributeType); |
| 222 | |
| 223 | return newAttr; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Create an attribute info object corresponding to a data model node and |
| 228 | * return it. |
| 229 | * |
| 230 | * @param curNode current data model node for which the java code generation |
| 231 | * is being handled |
| 232 | * @param parentNode parent node in which the current node is an attribute |
| 233 | * @param isListNode is the current added attribute needs to be a list |
| 234 | * @return AttributeInfo attribute details required to add in temporary |
| 235 | * files |
| 236 | */ |
| 237 | public static JavaAttributeInfo getCurNodeAsAttributeInParent( |
| 238 | YangNode curNode, YangNode parentNode, boolean isListNode) { |
| 239 | |
| 240 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
| 241 | |
| 242 | // if (curNode instanceof HasJavaFileInfo) { |
| 243 | // throw new RuntimeException("translator data model node does not have java info"); |
| 244 | // } |
| 245 | |
| 246 | String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName(); |
| 247 | |
| 248 | /* |
| 249 | * Get the import info corresponding to the attribute for import in |
| 250 | * generated java files or qualified access |
| 251 | */ |
| 252 | JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode, |
| 253 | curNodeName, isListNode); |
| 254 | newAttr.setImportInfo(qualifiedTypeInfo); |
| 255 | newAttr.setIsQualifiedAccess( |
| 256 | getIsQualifiedAccessOrAddToImportList(parentNode, |
| 257 | qualifiedTypeInfo)); |
| 258 | newAttr.setAttributeName(getCamelCase(curNodeName)); |
| 259 | newAttr.setListAttr(isListNode); |
| 260 | newAttr.setImportInfo(qualifiedTypeInfo); |
| 261 | newAttr.setAttributeType(null); |
| 262 | |
| 263 | return newAttr; |
| 264 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Create an attribute info object corresponding to the passed type def attribute |
| 268 | * information and return it. |
| 269 | * |
| 270 | * @param curNode current data model node for which the java file is being |
| 271 | * generated |
| 272 | * @param attributeType leaf data type |
| 273 | * @param attributeName leaf name |
| 274 | * @param isListAttribute is the current added attribute needs to be a list |
| 275 | * @return AttributeInfo attribute details required to add in temporary |
| 276 | * files |
| 277 | */ |
| 278 | public static JavaAttributeInfo getAttributeInfoOfTypeDef(YangNode curNode, |
| 279 | YangType<?> attributeType, String attributeName, |
| 280 | boolean isListAttribute) { |
| 281 | |
| 282 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
| 283 | |
| 284 | /* |
| 285 | * Get the import info corresponding to the attribute for import in |
| 286 | * generated java files or qualified access |
| 287 | */ |
| 288 | JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode, |
| 289 | attributeType, attributeName, isListAttribute); |
| 290 | AttributesJavaDataType.addImportInfo(importInfo); |
| 291 | newAttr.setImportInfo(importInfo); |
| 292 | newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList( |
| 293 | curNode, importInfo)); |
| 294 | newAttr.setAttributeName(getCamelCase(attributeName)); |
| 295 | newAttr.setListAttr(isListAttribute); |
| 296 | newAttr.setImportInfo(importInfo); |
| 297 | newAttr.setAttributeType(attributeType); |
| 298 | |
| 299 | return newAttr; |
| 300 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 301 | } |