blob: 75b92b45156afe0669a81c0deb542b1f0f293043 [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: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530305 if (!(((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnLengthRestriction(value) &&
306 ((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnPatternRestriction(value))) {
307 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
308 "string");
309 }
310 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530311 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530312 case BOOLEAN:
313 if (!(value.equals(DataModelUtils.TRUE) || value.equals(DataModelUtils.FALSE))) {
314 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
315 "boolean");
316 }
317 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530318 case ENUMERATION: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530319 Iterator<YangEnum> iterator = ((YangEnumeration) getDataTypeExtendedInfo()).getEnumSet().iterator();
320 boolean isValidated = false;
321 while (iterator.hasNext()) {
322 YangEnum enumTemp = (YangEnum) iterator.next();
323 if (enumTemp.getNamedValue().equals(value)) {
324 isValidated = true;
325 break;
326 }
327 }
328
329 if (!isValidated) {
330 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
331 "union");
332 }
333 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530334 }
335 case BITS: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530336 YangBits bits = (YangBits) getDataTypeExtendedInfo();
337 if (bits.fromString(value) == null) {
338 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
339 "bits");
340 }
341 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530342 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530343 case BINARY: {
344 if (!isValidBinary(value, (YangRangeRestriction) getDataTypeExtendedInfo())) {
345 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
346 "binary");
347 }
348 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530349 }
350 case LEAFREF: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530351 YangLeafRef<?> leafRef = (YangLeafRef<?>) getDataTypeExtendedInfo();
352 leafRef.validateDataOnExit();
353 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530354 }
355 case IDENTITYREF: {
356 // TODO TBD
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530357 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530358 }
359 case EMPTY: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530360 throw new DataTypeException("YANG file error : Input value \"" + value
361 + "\" is not a allowed for a data type " + "empty");
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530362 }
363 case UNION: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530364 ListIterator<YangType<?>> listIterator = ((YangUnion) getDataTypeExtendedInfo()).getTypeList()
365 .listIterator();
366 boolean isValidated = false;
367 while (listIterator.hasNext()) {
368 YangType<?> type = (YangType<?>) listIterator.next();
369 try {
370 type.isValidValue(value);
371 // If it is not thrown exception then validation is success
372 isValidated = true;
373 break;
374 } catch (Exception e) {
375 }
376 }
377
378 if (!isValidated) {
379 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
380 "union");
381 }
382 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530383 }
384 case INSTANCE_IDENTIFIER: {
385 // TODO TBD
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530386 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530387 }
388 case DERIVED: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530389 YangDataTypes dataType = ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType();
390 if (isOfRangeRestrictedType(dataType)) {
391 if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) {
392 getDataObjectFromString(value,
393 ((YangDerivedInfo) getDataTypeExtendedInfo())
394 .getEffectiveBuiltInType());
395 } else {
396 if (!((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
397 .getResolvedExtendedInfo()).isValidValueString(value)) {
398 throw new DataTypeException("YANG file error : Input value \"" + value
399 + "\" is not a valid " + dataType.toString());
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530400 }
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530401 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530402 } else if (dataType == YangDataTypes.STRING) {
403 if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() != null) {
404 YangStringRestriction stringRestriction =
405 ((YangStringRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
406 .getResolvedExtendedInfo());
407 if (!(stringRestriction.isValidStringOnLengthRestriction(value) &&
408 stringRestriction.isValidStringOnPatternRestriction(value))) {
409 throw new DataTypeException("YANG file error : Input value \"" + value
410 + "\" is not a valid " + dataType.toString());
411 }
412 }
413 } else if (dataType == YangDataTypes.BITS) {
414 YangBits bits = (YangBits) getDataTypeExtendedInfo();
415 if (bits.fromString(value) == null) {
416 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
417 "bits");
418 }
419 } else if (dataType == YangDataTypes.BINARY) {
420 if (!isValidBinary(value, (YangRangeRestriction) ((YangDerivedInfo)
421 getDataTypeExtendedInfo()).getResolvedExtendedInfo())) {
422 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
423 dataType.toString());
424 }
425 } else if (dataType == YangDataTypes.DECIMAL64) {
426 YangDerivedInfo derivedInfo = (YangDerivedInfo) getDataTypeExtendedInfo();
427 YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
428 YangType<YangDecimal64> decimal64Type =
429 (YangType<YangDecimal64>) typedef.getTypeList().iterator().next();
430 YangDecimal64<YangRangeRestriction> decimal64 = decimal64Type.getDataTypeExtendedInfo();
431 // Fraction-Digits and range needs to get it from yang
432 validateDecimal64(value, decimal64.getFractionDigit(),
433 decimal64.getRangeRestrictedExtendedInfo());
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530434 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530435 break;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530436 }
437 default: {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530438 throw new DataTypeException("YANG file error : Input value \"" + value + "\" is for unsupported " +
439 "data type.");
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530440 }
441 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530442 }
443
444
445 /**
446 * Checks whether specific string is valid decimal64 value.
447 *
448 * @param value decimal64 value
449 */
450 private void validateDecimal64(String value, int fractionDigit, YangRangeRestriction rangeRestriction)
451 throws DataModelException {
452 YangDecimal64<YangRangeRestriction> decimal64 = YangDecimal64.fromString(value);
453 decimal64.setFractionDigit(fractionDigit);
454 decimal64.setRangeRestrictedExtendedInfo(rangeRestriction);
455 decimal64.validateDecimal64();
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530456 }
457
458 /**
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530459 * Checks whether specific string is valid binary.
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530460 *
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530461 * @param value binary value
462 * @return true if validation success otherwise false
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530463 */
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530464 private boolean isValidBinary(String value, YangRangeRestriction lengthRestriction) {
465 YangBinary binary = new YangBinary(value);
466
467 // After decoding binary, its length should not be zero
468 if (binary.getBinaryData().length == 0) {
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530469 return false;
470 }
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530471
472 if (lengthRestriction == null || lengthRestriction.getAscendingRangeIntervals() == null
473 || lengthRestriction.getAscendingRangeIntervals().isEmpty()) {
474 // Length restriction is optional
475 return true;
476 }
477
478 ListIterator<YangRangeInterval<YangUint64>> rangeListIterator = lengthRestriction.getAscendingRangeIntervals()
479 .listIterator();
480 boolean isMatched = false;
481 while (rangeListIterator.hasNext()) {
482 YangRangeInterval rangeInterval = rangeListIterator.next();
483 BigInteger startValue = ((YangUint64) rangeInterval.getStartValue()).getValue();
484 BigInteger endValue = ((YangUint64) rangeInterval.getEndValue()).getValue();
485 // convert (encode) back and check length
486 if ((binary.toString().length() >= startValue.intValue()) &&
487 (binary.toString().length() <= endValue.intValue())) {
488 isMatched = true;
489 break;
490 }
491 }
492
493 return isMatched;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530494 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530495}