YANG string restriction listener + YANG Range restriction listener review comment fix
Change-Id: I9e7af9c67f8fbf918d4e0d8ff147a560889ff264
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java
index 09eb796..38b48bd 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangRangeRestriction.java
+++ b/utils/yangutils/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