blob: 5b18facbb527a46410c3011235c2b421daec7a81 [file] [log] [blame]
Vinod Kumar Scf044422016-02-09 19:53:45 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar Scf044422016-02-09 19:53:45 +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.datamodel;
18
19import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
20import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053021import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar Scf044422016-02-09 19:53:45 +053022
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053023import static org.onosproject.yangutils.datamodel.ResolvableStatus.INTRA_FILE_RESOLVED;
24
Vinod Kumar Scf044422016-02-09 19:53:45 +053025/*
26 * Reference:RFC 6020.
27 * The "type" statement takes as an argument a string that is the name
28 * of a YANG built-in type or a derived type, followed by an optional
29 * block of sub-statements that are used to put further restrictions
30 * on the type.
31 *
32 * The restrictions that can be applied depend on the type being restricted.
33 * The type's sub-statements
34 *
35 * +------------------+---------+-------------+------------------------------------+
36 * | substatement | section | cardinality | mapped data type |
37 * +------------------+---------+-------------+------------------------------------+
38 * | bit | 9.7.4 | 0..n | - YangBit used in YangBits |
39 * | enum | 9.6.4 | 0..n | - YangEnum used in YangEnumeration |
40 * | length | 9.4.4 | 0..1 | - used for string |
41 * | path | 9.9.2 | 0..1 | - TODO leaf-ref |
42 * | pattern | 9.4.6 | 0..n | - used for string |
43 * | range | 9.2.4 | 0..1 | - used for integer data type |
44 * | require-instance | 9.13.2 | 0..1 | - TODO instance-identifier |
45 * | type | 7.4 | 0..n | - TODO union |
46 * +------------------+---------+-------------+------------------------------------+
47 */
48
49/**
Bharat saraswald9822e92016-04-05 15:13:44 +053050 * Represents the data type information.
Vinod Kumar Scf044422016-02-09 19:53:45 +053051 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053052 * @param <T> YANG data type info
Vinod Kumar Scf044422016-02-09 19:53:45 +053053 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053054public class YangType<T>
55 implements Parsable, Resolvable {
Vinod Kumar Scf044422016-02-09 19:53:45 +053056
57 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053058 * YANG node identifier.
Vinod Kumar Scf044422016-02-09 19:53:45 +053059 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053060 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar Scf044422016-02-09 19:53:45 +053061
62 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053063 * Java package in which the Java type is defined.
64 */
65 private String javaPackage;
66
67 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +053068 * YANG data type.
69 */
70 private YangDataTypes dataType;
71
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053072 /**
73 * Additional information about data type, example restriction info, named
74 * values, etc. The extra information is based on the data type. Based on
75 * the data type, the extended info can vary.
76 */
77 private T dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +053078
79 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053080 * Effective built-in type, requried in case type of typedef is again a
81 * derived type. This information is to be added during linking.
82 */
83 private YangDataTypes effectiveBuiltInType;
84
85 /**
86 * Effective pattern restriction, requried in case type of typedef is again
87 * a derived type. This information is to be added during linking.
88 */
89 private YangPatternRestriction effectivePatternRestriction;
90
91 /**
92 * Status of resolution. If completely resolved enum value is "RESOLVED",
93 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
94 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053095 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053096 */
97 private ResolvableStatus resolvableStatus;
98
99 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530100 * Creates a YANG type object.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530101 */
102 public YangType() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530103
104 nodeIdentifier = new YangNodeIdentifier();
105 resolvableStatus = ResolvableStatus.UNRESOLVED;
106 }
107
108 /**
109 * Returns prefix associated with data type name.
110 *
111 * @return prefix associated with data type name
112 */
113 public String getPrefix() {
114 return nodeIdentifier.getPrefix();
115 }
116
117 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530118 * Sets prefix associated with data type name.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530119 *
120 * @param prefix prefix associated with data type name
121 */
122 public void setPrefix(String prefix) {
123 nodeIdentifier.setPrefix(prefix);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530124 }
125
126 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530127 * Returns the name of data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530128 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530129 * @return the name of data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530130 */
131 public String getDataTypeName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530132 return nodeIdentifier.getName();
Vinod Kumar Scf044422016-02-09 19:53:45 +0530133 }
134
135 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530136 * Sets the name of the data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530137 *
138 * @param typeName the name to set
139 */
140 public void setDataTypeName(String typeName) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530141 nodeIdentifier.setName(typeName);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530142 }
143
144 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530145 * Returns the Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530146 *
147 * @return Java package where the type is defined
148 */
149 public String getJavaPackage() {
150 return javaPackage;
151 }
152
153 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530154 * Sets Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530155 *
156 * @param javaPackage Java package where the type is defined
157 */
158 public void setJavaPackage(String javaPackage) {
159 this.javaPackage = javaPackage;
160 }
161
162 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530163 * Returns the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530164 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530165 * @return the data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530166 */
167 public YangDataTypes getDataType() {
168 return dataType;
169 }
170
171 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530172 * Sets the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530173 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 * @param dataType data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530175 */
176 public void setDataType(YangDataTypes dataType) {
177 this.dataType = dataType;
178 }
179
180 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530181 * Returns the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530182 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530183 * @return the data type meta data
Vinod Kumar Scf044422016-02-09 19:53:45 +0530184 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530185 public T getDataTypeExtendedInfo() {
186 return dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530187 }
188
189 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530190 * Sets the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530191 *
192 * @param dataTypeInfo the meta data to set
193 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530194 public void setDataTypeExtendedInfo(T dataTypeInfo) {
195 this.dataTypeExtendedInfo = dataTypeInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530196 }
197
198 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530199 * Returns node identifier.
200 *
201 * @return node identifier
202 */
203 public YangNodeIdentifier getNodeIdentifier() {
204 return nodeIdentifier;
205 }
206
207 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530208 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530209 *
210 * @param nodeIdentifier the node identifier
211 */
212 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
213 this.nodeIdentifier = nodeIdentifier;
214 }
215
216 /**
217 * Return effective built-in type.
218 *
219 * @return effective built-in type
220 */
221 public YangDataTypes getEffectiveBuiltInType() {
222 return effectiveBuiltInType;
223 }
224
225 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530226 * Sets effective built-in type.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530227 *
228 * @param effectiveBuiltInType effective built-in type
229 */
230 public void setEffectiveBuiltInType(YangDataTypes effectiveBuiltInType) {
231 this.effectiveBuiltInType = effectiveBuiltInType;
232 }
233
234 /**
235 * Returns effective pattern restriction.
236 *
237 * @return effective pattern restriction
238 */
239 public YangPatternRestriction getEffectivePatternRestriction() {
240 return effectivePatternRestriction;
241 }
242
243 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530244 * Sets effective pattern restriction.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530245 *
246 * @param effectivePatternRestriction effective pattern restriction
247 */
248 public void setEffectivePatternRestriction(YangPatternRestriction effectivePatternRestriction) {
249 this.effectivePatternRestriction = effectivePatternRestriction;
250 }
251
252 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +0530253 * Returns the type of the parsed data.
254 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530255 * @return returns TYPE_DATA
Vinod Kumar Scf044422016-02-09 19:53:45 +0530256 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530257 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530258 public YangConstructType getYangConstructType() {
259 return YangConstructType.TYPE_DATA;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530260 }
261
262 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530263 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530264 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530265 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530266 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530267 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530268 public void validateDataOnEntry()
269 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530270 // TODO auto-generated method stub, to be implemented by parser
271
272 }
273
274 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530275 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530276 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530277 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530278 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530279 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530280 public void validateDataOnExit()
281 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530282 // TODO auto-generated method stub, to be implemented by parser
283
284 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530285
286 @Override
287 public ResolvableStatus getResolvableStatus() {
288 return resolvableStatus;
289 }
290
291 @Override
292 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
293 this.resolvableStatus = resolvableStatus;
294 }
295
296 @Override
297 public void resolve() {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530298 /*
299 Inherit the Restriction from the referred typedef definition.
300 */
301 if (getDataType() != YangDataTypes.DERIVED) {
302 throw new RuntimeException("Resolve should only be called for derrived data types");
303 }
304
305 YangDerivedInfo<?> derrivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
306 YangType<?> baseType = derrivedInfo.getReferredTypeDef().getTypeDefBaseType();
307 if (YangDataTypes.DERIVED == baseType.getDataType()) {
308 if (baseType.getResolvableStatus() == INTRA_FILE_RESOLVED) {
309 setResolvableStatus(INTRA_FILE_RESOLVED);
310 }
311 }
312 //TODO:
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530313 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530314}