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; |
| 23 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; |
| 24 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
| 25 | |
| 26 | import java.io.Serializable; |
| 27 | import java.math.BigDecimal; |
| 28 | import java.util.ListIterator; |
| 29 | |
| 30 | /** |
| 31 | * Represents YANG decimal 64. |
| 32 | */ |
| 33 | public class YangDecimal64<T> |
| 34 | implements YangBuiltInDataTypeInfo<YangDecimal64>, Parsable, Serializable, Comparable<YangDecimal64> { |
| 35 | |
| 36 | private static final long serialVersionUID = 8006201668L; |
| 37 | |
| 38 | /** |
| 39 | * YANG's min keyword. |
| 40 | */ |
| 41 | private static final String MIN_KEYWORD = "min"; |
| 42 | |
| 43 | /** |
| 44 | * YANG's max keyword. |
| 45 | */ |
| 46 | private static final String MAX_KEYWORD = "max"; |
| 47 | |
| 48 | /** |
| 49 | * Valid minimum value of YANG's fraction-digits. |
| 50 | */ |
| 51 | public static final int MIN_FRACTION_DIGITS_VALUE = 1; |
| 52 | |
| 53 | /** |
| 54 | * Valid maximum value of YANG's fraction-digits. |
| 55 | */ |
| 56 | public static final int MAX_FRACTION_DIGITS_VALUE = 18; |
| 57 | |
| 58 | // Decimal64 value |
| 59 | private BigDecimal value; |
| 60 | |
| 61 | // fraction-digits |
| 62 | private int fractionDigit; |
| 63 | |
| 64 | /** |
| 65 | * Additional information about range restriction. |
| 66 | */ |
| 67 | private T rangeRestrictedExtendedInfo; |
| 68 | |
| 69 | /** |
| 70 | * Creates an instance of YANG decimal64. |
| 71 | */ |
| 72 | public YangDecimal64() { |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates an instance of YANG decimal64. |
| 77 | * |
| 78 | * @param value of decimal64 |
| 79 | */ |
| 80 | public YangDecimal64(BigDecimal value) { |
| 81 | setValue(value); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Creates an instance of YANG decimal64. |
| 86 | * |
| 87 | * @param value of decimal64 in string |
| 88 | * @throws DataModelException a violation of data model rules |
| 89 | */ |
| 90 | public YangDecimal64(String value) throws DataModelException { |
| 91 | fromString(value); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns decimal64 value. |
| 96 | * |
| 97 | * @return value |
| 98 | */ |
| 99 | public BigDecimal getValue() { |
| 100 | return value; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets the decimal64 value. |
| 105 | * |
| 106 | * @param value of decimal64 |
| 107 | */ |
| 108 | public void setValue(BigDecimal value) { |
| 109 | this.value = value; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns fraction digit. |
| 114 | * |
| 115 | * @return the fractionDigit |
| 116 | */ |
| 117 | public int getFractionDigit() { |
| 118 | return fractionDigit; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sets fraction digit. |
| 123 | * |
| 124 | * @param fractionDigit fraction digits. |
| 125 | */ |
| 126 | public void setFractionDigit(int fractionDigit) { |
| 127 | this.fractionDigit = fractionDigit; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns additional information about range restriction. |
| 132 | * |
| 133 | * @return resolved range restricted extended information |
| 134 | */ |
| 135 | public T getRangeRestrictedExtendedInfo() { |
| 136 | return rangeRestrictedExtendedInfo; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets additional information about range restriction. |
| 141 | * |
| 142 | * @param resolvedExtendedInfo resolved range restricted extended information |
| 143 | */ |
| 144 | public void setRangeRestrictedExtendedInfo(T resolvedExtendedInfo) { |
| 145 | this.rangeRestrictedExtendedInfo = resolvedExtendedInfo; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns object of YANG decimal64. |
| 150 | * |
| 151 | * @param value of decimal64 |
| 152 | * @return YANG decimal64 object |
| 153 | */ |
| 154 | public static YangDecimal64 of(BigDecimal value) { |
| 155 | return new YangDecimal64(value); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public YangDataTypes getYangType() { |
| 160 | return YangDataTypes.DECIMAL64; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public YangConstructType getYangConstructType() { |
| 165 | return YangConstructType.DECIMAL64_DATA; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public String toString() { |
| 170 | return value.toString(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns the object of YANG decimal64 from input string. |
| 175 | * |
| 176 | * @param valInString input String |
| 177 | * @return Object of YANG decimal64 |
| 178 | * @throws DataModelException a violation of data model rules |
| 179 | */ |
| 180 | public static YangDecimal64 fromString(String valInString) throws DataModelException { |
| 181 | YangDecimal64 decimal64; |
| 182 | decimal64 = of(new BigDecimal(valInString)); |
| 183 | decimal64.validateValue(); |
| 184 | return decimal64; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Validate decimal64 value. |
| 189 | * |
| 190 | * @throws DataModelException a violation of data model rules |
| 191 | */ |
| 192 | public void validateValue() throws DataModelException { |
| 193 | if (!(FractionDigits.isValidDecimal64(this.value, this.fractionDigit))) { |
| 194 | throw new DataModelException("YANG file error : decimal64 validation failed."); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Validate range restriction values based on fraction-digits decimal64 range value. |
| 200 | * |
| 201 | * @throws DataModelException a violation of data model rules |
| 202 | */ |
| 203 | public void validateRange() throws DataModelException { |
| 204 | YangRangeRestriction rangeRestriction = (YangRangeRestriction) getRangeRestrictedExtendedInfo(); |
| 205 | if (rangeRestriction == null) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals() |
| 210 | .listIterator(); |
| 211 | while (rangeListIterator.hasNext()) { |
| 212 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
| 213 | if (!(FractionDigits.isValidDecimal64(((YangDecimal64) rangeInterval.getStartValue()).getValue(), |
| 214 | getFractionDigit()))) { |
| 215 | throw new DataModelException("YANG file error : decimal64 validation failed."); |
| 216 | } |
| 217 | |
| 218 | if (!(FractionDigits.isValidDecimal64(((YangDecimal64) rangeInterval.getEndValue()).getValue(), |
| 219 | getFractionDigit()))) { |
| 220 | throw new DataModelException("YANG file error : decimal64 validation failed."); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public int compareTo(YangDecimal64 o) { |
| 227 | return Double.compare(value.doubleValue(), o.value.doubleValue()); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns decimal64 default range restriction based on fraction-digits. |
| 232 | * If range restriction is not provided then this default range value will be applicable. |
| 233 | * |
| 234 | * @return range restriction |
| 235 | * @throws DataModelException a violation of data model rules |
| 236 | */ |
| 237 | public YangRangeRestriction getDefaultRangeRestriction() throws DataModelException { |
| 238 | YangRangeRestriction refRangeRestriction = new YangRangeRestriction(); |
| 239 | YangRangeInterval rangeInterval = new YangRangeInterval<>(); |
| 240 | FractionDigits.Range range = FractionDigits.getRange(this.fractionDigit); |
| 241 | rangeInterval.setStartValue(new YangDecimal64(new BigDecimal((range.getMin())))); |
| 242 | rangeInterval.setEndValue(new YangDecimal64(new BigDecimal((range.getMax())))); |
| 243 | refRangeRestriction.addRangeRestrictionInterval(rangeInterval); |
| 244 | return refRangeRestriction; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Validates the data on entering the corresponding parse tree node. |
| 249 | * |
| 250 | * @throws DataModelException a violation of data model rules |
| 251 | */ |
| 252 | @Override |
| 253 | public void validateDataOnEntry() throws DataModelException { |
| 254 | // TODO auto-generated method stub, to be implemented by parser |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Validates the data on exiting the corresponding parse tree node. |
| 259 | * |
| 260 | * @throws DataModelException a violation of data model rules |
| 261 | */ |
| 262 | @Override |
| 263 | public void validateDataOnExit() throws DataModelException { |
| 264 | // TODO auto-generated method stub, to be implemented by parser |
| 265 | } |
| 266 | } |