blob: d49241ef78123940a78565fced629a402d211d7e [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
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
janani be18b5342016-07-13 21:06:41 +053020
Vinod Kumar Scf044422016-02-09 19:53:45 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.Parsable;
23import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
24import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vinod Kumar Scf044422016-02-09 19:53:45 +053026
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053027import static org.onosproject.yangutils.datamodel.BuiltInTypeObjectFactory.getDataObjectFromString;
28import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType;
29import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053030
Vinod Kumar Scf044422016-02-09 19:53:45 +053031/*
32 * Reference:RFC 6020.
33 * The "type" statement takes as an argument a string that is the name
34 * of a YANG built-in type or a derived type, followed by an optional
35 * block of sub-statements that are used to put further restrictions
36 * on the type.
37 *
38 * The restrictions that can be applied depend on the type being restricted.
39 * The type's sub-statements
40 *
41 * +------------------+---------+-------------+------------------------------------+
42 * | substatement | section | cardinality | mapped data type |
43 * +------------------+---------+-------------+------------------------------------+
44 * | bit | 9.7.4 | 0..n | - YangBit used in YangBits |
45 * | enum | 9.6.4 | 0..n | - YangEnum used in YangEnumeration |
46 * | length | 9.4.4 | 0..1 | - used for string |
janani be18b5342016-07-13 21:06:41 +053047 * | path | 9.9.2 | 0..1 | - path for referred leaf/leaf-list |
Vinod Kumar Scf044422016-02-09 19:53:45 +053048 * | pattern | 9.4.6 | 0..n | - used for string |
49 * | range | 9.2.4 | 0..1 | - used for integer data type |
50 * | require-instance | 9.13.2 | 0..1 | - TODO instance-identifier |
51 * | type | 7.4 | 0..n | - TODO union |
52 * +------------------+---------+-------------+------------------------------------+
53 */
54
55/**
Bharat saraswald9822e92016-04-05 15:13:44 +053056 * Represents the data type information.
Vinod Kumar Scf044422016-02-09 19:53:45 +053057 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053058 * @param <T> YANG data type info
Vinod Kumar Scf044422016-02-09 19:53:45 +053059 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053060public class YangType<T>
Bharat saraswal96dfef02016-06-16 00:29:12 +053061 implements Parsable, Resolvable, Serializable {
62
63 private static final long serialVersionUID = 8062016054L;
Vinod Kumar Scf044422016-02-09 19:53:45 +053064
65 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053066 * YANG node identifier.
Vinod Kumar Scf044422016-02-09 19:53:45 +053067 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053068 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar Scf044422016-02-09 19:53:45 +053069
70 /**
71 * YANG data type.
72 */
73 private YangDataTypes dataType;
74
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053075 /**
76 * Additional information about data type, example restriction info, named
77 * values, etc. The extra information is based on the data type. Based on
78 * the data type, the extended info can vary.
79 */
80 private T dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +053081
82 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053083 * Status of resolution. If completely resolved enum value is "RESOLVED",
84 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
85 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053086 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053087 */
88 private ResolvableStatus resolvableStatus;
89
90 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053091 * Creates a YANG type object.
Vinod Kumar Scf044422016-02-09 19:53:45 +053092 */
93 public YangType() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053094
95 nodeIdentifier = new YangNodeIdentifier();
96 resolvableStatus = ResolvableStatus.UNRESOLVED;
97 }
98
99 /**
100 * Returns prefix associated with data type name.
101 *
102 * @return prefix associated with data type name
103 */
104 public String getPrefix() {
105 return nodeIdentifier.getPrefix();
106 }
107
108 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Sets prefix associated with data type name.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530110 *
111 * @param prefix prefix associated with data type name
112 */
113 public void setPrefix(String prefix) {
114 nodeIdentifier.setPrefix(prefix);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530115 }
116
117 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530118 * Returns the name of data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530119 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530120 * @return the name of data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530121 */
122 public String getDataTypeName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530123 return nodeIdentifier.getName();
Vinod Kumar Scf044422016-02-09 19:53:45 +0530124 }
125
126 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530127 * Sets the name of the data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530128 *
129 * @param typeName the name to set
130 */
131 public void setDataTypeName(String typeName) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530132 nodeIdentifier.setName(typeName);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530133 }
134
135 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530136 * Returns the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530137 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530138 * @return the data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530139 */
140 public YangDataTypes getDataType() {
141 return dataType;
142 }
143
144 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530145 * Sets the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530146 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530147 * @param dataType data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530148 */
149 public void setDataType(YangDataTypes dataType) {
150 this.dataType = dataType;
151 }
152
153 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530154 * Returns the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530155 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530156 * @return the data type meta data
Vinod Kumar Scf044422016-02-09 19:53:45 +0530157 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530158 public T getDataTypeExtendedInfo() {
159 return dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530160 }
161
162 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530163 * Sets the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530164 *
165 * @param dataTypeInfo the meta data to set
166 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530167 public void setDataTypeExtendedInfo(T dataTypeInfo) {
168 this.dataTypeExtendedInfo = dataTypeInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530169 }
170
171 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530172 * Returns node identifier.
173 *
174 * @return node identifier
175 */
176 public YangNodeIdentifier getNodeIdentifier() {
177 return nodeIdentifier;
178 }
179
180 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530181 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530182 *
183 * @param nodeIdentifier the node identifier
184 */
185 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
186 this.nodeIdentifier = nodeIdentifier;
187 }
188
189 /**
janani be18b5342016-07-13 21:06:41 +0530190 * Resets the class attributes to its default value.
191 */
192 public void resetYangType() {
193 nodeIdentifier = new YangNodeIdentifier();
194 resolvableStatus = ResolvableStatus.UNRESOLVED;
195 dataType = null;
196 dataTypeExtendedInfo = null;
197 }
198
199 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +0530200 * Returns the type of the parsed data.
201 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530202 * @return returns TYPE_DATA
Vinod Kumar Scf044422016-02-09 19:53:45 +0530203 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530204 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530205 public YangConstructType getYangConstructType() {
206 return YangConstructType.TYPE_DATA;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530207 }
208
209 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530210 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530211 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530212 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530213 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530214 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530215 public void validateDataOnEntry()
216 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530217 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530218 }
219
220 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530221 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530222 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530223 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530224 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530225 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530226 public void validateDataOnExit()
227 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530228 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530229 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530230
231 @Override
232 public ResolvableStatus getResolvableStatus() {
233 return resolvableStatus;
234 }
235
236 @Override
237 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
238 this.resolvableStatus = resolvableStatus;
239 }
240
241 @Override
janani b23ccc312016-07-14 19:35:22 +0530242 public Object resolve()
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530243 throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530244 /*
245 * Check whether the data type is derived.
246 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530247 if (getDataType() != DERIVED) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530248 throw new DataModelException("Linker Error: Resolve should only be called for derived data types.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530249 }
250
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530251 // Check if the derived info is present.
252 YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
253 if (derivedInfo == null) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530254 throw new DataModelException("Linker Error: Derived information is missing.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530255 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530256
257 // Initiate the resolution
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530258 try {
259 setResolvableStatus(derivedInfo.resolve());
260 } catch (DataModelException e) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530261 throw new DataModelException(e.getMessage());
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530262 }
janani b23ccc312016-07-14 19:35:22 +0530263 return null;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530264 }
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530265
266 /**
267 * Validates the input data value against the permissible value for the
268 * type as per the YANG file.
269 *
270 * @param value input data value
271 * @return status of validation
272 */
273 public boolean isValidValue(String value) {
274 switch (getDataType()) {
275 case INT8:
276 case INT16:
277 case INT32:
278 case INT64:
279 case UINT8:
280 case UINT16:
281 case UINT32:
282 case UINT64: {
283 isValidValueForRangeRestrictedType(value);
284 }
285 case DECIMAL64: {
286 // TODO
287 }
288 case STRING: {
289 // TODO implement in string restriction similar to range restriction
290 }
291 case ENUMERATION: {
292 // TODO validate using list of YANG enum of enumeration class in extended info.
293 }
294 case BINARY: {
295 // TODO validate based on extended info
296 }
297 case BITS: {
298 // TODO validate based on extended info
299 }
300 case BOOLEAN: {
301 // TODO true or false
302 }
303 case LEAFREF: {
304 // TODO validate based on extended info
305 }
306 case IDENTITYREF: {
307 // TODO TBD
308 }
309 case EMPTY: {
310 // TODO true or false
311 }
312 case UNION: {
313 // TODO validate based on extended info
314 }
315 case INSTANCE_IDENTIFIER: {
316 // TODO TBD
317 }
318 case DERIVED: {
319 if (isOfRangeRestrictedType(((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType())) {
320 try {
321 if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) {
322 getDataObjectFromString(value,
323 ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType());
324 return true;
325 } else {
326 return ((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
327 .getResolvedExtendedInfo()).isValidValueString(value);
328 }
329 } catch (Exception e) {
330 return false;
331 }
332 } else {
333 // TODO
334 }
335 }
336 default: {
337 // TODO
338 }
339 }
340 return true;
341 }
342
343 /**
344 * Validates the input data value for range restricted types against the
345 * permissible value for the type as per the YANG file.
346 *
347 * @param value input data value
348 * @return status of validation
349 */
350 private boolean isValidValueForRangeRestrictedType(String value) {
351 try {
352 if (getDataTypeExtendedInfo() == null) {
353 getDataObjectFromString(value, getDataType());
354 return true;
355 } else {
356 return ((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value);
357 }
358 } catch (Exception e) {
359 return false;
360 }
361 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530362}