Committed by
Patrick Liu
[ONOS-4910, ONOS-4921] Framework: utils validation and defect fix
Change-Id: I821920fa8c88e64406b702c2b8736bdeaf231474
Showing
39 changed files
with
578 additions
and
106 deletions
... | @@ -16,7 +16,6 @@ | ... | @@ -16,7 +16,6 @@ |
16 | package org.onosproject.yangutils.datamodel; | 16 | package org.onosproject.yangutils.datamodel; |
17 | 17 | ||
18 | import java.io.Serializable; | 18 | import java.io.Serializable; |
19 | -import java.math.BigDecimal; | ||
20 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException; | 19 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException; |
21 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; | 20 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; |
22 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; | 21 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
... | @@ -80,7 +79,7 @@ public final class BuiltInTypeObjectFactory implements Serializable { | ... | @@ -80,7 +79,7 @@ public final class BuiltInTypeObjectFactory implements Serializable { |
80 | return (T) new YangUint64(valueInStr); | 79 | return (T) new YangUint64(valueInStr); |
81 | } | 80 | } |
82 | case DECIMAL64: { | 81 | case DECIMAL64: { |
83 | - return (T) new YangDecimal64(new BigDecimal(valueInStr)); | 82 | + return (T) new YangDecimal64(valueInStr); |
84 | } | 83 | } |
85 | default: { | 84 | default: { |
86 | throw new DataTypeException("YANG file error : Unsupported data type"); | 85 | throw new DataTypeException("YANG file error : Unsupported data type"); | ... | ... |
... | @@ -67,17 +67,6 @@ public class YangBits implements Parsable, Serializable { | ... | @@ -67,17 +67,6 @@ public class YangBits implements Parsable, Serializable { |
67 | } | 67 | } |
68 | 68 | ||
69 | /** | 69 | /** |
70 | - * Creates an instance of YANG bits. | ||
71 | - * | ||
72 | - * @param bits set of bit names | ||
73 | - * @throws DataModelException due to violation in data model rules | ||
74 | - */ | ||
75 | - public YangBits(String bits) throws DataModelException { | ||
76 | - String[] bitNames = bits.trim().split(Pattern.quote(SPACE)); | ||
77 | - setBitDataSet(bitNames); | ||
78 | - } | ||
79 | - | ||
80 | - /** | ||
81 | * Returns the bits name. | 70 | * Returns the bits name. |
82 | * | 71 | * |
83 | * @return the bits name | 72 | * @return the bits name |
... | @@ -229,9 +218,11 @@ public class YangBits implements Parsable, Serializable { | ... | @@ -229,9 +218,11 @@ public class YangBits implements Parsable, Serializable { |
229 | * @param bits set of bit names | 218 | * @param bits set of bit names |
230 | * @return Object of YANG bits | 219 | * @return Object of YANG bits |
231 | */ | 220 | */ |
232 | - public static YangBits fromString(String bits) { | 221 | + public YangBits fromString(String bits) { |
233 | try { | 222 | try { |
234 | - return new YangBits(bits); | 223 | + String[] bitNames = bits.trim().split(Pattern.quote(SPACE)); |
224 | + setBitDataSet(bitNames); | ||
225 | + return this; | ||
235 | } catch (Exception e) { | 226 | } catch (Exception e) { |
236 | } | 227 | } |
237 | return null; | 228 | return null; | ... | ... |
... | @@ -84,32 +84,6 @@ public class YangChoice extends YangNode | ... | @@ -84,32 +84,6 @@ public class YangChoice extends YangNode |
84 | private boolean isConfig; | 84 | private boolean isConfig; |
85 | 85 | ||
86 | /** | 86 | /** |
87 | - * Reference RFC 6020. | ||
88 | - * | ||
89 | - * The "default" statement indicates if a case should be considered as the | ||
90 | - * default if no child nodes from any of the choice's cases exist. The | ||
91 | - * argument is the identifier of the "case" statement. If the "default" | ||
92 | - * statement is missing, there is no default case. | ||
93 | - * | ||
94 | - * The "default" statement MUST NOT be present on choices where "mandatory" | ||
95 | - * is true. | ||
96 | - * | ||
97 | - * The default case is only important when considering the default values of | ||
98 | - * nodes under the cases. The default values for nodes under the default | ||
99 | - * case are used if none of the nodes under any of the cases are present. | ||
100 | - * | ||
101 | - * There MUST NOT be any mandatory nodes directly under the default case. | ||
102 | - * | ||
103 | - * Default values for child nodes under a case are only used if one of the | ||
104 | - * nodes under that case is present, or if that case is the default case. If | ||
105 | - * none of the nodes under a case are present and the case is not the | ||
106 | - * default case, the default values of the cases' child nodes are ignored. | ||
107 | - * | ||
108 | - * the default case to be used if no case members is present. | ||
109 | - */ | ||
110 | - private String defaultCase; | ||
111 | - | ||
112 | - /** | ||
113 | * Description of choice. | 87 | * Description of choice. |
114 | */ | 88 | */ |
115 | private String description; | 89 | private String description; |
... | @@ -146,8 +120,28 @@ public class YangChoice extends YangNode | ... | @@ -146,8 +120,28 @@ public class YangChoice extends YangNode |
146 | private YangStatusType status; | 120 | private YangStatusType status; |
147 | 121 | ||
148 | /** | 122 | /** |
149 | - * Default value in string, needs to be converted to the target object, | 123 | + * Reference RFC 6020. |
150 | - * based on the type. | 124 | + * <p> |
125 | + * The "default" statement indicates if a case should be considered as the | ||
126 | + * default if no child nodes from any of the choice's cases exist. The | ||
127 | + * argument is the identifier of the "case" statement. If the "default" | ||
128 | + * statement is missing, there is no default case. | ||
129 | + * <p> | ||
130 | + * The "default" statement MUST NOT be present on choices where "mandatory" | ||
131 | + * is true. | ||
132 | + * <p> | ||
133 | + * The default case is only important when considering the default values of | ||
134 | + * nodes under the cases. The default values for nodes under the default | ||
135 | + * case are used if none of the nodes under any of the cases are present. | ||
136 | + * <p> | ||
137 | + * There MUST NOT be any mandatory nodes directly under the default case. | ||
138 | + * <p> | ||
139 | + * Default values for child nodes under a case are only used if one of the | ||
140 | + * nodes under that case is present, or if that case is the default case. If | ||
141 | + * none of the nodes under a case are present and the case is not the | ||
142 | + * default case, the default values of the cases' child nodes are ignored. | ||
143 | + * <p> | ||
144 | + * the default case to be used if no case members is present. | ||
151 | */ | 145 | */ |
152 | private String defaultValueInString; | 146 | private String defaultValueInString; |
153 | 147 | ||
... | @@ -238,24 +232,6 @@ public class YangChoice extends YangNode | ... | @@ -238,24 +232,6 @@ public class YangChoice extends YangNode |
238 | } | 232 | } |
239 | 233 | ||
240 | /** | 234 | /** |
241 | - * Returns the default case. | ||
242 | - * | ||
243 | - * @return the default case | ||
244 | - */ | ||
245 | - public String getDefaultCase() { | ||
246 | - return defaultCase; | ||
247 | - } | ||
248 | - | ||
249 | - /** | ||
250 | - * Sets the default case. | ||
251 | - * | ||
252 | - * @param defaultCase the default case to set | ||
253 | - */ | ||
254 | - public void setDefaultCase(String defaultCase) { | ||
255 | - this.defaultCase = defaultCase; | ||
256 | - } | ||
257 | - | ||
258 | - /** | ||
259 | * Returns the mandatory status. | 235 | * Returns the mandatory status. |
260 | * | 236 | * |
261 | * @return the mandatory status | 237 | * @return the mandatory status |
... | @@ -378,7 +354,25 @@ public class YangChoice extends YangNode | ... | @@ -378,7 +354,25 @@ public class YangChoice extends YangNode |
378 | */ | 354 | */ |
379 | @Override | 355 | @Override |
380 | public void validateDataOnExit() throws DataModelException { | 356 | public void validateDataOnExit() throws DataModelException { |
381 | - // TODO auto-generated method stub, to be implemented by parser | 357 | + if (defaultValueInString != null && !defaultValueInString.isEmpty()) { |
358 | + YangNode node = getChild(); | ||
359 | + boolean matched = false; | ||
360 | + // Check whether default string matches the case | ||
361 | + while (node != null) { | ||
362 | + if (node instanceof YangCase) { | ||
363 | + if (defaultValueInString.equals(((YangCase) node).getName())) { | ||
364 | + matched = true; | ||
365 | + break; | ||
366 | + } | ||
367 | + } | ||
368 | + node = node.getNextSibling(); | ||
369 | + } | ||
370 | + | ||
371 | + if (!matched) { | ||
372 | + throw new DataModelException("YANG file error: default string \"" + defaultValueInString | ||
373 | + + "\" not matching choice \"" + getName() + "\" case."); | ||
374 | + } | ||
375 | + } | ||
382 | } | 376 | } |
383 | 377 | ||
384 | @Override | 378 | @Override | ... | ... |
... | @@ -20,6 +20,7 @@ import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ... | @@ -20,6 +20,7 @@ import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
20 | import org.onosproject.yangutils.datamodel.utils.FractionDigits; | 20 | import org.onosproject.yangutils.datamodel.utils.FractionDigits; |
21 | import org.onosproject.yangutils.datamodel.utils.Parsable; | 21 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
22 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; | 22 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
23 | +import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException; | ||
23 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; | 24 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; |
24 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; | 25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
25 | 26 | ||
... | @@ -55,6 +56,16 @@ public class YangDecimal64<T> | ... | @@ -55,6 +56,16 @@ public class YangDecimal64<T> |
55 | */ | 56 | */ |
56 | public static final int MAX_FRACTION_DIGITS_VALUE = 18; | 57 | public static final int MAX_FRACTION_DIGITS_VALUE = 18; |
57 | 58 | ||
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 | + | ||
58 | // Decimal64 value | 69 | // Decimal64 value |
59 | private BigDecimal value; | 70 | private BigDecimal value; |
60 | 71 | ||
... | @@ -84,11 +95,29 @@ public class YangDecimal64<T> | ... | @@ -84,11 +95,29 @@ public class YangDecimal64<T> |
84 | /** | 95 | /** |
85 | * Creates an instance of YANG decimal64. | 96 | * Creates an instance of YANG decimal64. |
86 | * | 97 | * |
87 | - * @param value of decimal64 in string | 98 | + * @param valueInString of decimal64 in string |
88 | - * @throws DataModelException a violation of data model rules | ||
89 | */ | 99 | */ |
90 | - public YangDecimal64(String value) throws DataModelException { | 100 | + public YangDecimal64(String valueInString) { |
91 | - fromString(value); | 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 | + } | ||
92 | } | 121 | } |
93 | 122 | ||
94 | /** | 123 | /** |
... | @@ -178,22 +207,55 @@ public class YangDecimal64<T> | ... | @@ -178,22 +207,55 @@ public class YangDecimal64<T> |
178 | * @throws DataModelException a violation of data model rules | 207 | * @throws DataModelException a violation of data model rules |
179 | */ | 208 | */ |
180 | public static YangDecimal64 fromString(String valInString) throws DataModelException { | 209 | public static YangDecimal64 fromString(String valInString) throws DataModelException { |
181 | - YangDecimal64 decimal64; | 210 | + return new YangDecimal64(valInString); |
182 | - decimal64 = of(new BigDecimal(valInString)); | 211 | + } |
183 | - decimal64.validateValue(); | 212 | + |
184 | - return decimal64; | 213 | + /** |
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; | ||
185 | } | 223 | } |
186 | 224 | ||
225 | + | ||
187 | /** | 226 | /** |
188 | - * Validate decimal64 value. | 227 | + * Checks whether value is in correct decimal64 value range. |
189 | * | 228 | * |
190 | * @throws DataModelException a violation of data model rules | 229 | * @throws DataModelException a violation of data model rules |
191 | */ | 230 | */ |
192 | - public void validateValue() throws DataModelException { | 231 | + public void validateDecimal64() throws DataModelException { |
193 | - if (!(FractionDigits.isValidDecimal64(this.value, this.fractionDigit))) { | 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())) { | ||
194 | throw new DataModelException("YANG file error : decimal64 validation failed."); | 255 | throw new DataModelException("YANG file error : decimal64 validation failed."); |
195 | } | 256 | } |
196 | } | 257 | } |
258 | + } | ||
197 | 259 | ||
198 | /** | 260 | /** |
199 | * Validate range restriction values based on fraction-digits decimal64 range value. | 261 | * Validate range restriction values based on fraction-digits decimal64 range value. |
... | @@ -203,6 +265,7 @@ public class YangDecimal64<T> | ... | @@ -203,6 +265,7 @@ public class YangDecimal64<T> |
203 | public void validateRange() throws DataModelException { | 265 | public void validateRange() throws DataModelException { |
204 | YangRangeRestriction rangeRestriction = (YangRangeRestriction) getRangeRestrictedExtendedInfo(); | 266 | YangRangeRestriction rangeRestriction = (YangRangeRestriction) getRangeRestrictedExtendedInfo(); |
205 | if (rangeRestriction == null) { | 267 | if (rangeRestriction == null) { |
268 | + // No need to validate. Range is optional. | ||
206 | return; | 269 | return; |
207 | } | 270 | } |
208 | 271 | ||
... | @@ -210,14 +273,14 @@ public class YangDecimal64<T> | ... | @@ -210,14 +273,14 @@ public class YangDecimal64<T> |
210 | .listIterator(); | 273 | .listIterator(); |
211 | while (rangeListIterator.hasNext()) { | 274 | while (rangeListIterator.hasNext()) { |
212 | YangRangeInterval rangeInterval = rangeListIterator.next(); | 275 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
213 | - if (!(FractionDigits.isValidDecimal64(((YangDecimal64) rangeInterval.getStartValue()).getValue(), | 276 | + if (!(FractionDigits.isValueInDecimal64Range(((YangDecimal64) rangeInterval.getStartValue()).getValue(), |
214 | getFractionDigit()))) { | 277 | getFractionDigit()))) { |
215 | - throw new DataModelException("YANG file error : decimal64 validation failed."); | 278 | + throw new DataModelException("YANG file error : range validation failed."); |
216 | } | 279 | } |
217 | 280 | ||
218 | - if (!(FractionDigits.isValidDecimal64(((YangDecimal64) rangeInterval.getEndValue()).getValue(), | 281 | + if (!(FractionDigits.isValueInDecimal64Range(((YangDecimal64) rangeInterval.getEndValue()).getValue(), |
219 | getFractionDigit()))) { | 282 | getFractionDigit()))) { |
220 | - throw new DataModelException("YANG file error : decimal64 validation failed."); | 283 | + throw new DataModelException("YANG file error : range validation failed."); |
221 | } | 284 | } |
222 | } | 285 | } |
223 | } | 286 | } | ... | ... |
... | @@ -380,8 +380,9 @@ public class YangLeaf | ... | @@ -380,8 +380,9 @@ public class YangLeaf |
380 | @Override | 380 | @Override |
381 | public void validateDataOnExit() | 381 | public void validateDataOnExit() |
382 | throws DataModelException { | 382 | throws DataModelException { |
383 | - // TODO auto-generated method stub, to be implemented by parser | 383 | + if (defaultValueInString != null && !defaultValueInString.isEmpty() && dataType != null) { |
384 | - | 384 | + dataType.isValidValue(defaultValueInString); |
385 | + } | ||
385 | } | 386 | } |
386 | 387 | ||
387 | @Override | 388 | @Override | ... | ... |
... | @@ -17,6 +17,8 @@ | ... | @@ -17,6 +17,8 @@ |
17 | package org.onosproject.yangutils.datamodel; | 17 | package org.onosproject.yangutils.datamodel; |
18 | 18 | ||
19 | import java.io.Serializable; | 19 | import java.io.Serializable; |
20 | +import java.math.BigInteger; | ||
21 | +import java.util.ListIterator; | ||
20 | 22 | ||
21 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | 23 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
22 | import org.onosproject.yangutils.datamodel.utils.Parsable; | 24 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
... | @@ -193,6 +195,61 @@ public class YangStringRestriction implements YangDesc, YangReference, Parsable, | ... | @@ -193,6 +195,61 @@ public class YangStringRestriction implements YangDesc, YangReference, Parsable, |
193 | return YangConstructType.PATTERN_DATA; | 195 | return YangConstructType.PATTERN_DATA; |
194 | } | 196 | } |
195 | 197 | ||
198 | + /** | ||
199 | + * Validates if the given value is correct as per the length restriction. | ||
200 | + * | ||
201 | + * @param valueInString value | ||
202 | + * @return true, if the value is confirming to length restriction, false otherwise | ||
203 | + */ | ||
204 | + public boolean isValidStringOnLengthRestriction(String valueInString) { | ||
205 | + if (lengthRestriction == null || lengthRestriction.getAscendingRangeIntervals() == null | ||
206 | + || lengthRestriction.getAscendingRangeIntervals().isEmpty()) { | ||
207 | + // Length restriction is optional | ||
208 | + return true; | ||
209 | + } | ||
210 | + | ||
211 | + ListIterator<YangRangeInterval<YangUint64>> rangeListIterator = lengthRestriction.getAscendingRangeIntervals() | ||
212 | + .listIterator(); | ||
213 | + boolean isMatched = false; | ||
214 | + while (rangeListIterator.hasNext()) { | ||
215 | + YangRangeInterval rangeInterval = rangeListIterator.next(); | ||
216 | + BigInteger startValue = ((YangUint64) rangeInterval.getStartValue()).getValue(); | ||
217 | + BigInteger endValue = ((YangUint64) rangeInterval.getEndValue()).getValue(); | ||
218 | + if ((valueInString.length() >= startValue.intValue()) && | ||
219 | + (valueInString.length() <= endValue.intValue())) { | ||
220 | + isMatched = true; | ||
221 | + break; | ||
222 | + } | ||
223 | + } | ||
224 | + | ||
225 | + return isMatched; | ||
226 | + } | ||
227 | + | ||
228 | + /** | ||
229 | + * Validates if the given value is correct as per the pattern restriction. | ||
230 | + * | ||
231 | + * @param valueInString value | ||
232 | + * @return true, if the value is confirming to pattern restriction, false otherwise | ||
233 | + */ | ||
234 | + public boolean isValidStringOnPatternRestriction(String valueInString) { | ||
235 | + if (patternRestriction == null | ||
236 | + || patternRestriction.getPatternList().isEmpty()) { | ||
237 | + // Pattern restriction is optional | ||
238 | + return true; | ||
239 | + } | ||
240 | + | ||
241 | + ListIterator<String> patternListIterator = patternRestriction.getPatternList().listIterator(); | ||
242 | + boolean isMatched = false; | ||
243 | + while (patternListIterator.hasNext()) { | ||
244 | + if (valueInString.matches(patternListIterator.next())) { | ||
245 | + isMatched = true; | ||
246 | + break; | ||
247 | + } | ||
248 | + } | ||
249 | + | ||
250 | + return isMatched; | ||
251 | + } | ||
252 | + | ||
196 | @Override | 253 | @Override |
197 | public void validateDataOnEntry() throws DataModelException { | 254 | public void validateDataOnEntry() throws DataModelException { |
198 | // TODO: implement the method. | 255 | // TODO: implement the method. | ... | ... |
This diff is collapsed. Click to expand it.
... | @@ -251,7 +251,9 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, Y | ... | @@ -251,7 +251,9 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, Y |
251 | */ | 251 | */ |
252 | @Override | 252 | @Override |
253 | public void validateDataOnExit() throws DataModelException { | 253 | public void validateDataOnExit() throws DataModelException { |
254 | - // TODO auto-generated method stub, to be implemented by parser | 254 | + if (defaultValueInString != null && !defaultValueInString.isEmpty() && getTypeDefBaseType() != null) { |
255 | + getTypeDefBaseType().isValidValue(defaultValueInString); | ||
256 | + } | ||
255 | } | 257 | } |
256 | 258 | ||
257 | /** | 259 | /** | ... | ... |
... | @@ -55,6 +55,8 @@ import java.util.Set; | ... | @@ -55,6 +55,8 @@ import java.util.Set; |
55 | * Represents utilities for data model tree. | 55 | * Represents utilities for data model tree. |
56 | */ | 56 | */ |
57 | public final class DataModelUtils { | 57 | public final class DataModelUtils { |
58 | + public static final String TRUE = "true"; | ||
59 | + public static final String FALSE = "false"; | ||
58 | 60 | ||
59 | /** | 61 | /** |
60 | * Creates a new data model tree utility. | 62 | * Creates a new data model tree utility. | ... | ... |
... | @@ -105,7 +105,7 @@ public final class FractionDigits { | ... | @@ -105,7 +105,7 @@ public final class FractionDigits { |
105 | * | 105 | * |
106 | * @return decimal64 value range by fraction-digits as index | 106 | * @return decimal64 value range by fraction-digits as index |
107 | */ | 107 | */ |
108 | - private static ArrayList<Range> getDecimal64ValueRange() { | 108 | + public static ArrayList<Range> getDecimal64ValueRange() { |
109 | if (decimal64ValueRange == null) { | 109 | if (decimal64ValueRange == null) { |
110 | decimal64ValueRange = new ArrayList<>(); | 110 | decimal64ValueRange = new ArrayList<>(); |
111 | decimal64ValueRange.add(new Range(-922337203685477580.8, 922337203685477580.7)); // fraction-digit: 1 | 111 | decimal64ValueRange.add(new Range(-922337203685477580.8, 922337203685477580.7)); // fraction-digit: 1 |
... | @@ -131,37 +131,38 @@ public final class FractionDigits { | ... | @@ -131,37 +131,38 @@ public final class FractionDigits { |
131 | } | 131 | } |
132 | 132 | ||
133 | /** | 133 | /** |
134 | - * Checks given decimal64 value is in the specific range based on given fraction-digit. | 134 | + * Retrieve range based on fraction-digits. |
135 | * | 135 | * |
136 | - * @param value decimal64 value | ||
137 | * @param fractionDigit fraction-digits | 136 | * @param fractionDigit fraction-digits |
138 | - * @return success when it is in specific range otherwise false | 137 | + * @return range |
138 | + * @throws DataModelException a violation of data model rules | ||
139 | */ | 139 | */ |
140 | - public static boolean isValidDecimal64(BigDecimal value, int fractionDigit) { | 140 | + public static Range getRange(int fractionDigit) throws DataModelException { |
141 | if (!((fractionDigit >= 1) && (fractionDigit <= 18))) { | 141 | if (!((fractionDigit >= 1) && (fractionDigit <= 18))) { |
142 | - return false; | 142 | + throw new DataModelException("YANG file error : given fraction-digit is not in its range (1..18)."); |
143 | } | 143 | } |
144 | 144 | ||
145 | - // ArrayList index starts from 0. | 145 | + return getDecimal64ValueRange().get(fractionDigit - 1); |
146 | - Range range = getDecimal64ValueRange().get(fractionDigit - 1); | ||
147 | - if ((value.doubleValue() >= range.min) && (value.doubleValue() <= range.max)) { | ||
148 | - return true; | ||
149 | - } | ||
150 | - return false; | ||
151 | } | 146 | } |
152 | 147 | ||
153 | /** | 148 | /** |
154 | - * Retrieve range based on fraction-digits. | 149 | + * Checks whether specific decimal64 value is in correct range based fraction-digit. |
155 | * | 150 | * |
151 | + * @param value decimal64 value | ||
156 | * @param fractionDigit fraction-digits | 152 | * @param fractionDigit fraction-digits |
157 | - * @return range | 153 | + * @return true when it is in correct range otherwise false |
158 | - * @throws DataModelException a violation of data model rules | ||
159 | */ | 154 | */ |
160 | - public static Range getRange(int fractionDigit) throws DataModelException { | 155 | + public static boolean isValueInDecimal64Range(BigDecimal value, int fractionDigit) { |
156 | + // Fraction-digits should be in correct its own range. | ||
161 | if (!((fractionDigit >= 1) && (fractionDigit <= 18))) { | 157 | if (!((fractionDigit >= 1) && (fractionDigit <= 18))) { |
162 | - throw new DataModelException("YANG file error : given fraction-digit is not in its range (1..18)."); | 158 | + return false; |
163 | } | 159 | } |
164 | 160 | ||
165 | - return getDecimal64ValueRange().get(fractionDigit - 1); | 161 | + // ArrayList index starts from 0. |
162 | + FractionDigits.Range range = FractionDigits.getDecimal64ValueRange().get(fractionDigit - 1); | ||
163 | + if ((value.doubleValue() >= range.getMin()) && (value.doubleValue() <= range.getMax())) { | ||
164 | + return true; | ||
165 | + } | ||
166 | + return false; | ||
166 | } | 167 | } |
167 | } | 168 | } | ... | ... |
... | @@ -49,6 +49,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLoc | ... | @@ -49,6 +49,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLoc |
49 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; | 49 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
50 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | 50 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
51 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | 51 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
52 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT; | ||
52 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | 53 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
53 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | 54 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
54 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; | 55 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
... | @@ -156,6 +157,13 @@ public final class ChoiceListener { | ... | @@ -156,6 +157,13 @@ public final class ChoiceListener { |
156 | checkStackIsNotEmpty(listener, MISSING_HOLDER, CHOICE_DATA, ctx.identifier().getText(), EXIT); | 157 | checkStackIsNotEmpty(listener, MISSING_HOLDER, CHOICE_DATA, ctx.identifier().getText(), EXIT); |
157 | 158 | ||
158 | if (listener.getParsedDataStack().peek() instanceof YangChoice) { | 159 | if (listener.getParsedDataStack().peek() instanceof YangChoice) { |
160 | + YangChoice choiceNode = (YangChoice) listener.getParsedDataStack().peek(); | ||
161 | + try { | ||
162 | + choiceNode.validateDataOnExit(); | ||
163 | + } catch (DataModelException e) { | ||
164 | + throw new ParserException(constructListenerErrorMessage(INVALID_CONTENT, CHOICE_DATA, | ||
165 | + ctx.identifier().getText(), EXIT)); | ||
166 | + } | ||
159 | listener.getParsedDataStack().pop(); | 167 | listener.getParsedDataStack().pop(); |
160 | } else { | 168 | } else { |
161 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CHOICE_DATA, | 169 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CHOICE_DATA, | ... | ... |
... | @@ -46,6 +46,7 @@ import org.onosproject.yangutils.datamodel.utils.Parsable; | ... | @@ -46,6 +46,7 @@ import org.onosproject.yangutils.datamodel.utils.Parsable; |
46 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | 46 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
47 | import org.onosproject.yangutils.parser.exceptions.ParserException; | 47 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
48 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; | 48 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
49 | +import org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil; | ||
49 | 50 | ||
50 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DEFAULT_DATA; | 51 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DEFAULT_DATA; |
51 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | 52 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
... | @@ -82,17 +83,17 @@ public final class DefaultListener { | ... | @@ -82,17 +83,17 @@ public final class DefaultListener { |
82 | switch (tmpNode.getYangConstructType()) { | 83 | switch (tmpNode.getYangConstructType()) { |
83 | case TYPEDEF_DATA: { | 84 | case TYPEDEF_DATA: { |
84 | YangTypeDef typeDef = (YangTypeDef) tmpNode; | 85 | YangTypeDef typeDef = (YangTypeDef) tmpNode; |
85 | - typeDef.setDefaultValueInString(ctx.string().getText()); | 86 | + typeDef.setDefaultValueInString(ListenerUtil.removeQuotesAndHandleConcat(ctx.string().getText())); |
86 | break; | 87 | break; |
87 | } | 88 | } |
88 | case LEAF_DATA: { | 89 | case LEAF_DATA: { |
89 | YangLeaf leaf = (YangLeaf) tmpNode; | 90 | YangLeaf leaf = (YangLeaf) tmpNode; |
90 | - leaf.setDefaultValueInString(ctx.string().getText()); | 91 | + leaf.setDefaultValueInString(ListenerUtil.removeQuotesAndHandleConcat(ctx.string().getText())); |
91 | break; | 92 | break; |
92 | } | 93 | } |
93 | case CHOICE_DATA: { | 94 | case CHOICE_DATA: { |
94 | YangChoice choice = (YangChoice) tmpNode; | 95 | YangChoice choice = (YangChoice) tmpNode; |
95 | - choice.setDefaultValueInString(ctx.string().getText()); | 96 | + choice.setDefaultValueInString(ListenerUtil.removeQuotesAndHandleConcat(ctx.string().getText())); |
96 | break; | 97 | break; |
97 | } | 98 | } |
98 | default: | 99 | default: | ... | ... |
... | @@ -22,6 +22,7 @@ package org.onosproject.yangutils.parser.impl.listeners; | ... | @@ -22,6 +22,7 @@ package org.onosproject.yangutils.parser.impl.listeners; |
22 | 22 | ||
23 | import org.onosproject.yangutils.datamodel.YangLeaf; | 23 | import org.onosproject.yangutils.datamodel.YangLeaf; |
24 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; | 24 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
25 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
25 | import org.onosproject.yangutils.datamodel.utils.Parsable; | 26 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
26 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | 27 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
27 | import org.onosproject.yangutils.parser.exceptions.ParserException; | 28 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
... | @@ -42,6 +43,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLoc | ... | @@ -42,6 +43,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLoc |
42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction | 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction |
43 | .constructListenerErrorMessage; | 44 | .constructListenerErrorMessage; |
44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
46 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT; | ||
45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; | 49 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
... | @@ -143,6 +145,13 @@ public final class LeafListener { | ... | @@ -143,6 +145,13 @@ public final class LeafListener { |
143 | checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), EXIT); | 145 | checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), EXIT); |
144 | 146 | ||
145 | if (listener.getParsedDataStack().peek() instanceof YangLeaf) { | 147 | if (listener.getParsedDataStack().peek() instanceof YangLeaf) { |
148 | + YangLeaf leafNode = (YangLeaf) listener.getParsedDataStack().peek(); | ||
149 | + try { | ||
150 | + leafNode.validateDataOnExit(); | ||
151 | + } catch (DataModelException e) { | ||
152 | + throw new ParserException(constructListenerErrorMessage(INVALID_CONTENT, LEAF_DATA, | ||
153 | + ctx.identifier().getText(), EXIT)); | ||
154 | + } | ||
146 | listener.getParsedDataStack().pop(); | 155 | listener.getParsedDataStack().pop(); |
147 | } else { | 156 | } else { |
148 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA, | 157 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA, | ... | ... |
... | @@ -1143,7 +1143,7 @@ public final class MethodsGenerator { | ... | @@ -1143,7 +1143,7 @@ public final class MethodsGenerator { |
1143 | JavaAttributeInfo fromStringAttributeInfo) { | 1143 | JavaAttributeInfo fromStringAttributeInfo) { |
1144 | 1144 | ||
1145 | return EIGHT_SPACE_INDENTATION + getTrySubString() + NEW_LINE + TWELVE_SPACE_INDENTATION | 1145 | return EIGHT_SPACE_INDENTATION + getTrySubString() + NEW_LINE + TWELVE_SPACE_INDENTATION |
1146 | - + getParsedSubString(attr, fromStringAttributeInfo) + SEMI_COLAN + NEW_LINE + TWELVE_SPACE_INDENTATION | 1146 | + + getParsedSubString(attr, fromStringAttributeInfo) + NEW_LINE + TWELVE_SPACE_INDENTATION |
1147 | + getReturnOfSubString() + NEW_LINE + EIGHT_SPACE_INDENTATION + getCatchSubString() | 1147 | + getReturnOfSubString() + NEW_LINE + EIGHT_SPACE_INDENTATION + getCatchSubString() |
1148 | + NEW_LINE + EIGHT_SPACE_INDENTATION + CLOSE_CURLY_BRACKET; | 1148 | + NEW_LINE + EIGHT_SPACE_INDENTATION + CLOSE_CURLY_BRACKET; |
1149 | } | 1149 | } | ... | ... |
... | @@ -145,9 +145,9 @@ public class Decimal64ListenerTest { | ... | @@ -145,9 +145,9 @@ public class Decimal64ListenerTest { |
145 | assertThat(decimal64.getFractionDigit(), is(18)); | 145 | assertThat(decimal64.getFractionDigit(), is(18)); |
146 | 146 | ||
147 | decimal64.setValue(new BigDecimal(-9.223372036854775808)); | 147 | decimal64.setValue(new BigDecimal(-9.223372036854775808)); |
148 | - decimal64.validateValue(); | 148 | + decimal64.validateDecimal64(); |
149 | decimal64.setValue(new BigDecimal(9.223372036854775807)); | 149 | decimal64.setValue(new BigDecimal(9.223372036854775807)); |
150 | - decimal64.validateValue(); | 150 | + decimal64.validateDecimal64(); |
151 | } | 151 | } |
152 | 152 | ||
153 | /** | 153 | /** |
... | @@ -181,7 +181,7 @@ public class Decimal64ListenerTest { | ... | @@ -181,7 +181,7 @@ public class Decimal64ListenerTest { |
181 | 181 | ||
182 | decimal64.setValue(new BigDecimal(-92233720368547758.08)); | 182 | decimal64.setValue(new BigDecimal(-92233720368547758.08)); |
183 | // validation should fail | 183 | // validation should fail |
184 | - decimal64.validateValue(); | 184 | + decimal64.validateDecimal64(); |
185 | } | 185 | } |
186 | 186 | ||
187 | /** | 187 | /** |
... | @@ -271,7 +271,7 @@ public class Decimal64ListenerTest { | ... | @@ -271,7 +271,7 @@ public class Decimal64ListenerTest { |
271 | @Test | 271 | @Test |
272 | public void processDecimal64InvalidRange() throws IOException, ParserException, DataModelException { | 272 | public void processDecimal64InvalidRange() throws IOException, ParserException, DataModelException { |
273 | thrown.expect(ParserException.class); | 273 | thrown.expect(ParserException.class); |
274 | - thrown.expectMessage("YANG file error : decimal64 validation failed."); | 274 | + thrown.expectMessage("YANG file error : range validation failed."); |
275 | 275 | ||
276 | manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidRangeStmnt.yang"); | 276 | manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidRangeStmnt.yang"); |
277 | } | 277 | } | ... | ... |
This diff is collapsed. Click to expand it.
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + container food { | ||
6 | + choice snack { | ||
7 | + case sports-arena { | ||
8 | + leaf pretzel { | ||
9 | + type string; | ||
10 | + } | ||
11 | + leaf beer { | ||
12 | + type string; | ||
13 | + } | ||
14 | + } | ||
15 | + case late-night { | ||
16 | + leaf chocolate { | ||
17 | + type string; | ||
18 | + } | ||
19 | + } | ||
20 | + default "sports-arena"; | ||
21 | + } | ||
22 | + } | ||
23 | +} |
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + | ||
6 | + leaf mybits { | ||
7 | + type bits { | ||
8 | + bit disable-nagle { | ||
9 | + position 0; | ||
10 | + } | ||
11 | + bit auto-sense-speed { | ||
12 | + position 1; | ||
13 | + } | ||
14 | + bit Mb-only { | ||
15 | + position 2; | ||
16 | + } | ||
17 | + } | ||
18 | + default "xyz"; | ||
19 | + } | ||
20 | +} |
utils/yangutils/plugin/src/test/resources/default/DefaultInvalidValueInChoiceSubStmt.yang
0 → 100644
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + container food { | ||
6 | + choice snack { | ||
7 | + case sports-arena { | ||
8 | + leaf pretzel { | ||
9 | + type string; | ||
10 | + } | ||
11 | + leaf beer { | ||
12 | + type string; | ||
13 | + } | ||
14 | + } | ||
15 | + case late-night { | ||
16 | + leaf chocolate { | ||
17 | + type string; | ||
18 | + } | ||
19 | + } | ||
20 | + default "hello"; | ||
21 | + } | ||
22 | + } | ||
23 | +} |
utils/yangutils/plugin/src/test/resources/default/DefaultInvalidValueInLeafSubStatement.yang
0 → 100644
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + leaf invalid-interval { | ||
6 | + type "uint16"; | ||
7 | + units "seconds"; | ||
8 | + default "x"; | ||
9 | + description "Interval before a route is declared invalid"; | ||
10 | + config true; | ||
11 | + mandatory true; | ||
12 | + status current; | ||
13 | + reference "RFC 6020"; | ||
14 | + } | ||
15 | +} |
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + | ||
6 | + leaf mybits { | ||
7 | + type bits { | ||
8 | + bit disable-nagle { | ||
9 | + position 0; | ||
10 | + } | ||
11 | + bit auto-sense-speed { | ||
12 | + position 1; | ||
13 | + } | ||
14 | + bit Mb-only { | ||
15 | + position 2; | ||
16 | + } | ||
17 | + } | ||
18 | + default "auto-sense-speed"; | ||
19 | + } | ||
20 | +} |
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + leaf invalid-interval { | ||
6 | + type "uint16"; | ||
7 | + units "seconds"; | ||
8 | + default "1"; | ||
9 | + description "Interval before a route is declared invalid"; | ||
10 | + config true; | ||
11 | + mandatory true; | ||
12 | + status current; | ||
13 | + reference "RFC 6020"; | ||
14 | + } | ||
15 | +} |
-
Please register or login to post a comment