blob: 485878b3110d9366b5592ec5552ec1a76a3c1666 [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
Vinod Kumar S38046502016-03-23 15:30:27 +053022/**
Bharat saraswald9822e92016-04-05 15:13:44 +053023 * Represents the attribute info corresponding to class/interface generated.
Vinod Kumar S38046502016-03-23 15:30:27 +053024 */
25public final class JavaAttributeInfo {
26
27 /**
28 * The data type info of attribute.
29 */
30 private YangType<?> attrType;
31
32 /**
33 * Name of the attribute.
34 */
35 private String name;
36
37 /**
38 * If the added attribute is a list of info.
39 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053040 private boolean isListAttr;
Vinod Kumar S38046502016-03-23 15:30:27 +053041
42 /**
43 * If the added attribute has to be accessed in a fully qualified manner.
44 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053045 private boolean isQualifiedName;
Vinod Kumar S38046502016-03-23 15:30:27 +053046
47 /**
48 * The class info will be used to set the attribute type and package info
49 * will be use for qualified name.
50 */
51 private JavaQualifiedTypeInfo importInfo;
52
53 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053054 * Creates a java attribute info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053055 */
56 private JavaAttributeInfo() {
57 }
58
59 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053060 * Creates object of java attribute info.
Bharat saraswald6f12412016-03-28 15:50:13 +053061 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053062 * @param attrType YANG type
63 * @param name attribute name
64 * @param isListAttr is list attribute
Bharat saraswald6f12412016-03-28 15:50:13 +053065 * @param isQualifiedName is qualified name
66 */
67 public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) {
68 this.attrType = attrType;
69 this.name = name;
70 this.isListAttr = isListAttr;
71 this.isQualifiedName = isQualifiedName;
72 }
73
74 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053075 * Returns the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053076 *
77 * @return the data type info of attribute
78 */
79 public YangType<?> getAttributeType() {
80
81 if (attrType == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053082 throw new TranslatorException("Expected java attribute type is null");
Vinod Kumar S38046502016-03-23 15:30:27 +053083 }
84 return attrType;
85 }
86
87 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053088 * Sets the data type info of attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053089 *
90 * @param type the data type info of attribute
91 */
92 public void setAttributeType(YangType<?> type) {
Vinod Kumar S38046502016-03-23 15:30:27 +053093 attrType = type;
94 }
95
96 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053097 * Returns name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +053098 *
99 * @return name of the attribute
100 */
101 public String getAttributeName() {
102
103 if (name == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530104 throw new TranslatorException("Expected java attribute name is null");
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 }
106 return name;
107 }
108
109 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530110 * Sets name of the attribute.
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 *
112 * @param attrName name of the attribute
113 */
114 public void setAttributeName(String attrName) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530115 name = attrName;
116 }
117
118 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530119 * Returns if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 *
121 * @return the if the added attribute is a list of info
122 */
123 public boolean isListAttr() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530124 return isListAttr;
125 }
126
127 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530128 * Sets if the added attribute is a list of info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530129 *
130 * @param isList if the added attribute is a list of info
131 */
132 public void setListAttr(boolean isList) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 isListAttr = isList;
134 }
135
136 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530137 * Returns if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530138 * manner.
139 *
140 * @return the if the added attribute has to be accessed in a fully
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530141 * qualified manner.
Vinod Kumar S38046502016-03-23 15:30:27 +0530142 */
143 public boolean isQualifiedName() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530144 return isQualifiedName;
145 }
146
147 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530148 * Sets if the added attribute has to be accessed in a fully qualified
Vinod Kumar S38046502016-03-23 15:30:27 +0530149 * manner.
150 *
151 * @param isQualified if the added attribute has to be accessed in a fully
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 * qualified manner
Vinod Kumar S38046502016-03-23 15:30:27 +0530153 */
154 public void setIsQualifiedAccess(boolean isQualified) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 isQualifiedName = isQualified;
156 }
157
158 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530159 * Returns the import info for the attribute type. It will be null, if the type
Vinod Kumar S38046502016-03-23 15:30:27 +0530160 * is basic built-in java type.
161 *
162 * @return import info
163 */
164 public JavaQualifiedTypeInfo getImportInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530165 return importInfo;
166 }
167
168 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530169 * Sets the import info for the attribute type.
Vinod Kumar S38046502016-03-23 15:30:27 +0530170 *
171 * @param importInfo import info for the attribute type
172 */
173 public void setImportInfo(JavaQualifiedTypeInfo importInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530174 this.importInfo = importInfo;
175 }
176
177 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530178 * Returns java attribute info.
179 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 * @param importInfo java qualified type info
181 * @param attributeName attribute name
182 * @param attributeType attribute type
183 * @param isQualifiedAccess is the attribute a qualified access
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530184 * @param isListAttribute is list attribute
185 * @return java attribute info.
186 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530187 public static JavaAttributeInfo getAttributeInfoForTheData(JavaQualifiedTypeInfo importInfo, String attributeName,
188 YangType<?> attributeType, boolean isQualifiedAccess,
189 boolean isListAttribute) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530190
191 JavaAttributeInfo newAttr = new JavaAttributeInfo();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530192 newAttr.setImportInfo(importInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530193 newAttr.setAttributeName(attributeName);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530194 newAttr.setAttributeType(attributeType);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530195 newAttr.setIsQualifiedAccess(isQualifiedAccess);
196 newAttr.setListAttr(isListAttribute);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530197
198 return newAttr;
199 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530200}