blob: 6815a3ac0e0eb84800d67f40453bfbdd5f4fa9cd [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 {
292 ((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value);
293 }
294 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530295 }
296 case DECIMAL64: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530297 // Fraction-Digits and range needs to get it from yang
298 YangDecimal64<YangRangeRestriction> decimal64 =
299 (YangDecimal64<YangRangeRestriction>) getDataTypeExtendedInfo();
300 validateDecimal64(value, decimal64.getFractionDigit(),
301 decimal64.getRangeRestrictedExtendedInfo());
302 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530303 }
304 case STRING: {
Bharat saraswal6a5911f2016-08-02 18:43:16 +0530305 if (getDataTypeExtendedInfo() == null) {
306 break;
307 } else if (!(((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnLengthRestriction(value)
308 && ((YangStringRestriction) getDataTypeExtendedInfo())
309 .isValidStringOnPatternRestriction(value))) {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530310 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
311 "string");
312 }
313 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530314 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530315 case BOOLEAN:
316 if (!(value.equals(DataModelUtils.TRUE) || value.equals(DataModelUtils.FALSE))) {
317 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
318 "boolean");
319 }
320 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530321 case ENUMERATION: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530322 Iterator<YangEnum> iterator = ((YangEnumeration) getDataTypeExtendedInfo()).getEnumSet().iterator();
323 boolean isValidated = false;
324 while (iterator.hasNext()) {
Bharat saraswal6a5911f2016-08-02 18:43:16 +0530325 YangEnum enumTemp = iterator.next();
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530326 if (enumTemp.getNamedValue().equals(value)) {
327 isValidated = true;
328 break;
329 }
330 }
331
332 if (!isValidated) {
333 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
334 "union");
335 }
336 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530337 }
338 case BITS: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530339 YangBits bits = (YangBits) getDataTypeExtendedInfo();
340 if (bits.fromString(value) == null) {
341 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
342 "bits");
343 }
344 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530345 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530346 case BINARY: {
347 if (!isValidBinary(value, (YangRangeRestriction) getDataTypeExtendedInfo())) {
348 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
349 "binary");
350 }
351 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530352 }
353 case LEAFREF: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530354 YangLeafRef<?> leafRef = (YangLeafRef<?>) getDataTypeExtendedInfo();
355 leafRef.validateDataOnExit();
356 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530357 }
358 case IDENTITYREF: {
359 // TODO TBD
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530360 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530361 }
362 case EMPTY: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530363 throw new DataTypeException("YANG file error : Input value \"" + value
364 + "\" is not a allowed for a data type " + "empty");
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530365 }
366 case UNION: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530367 ListIterator<YangType<?>> listIterator = ((YangUnion) getDataTypeExtendedInfo()).getTypeList()
368 .listIterator();
369 boolean isValidated = false;
370 while (listIterator.hasNext()) {
371 YangType<?> type = (YangType<?>) listIterator.next();
372 try {
373 type.isValidValue(value);
374 // If it is not thrown exception then validation is success
375 isValidated = true;
376 break;
377 } catch (Exception e) {
378 }
379 }
380
381 if (!isValidated) {
382 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
383 "union");
384 }
385 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530386 }
387 case INSTANCE_IDENTIFIER: {
388 // TODO TBD
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530389 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530390 }
391 case DERIVED: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530392 YangDataTypes dataType = ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType();
393 if (isOfRangeRestrictedType(dataType)) {
394 if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) {
395 getDataObjectFromString(value,
396 ((YangDerivedInfo) getDataTypeExtendedInfo())
397 .getEffectiveBuiltInType());
398 } else {
399 if (!((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
400 .getResolvedExtendedInfo()).isValidValueString(value)) {
401 throw new DataTypeException("YANG file error : Input value \"" + value
402 + "\" is not a valid " + dataType.toString());
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530403 }
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530404 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530405 } else if (dataType == YangDataTypes.STRING) {
406 if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() != null) {
407 YangStringRestriction stringRestriction =
408 ((YangStringRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
409 .getResolvedExtendedInfo());
410 if (!(stringRestriction.isValidStringOnLengthRestriction(value) &&
411 stringRestriction.isValidStringOnPatternRestriction(value))) {
412 throw new DataTypeException("YANG file error : Input value \"" + value
413 + "\" is not a valid " + dataType.toString());
414 }
415 }
416 } else if (dataType == YangDataTypes.BITS) {
417 YangBits bits = (YangBits) getDataTypeExtendedInfo();
418 if (bits.fromString(value) == null) {
419 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
420 "bits");
421 }
422 } else if (dataType == YangDataTypes.BINARY) {
423 if (!isValidBinary(value, (YangRangeRestriction) ((YangDerivedInfo)
424 getDataTypeExtendedInfo()).getResolvedExtendedInfo())) {
425 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
426 dataType.toString());
427 }
428 } else if (dataType == YangDataTypes.DECIMAL64) {
429 YangDerivedInfo derivedInfo = (YangDerivedInfo) getDataTypeExtendedInfo();
430 YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
431 YangType<YangDecimal64> decimal64Type =
432 (YangType<YangDecimal64>) typedef.getTypeList().iterator().next();
433 YangDecimal64<YangRangeRestriction> decimal64 = decimal64Type.getDataTypeExtendedInfo();
434 // Fraction-Digits and range needs to get it from yang
435 validateDecimal64(value, decimal64.getFractionDigit(),
436 decimal64.getRangeRestrictedExtendedInfo());
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530437 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530438 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530439 }
440 default: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530441 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is for unsupported " +
442 "data type.");
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530443 }
444 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530445 }
446
447
448 /**
449 * Checks whether specific string is valid decimal64 value.
450 *
451 * @param value decimal64 value
452 */
453 private void validateDecimal64(String value, int fractionDigit, YangRangeRestriction rangeRestriction)
454 throws DataModelException {
455 YangDecimal64<YangRangeRestriction> decimal64 = YangDecimal64.fromString(value);
456 decimal64.setFractionDigit(fractionDigit);
457 decimal64.setRangeRestrictedExtendedInfo(rangeRestriction);
458 decimal64.validateDecimal64();
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530459 }
460
461 /**
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530462 * Checks whether specific string is valid binary.
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530463 *
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530464 * @param value binary value
465 * @return true if validation success otherwise false
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530466 */
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530467 private boolean isValidBinary(String value, YangRangeRestriction lengthRestriction) {
468 YangBinary binary = new YangBinary(value);
469
470 // After decoding binary, its length should not be zero
471 if (binary.getBinaryData().length == 0) {
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530472 return false;
473 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530474
475 if (lengthRestriction == null || lengthRestriction.getAscendingRangeIntervals() == null
476 || lengthRestriction.getAscendingRangeIntervals().isEmpty()) {
477 // Length restriction is optional
478 return true;
479 }
480
481 ListIterator<YangRangeInterval<YangUint64>> rangeListIterator = lengthRestriction.getAscendingRangeIntervals()
482 .listIterator();
483 boolean isMatched = false;
484 while (rangeListIterator.hasNext()) {
485 YangRangeInterval rangeInterval = rangeListIterator.next();
486 BigInteger startValue = ((YangUint64) rangeInterval.getStartValue()).getValue();
487 BigInteger endValue = ((YangUint64) rangeInterval.getEndValue()).getValue();
488 // convert (encode) back and check length
489 if ((binary.toString().length() >= startValue.intValue()) &&
490 (binary.toString().length() <= endValue.intValue())) {
491 isMatched = true;
492 break;
493 }
494 }
495
496 return isMatched;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530497 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530498}