blob: 45f0df694ad4dcf4dbdeb61a34ed61a8abf23ead [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;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053020import java.math.BigInteger;
21import java.util.Iterator;
22import java.util.ListIterator;
janani be18b5342016-07-13 21:06:41 +053023
Vinod Kumar Scf044422016-02-09 19:53:45 +053024import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053025import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Bharat saraswal96dfef02016-06-16 00:29:12 +053026import org.onosproject.yangutils.datamodel.utils.Parsable;
27import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
28import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053029import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053030import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053031import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64;
Vinod Kumar Scf044422016-02-09 19:53:45 +053032
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053033import static org.onosproject.yangutils.datamodel.BuiltInTypeObjectFactory.getDataObjectFromString;
34import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType;
35import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053036
Vinod Kumar Scf044422016-02-09 19:53:45 +053037/*
38 * Reference:RFC 6020.
39 * The "type" statement takes as an argument a string that is the name
40 * of a YANG built-in type or a derived type, followed by an optional
41 * block of sub-statements that are used to put further restrictions
42 * on the type.
43 *
44 * The restrictions that can be applied depend on the type being restricted.
45 * The type's sub-statements
46 *
47 * +------------------+---------+-------------+------------------------------------+
48 * | substatement | section | cardinality | mapped data type |
49 * +------------------+---------+-------------+------------------------------------+
50 * | bit | 9.7.4 | 0..n | - YangBit used in YangBits |
51 * | enum | 9.6.4 | 0..n | - YangEnum used in YangEnumeration |
52 * | length | 9.4.4 | 0..1 | - used for string |
janani be18b5342016-07-13 21:06:41 +053053 * | path | 9.9.2 | 0..1 | - path for referred leaf/leaf-list |
Vinod Kumar Scf044422016-02-09 19:53:45 +053054 * | pattern | 9.4.6 | 0..n | - used for string |
55 * | range | 9.2.4 | 0..1 | - used for integer data type |
56 * | require-instance | 9.13.2 | 0..1 | - TODO instance-identifier |
57 * | type | 7.4 | 0..n | - TODO union |
58 * +------------------+---------+-------------+------------------------------------+
59 */
60
61/**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Represents the data type information.
Vinod Kumar Scf044422016-02-09 19:53:45 +053063 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053064 * @param <T> YANG data type info
Vinod Kumar Scf044422016-02-09 19:53:45 +053065 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053066public class YangType<T>
Bharat saraswal96dfef02016-06-16 00:29:12 +053067 implements Parsable, Resolvable, Serializable {
68
69 private static final long serialVersionUID = 8062016054L;
Vinod Kumar Scf044422016-02-09 19:53:45 +053070
71 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053072 * YANG node identifier.
Vinod Kumar Scf044422016-02-09 19:53:45 +053073 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053074 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar Scf044422016-02-09 19:53:45 +053075
76 /**
77 * YANG data type.
78 */
79 private YangDataTypes dataType;
80
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053081 /**
82 * Additional information about data type, example restriction info, named
83 * values, etc. The extra information is based on the data type. Based on
84 * the data type, the extended info can vary.
85 */
86 private T dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +053087
88 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053089 * Status of resolution. If completely resolved enum value is "RESOLVED",
90 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
91 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053092 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053093 */
94 private ResolvableStatus resolvableStatus;
95
96 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053097 * Creates a YANG type object.
Vinod Kumar Scf044422016-02-09 19:53:45 +053098 */
99 public YangType() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530100
101 nodeIdentifier = new YangNodeIdentifier();
102 resolvableStatus = ResolvableStatus.UNRESOLVED;
103 }
104
105 /**
106 * Returns prefix associated with data type name.
107 *
108 * @return prefix associated with data type name
109 */
110 public String getPrefix() {
111 return nodeIdentifier.getPrefix();
112 }
113
114 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530115 * Sets prefix associated with data type name.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530116 *
117 * @param prefix prefix associated with data type name
118 */
119 public void setPrefix(String prefix) {
120 nodeIdentifier.setPrefix(prefix);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530121 }
122
123 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530124 * Returns the name of data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530125 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530126 * @return the name of data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530127 */
128 public String getDataTypeName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530129 return nodeIdentifier.getName();
Vinod Kumar Scf044422016-02-09 19:53:45 +0530130 }
131
132 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530133 * Sets the name of the data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530134 *
135 * @param typeName the name to set
136 */
137 public void setDataTypeName(String typeName) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530138 nodeIdentifier.setName(typeName);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530139 }
140
141 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530142 * Returns the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530143 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530144 * @return the data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530145 */
146 public YangDataTypes getDataType() {
147 return dataType;
148 }
149
150 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Sets the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530152 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530153 * @param dataType data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530154 */
155 public void setDataType(YangDataTypes dataType) {
156 this.dataType = dataType;
157 }
158
159 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530160 * Returns the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530161 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530162 * @return the data type meta data
Vinod Kumar Scf044422016-02-09 19:53:45 +0530163 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530164 public T getDataTypeExtendedInfo() {
165 return dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530166 }
167
168 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530169 * Sets the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530170 *
171 * @param dataTypeInfo the meta data to set
172 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530173 public void setDataTypeExtendedInfo(T dataTypeInfo) {
174 this.dataTypeExtendedInfo = dataTypeInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530175 }
176
177 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530178 * Returns node identifier.
179 *
180 * @return node identifier
181 */
182 public YangNodeIdentifier getNodeIdentifier() {
183 return nodeIdentifier;
184 }
185
186 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530187 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530188 *
189 * @param nodeIdentifier the node identifier
190 */
191 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
192 this.nodeIdentifier = nodeIdentifier;
193 }
194
195 /**
janani be18b5342016-07-13 21:06:41 +0530196 * Resets the class attributes to its default value.
197 */
198 public void resetYangType() {
199 nodeIdentifier = new YangNodeIdentifier();
200 resolvableStatus = ResolvableStatus.UNRESOLVED;
201 dataType = null;
202 dataTypeExtendedInfo = null;
203 }
204
205 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +0530206 * Returns the type of the parsed data.
207 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530208 * @return returns TYPE_DATA
Vinod Kumar Scf044422016-02-09 19:53:45 +0530209 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530210 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530211 public YangConstructType getYangConstructType() {
212 return YangConstructType.TYPE_DATA;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530213 }
214
215 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530216 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530217 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530218 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530219 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530220 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530221 public void validateDataOnEntry()
222 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530223 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530224 }
225
226 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530227 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530228 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530229 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530230 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530231 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530232 public void validateDataOnExit()
233 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530234 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530235 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530236
237 @Override
238 public ResolvableStatus getResolvableStatus() {
239 return resolvableStatus;
240 }
241
242 @Override
243 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
244 this.resolvableStatus = resolvableStatus;
245 }
246
247 @Override
janani b23ccc312016-07-14 19:35:22 +0530248 public Object resolve()
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530249 throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530250 /*
251 * Check whether the data type is derived.
252 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530253 if (getDataType() != DERIVED) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530254 throw new DataModelException("Linker Error: Resolve should only be called for derived data types.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530255 }
256
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530257 // Check if the derived info is present.
258 YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
259 if (derivedInfo == null) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530260 throw new DataModelException("Linker Error: Derived information is missing.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530261 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530262
263 // Initiate the resolution
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530264 try {
265 setResolvableStatus(derivedInfo.resolve());
266 } catch (DataModelException e) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530267 throw new DataModelException(e.getMessage());
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530268 }
janani b23ccc312016-07-14 19:35:22 +0530269 return null;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530270 }
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530271
272 /**
273 * Validates the input data value against the permissible value for the
274 * type as per the YANG file.
275 *
276 * @param value input data value
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530277 * @throws DataModelException a violation of data model rules
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530278 */
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530279 public void isValidValue(String value) throws DataModelException {
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530280 switch (getDataType()) {
281 case INT8:
282 case INT16:
283 case INT32:
284 case INT64:
285 case UINT8:
286 case UINT16:
287 case UINT32:
288 case UINT64: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530289 if (getDataTypeExtendedInfo() == null) {
290 getDataObjectFromString(value, getDataType());
291 } else {
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530292 if (!((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value)) {
293 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
294 getDataType());
295 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530296 }
297 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530298 }
299 case DECIMAL64: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530300 // Fraction-Digits and range needs to get it from yang
301 YangDecimal64<YangRangeRestriction> decimal64 =
302 (YangDecimal64<YangRangeRestriction>) getDataTypeExtendedInfo();
303 validateDecimal64(value, decimal64.getFractionDigit(),
304 decimal64.getRangeRestrictedExtendedInfo());
305 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530306 }
307 case STRING: {
Bharat saraswal6a5911f2016-08-02 18:43:16 +0530308 if (getDataTypeExtendedInfo() == null) {
309 break;
310 } else if (!(((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnLengthRestriction(value)
311 && ((YangStringRestriction) getDataTypeExtendedInfo())
312 .isValidStringOnPatternRestriction(value))) {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530313 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530314 getDataType());
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530315 }
316 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530317 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530318 case BOOLEAN:
319 if (!(value.equals(DataModelUtils.TRUE) || value.equals(DataModelUtils.FALSE))) {
320 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530321 getDataType());
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530322 }
323 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530324 case ENUMERATION: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530325 Iterator<YangEnum> iterator = ((YangEnumeration) getDataTypeExtendedInfo()).getEnumSet().iterator();
326 boolean isValidated = false;
327 while (iterator.hasNext()) {
Bharat saraswal6a5911f2016-08-02 18:43:16 +0530328 YangEnum enumTemp = iterator.next();
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530329 if (enumTemp.getNamedValue().equals(value)) {
330 isValidated = true;
331 break;
332 }
333 }
334
335 if (!isValidated) {
336 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530337 getDataType());
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530338 }
339 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530340 }
341 case BITS: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530342 YangBits bits = (YangBits) getDataTypeExtendedInfo();
343 if (bits.fromString(value) == null) {
344 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530345 getDataType());
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530346 }
347 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530348 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530349 case BINARY: {
350 if (!isValidBinary(value, (YangRangeRestriction) getDataTypeExtendedInfo())) {
351 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530352 getDataType());
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530353 }
354 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530355 }
356 case LEAFREF: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530357 YangLeafRef<?> leafRef = (YangLeafRef<?>) getDataTypeExtendedInfo();
358 leafRef.validateDataOnExit();
359 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530360 }
361 case IDENTITYREF: {
362 // TODO TBD
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530363 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530364 }
365 case EMPTY: {
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530366 if (value.length() > 0) {
367 throw new DataTypeException("YANG file error : Input value \"" + value
368 + "\" is not allowed for a data type " + getDataType());
369 }
370 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530371 }
372 case UNION: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530373 ListIterator<YangType<?>> listIterator = ((YangUnion) getDataTypeExtendedInfo()).getTypeList()
374 .listIterator();
375 boolean isValidated = false;
376 while (listIterator.hasNext()) {
377 YangType<?> type = (YangType<?>) listIterator.next();
378 try {
379 type.isValidValue(value);
380 // If it is not thrown exception then validation is success
381 isValidated = true;
382 break;
383 } catch (Exception e) {
384 }
385 }
386
387 if (!isValidated) {
388 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530389 getDataType());
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530390 }
391 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530392 }
393 case INSTANCE_IDENTIFIER: {
394 // TODO TBD
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530395 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530396 }
397 case DERIVED: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530398 YangDataTypes dataType = ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType();
399 if (isOfRangeRestrictedType(dataType)) {
400 if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) {
401 getDataObjectFromString(value,
402 ((YangDerivedInfo) getDataTypeExtendedInfo())
403 .getEffectiveBuiltInType());
404 } else {
405 if (!((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
406 .getResolvedExtendedInfo()).isValidValueString(value)) {
407 throw new DataTypeException("YANG file error : Input value \"" + value
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530408 + "\" is not a valid " + dataType);
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530409 }
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530410 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530411 } else if (dataType == YangDataTypes.STRING) {
412 if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() != null) {
413 YangStringRestriction stringRestriction =
414 ((YangStringRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
415 .getResolvedExtendedInfo());
416 if (!(stringRestriction.isValidStringOnLengthRestriction(value) &&
417 stringRestriction.isValidStringOnPatternRestriction(value))) {
418 throw new DataTypeException("YANG file error : Input value \"" + value
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530419 + "\" is not a valid " + dataType);
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530420 }
421 }
422 } else if (dataType == YangDataTypes.BITS) {
423 YangBits bits = (YangBits) getDataTypeExtendedInfo();
424 if (bits.fromString(value) == null) {
425 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530426 dataType);
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530427 }
428 } else if (dataType == YangDataTypes.BINARY) {
429 if (!isValidBinary(value, (YangRangeRestriction) ((YangDerivedInfo)
430 getDataTypeExtendedInfo()).getResolvedExtendedInfo())) {
431 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530432 dataType);
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530433 }
434 } else if (dataType == YangDataTypes.DECIMAL64) {
435 YangDerivedInfo derivedInfo = (YangDerivedInfo) getDataTypeExtendedInfo();
436 YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
437 YangType<YangDecimal64> decimal64Type =
438 (YangType<YangDecimal64>) typedef.getTypeList().iterator().next();
439 YangDecimal64<YangRangeRestriction> decimal64 = decimal64Type.getDataTypeExtendedInfo();
440 // Fraction-Digits and range needs to get it from yang
441 validateDecimal64(value, decimal64.getFractionDigit(),
442 decimal64.getRangeRestrictedExtendedInfo());
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530443 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530444 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530445 }
446 default: {
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530447 throw new DataTypeException("YANG file error : Input value \"" + value + "\" received for " +
448 "unsupported data type " + getDataType());
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530449 }
450 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530451 }
452
453
454 /**
455 * Checks whether specific string is valid decimal64 value.
456 *
457 * @param value decimal64 value
458 */
459 private void validateDecimal64(String value, int fractionDigit, YangRangeRestriction rangeRestriction)
460 throws DataModelException {
461 YangDecimal64<YangRangeRestriction> decimal64 = YangDecimal64.fromString(value);
462 decimal64.setFractionDigit(fractionDigit);
463 decimal64.setRangeRestrictedExtendedInfo(rangeRestriction);
464 decimal64.validateDecimal64();
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530465 }
466
467 /**
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530468 * Checks whether specific string is valid binary.
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530469 *
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530470 * @param value binary value
471 * @return true if validation success otherwise false
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530472 */
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530473 private boolean isValidBinary(String value, YangRangeRestriction lengthRestriction) {
474 YangBinary binary = new YangBinary(value);
475
476 // After decoding binary, its length should not be zero
477 if (binary.getBinaryData().length == 0) {
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530478 return false;
479 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530480
481 if (lengthRestriction == null || lengthRestriction.getAscendingRangeIntervals() == null
482 || lengthRestriction.getAscendingRangeIntervals().isEmpty()) {
483 // Length restriction is optional
484 return true;
485 }
486
487 ListIterator<YangRangeInterval<YangUint64>> rangeListIterator = lengthRestriction.getAscendingRangeIntervals()
488 .listIterator();
489 boolean isMatched = false;
490 while (rangeListIterator.hasNext()) {
491 YangRangeInterval rangeInterval = rangeListIterator.next();
492 BigInteger startValue = ((YangUint64) rangeInterval.getStartValue()).getValue();
493 BigInteger endValue = ((YangUint64) rangeInterval.getEndValue()).getValue();
494 // convert (encode) back and check length
495 if ((binary.toString().length() >= startValue.intValue()) &&
496 (binary.toString().length() <= endValue.intValue())) {
497 isMatched = true;
498 break;
499 }
500 }
501
502 return isMatched;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530503 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530504}