Committed by
Patrick Liu
[ONOS-4910, ONOS-4921] Framework: utils validation and defect fix
Change-Id: I821920fa8c88e64406b702c2b8736bdeaf231474
Showing
39 changed files
with
1183 additions
and
154 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. | ... | ... |
... | @@ -17,12 +17,18 @@ | ... | @@ -17,12 +17,18 @@ |
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.Iterator; | ||
22 | +import java.util.ListIterator; | ||
20 | 23 | ||
21 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | 24 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
25 | +import org.onosproject.yangutils.datamodel.utils.DataModelUtils; | ||
22 | import org.onosproject.yangutils.datamodel.utils.Parsable; | 26 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
23 | import org.onosproject.yangutils.datamodel.utils.ResolvableStatus; | 27 | import org.onosproject.yangutils.datamodel.utils.ResolvableStatus; |
24 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; | 28 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
29 | +import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException; | ||
25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; | 30 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
31 | +import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64; | ||
26 | 32 | ||
27 | import static org.onosproject.yangutils.datamodel.BuiltInTypeObjectFactory.getDataObjectFromString; | 33 | import static org.onosproject.yangutils.datamodel.BuiltInTypeObjectFactory.getDataObjectFromString; |
28 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType; | 34 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType; |
... | @@ -268,9 +274,9 @@ public class YangType<T> | ... | @@ -268,9 +274,9 @@ public class YangType<T> |
268 | * type as per the YANG file. | 274 | * type as per the YANG file. |
269 | * | 275 | * |
270 | * @param value input data value | 276 | * @param value input data value |
271 | - * @return status of validation | 277 | + * @throws DataModelException a violation of data model rules |
272 | */ | 278 | */ |
273 | - public boolean isValidValue(String value) { | 279 | + public void isValidValue(String value) throws DataModelException { |
274 | switch (getDataType()) { | 280 | switch (getDataType()) { |
275 | case INT8: | 281 | case INT8: |
276 | case INT16: | 282 | case INT16: |
... | @@ -280,83 +286,210 @@ public class YangType<T> | ... | @@ -280,83 +286,210 @@ public class YangType<T> |
280 | case UINT16: | 286 | case UINT16: |
281 | case UINT32: | 287 | case UINT32: |
282 | case UINT64: { | 288 | case UINT64: { |
283 | - isValidValueForRangeRestrictedType(value); | 289 | + if (getDataTypeExtendedInfo() == null) { |
290 | + getDataObjectFromString(value, getDataType()); | ||
291 | + } else { | ||
292 | + ((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value); | ||
293 | + } | ||
294 | + break; | ||
284 | } | 295 | } |
285 | case DECIMAL64: { | 296 | case DECIMAL64: { |
286 | - // TODO | 297 | + // 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; | ||
287 | } | 303 | } |
288 | case STRING: { | 304 | case STRING: { |
289 | - // TODO implement in string restriction similar to range restriction | 305 | + 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; | ||
311 | + } | ||
312 | + 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"); | ||
290 | } | 316 | } |
317 | + break; | ||
291 | case ENUMERATION: { | 318 | case ENUMERATION: { |
292 | - // TODO validate using list of YANG enum of enumeration class in extended info. | 319 | + 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; | ||
293 | } | 326 | } |
294 | - case BINARY: { | 327 | + } |
295 | - // TODO validate based on extended info | 328 | + |
329 | + if (!isValidated) { | ||
330 | + throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + | ||
331 | + "union"); | ||
332 | + } | ||
333 | + break; | ||
296 | } | 334 | } |
297 | case BITS: { | 335 | case BITS: { |
298 | - // TODO validate based on extended info | 336 | + 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; | ||
299 | } | 342 | } |
300 | - case BOOLEAN: { | 343 | + case BINARY: { |
301 | - // TODO true or false | 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; | ||
302 | } | 349 | } |
303 | case LEAFREF: { | 350 | case LEAFREF: { |
304 | - // TODO validate based on extended info | 351 | + YangLeafRef<?> leafRef = (YangLeafRef<?>) getDataTypeExtendedInfo(); |
352 | + leafRef.validateDataOnExit(); | ||
353 | + break; | ||
305 | } | 354 | } |
306 | case IDENTITYREF: { | 355 | case IDENTITYREF: { |
307 | // TODO TBD | 356 | // TODO TBD |
357 | + break; | ||
308 | } | 358 | } |
309 | case EMPTY: { | 359 | case EMPTY: { |
310 | - // TODO true or false | 360 | + throw new DataTypeException("YANG file error : Input value \"" + value |
361 | + + "\" is not a allowed for a data type " + "empty"); | ||
311 | } | 362 | } |
312 | case UNION: { | 363 | case UNION: { |
313 | - // TODO validate based on extended info | 364 | + 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; | ||
314 | } | 383 | } |
315 | case INSTANCE_IDENTIFIER: { | 384 | case INSTANCE_IDENTIFIER: { |
316 | // TODO TBD | 385 | // TODO TBD |
386 | + break; | ||
317 | } | 387 | } |
318 | case DERIVED: { | 388 | case DERIVED: { |
319 | - if (isOfRangeRestrictedType(((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType())) { | 389 | + YangDataTypes dataType = ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType(); |
320 | - try { | 390 | + if (isOfRangeRestrictedType(dataType)) { |
321 | if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) { | 391 | if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) { |
322 | getDataObjectFromString(value, | 392 | getDataObjectFromString(value, |
323 | - ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType()); | 393 | + ((YangDerivedInfo) getDataTypeExtendedInfo()) |
324 | - return true; | 394 | + .getEffectiveBuiltInType()); |
325 | - } else { | ||
326 | - return ((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo()) | ||
327 | - .getResolvedExtendedInfo()).isValidValueString(value); | ||
328 | - } | ||
329 | - } catch (Exception e) { | ||
330 | - return false; | ||
331 | - } | ||
332 | } else { | 395 | } else { |
333 | - // TODO | 396 | + if (!((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo()) |
334 | - } | 397 | + .getResolvedExtendedInfo()).isValidValueString(value)) { |
398 | + throw new DataTypeException("YANG file error : Input value \"" + value | ||
399 | + + "\" is not a valid " + dataType.toString()); | ||
400 | + } | ||
401 | + } | ||
402 | + } 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()); | ||
434 | + } | ||
435 | + break; | ||
335 | } | 436 | } |
336 | default: { | 437 | default: { |
337 | - // TODO | 438 | + throw new DataTypeException("YANG file error : Input value \"" + value + "\" is for unsupported " + |
439 | + "data type."); | ||
338 | } | 440 | } |
339 | } | 441 | } |
340 | - return true; | ||
341 | } | 442 | } |
342 | 443 | ||
444 | + | ||
343 | /** | 445 | /** |
344 | - * Validates the input data value for range restricted types against the | 446 | + * Checks whether specific string is valid decimal64 value. |
345 | - * permissible value for the type as per the YANG file. | ||
346 | * | 447 | * |
347 | - * @param value input data value | 448 | + * @param value decimal64 value |
348 | - * @return status of validation | ||
349 | */ | 449 | */ |
350 | - private boolean isValidValueForRangeRestrictedType(String value) { | 450 | + private void validateDecimal64(String value, int fractionDigit, YangRangeRestriction rangeRestriction) |
351 | - try { | 451 | + throws DataModelException { |
352 | - if (getDataTypeExtendedInfo() == null) { | 452 | + YangDecimal64<YangRangeRestriction> decimal64 = YangDecimal64.fromString(value); |
353 | - getDataObjectFromString(value, getDataType()); | 453 | + decimal64.setFractionDigit(fractionDigit); |
354 | - return true; | 454 | + decimal64.setRangeRestrictedExtendedInfo(rangeRestriction); |
355 | - } else { | 455 | + decimal64.validateDecimal64(); |
356 | - return ((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value); | ||
357 | } | 456 | } |
358 | - } catch (Exception e) { | 457 | + |
458 | + /** | ||
459 | + * Checks whether specific string is valid binary. | ||
460 | + * | ||
461 | + * @param value binary value | ||
462 | + * @return true if validation success otherwise false | ||
463 | + */ | ||
464 | + 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) { | ||
359 | return false; | 469 | return false; |
360 | } | 470 | } |
471 | + | ||
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; | ||
361 | } | 494 | } |
362 | } | 495 | } | ... | ... |
... | @@ -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 | } | ... | ... |
... | @@ -19,13 +19,21 @@ package org.onosproject.yangutils.parser.impl.listeners; | ... | @@ -19,13 +19,21 @@ package org.onosproject.yangutils.parser.impl.listeners; |
19 | import java.io.IOException; | 19 | import java.io.IOException; |
20 | import java.util.ListIterator; | 20 | import java.util.ListIterator; |
21 | 21 | ||
22 | +import org.junit.Rule; | ||
22 | import org.junit.Test; | 23 | import org.junit.Test; |
24 | +import org.junit.rules.ExpectedException; | ||
25 | +import org.onosproject.yangutils.datamodel.YangDerivedInfo; | ||
23 | import org.onosproject.yangutils.datamodel.YangNode; | 26 | import org.onosproject.yangutils.datamodel.YangNode; |
24 | import org.onosproject.yangutils.datamodel.YangModule; | 27 | import org.onosproject.yangutils.datamodel.YangModule; |
25 | import org.onosproject.yangutils.datamodel.YangLeaf; | 28 | import org.onosproject.yangutils.datamodel.YangLeaf; |
26 | import org.onosproject.yangutils.datamodel.YangChoice; | 29 | import org.onosproject.yangutils.datamodel.YangChoice; |
27 | import org.onosproject.yangutils.datamodel.YangContainer; | 30 | import org.onosproject.yangutils.datamodel.YangContainer; |
28 | import org.onosproject.yangutils.datamodel.YangNodeType; | 31 | import org.onosproject.yangutils.datamodel.YangNodeType; |
32 | +import org.onosproject.yangutils.datamodel.YangType; | ||
33 | +import org.onosproject.yangutils.datamodel.YangTypeDef; | ||
34 | +import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException; | ||
35 | +import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; | ||
36 | +import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt64; | ||
29 | import org.onosproject.yangutils.parser.exceptions.ParserException; | 37 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
30 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | 38 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
31 | 39 | ||
... | @@ -36,6 +44,8 @@ import static org.junit.Assert.assertThat; | ... | @@ -36,6 +44,8 @@ import static org.junit.Assert.assertThat; |
36 | * Test cases for testing default listener functionality. | 44 | * Test cases for testing default listener functionality. |
37 | */ | 45 | */ |
38 | public class DefaultListenerTest { | 46 | public class DefaultListenerTest { |
47 | + @Rule | ||
48 | + public ExpectedException thrown = ExpectedException.none(); | ||
39 | 49 | ||
40 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); | 50 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
41 | 51 | ||
... | @@ -43,9 +53,9 @@ public class DefaultListenerTest { | ... | @@ -43,9 +53,9 @@ public class DefaultListenerTest { |
43 | * Checks if default value is set correctly. | 53 | * Checks if default value is set correctly. |
44 | */ | 54 | */ |
45 | @Test | 55 | @Test |
46 | - public void processLeafSubStatementDefault() throws IOException, ParserException { | 56 | + public void processDefaultValueInLeafSubStatement() throws IOException, ParserException { |
47 | 57 | ||
48 | - YangNode node = manager.getDataModel("src/test/resources/LeafSubStatementDefault.yang"); | 58 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInLeafSubStatement.yang"); |
49 | // Check whether the data model tree returned is of type module. | 59 | // Check whether the data model tree returned is of type module. |
50 | assertThat((node instanceof YangModule), is(true)); | 60 | assertThat((node instanceof YangModule), is(true)); |
51 | 61 | ||
... | @@ -60,16 +70,28 @@ public class DefaultListenerTest { | ... | @@ -60,16 +70,28 @@ public class DefaultListenerTest { |
60 | YangLeaf leafInfo = leafIterator.next(); | 70 | YangLeaf leafInfo = leafIterator.next(); |
61 | 71 | ||
62 | assertThat(leafInfo.getName(), is("invalid-interval")); | 72 | assertThat(leafInfo.getName(), is("invalid-interval")); |
63 | - assertThat(leafInfo.getDefaultValueInString(), is("\"1\"")); | 73 | + assertThat(leafInfo.getDefaultValueInString(), is("1")); |
64 | } | 74 | } |
65 | 75 | ||
66 | /** | 76 | /** |
67 | - * Checks if default value is set correctly. | 77 | + * Validates default invalid value in leaf. |
78 | + */ | ||
79 | + @Test | ||
80 | + public void processDefaultInalueInLeafSubStatement() throws IOException, ParserException { | ||
81 | + | ||
82 | + thrown.expect(DataTypeException.class); | ||
83 | + thrown.expectMessage("YANG file error : Input value \"x\" is not a valid uint16."); | ||
84 | + | ||
85 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueInLeafSubStatement.yang"); | ||
86 | + } | ||
87 | + | ||
88 | + /** | ||
89 | + * Validates default case value in choice statement. | ||
68 | */ | 90 | */ |
69 | @Test | 91 | @Test |
70 | - public void processChoiceSubStatementDefault() throws IOException, ParserException { | 92 | + public void processDefaultCaseInChoiceSubStatement() throws IOException, ParserException { |
71 | 93 | ||
72 | - YangNode node = manager.getDataModel("src/test/resources/ChoiceSubStatementDefault.yang"); | 94 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultCaseInChoiceSubStatement.yang"); |
73 | // Check whether the data model tree returned is of type module. | 95 | // Check whether the data model tree returned is of type module. |
74 | assertThat((node instanceof YangModule), is(true)); | 96 | assertThat((node instanceof YangModule), is(true)); |
75 | 97 | ||
... | @@ -85,6 +107,408 @@ public class DefaultListenerTest { | ... | @@ -85,6 +107,408 @@ public class DefaultListenerTest { |
85 | 107 | ||
86 | YangChoice yangChoice = (YangChoice) yangContainer.getChild(); | 108 | YangChoice yangChoice = (YangChoice) yangContainer.getChild(); |
87 | assertThat(yangChoice.getName(), is("snack")); | 109 | assertThat(yangChoice.getName(), is("snack")); |
88 | - assertThat(yangChoice.getDefaultValueInString(), is("\"hello\"")); | 110 | + assertThat(yangChoice.getDefaultValueInString(), is("sports-arena")); |
111 | + } | ||
112 | + | ||
113 | + /** | ||
114 | + * Validates default invalide case in choice statement. | ||
115 | + */ | ||
116 | + @Test | ||
117 | + public void processDefaultInvalidCaseInChoiceSubStatement() throws IOException, ParserException { | ||
118 | + | ||
119 | + thrown.expect(ParserException.class); | ||
120 | + thrown.expectMessage("Internal parser error detected: Invalid content in choice \"snack\" after processing."); | ||
121 | + | ||
122 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueInChoiceSubStmt.yang"); | ||
123 | + } | ||
124 | + | ||
125 | + /** | ||
126 | + * Validates default value in typedef. | ||
127 | + */ | ||
128 | + @Test | ||
129 | + public void processDefaultInTypedef() throws IOException, ParserException { | ||
130 | + | ||
131 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInTypeDef.yang"); | ||
132 | + // Check whether the data model tree returned is of type module. | ||
133 | + assertThat((node instanceof YangModule), is(true)); | ||
134 | + | ||
135 | + // Check whether the node type is set properly to module. | ||
136 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
137 | + | ||
138 | + // Check whether the module name is set correctly. | ||
139 | + YangModule yangNode = (YangModule) node; | ||
140 | + assertThat(yangNode.getName(), is("Test")); | ||
141 | + | ||
142 | + // check typedef | ||
143 | + YangTypeDef typedef = (YangTypeDef) yangNode.getChild(); | ||
144 | + assertThat(typedef.getName(), is("topInt")); | ||
145 | + assertThat(typedef.getDefaultValueInString(), is("10")); | ||
146 | + | ||
147 | + YangType type = typedef.getTypeList().iterator().next(); | ||
148 | + assertThat(type.getDataType(), is(YangDataTypes.INT64)); | ||
149 | + assertThat(type.getDataTypeName(), is("int64")); | ||
150 | + | ||
151 | + // check leaf | ||
152 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
153 | + YangLeaf leafInfo = leafIterator.next(); | ||
154 | + assertThat(leafInfo.getName(), is("myValue")); | ||
155 | + | ||
156 | + // Check leaf reffered typedef | ||
157 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("topInt")); | ||
158 | + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED)); | ||
159 | + YangType<YangDerivedInfo> typeDerived = (YangType<YangDerivedInfo>) leafInfo.getDataType(); | ||
160 | + YangDerivedInfo derivedInfo = (YangDerivedInfo) typeDerived.getDataTypeExtendedInfo(); | ||
161 | + YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef(); | ||
162 | + assertThat(prevTypedef.getName(), is("topInt")); | ||
163 | + assertThat(prevTypedef.getDefaultValueInString(), is("10")); | ||
164 | + YangType topType = prevTypedef.getTypeList().iterator().next(); | ||
165 | + assertThat(topType.getDataType(), is(YangDataTypes.INT64)); | ||
166 | + assertThat(topType.getDataTypeName(), is("int64")); | ||
167 | + YangType<YangInt64> typeInt64 = (YangType<YangInt64>) topType; | ||
168 | + YangInt64 int64Obj = typeInt64.getDataTypeExtendedInfo(); | ||
169 | + } | ||
170 | + | ||
171 | + /** | ||
172 | + * Validates invalid default value in typedef. | ||
173 | + */ | ||
174 | + @Test | ||
175 | + public void processInvalidDefaultValueInTypdeDef() throws IOException, ParserException { | ||
176 | + thrown.expect(DataTypeException.class); | ||
177 | + thrown.expectMessage("YANG file error : Input value \"x\" is not a valid int64."); | ||
178 | + | ||
179 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueInTypeDef.yang"); | ||
180 | + } | ||
181 | + | ||
182 | + /** | ||
183 | + * Validates default value decimal64 in leaf. | ||
184 | + */ | ||
185 | + @Test | ||
186 | + public void processDefaultValueDecimal64InLeaf() throws IOException, ParserException { | ||
187 | + | ||
188 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueDecimal64InLeaf.yang"); | ||
189 | + // Check whether the data model tree returned is of type module. | ||
190 | + assertThat((node instanceof YangModule), is(true)); | ||
191 | + | ||
192 | + // Check whether the node type is set properly to module. | ||
193 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
194 | + | ||
195 | + // Check whether the module name is set correctly. | ||
196 | + YangModule yangNode = (YangModule) node; | ||
197 | + assertThat(yangNode.getName(), is("Test")); | ||
198 | + | ||
199 | + // check leaf | ||
200 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
201 | + YangLeaf leafInfo = leafIterator.next(); | ||
202 | + | ||
203 | + // check the default value | ||
204 | + // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit() | ||
205 | + assertThat(leafInfo.getName(), is("mydecimal")); | ||
206 | + assertThat(leafInfo.getDefaultValueInString(), is("5")); | ||
207 | + } | ||
208 | + | ||
209 | + /** | ||
210 | + * Validates default invalid value decimal64 in leaf. | ||
211 | + */ | ||
212 | + @Test | ||
213 | + public void processDefaultInvalidValueDecimal64InLeaf() throws IOException, ParserException { | ||
214 | + thrown.expect(DataTypeException.class); | ||
215 | + thrown.expectMessage("YANG file error : Input value \"x\" is not a valid decimal64."); | ||
216 | + | ||
217 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueDecimal64InLeaf.yang"); | ||
218 | + } | ||
219 | + | ||
220 | + /** | ||
221 | + * Validates default value string in leaf. | ||
222 | + */ | ||
223 | + @Test | ||
224 | + public void processDefaultValueStringInLeaf() throws IOException, ParserException { | ||
225 | + | ||
226 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueStringInLeaf.yang"); | ||
227 | + // Check whether the data model tree returned is of type module. | ||
228 | + assertThat((node instanceof YangModule), is(true)); | ||
229 | + | ||
230 | + // Check whether the node type is set properly to module. | ||
231 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
232 | + | ||
233 | + // Check whether the module name is set correctly. | ||
234 | + YangModule yangNode = (YangModule) node; | ||
235 | + assertThat(yangNode.getName(), is("Test")); | ||
236 | + | ||
237 | + // check leaf | ||
238 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
239 | + YangLeaf leafInfo = leafIterator.next(); | ||
240 | + | ||
241 | + // check the default value | ||
242 | + // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit() | ||
243 | + assertThat(leafInfo.getName(), is("MyString")); | ||
244 | + assertThat(leafInfo.getDefaultValueInString(), is("2bB")); | ||
245 | + } | ||
246 | + | ||
247 | + /** | ||
248 | + * Validates default invalid value string in leaf. | ||
249 | + */ | ||
250 | + @Test | ||
251 | + public void processDefaultInvalidValueStringInLeaf() throws IOException, ParserException { | ||
252 | + thrown.expect(DataTypeException.class); | ||
253 | + thrown.expectMessage("YANG file error : Input value \"2bB2bB\" is not a valid string"); | ||
254 | + | ||
255 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueStringInLeaf.yang"); | ||
256 | + } | ||
257 | + | ||
258 | + /** | ||
259 | + * Validates default value boolean in leaf. | ||
260 | + */ | ||
261 | + @Test | ||
262 | + public void processDefaultValueBooleanInLeaf() throws IOException, ParserException { | ||
263 | + | ||
264 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBooleanInLeaf.yang"); | ||
265 | + // Check whether the data model tree returned is of type module. | ||
266 | + assertThat((node instanceof YangModule), is(true)); | ||
267 | + | ||
268 | + // Check whether the node type is set properly to module. | ||
269 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
270 | + | ||
271 | + // Check whether the module name is set correctly. | ||
272 | + YangModule yangNode = (YangModule) node; | ||
273 | + assertThat(yangNode.getName(), is("Test")); | ||
274 | + | ||
275 | + // check leaf | ||
276 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
277 | + YangLeaf leafInfo = leafIterator.next(); | ||
278 | + | ||
279 | + // check the default value | ||
280 | + // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit() | ||
281 | + assertThat(leafInfo.getName(), is("myboolean")); | ||
282 | + assertThat(leafInfo.getDefaultValueInString(), is("true")); | ||
283 | + } | ||
284 | + | ||
285 | + /** | ||
286 | + * Validates default invalid value boolean in leaf. | ||
287 | + */ | ||
288 | + @Test | ||
289 | + public void processDefaultInvalidValueBooleanInLeaf() throws IOException, ParserException { | ||
290 | + thrown.expect(DataTypeException.class); | ||
291 | + thrown.expectMessage("YANG file error : Input value \"yes\" is not a valid boolean"); | ||
292 | + | ||
293 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueBooleanInLeaf.yang"); | ||
294 | + } | ||
295 | + | ||
296 | + /** | ||
297 | + * Validates default value enumeration in leaf. | ||
298 | + */ | ||
299 | + @Test | ||
300 | + public void processDefaultValueEnumberationInLeaf() throws IOException, ParserException { | ||
301 | + | ||
302 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueEnumerationInLeaf.yang"); | ||
303 | + // Check whether the data model tree returned is of type module. | ||
304 | + assertThat((node instanceof YangModule), is(true)); | ||
305 | + | ||
306 | + // Check whether the node type is set properly to module. | ||
307 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
308 | + | ||
309 | + // Check whether the module name is set correctly. | ||
310 | + YangModule yangNode = (YangModule) node; | ||
311 | + assertThat(yangNode.getName(), is("Test")); | ||
312 | + | ||
313 | + // check leaf | ||
314 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
315 | + YangLeaf leafInfo = leafIterator.next(); | ||
316 | + | ||
317 | + // check the default value | ||
318 | + // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit() | ||
319 | + assertThat(leafInfo.getName(), is("myenum")); | ||
320 | + assertThat(leafInfo.getDefaultValueInString(), is("one")); | ||
321 | + } | ||
322 | + | ||
323 | + /** | ||
324 | + * Validates default invalid value enumeration in leaf. | ||
325 | + */ | ||
326 | + @Test | ||
327 | + public void processDefaultInvalidValueEnumberationInLeaf() throws IOException, ParserException { | ||
328 | + thrown.expect(DataTypeException.class); | ||
329 | + thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid union"); | ||
330 | + | ||
331 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueEnumerationInLeaf.yang"); | ||
332 | + } | ||
333 | + | ||
334 | + /** | ||
335 | + * Validates default value bits in leaf. | ||
336 | + */ | ||
337 | + @Test | ||
338 | + public void processDefaultValueBitsInLeaf() throws IOException, ParserException { | ||
339 | + | ||
340 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBitsInLeaf.yang"); | ||
341 | + // Check whether the data model tree returned is of type module. | ||
342 | + assertThat((node instanceof YangModule), is(true)); | ||
343 | + | ||
344 | + // Check whether the node type is set properly to module. | ||
345 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
346 | + | ||
347 | + // Check whether the module name is set correctly. | ||
348 | + YangModule yangNode = (YangModule) node; | ||
349 | + assertThat(yangNode.getName(), is("Test")); | ||
350 | + | ||
351 | + // check leaf | ||
352 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
353 | + YangLeaf leafInfo = leafIterator.next(); | ||
354 | + | ||
355 | + // check the default value | ||
356 | + // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit() | ||
357 | + assertThat(leafInfo.getName(), is("mybits")); | ||
358 | + assertThat(leafInfo.getDefaultValueInString(), is("auto-sense-speed")); | ||
359 | + } | ||
360 | + | ||
361 | + /** | ||
362 | + * Validates default invalid value bits in leaf. | ||
363 | + */ | ||
364 | + @Test | ||
365 | + public void processDefaultInvalidValueBitsInLeaf() throws IOException, ParserException { | ||
366 | + thrown.expect(DataTypeException.class); | ||
367 | + thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid bits"); | ||
368 | + | ||
369 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueBitsInLeaf.yang"); | ||
370 | + } | ||
371 | + | ||
372 | + /** | ||
373 | + * Validates default value binary in leaf. | ||
374 | + */ | ||
375 | + @Test | ||
376 | + public void processDefaultValueBinaryInLeaf() throws IOException, ParserException { | ||
377 | + | ||
378 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBinaryInLeaf.yang"); | ||
379 | + // Check whether the data model tree returned is of type module. | ||
380 | + assertThat((node instanceof YangModule), is(true)); | ||
381 | + | ||
382 | + // Check whether the node type is set properly to module. | ||
383 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
384 | + | ||
385 | + // Check whether the module name is set correctly. | ||
386 | + YangModule yangNode = (YangModule) node; | ||
387 | + assertThat(yangNode.getName(), is("Test")); | ||
388 | + | ||
389 | + // check leaf | ||
390 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
391 | + YangLeaf leafInfo = leafIterator.next(); | ||
392 | + | ||
393 | + // check the default value | ||
394 | + // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit() | ||
395 | + assertThat(leafInfo.getName(), is("message")); | ||
396 | + assertThat(leafInfo.getDefaultValueInString(), is("10010010")); | ||
397 | + } | ||
398 | + | ||
399 | + /** | ||
400 | + * Validates default invalid value binary in leaf. | ||
401 | + */ | ||
402 | + @Test | ||
403 | + public void processDefaultInvlaidValueBinaryInLeaf() throws IOException, ParserException { | ||
404 | + thrown.expect(DataTypeException.class); | ||
405 | + thrown.expectMessage("YANG file error : Input value \"000\" is not a valid binary"); | ||
406 | + | ||
407 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueBinaryInLeaf.yang"); | ||
408 | + } | ||
409 | + | ||
410 | + /** | ||
411 | + * Validates default value empty in leaf. | ||
412 | + */ | ||
413 | + @Test | ||
414 | + public void processDefaultValueEmptyInLeaf() throws IOException, ParserException { | ||
415 | + thrown.expect(DataTypeException.class); | ||
416 | + thrown.expectMessage("YANG file error : Input value \"something\" is not a allowed for a data type empty"); | ||
417 | + | ||
418 | + manager.getDataModel("src/test/resources/default/DefaultValueEmptyInLeaf.yang"); | ||
419 | + } | ||
420 | + | ||
421 | + /** | ||
422 | + * Validates default value union in leaf. | ||
423 | + */ | ||
424 | + @Test | ||
425 | + public void processDefaultValueUnionInLeaf() throws IOException, ParserException { | ||
426 | + | ||
427 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueUnionInLeaf.yang"); | ||
428 | + // Check whether the data model tree returned is of type module. | ||
429 | + assertThat((node instanceof YangModule), is(true)); | ||
430 | + | ||
431 | + // Check whether the node type is set properly to module. | ||
432 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
433 | + | ||
434 | + // Check whether the module name is set correctly. | ||
435 | + YangModule yangNode = (YangModule) node; | ||
436 | + assertThat(yangNode.getName(), is("Test")); | ||
437 | + | ||
438 | + // check leaf | ||
439 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
440 | + YangLeaf leafInfo = leafIterator.next(); | ||
441 | + | ||
442 | + // check the default value | ||
443 | + // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit() | ||
444 | + assertThat(leafInfo.getName(), is("message")); | ||
445 | + assertThat(leafInfo.getDefaultValueInString(), is("unbounded")); | ||
446 | + } | ||
447 | + | ||
448 | + /** | ||
449 | + * Validates default invalid value union in leaf. | ||
450 | + */ | ||
451 | + @Test | ||
452 | + public void processDefaultInvalidValueUnionInLeaf() throws IOException, ParserException { | ||
453 | + thrown.expect(DataTypeException.class); | ||
454 | + thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid union"); | ||
455 | + | ||
456 | + manager.getDataModel("src/test/resources/default/DefaultInvalidValueUnionInLeaf.yang"); | ||
457 | + } | ||
458 | + | ||
459 | + /** | ||
460 | + * Validates default value in multiple typedef. | ||
461 | + */ | ||
462 | + @Test | ||
463 | + public void processDefaultInMultiTypedef() throws IOException, ParserException { | ||
464 | + | ||
465 | + YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInMultiTypeDef.yang"); | ||
466 | + // Check whether the data model tree returned is of type module. | ||
467 | + assertThat((node instanceof YangModule), is(true)); | ||
468 | + | ||
469 | + // Check whether the node type is set properly to module. | ||
470 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
471 | + | ||
472 | + // Check whether the module name is set correctly. | ||
473 | + YangModule yangNode = (YangModule) node; | ||
474 | + assertThat(yangNode.getName(), is("Test")); | ||
475 | + | ||
476 | + // check typedef | ||
477 | + YangTypeDef typedef = (YangTypeDef) yangNode.getChild(); | ||
478 | + assertThat(typedef.getName(), is("topInt")); | ||
479 | + assertThat(typedef.getDefaultValueInString(), is("10")); | ||
480 | + | ||
481 | + YangType type = typedef.getTypeList().iterator().next(); | ||
482 | + assertThat(type.getDataType(), is(YangDataTypes.INT64)); | ||
483 | + assertThat(type.getDataTypeName(), is("int64")); | ||
484 | + | ||
485 | + // check leaf | ||
486 | + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); | ||
487 | + YangLeaf leafInfo = leafIterator.next(); | ||
488 | + assertThat(leafInfo.getName(), is("lowInt")); | ||
489 | + | ||
490 | + // check leaf type | ||
491 | + assertThat(leafInfo.getName(), is("lowInt")); | ||
492 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("midInt")); | ||
493 | + YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType(); | ||
494 | + assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED)); | ||
495 | + YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo(); | ||
496 | + | ||
497 | + // check previous typedef | ||
498 | + YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef(); | ||
499 | + assertThat(prevTypedef.getName(), is("midInt")); | ||
500 | + type = prevTypedef.getTypeList().iterator().next(); | ||
501 | + assertThat(type.getDataType(), is(YangDataTypes.DERIVED)); | ||
502 | + derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo(); | ||
503 | + | ||
504 | + // check top typedef | ||
505 | + YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef(); | ||
506 | + assertThat(topTypedef.getName(), is("topInt")); | ||
507 | + assertThat(topTypedef.getDefaultValueInString(), is("10")); | ||
508 | + YangType topType = topTypedef.getTypeList().iterator().next(); | ||
509 | + assertThat(topType.getDataType(), is(YangDataTypes.INT64)); | ||
510 | + assertThat(topType.getDataTypeName(), is("int64")); | ||
511 | + YangType<YangInt64> typeInt64 = (YangType<YangInt64>) topType; | ||
512 | + YangInt64 int64Obj = typeInt64.getDataTypeExtendedInfo(); | ||
89 | } | 513 | } |
90 | } | 514 | } | ... | ... |
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