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