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 | |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangCompilerAnnotation; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 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 | |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 23 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeLeafref; |
| 24 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeNameLeafref; |
| 25 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 27 | * Represents the attribute info corresponding to class/interface generated. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 28 | */ |
| 29 | public final class JavaAttributeInfo { |
| 30 | |
| 31 | /** |
| 32 | * The data type info of attribute. |
| 33 | */ |
| 34 | private YangType<?> attrType; |
| 35 | |
| 36 | /** |
| 37 | * Name of the attribute. |
| 38 | */ |
| 39 | private String name; |
| 40 | |
| 41 | /** |
| 42 | * If the added attribute is a list of info. |
| 43 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 44 | private boolean isListAttr; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * If the added attribute has to be accessed in a fully qualified manner. |
| 48 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 49 | private boolean isQualifiedName; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * The class info will be used to set the attribute type and package info |
| 53 | * will be use for qualified name. |
| 54 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 55 | private JavaQualifiedTypeInfoTranslator importInfo; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 56 | |
| 57 | /** |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 58 | * Compiler annotation attribute info. |
| 59 | */ |
| 60 | private YangCompilerAnnotation compilerAnnotation; |
| 61 | |
| 62 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 63 | * If conflict occurs. |
| 64 | */ |
| 65 | private boolean isIntConflict; |
| 66 | |
| 67 | /** |
| 68 | * If conflict occurs. |
| 69 | */ |
| 70 | private boolean isLongConflict; |
| 71 | |
| 72 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 73 | * Creates a java attribute info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 74 | */ |
| 75 | private JavaAttributeInfo() { |
| 76 | } |
| 77 | |
| 78 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 79 | * Creates object of java attribute info. |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 80 | * |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 81 | * @param attrType YANG type |
| 82 | * @param name attribute name |
| 83 | * @param isListAttr is list attribute |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 84 | * @param isQualifiedName is qualified name |
| 85 | */ |
| 86 | public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) { |
| 87 | this.attrType = attrType; |
| 88 | this.name = name; |
| 89 | this.isListAttr = isListAttr; |
| 90 | this.isQualifiedName = isQualifiedName; |
| 91 | } |
| 92 | |
| 93 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 94 | * Returns the data type info of attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 95 | * |
| 96 | * @return the data type info of attribute |
| 97 | */ |
| 98 | public YangType<?> getAttributeType() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 99 | return attrType; |
| 100 | } |
| 101 | |
| 102 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 103 | * Sets the data type info of attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 104 | * |
| 105 | * @param type the data type info of attribute |
| 106 | */ |
| 107 | public void setAttributeType(YangType<?> type) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 108 | attrType = type; |
| 109 | } |
| 110 | |
| 111 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 112 | * Returns name of the attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 113 | * |
| 114 | * @return name of the attribute |
| 115 | */ |
| 116 | public String getAttributeName() { |
| 117 | |
| 118 | if (name == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 119 | throw new TranslatorException("Expected java attribute name is null"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 120 | } |
| 121 | return name; |
| 122 | } |
| 123 | |
| 124 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 125 | * Sets name of the attribute. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 126 | * |
| 127 | * @param attrName name of the attribute |
| 128 | */ |
| 129 | public void setAttributeName(String attrName) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 130 | name = attrName; |
| 131 | } |
| 132 | |
| 133 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 134 | * Returns if the added attribute is a list of info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 135 | * |
| 136 | * @return the if the added attribute is a list of info |
| 137 | */ |
| 138 | public boolean isListAttr() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 139 | return isListAttr; |
| 140 | } |
| 141 | |
| 142 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 143 | * Sets if the added attribute is a list of info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 144 | * |
| 145 | * @param isList if the added attribute is a list of info |
| 146 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 147 | private void setListAttr(boolean isList) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 148 | isListAttr = isList; |
| 149 | } |
| 150 | |
| 151 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 152 | * 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] | 153 | * manner. |
| 154 | * |
| 155 | * @return the if the added attribute has to be accessed in a fully |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 156 | * qualified manner. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 157 | */ |
| 158 | public boolean isQualifiedName() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 159 | return isQualifiedName; |
| 160 | } |
| 161 | |
| 162 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 163 | * 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] | 164 | * manner. |
| 165 | * |
| 166 | * @param isQualified if the added attribute has to be accessed in a fully |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 167 | * qualified manner |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 168 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 169 | private void setIsQualifiedAccess(boolean isQualified) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 170 | isQualifiedName = isQualified; |
| 171 | } |
| 172 | |
| 173 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 174 | * 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] | 175 | * is basic built-in java type. |
| 176 | * |
| 177 | * @return import info |
| 178 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 179 | public JavaQualifiedTypeInfoTranslator getImportInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 180 | return importInfo; |
| 181 | } |
| 182 | |
| 183 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 184 | * Sets the import info for the attribute type. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 185 | * |
| 186 | * @param importInfo import info for the attribute type |
| 187 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 188 | public void setImportInfo(JavaQualifiedTypeInfoTranslator importInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 189 | this.importInfo = importInfo; |
| 190 | } |
| 191 | |
| 192 | /** |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 193 | * Returns the compiler annotation. |
| 194 | * |
| 195 | * @return compiler annotation info |
| 196 | */ |
| 197 | public YangCompilerAnnotation getCompilerAnnotation() { |
| 198 | return compilerAnnotation; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Sets the compiler annotation. |
| 203 | * |
| 204 | * @param compilerAnnotation the compiler annotation to set |
| 205 | */ |
| 206 | public void setCompilerAnnotation(YangCompilerAnnotation compilerAnnotation) { |
| 207 | this.compilerAnnotation = compilerAnnotation; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns true if conflict between int and uint. |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 212 | * |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 213 | * @return true if conflict between int and uInt |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 214 | */ |
| 215 | public boolean isIntConflict() { |
| 216 | return isIntConflict; |
| 217 | } |
| 218 | |
| 219 | /** |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 220 | * Sets true if conflict between int and uInt. |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 221 | * |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 222 | * @param intConflict true if conflict between int and uInt |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 223 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 224 | void setIntConflict(boolean intConflict) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 225 | isIntConflict = intConflict; |
| 226 | } |
| 227 | |
| 228 | /** |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 229 | * Returns true if conflict between long and uLong. |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 230 | * |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 231 | * @return true if conflict between long and uLong |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 232 | */ |
| 233 | public boolean isLongConflict() { |
| 234 | return isLongConflict; |
| 235 | } |
| 236 | |
| 237 | /** |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 238 | * Sets true if conflict between long and uLong. |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 239 | * |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 240 | * @param longConflict true if conflict between long and uLong |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 241 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 242 | void setLongConflict(boolean longConflict) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 243 | isLongConflict = longConflict; |
| 244 | } |
| 245 | |
| 246 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 247 | * Returns java attribute info. |
| 248 | * |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 249 | * @param importInfo java qualified type info |
| 250 | * @param attributeName attribute name |
| 251 | * @param attributeType attribute type |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 252 | * @param isQualifiedAccess is the attribute a qualified access |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 253 | * @param isListAttribute is list attribute |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 254 | * @return java attribute info. |
| 255 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 256 | public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfoTranslator importInfo, |
| 257 | String attributeName, |
| 258 | YangType<?> attributeType, boolean isQualifiedAccess, |
| 259 | boolean isListAttribute) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 260 | |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 261 | if (attributeType != null) { |
| 262 | attributeType = isTypeLeafref(attributeType); |
| 263 | } |
| 264 | attributeName = isTypeNameLeafref(attributeName, attributeType); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 265 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 266 | newAttr.setImportInfo(importInfo); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 267 | newAttr.setAttributeName(attributeName); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 268 | newAttr.setAttributeType(attributeType); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 269 | newAttr.setIsQualifiedAccess(isQualifiedAccess); |
| 270 | newAttr.setListAttr(isListAttribute); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 271 | |
| 272 | return newAttr; |
| 273 | } |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Returns java attribute info. |
| 277 | * |
| 278 | * @param importInfo java qualified type info |
| 279 | * @param attributeName attribute name |
| 280 | * @param attributeType attribute type |
| 281 | * @param isQualifiedAccess is the attribute a qualified access |
| 282 | * @param isListAttribute is list attribute |
| 283 | * @param compilerAnnotation compiler annotation |
| 284 | * @return java attribute info. |
| 285 | */ |
| 286 | public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfoTranslator importInfo, |
| 287 | String attributeName, YangType<?> attributeType, |
| 288 | boolean isQualifiedAccess, boolean isListAttribute, |
| 289 | YangCompilerAnnotation compilerAnnotation) { |
| 290 | JavaAttributeInfo newAttr = getAttributeInfoForTheData(importInfo, attributeName, attributeType, |
| 291 | isQualifiedAccess, isListAttribute); |
| 292 | |
| 293 | newAttr.setCompilerAnnotation(compilerAnnotation); |
| 294 | |
| 295 | return newAttr; |
| 296 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 297 | } |