blob: 2a0c8784f8c069a7d1921ade1f53826ca1a01b39 [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;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053024import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053025
Vinod Kumar Scf044422016-02-09 19:53:45 +053026/*
27 * Reference:RFC 6020.
28 * The "type" statement takes as an argument a string that is the name
29 * of a YANG built-in type or a derived type, followed by an optional
30 * block of sub-statements that are used to put further restrictions
31 * on the type.
32 *
33 * The restrictions that can be applied depend on the type being restricted.
34 * The type's sub-statements
35 *
36 * +------------------+---------+-------------+------------------------------------+
37 * | substatement | section | cardinality | mapped data type |
38 * +------------------+---------+-------------+------------------------------------+
39 * | bit | 9.7.4 | 0..n | - YangBit used in YangBits |
40 * | enum | 9.6.4 | 0..n | - YangEnum used in YangEnumeration |
41 * | length | 9.4.4 | 0..1 | - used for string |
42 * | path | 9.9.2 | 0..1 | - TODO leaf-ref |
43 * | pattern | 9.4.6 | 0..n | - used for string |
44 * | range | 9.2.4 | 0..1 | - used for integer data type |
45 * | require-instance | 9.13.2 | 0..1 | - TODO instance-identifier |
46 * | type | 7.4 | 0..n | - TODO union |
47 * +------------------+---------+-------------+------------------------------------+
48 */
49
50/**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Represents the data type information.
Vinod Kumar Scf044422016-02-09 19:53:45 +053052 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053053 * @param <T> YANG data type info
Vinod Kumar Scf044422016-02-09 19:53:45 +053054 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053055public class YangType<T>
56 implements Parsable, Resolvable {
Vinod Kumar Scf044422016-02-09 19:53:45 +053057
58 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053059 * YANG node identifier.
Vinod Kumar Scf044422016-02-09 19:53:45 +053060 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053061 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar Scf044422016-02-09 19:53:45 +053062
63 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053064 * Java package in which the Java type is defined.
65 */
66 private String javaPackage;
67
68 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +053069 * YANG data type.
70 */
71 private YangDataTypes dataType;
72
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053073 /**
74 * Additional information about data type, example restriction info, named
75 * values, etc. The extra information is based on the data type. Based on
76 * the data type, the extended info can vary.
77 */
78 private T dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +053079
80 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053081 * Effective built-in type, requried in case type of typedef is again a
82 * derived type. This information is to be added during linking.
83 */
84 private YangDataTypes effectiveBuiltInType;
85
86 /**
87 * Effective pattern restriction, requried in case type of typedef is again
88 * a derived type. This information is to be added during linking.
89 */
90 private YangPatternRestriction effectivePatternRestriction;
91
92 /**
93 * Status of resolution. If completely resolved enum value is "RESOLVED",
94 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
95 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053096 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053097 */
98 private ResolvableStatus resolvableStatus;
99
100 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530101 * Creates a YANG type object.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530102 */
103 public YangType() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530104
105 nodeIdentifier = new YangNodeIdentifier();
106 resolvableStatus = ResolvableStatus.UNRESOLVED;
107 }
108
109 /**
110 * Returns prefix associated with data type name.
111 *
112 * @return prefix associated with data type name
113 */
114 public String getPrefix() {
115 return nodeIdentifier.getPrefix();
116 }
117
118 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530119 * Sets prefix associated with data type name.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530120 *
121 * @param prefix prefix associated with data type name
122 */
123 public void setPrefix(String prefix) {
124 nodeIdentifier.setPrefix(prefix);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530125 }
126
127 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530128 * Returns the name of data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530129 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530130 * @return the name of data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530131 */
132 public String getDataTypeName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530133 return nodeIdentifier.getName();
Vinod Kumar Scf044422016-02-09 19:53:45 +0530134 }
135
136 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530137 * Sets the name of the data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530138 *
139 * @param typeName the name to set
140 */
141 public void setDataTypeName(String typeName) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530142 nodeIdentifier.setName(typeName);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530143 }
144
145 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530146 * Returns the Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530147 *
148 * @return Java package where the type is defined
149 */
150 public String getJavaPackage() {
151 return javaPackage;
152 }
153
154 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530155 * Sets Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530156 *
157 * @param javaPackage Java package where the type is defined
158 */
159 public void setJavaPackage(String javaPackage) {
160 this.javaPackage = javaPackage;
161 }
162
163 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530164 * Returns the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530165 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530166 * @return the data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530167 */
168 public YangDataTypes getDataType() {
169 return dataType;
170 }
171
172 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530173 * Sets the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530174 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530175 * @param dataType data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530176 */
177 public void setDataType(YangDataTypes dataType) {
178 this.dataType = dataType;
179 }
180
181 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530182 * Returns the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530183 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530184 * @return the data type meta data
Vinod Kumar Scf044422016-02-09 19:53:45 +0530185 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530186 public T getDataTypeExtendedInfo() {
187 return dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530188 }
189
190 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530191 * Sets the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530192 *
193 * @param dataTypeInfo the meta data to set
194 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530195 public void setDataTypeExtendedInfo(T dataTypeInfo) {
196 this.dataTypeExtendedInfo = dataTypeInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530197 }
198
199 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530200 * Returns node identifier.
201 *
202 * @return node identifier
203 */
204 public YangNodeIdentifier getNodeIdentifier() {
205 return nodeIdentifier;
206 }
207
208 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530209 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530210 *
211 * @param nodeIdentifier the node identifier
212 */
213 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
214 this.nodeIdentifier = nodeIdentifier;
215 }
216
217 /**
218 * Return effective built-in type.
219 *
220 * @return effective built-in type
221 */
222 public YangDataTypes getEffectiveBuiltInType() {
223 return effectiveBuiltInType;
224 }
225
226 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530227 * Sets effective built-in type.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530228 *
229 * @param effectiveBuiltInType effective built-in type
230 */
231 public void setEffectiveBuiltInType(YangDataTypes effectiveBuiltInType) {
232 this.effectiveBuiltInType = effectiveBuiltInType;
233 }
234
235 /**
236 * Returns effective pattern restriction.
237 *
238 * @return effective pattern restriction
239 */
240 public YangPatternRestriction getEffectivePatternRestriction() {
241 return effectivePatternRestriction;
242 }
243
244 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530245 * Sets effective pattern restriction.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530246 *
247 * @param effectivePatternRestriction effective pattern restriction
248 */
249 public void setEffectivePatternRestriction(YangPatternRestriction effectivePatternRestriction) {
250 this.effectivePatternRestriction = effectivePatternRestriction;
251 }
252
253 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +0530254 * Returns the type of the parsed data.
255 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530256 * @return returns TYPE_DATA
Vinod Kumar Scf044422016-02-09 19:53:45 +0530257 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530258 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530259 public YangConstructType getYangConstructType() {
260 return YangConstructType.TYPE_DATA;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530261 }
262
263 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530264 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530265 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530266 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530267 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530268 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530269 public void validateDataOnEntry()
270 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530271 // TODO auto-generated method stub, to be implemented by parser
272
273 }
274
275 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530276 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530277 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530278 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530279 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530280 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530281 public void validateDataOnExit()
282 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530283 // TODO auto-generated method stub, to be implemented by parser
284
285 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530286
287 @Override
288 public ResolvableStatus getResolvableStatus() {
289 return resolvableStatus;
290 }
291
292 @Override
293 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
294 this.resolvableStatus = resolvableStatus;
295 }
296
297 @Override
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530298 public void resolve() throws DataModelException {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530299 /*
300 Inherit the Restriction from the referred typedef definition.
301 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530302 if (getDataType() != DERIVED) {
303 throw new DataModelException("Resolve should only be called for derived data types");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530304 }
305
306 YangDerivedInfo<?> derrivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
307 YangType<?> baseType = derrivedInfo.getReferredTypeDef().getTypeDefBaseType();
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530308 if (DERIVED == baseType.getDataType() && baseType.getResolvableStatus() == INTRA_FILE_RESOLVED) {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530309 setResolvableStatus(INTRA_FILE_RESOLVED);
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530310 }
311 //TODO:
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530312 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530313}