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