Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.datamodel; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 20 | import org.onosproject.yangutils.datamodel.utils.FractionDigits; |
| 21 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 22 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 23 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException; |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; |
| 25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
| 26 | |
| 27 | import java.io.Serializable; |
| 28 | import java.math.BigDecimal; |
| 29 | import java.util.ListIterator; |
| 30 | |
| 31 | /** |
| 32 | * Represents YANG decimal 64. |
| 33 | */ |
| 34 | public class YangDecimal64<T> |
| 35 | implements YangBuiltInDataTypeInfo<YangDecimal64>, Parsable, Serializable, Comparable<YangDecimal64> { |
| 36 | |
| 37 | private static final long serialVersionUID = 8006201668L; |
| 38 | |
| 39 | /** |
| 40 | * YANG's min keyword. |
| 41 | */ |
| 42 | private static final String MIN_KEYWORD = "min"; |
| 43 | |
| 44 | /** |
| 45 | * YANG's max keyword. |
| 46 | */ |
| 47 | private static final String MAX_KEYWORD = "max"; |
| 48 | |
| 49 | /** |
| 50 | * Valid minimum value of YANG's fraction-digits. |
| 51 | */ |
| 52 | public static final int MIN_FRACTION_DIGITS_VALUE = 1; |
| 53 | |
| 54 | /** |
| 55 | * Valid maximum value of YANG's fraction-digits. |
| 56 | */ |
| 57 | public static final int MAX_FRACTION_DIGITS_VALUE = 18; |
| 58 | |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 59 | /** |
| 60 | * Valid minimum value of YANG's decimal64. |
| 61 | */ |
| 62 | public static final BigDecimal MIN_VALUE = BigDecimal.valueOf(-922337203685477580.8); |
| 63 | |
| 64 | /** |
| 65 | * Valid maximum value of YANG's decimal64. |
| 66 | */ |
| 67 | public static final BigDecimal MAX_VALUE = BigDecimal.valueOf(922337203685477580.7); |
| 68 | |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 69 | // Decimal64 value |
| 70 | private BigDecimal value; |
| 71 | |
| 72 | // fraction-digits |
| 73 | private int fractionDigit; |
| 74 | |
| 75 | /** |
| 76 | * Additional information about range restriction. |
| 77 | */ |
| 78 | private T rangeRestrictedExtendedInfo; |
| 79 | |
| 80 | /** |
| 81 | * Creates an instance of YANG decimal64. |
| 82 | */ |
| 83 | public YangDecimal64() { |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates an instance of YANG decimal64. |
| 88 | * |
| 89 | * @param value of decimal64 |
| 90 | */ |
| 91 | public YangDecimal64(BigDecimal value) { |
| 92 | setValue(value); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates an instance of YANG decimal64. |
| 97 | * |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 98 | * @param valueInString of decimal64 in string |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 99 | */ |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 100 | public YangDecimal64(String valueInString) { |
| 101 | if (valueInString.matches(MIN_KEYWORD)) { |
| 102 | value = MIN_VALUE; |
| 103 | } else if (valueInString.matches(MAX_KEYWORD)) { |
| 104 | value = MAX_VALUE; |
| 105 | } else { |
| 106 | try { |
| 107 | value = new BigDecimal(valueInString); |
| 108 | } catch (Exception e) { |
| 109 | throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " + |
| 110 | "decimal64."); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (value.doubleValue() < MIN_VALUE.doubleValue()) { |
| 115 | throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value " |
| 116 | + MIN_VALUE + "."); |
| 117 | } else if (value.doubleValue() > MAX_VALUE.doubleValue()) { |
| 118 | throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value " |
| 119 | + MAX_VALUE + "."); |
| 120 | } |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns decimal64 value. |
| 125 | * |
| 126 | * @return value |
| 127 | */ |
| 128 | public BigDecimal getValue() { |
| 129 | return value; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Sets the decimal64 value. |
| 134 | * |
| 135 | * @param value of decimal64 |
| 136 | */ |
| 137 | public void setValue(BigDecimal value) { |
| 138 | this.value = value; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns fraction digit. |
| 143 | * |
| 144 | * @return the fractionDigit |
| 145 | */ |
| 146 | public int getFractionDigit() { |
| 147 | return fractionDigit; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Sets fraction digit. |
| 152 | * |
| 153 | * @param fractionDigit fraction digits. |
| 154 | */ |
| 155 | public void setFractionDigit(int fractionDigit) { |
| 156 | this.fractionDigit = fractionDigit; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns additional information about range restriction. |
| 161 | * |
| 162 | * @return resolved range restricted extended information |
| 163 | */ |
| 164 | public T getRangeRestrictedExtendedInfo() { |
| 165 | return rangeRestrictedExtendedInfo; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Sets additional information about range restriction. |
| 170 | * |
| 171 | * @param resolvedExtendedInfo resolved range restricted extended information |
| 172 | */ |
| 173 | public void setRangeRestrictedExtendedInfo(T resolvedExtendedInfo) { |
| 174 | this.rangeRestrictedExtendedInfo = resolvedExtendedInfo; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns object of YANG decimal64. |
| 179 | * |
| 180 | * @param value of decimal64 |
| 181 | * @return YANG decimal64 object |
| 182 | */ |
| 183 | public static YangDecimal64 of(BigDecimal value) { |
| 184 | return new YangDecimal64(value); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public YangDataTypes getYangType() { |
| 189 | return YangDataTypes.DECIMAL64; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public YangConstructType getYangConstructType() { |
| 194 | return YangConstructType.DECIMAL64_DATA; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public String toString() { |
| 199 | return value.toString(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns the object of YANG decimal64 from input string. |
| 204 | * |
| 205 | * @param valInString input String |
| 206 | * @return Object of YANG decimal64 |
| 207 | * @throws DataModelException a violation of data model rules |
| 208 | */ |
| 209 | public static YangDecimal64 fromString(String valInString) throws DataModelException { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 210 | return new YangDecimal64(valInString); |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 214 | * Checks whether specific fraction-digit in its range. |
| 215 | * |
| 216 | * @return true if fraction-digit is in its range otherwise false |
| 217 | */ |
| 218 | public boolean isValidFractionDigit() { |
| 219 | if ((fractionDigit >= 1) && (fractionDigit <= 18)) { |
| 220 | return true; |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Checks whether value is in correct decimal64 value range. |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 228 | * |
| 229 | * @throws DataModelException a violation of data model rules |
| 230 | */ |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 231 | public void validateDecimal64() throws DataModelException { |
| 232 | YangRangeRestriction rangeRestriction = (YangRangeRestriction) getRangeRestrictedExtendedInfo(); |
| 233 | if (rangeRestriction != null) { |
| 234 | // Check whether value is within provided range value |
| 235 | ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals() |
| 236 | .listIterator(); |
| 237 | boolean isMatched = false; |
| 238 | while (rangeListIterator.hasNext()) { |
| 239 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
| 240 | BigDecimal startValue = ((YangDecimal64) rangeInterval.getStartValue()).getValue(); |
| 241 | BigDecimal endValue = ((YangDecimal64) rangeInterval.getEndValue()).getValue(); |
| 242 | if ((this.value.doubleValue() >= startValue.doubleValue()) && |
| 243 | (this.value.doubleValue() <= endValue.doubleValue())) { |
| 244 | isMatched = true; |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | // If range is not matched then throw error |
| 249 | if (!isMatched) { |
| 250 | throw new DataModelException("YANG file error : decimal64 validation failed."); |
| 251 | } |
| 252 | } else { |
| 253 | // Check value is in fraction-digits decimal64 value range |
| 254 | if (!FractionDigits.isValueInDecimal64Range(this.value, getFractionDigit())) { |
| 255 | throw new DataModelException("YANG file error : decimal64 validation failed."); |
| 256 | } |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Validate range restriction values based on fraction-digits decimal64 range value. |
| 262 | * |
| 263 | * @throws DataModelException a violation of data model rules |
| 264 | */ |
| 265 | public void validateRange() throws DataModelException { |
| 266 | YangRangeRestriction rangeRestriction = (YangRangeRestriction) getRangeRestrictedExtendedInfo(); |
| 267 | if (rangeRestriction == null) { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 268 | // No need to validate. Range is optional. |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
| 272 | ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals() |
| 273 | .listIterator(); |
| 274 | while (rangeListIterator.hasNext()) { |
| 275 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 276 | if (!(FractionDigits.isValueInDecimal64Range(((YangDecimal64) rangeInterval.getStartValue()).getValue(), |
| 277 | getFractionDigit()))) { |
| 278 | throw new DataModelException("YANG file error : range validation failed."); |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 279 | } |
| 280 | |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 281 | if (!(FractionDigits.isValueInDecimal64Range(((YangDecimal64) rangeInterval.getEndValue()).getValue(), |
| 282 | getFractionDigit()))) { |
| 283 | throw new DataModelException("YANG file error : range validation failed."); |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public int compareTo(YangDecimal64 o) { |
| 290 | return Double.compare(value.doubleValue(), o.value.doubleValue()); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Returns decimal64 default range restriction based on fraction-digits. |
| 295 | * If range restriction is not provided then this default range value will be applicable. |
| 296 | * |
| 297 | * @return range restriction |
| 298 | * @throws DataModelException a violation of data model rules |
| 299 | */ |
| 300 | public YangRangeRestriction getDefaultRangeRestriction() throws DataModelException { |
| 301 | YangRangeRestriction refRangeRestriction = new YangRangeRestriction(); |
| 302 | YangRangeInterval rangeInterval = new YangRangeInterval<>(); |
| 303 | FractionDigits.Range range = FractionDigits.getRange(this.fractionDigit); |
| 304 | rangeInterval.setStartValue(new YangDecimal64(new BigDecimal((range.getMin())))); |
| 305 | rangeInterval.setEndValue(new YangDecimal64(new BigDecimal((range.getMax())))); |
| 306 | refRangeRestriction.addRangeRestrictionInterval(rangeInterval); |
| 307 | return refRangeRestriction; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Validates the data on entering the corresponding parse tree node. |
| 312 | * |
| 313 | * @throws DataModelException a violation of data model rules |
| 314 | */ |
| 315 | @Override |
| 316 | public void validateDataOnEntry() throws DataModelException { |
| 317 | // TODO auto-generated method stub, to be implemented by parser |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Validates the data on exiting the corresponding parse tree node. |
| 322 | * |
| 323 | * @throws DataModelException a violation of data model rules |
| 324 | */ |
| 325 | @Override |
| 326 | public void validateDataOnExit() throws DataModelException { |
| 327 | // TODO auto-generated method stub, to be implemented by parser |
| 328 | } |
| 329 | } |