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