blob: 254ea4a90adcf65fe01c56f2d2b62bb5047908ff [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +05303 *
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
17package org.onosproject.yangutils.translator.tojava;
18
Vinod Kumar S38046502016-03-23 15:30:27 +053019import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053020import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053021
janani bf9819ff2016-08-03 16:40:01 +053022import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeLeafref;
23import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeNameLeafref;
24
Vinod Kumar S38046502016-03-23 15:30:27 +053025/**
Bharat saraswald9822e92016-04-05 15:13:44 +053026 * Represents the attribute info corresponding to class/interface generated.
Vinod Kumar S38046502016-03-23 15:30:27 +053027 */
28public 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 Se4b9b0c2016-04-30 21:09:15 +053043 private boolean isListAttr;
Vinod Kumar S38046502016-03-23 15:30:27 +053044
45 /**
46 * If the added attribute has to be accessed in a fully qualified manner.
47 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053048 private boolean isQualifiedName;
Vinod Kumar S38046502016-03-23 15:30:27 +053049
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-Huaweibdf24bb2016-08-02 18:13:13 +053054 private JavaQualifiedTypeInfoTranslator importInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053055
56 /**
Bharat saraswale707f032016-07-14 23:33:55 +053057 * If conflict occurs.
58 */
59 private boolean isIntConflict;
60
61 /**
62 * If conflict occurs.
63 */
64 private boolean isLongConflict;
65
66 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053067 * Creates a java attribute info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053068 */
69 private JavaAttributeInfo() {
70 }
71
72 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053073 * Creates object of java attribute info.
Bharat saraswald6f12412016-03-28 15:50:13 +053074 *
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053075 * @param attrType YANG type
76 * @param name attribute name
77 * @param isListAttr is list attribute
Bharat saraswald6f12412016-03-28 15:50:13 +053078 * @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 saraswald9822e92016-04-05 15:13:44 +053088 * Returns the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053089 *
90 * @return the data type info of attribute
91 */
92 public YangType<?> getAttributeType() {
Vinod Kumar S38046502016-03-23 15:30:27 +053093 return attrType;
94 }
95
96 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053097 * Sets the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053098 *
99 * @param type the data type info of attribute
100 */
101 public void setAttributeType(YangType<?> type) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530102 attrType = type;
103 }
104
105 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530106 * Returns name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 *
108 * @return name of the attribute
109 */
110 public String getAttributeName() {
111
112 if (name == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530113 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530114 }
115 return name;
116 }
117
118 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530119 * Sets name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 *
121 * @param attrName name of the attribute
122 */
123 public void setAttributeName(String attrName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530124 name = attrName;
125 }
126
127 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530128 * Returns if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530129 *
130 * @return the if the added attribute is a list of info
131 */
132 public boolean isListAttr() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 return isListAttr;
134 }
135
136 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530137 * Sets if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530138 *
139 * @param isList if the added attribute is a list of info
140 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530141 private void setListAttr(boolean isList) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530142 isListAttr = isList;
143 }
144
145 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530146 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530147 * manner.
148 *
149 * @return the if the added attribute has to be accessed in a fully
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530150 * qualified manner.
Vinod Kumar S38046502016-03-23 15:30:27 +0530151 */
152 public boolean isQualifiedName() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530153 return isQualifiedName;
154 }
155
156 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530157 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530158 * manner.
159 *
160 * @param isQualified if the added attribute has to be accessed in a fully
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530161 * qualified manner
Vinod Kumar S38046502016-03-23 15:30:27 +0530162 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530163 private void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530164 isQualifiedName = isQualified;
165 }
166
167 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530168 * Returns the import info for the attribute type. It will be null, if the type
Vinod Kumar S38046502016-03-23 15:30:27 +0530169 * is basic built-in java type.
170 *
171 * @return import info
172 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530173 public JavaQualifiedTypeInfoTranslator getImportInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530174 return importInfo;
175 }
176
177 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530178 * Sets the import info for the attribute type.
Vinod Kumar S38046502016-03-23 15:30:27 +0530179 *
180 * @param importInfo import info for the attribute type
181 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530182 public void setImportInfo(JavaQualifiedTypeInfoTranslator importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530183 this.importInfo = importInfo;
184 }
185
186 /**
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530187 * Returns true if conflict between int and uInt.
Bharat saraswale707f032016-07-14 23:33:55 +0530188 *
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530189 * @return true if conflict between int and uInt
Bharat saraswale707f032016-07-14 23:33:55 +0530190 */
191 public boolean isIntConflict() {
192 return isIntConflict;
193 }
194
195 /**
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530196 * Sets true if conflict between int and uInt.
Bharat saraswale707f032016-07-14 23:33:55 +0530197 *
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530198 * @param intConflict true if conflict between int and uInt
Bharat saraswale707f032016-07-14 23:33:55 +0530199 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530200 void setIntConflict(boolean intConflict) {
Bharat saraswale707f032016-07-14 23:33:55 +0530201 isIntConflict = intConflict;
202 }
203
204 /**
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530205 * Returns true if conflict between long and uLong.
Bharat saraswale707f032016-07-14 23:33:55 +0530206 *
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530207 * @return true if conflict between long and uLong
Bharat saraswale707f032016-07-14 23:33:55 +0530208 */
209 public boolean isLongConflict() {
210 return isLongConflict;
211 }
212
213 /**
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530214 * Sets true if conflict between long and uLong.
Bharat saraswale707f032016-07-14 23:33:55 +0530215 *
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530216 * @param longConflict true if conflict between long and uLong
Bharat saraswale707f032016-07-14 23:33:55 +0530217 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530218 void setLongConflict(boolean longConflict) {
Bharat saraswale707f032016-07-14 23:33:55 +0530219 isLongConflict = longConflict;
220 }
221
222 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530223 * Returns java attribute info.
224 *
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530225 * @param importInfo java qualified type info
226 * @param attributeName attribute name
227 * @param attributeType attribute type
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530228 * @param isQualifiedAccess is the attribute a qualified access
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530229 * @param isListAttribute is list attribute
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530230 * @return java attribute info.
231 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530232 public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfoTranslator importInfo,
233 String attributeName,
234 YangType<?> attributeType, boolean isQualifiedAccess,
235 boolean isListAttribute) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530236
janani bf9819ff2016-08-03 16:40:01 +0530237 if (attributeType != null) {
238 attributeType = isTypeLeafref(attributeType);
239 }
240 attributeName = isTypeNameLeafref(attributeName, attributeType);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530241 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530242 newAttr.setImportInfo(importInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530243 newAttr.setAttributeName(attributeName);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530244 newAttr.setAttributeType(attributeType);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530245 newAttr.setIsQualifiedAccess(isQualifiedAccess);
246 newAttr.setListAttr(isListAttribute);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530247
248 return newAttr;
249 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530250}