Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 3804650 | 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 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangType; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 21 | |
janani b | f9819ff | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 22 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeLeafref; |
| 23 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeNameLeafref; |
| 24 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 25 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 26 | * Represents the attribute info corresponding to class/interface generated. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | */ |
| 28 | public final class JavaAttributeInfo { |
| 29 | |
| 30 | /** |
| 31 | * The data type info of attribute. |
| 32 | */ |
| 33 | private YangType<?> attrType; |
| 34 | |
| 35 | /** |
| 36 | * Name of the attribute. |
| 37 | */ |
| 38 | private String name; |
| 39 | |
| 40 | /** |
| 41 | * If the added attribute is a list of info. |
| 42 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 43 | private boolean isListAttr; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * If the added attribute has to be accessed in a fully qualified manner. |
| 47 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 48 | private boolean isQualifiedName; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * The class info will be used to set the attribute type and package info |
| 52 | * will be use for qualified name. |
| 53 | */ |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 54 | private JavaQualifiedTypeInfoTranslator importInfo; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 55 | |
| 56 | /** |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 57 | * If conflict occurs. |
| 58 | */ |
| 59 | private boolean isIntConflict; |
| 60 | |
| 61 | /** |
| 62 | * If conflict occurs. |
| 63 | */ |
| 64 | private boolean isLongConflict; |
| 65 | |
| 66 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 67 | * Creates a java attribute info object. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 68 | */ |
| 69 | private JavaAttributeInfo() { |
| 70 | } |
| 71 | |
| 72 | /** |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 73 | * Creates object of java attribute info. |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 74 | * |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 75 | * @param attrType YANG type |
| 76 | * @param name attribute name |
| 77 | * @param isListAttr is list attribute |
Bharat saraswal | d6f1241 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 78 | * @param isQualifiedName is qualified name |
| 79 | */ |
| 80 | public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) { |
| 81 | this.attrType = attrType; |
| 82 | this.name = name; |
| 83 | this.isListAttr = isListAttr; |
| 84 | this.isQualifiedName = isQualifiedName; |
| 85 | } |
| 86 | |
| 87 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 88 | * Returns the data type info of attribute. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | * |
| 90 | * @return the data type info of attribute |
| 91 | */ |
| 92 | public YangType<?> getAttributeType() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 93 | return attrType; |
| 94 | } |
| 95 | |
| 96 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 97 | * Sets the data type info of attribute. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 98 | * |
| 99 | * @param type the data type info of attribute |
| 100 | */ |
| 101 | public void setAttributeType(YangType<?> type) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 102 | attrType = type; |
| 103 | } |
| 104 | |
| 105 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 106 | * Returns name of the attribute. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 107 | * |
| 108 | * @return name of the attribute |
| 109 | */ |
| 110 | public String getAttributeName() { |
| 111 | |
| 112 | if (name == null) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 113 | throw new TranslatorException("Expected java attribute name is null"); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 114 | } |
| 115 | return name; |
| 116 | } |
| 117 | |
| 118 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 119 | * Sets name of the attribute. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 120 | * |
| 121 | * @param attrName name of the attribute |
| 122 | */ |
| 123 | public void setAttributeName(String attrName) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 124 | name = attrName; |
| 125 | } |
| 126 | |
| 127 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 128 | * Returns if the added attribute is a list of info. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 129 | * |
| 130 | * @return the if the added attribute is a list of info |
| 131 | */ |
| 132 | public boolean isListAttr() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 133 | return isListAttr; |
| 134 | } |
| 135 | |
| 136 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 137 | * Sets if the added attribute is a list of info. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 138 | * |
| 139 | * @param isList if the added attribute is a list of info |
| 140 | */ |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 141 | private void setListAttr(boolean isList) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 142 | isListAttr = isList; |
| 143 | } |
| 144 | |
| 145 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 146 | * Returns if the added attribute has to be accessed in a fully qualified |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 147 | * manner. |
| 148 | * |
| 149 | * @return the if the added attribute has to be accessed in a fully |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 150 | * qualified manner. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 151 | */ |
| 152 | public boolean isQualifiedName() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 153 | return isQualifiedName; |
| 154 | } |
| 155 | |
| 156 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 157 | * Sets if the added attribute has to be accessed in a fully qualified |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 158 | * manner. |
| 159 | * |
| 160 | * @param isQualified if the added attribute has to be accessed in a fully |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 161 | * qualified manner |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 162 | */ |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 163 | private void setIsQualifiedAccess(boolean isQualified) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 164 | isQualifiedName = isQualified; |
| 165 | } |
| 166 | |
| 167 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 168 | * Returns the import info for the attribute type. It will be null, if the type |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 169 | * is basic built-in java type. |
| 170 | * |
| 171 | * @return import info |
| 172 | */ |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 173 | public JavaQualifiedTypeInfoTranslator getImportInfo() { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 174 | return importInfo; |
| 175 | } |
| 176 | |
| 177 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 178 | * Sets the import info for the attribute type. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 179 | * |
| 180 | * @param importInfo import info for the attribute type |
| 181 | */ |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 182 | public void setImportInfo(JavaQualifiedTypeInfoTranslator importInfo) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 183 | this.importInfo = importInfo; |
| 184 | } |
| 185 | |
| 186 | /** |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 187 | * Returns true if conflict between int and uInt. |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 188 | * |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 189 | * @return true if conflict between int and uInt |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 190 | */ |
| 191 | public boolean isIntConflict() { |
| 192 | return isIntConflict; |
| 193 | } |
| 194 | |
| 195 | /** |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 196 | * Sets true if conflict between int and uInt. |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 197 | * |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 198 | * @param intConflict true if conflict between int and uInt |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 199 | */ |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 200 | void setIntConflict(boolean intConflict) { |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 201 | isIntConflict = intConflict; |
| 202 | } |
| 203 | |
| 204 | /** |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 205 | * Returns true if conflict between long and uLong. |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 206 | * |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 207 | * @return true if conflict between long and uLong |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 208 | */ |
| 209 | public boolean isLongConflict() { |
| 210 | return isLongConflict; |
| 211 | } |
| 212 | |
| 213 | /** |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 214 | * Sets true if conflict between long and uLong. |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 215 | * |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 216 | * @param longConflict true if conflict between long and uLong |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 217 | */ |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 218 | void setLongConflict(boolean longConflict) { |
Bharat saraswal | e707f03 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 219 | isLongConflict = longConflict; |
| 220 | } |
| 221 | |
| 222 | /** |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 223 | * Returns java attribute info. |
| 224 | * |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 225 | * @param importInfo java qualified type info |
| 226 | * @param attributeName attribute name |
| 227 | * @param attributeType attribute type |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 228 | * @param isQualifiedAccess is the attribute a qualified access |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 229 | * @param isListAttribute is list attribute |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 230 | * @return java attribute info. |
| 231 | */ |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 232 | public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfoTranslator importInfo, |
| 233 | String attributeName, |
| 234 | YangType<?> attributeType, boolean isQualifiedAccess, |
| 235 | boolean isListAttribute) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 236 | |
janani b | f9819ff | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 237 | if (attributeType != null) { |
| 238 | attributeType = isTypeLeafref(attributeType); |
| 239 | } |
| 240 | attributeName = isTypeNameLeafref(attributeName, attributeType); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 241 | JavaAttributeInfo newAttr = new JavaAttributeInfo(); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 242 | newAttr.setImportInfo(importInfo); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 243 | newAttr.setAttributeName(attributeName); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 244 | newAttr.setAttributeType(attributeType); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 245 | newAttr.setIsQualifiedAccess(isQualifiedAccess); |
| 246 | newAttr.setListAttr(isListAttribute); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 247 | |
| 248 | return newAttr; |
| 249 | } |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 250 | } |