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; |
| 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 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 29 | * Represents the attribute info corresponding to class/interface generated. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 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 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 60 | * Creates a java attribute info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 61 | */ |
| 62 | private JavaAttributeInfo() { |
| 63 | } |
| 64 | |
| 65 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 66 | * Creates object of java attribute info. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 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 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 81 | * Returns the data type info of attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 82 | * |
| 83 | * @return the data type info of attribute |
| 84 | */ |
| 85 | public YangType<?> getAttributeType() { |
| 86 | |
| 87 | if (attrType == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 88 | throw new TranslatorException("Expected java attribute type is null"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | } |
| 90 | return attrType; |
| 91 | } |
| 92 | |
| 93 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 94 | * Sets the data type info of attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 95 | * |
| 96 | * @param type the data type info of attribute |
| 97 | */ |
| 98 | public void setAttributeType(YangType<?> type) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 99 | attrType = type; |
| 100 | } |
| 101 | |
| 102 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 103 | * Returns name of the attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 104 | * |
| 105 | * @return name of the attribute |
| 106 | */ |
| 107 | public String getAttributeName() { |
| 108 | |
| 109 | if (name == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 110 | throw new TranslatorException("Expected java attribute name is null"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 111 | } |
| 112 | return name; |
| 113 | } |
| 114 | |
| 115 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 116 | * Sets name of the attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 117 | * |
| 118 | * @param attrName name of the attribute |
| 119 | */ |
| 120 | public void setAttributeName(String attrName) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 121 | name = attrName; |
| 122 | } |
| 123 | |
| 124 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 125 | * Returns if the added attribute is a list of info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 126 | * |
| 127 | * @return the if the added attribute is a list of info |
| 128 | */ |
| 129 | public boolean isListAttr() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 130 | return isListAttr; |
| 131 | } |
| 132 | |
| 133 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 134 | * Sets if the added attribute is a list of info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 135 | * |
| 136 | * @param isList if the added attribute is a list of info |
| 137 | */ |
| 138 | public void setListAttr(boolean isList) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 139 | isListAttr = isList; |
| 140 | } |
| 141 | |
| 142 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 143 | * 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] | 144 | * 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 150 | return isQualifiedName; |
| 151 | } |
| 152 | |
| 153 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 154 | * 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] | 155 | * 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 161 | isQualifiedName = isQualified; |
| 162 | } |
| 163 | |
| 164 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 165 | * 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] | 166 | * is basic built-in java type. |
| 167 | * |
| 168 | * @return import info |
| 169 | */ |
| 170 | public JavaQualifiedTypeInfo getImportInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 171 | return importInfo; |
| 172 | } |
| 173 | |
| 174 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 175 | * Sets the import info for the attribute type. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 176 | * |
| 177 | * @param importInfo import info for the attribute type |
| 178 | */ |
| 179 | public void setImportInfo(JavaQualifiedTypeInfo importInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 180 | this.importInfo = importInfo; |
| 181 | } |
| 182 | |
| 183 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 184 | * Creates an attribute info object corresponding to the passed leaf |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 185 | * 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 199 | /* |
| 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 205 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 206 | return getAttributeInfoForTheData(importInfo, attributeName, attributeType, curNode, isListAttribute); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 210 | * 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] | 211 | * 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 223 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 231 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 232 | return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, parentNode, isListNode); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 233 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 234 | |
| 235 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 236 | * Creates an attribute info object corresponding to the passed type def attribute |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 237 | * 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 saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 251 | /* |
| 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 saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 257 | |
| 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 saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 275 | newAttr.setImportInfo(importInfo); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 276 | newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(curNode, importInfo)); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 277 | newAttr.setAttributeName(getCamelCase(attributeName, null)); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 278 | newAttr.setListAttr(isListAttribute); |
| 279 | newAttr.setImportInfo(importInfo); |
| 280 | newAttr.setAttributeType(attributeType); |
| 281 | |
| 282 | return newAttr; |
| 283 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 284 | } |