YANG string restriction listener + YANG Range restriction listener review comment fix
Change-Id: I9e7af9c67f8fbf918d4e0d8ff147a560889ff264
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangRangeInterval.java b/src/main/java/org/onosproject/yangutils/datamodel/YangRangeInterval.java
index 442cda0..9ccd5ff 100644
--- a/src/main/java/org/onosproject/yangutils/datamodel/YangRangeInterval.java
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangRangeInterval.java
@@ -13,12 +13,14 @@
limitations under the License.*/
package org.onosproject.yangutils.datamodel;
+import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
+
/**
* Represents single interval information of a range.
*
- * @param <T> range type based on the data type.
+ * @param <T> range type based on the data type
*/
-public class YangRangeInterval<T extends Comparable<T>> {
+public class YangRangeInterval<T extends YangBuiltInDataTypeInfo<T>> {
/**
* Starting value of the range interval.
@@ -39,7 +41,7 @@
/**
* Returns the starting value of the range interval.
*
- * @return the starting value of the range interval.
+ * @return the starting value of the range interval
*/
public T getStartValue() {
return startValue;
@@ -48,7 +50,7 @@
/**
* Sets the starting value of the range interval.
*
- * @param startValue the starting value of the range interval.
+ * @param startValue the starting value of the range interval
*/
public void setStartValue(T startValue) {
this.startValue = startValue;
@@ -57,7 +59,7 @@
/**
* Returns the last value of the range interval.
*
- * @return last value of the range interval.
+ * @return last value of the range interval
*/
public T getEndValue() {
return endValue;
@@ -66,7 +68,7 @@
/**
* Sets the last value of the range interval.
*
- * @param endValue last value of the range interval.
+ * @param endValue last value of the range interval
*/
public void setEndValue(T endValue) {
this.endValue = endValue;
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java b/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java
index 09eb796..38b48bd 100644
--- a/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java
@@ -20,7 +20,9 @@
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
+import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
import static com.google.common.base.Preconditions.checkNotNull;
/*-
@@ -47,12 +49,14 @@
* "max". "min" and "max" mean the minimum and maximum value accepted
* for the type being restricted, respectively.
*/
+
/**
* Represents ascending range restriction information.
*
* @param <T> range type (data type)
*/
-public class YangRangeRestriction<T extends Comparable<T>> implements YangDesc, YangReference, YangAppErrorInfo {
+public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
+ implements YangDesc, YangReference, YangAppErrorInfo {
/**
* Ascending list of range interval restriction. If the restriction is a
@@ -171,6 +175,37 @@
}
/**
+ * Check if the given value is correct as per the restriction.
+ *
+ * @param valueInString value
+ * @return true, if the value is confirming to restriction, false otherwise
+ * @throws DataModelException data model error
+ */
+ public boolean isValidValueString(String valueInString) throws DataModelException {
+
+ if (getAscendingRangeIntervals() == null
+ || getAscendingRangeIntervals().size() == 0) {
+ // Throw exception, At least one default range needs to be set in constructor or in linker.
+ throw new DataModelException("Range interval missing in range restriction.");
+
+ }
+
+ YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType();
+ YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type);
+
+ for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) {
+ int rangeStartCompareRes = interval.getStartValue().compareTo((T) value);
+ int rangeEndCompareRes = interval.getEndValue().compareTo((T) value);
+
+ if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
* Returns the textual reference of the length restriction.
*
* @return textual reference of the length restriction
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangStringRestriction.java b/src/main/java/org/onosproject/yangutils/datamodel/YangStringRestriction.java
index 50a1f83..f2c67c3 100644
--- a/src/main/java/org/onosproject/yangutils/datamodel/YangStringRestriction.java
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangStringRestriction.java
@@ -16,7 +16,7 @@
package org.onosproject.yangutils.datamodel;
-import java.math.BigInteger;
+import org.onosproject.yangutils.utils.builtindatatype.YangUint64;
/*-
* Reference RFC 6020.
@@ -64,10 +64,11 @@
* | reference | 7.19.4 | 0..1 | string |
* +---------------+---------+-------------+-----------------+
*/
+
/**
* Length restriction information.
*/
- private YangRangeRestriction<BigInteger> lengthRestriction;
+ private YangRangeRestriction<YangUint64> lengthRestriction;
/**
* Effective pattern restriction for the type.
@@ -85,7 +86,7 @@
*
* @return length restriction on the string data
*/
- public YangRangeRestriction<BigInteger> getLengthRestriction() {
+ public YangRangeRestriction<YangUint64> getLengthRestriction() {
return lengthRestriction;
}
@@ -94,7 +95,7 @@
*
* @param lengthRestriction length restriction on the string data
*/
- public void setLengthRestriction(YangRangeRestriction<BigInteger> lengthRestriction) {
+ public void setLengthRestriction(YangRangeRestriction<YangUint64> lengthRestriction) {
this.lengthRestriction = lengthRestriction;
}