Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 16 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 17 | package org.onosproject.yangutils.datamodel; |
| 18 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 19 | import java.io.Serializable; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 24 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 27 | |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.datamodel.BuiltInTypeObjectFactory.getDataObjectFromString; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /*- |
| 32 | * Reference RFC 6020. |
| 33 | * |
| 34 | * The range Statement |
| 35 | * |
| 36 | * The "range" statement, which is an optional sub-statement to the |
| 37 | * "type" statement, takes as an argument a range expression string. It |
| 38 | * is used to restrict integer and decimal built-in types, or types |
| 39 | * derived from those. |
| 40 | * |
| 41 | * A range consists of an explicit value, or a lower-inclusive bound, |
| 42 | * two consecutive dots "..", and an upper-inclusive bound. Multiple |
| 43 | * values or ranges can be given, separated by "|". If multiple values |
| 44 | * or ranges are given, they all MUST be disjoint and MUST be in |
| 45 | * ascending order. If a range restriction is applied to an already |
| 46 | * range-restricted type, the new restriction MUST be equal or more |
| 47 | * limiting, that is raising the lower bounds, reducing the upper |
| 48 | * bounds, removing explicit values or ranges, or splitting ranges into |
| 49 | * multiple ranges with intermediate gaps. Each explicit value and |
| 50 | * range boundary value given in the range expression MUST match the |
| 51 | * type being restricted, or be one of the special values "min" or |
| 52 | * "max". "min" and "max" mean the minimum and maximum value accepted |
| 53 | * for the type being restricted, respectively. |
| 54 | */ |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 55 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 56 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 57 | * Represents ascending range restriction information. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 58 | * |
| 59 | * @param <T> range type (data type) |
| 60 | */ |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 61 | public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>> |
rama-huawei | 6c728a9 | 2016-07-11 14:48:12 +0530 | [diff] [blame] | 62 | implements YangDesc, YangReference, Parsable, Serializable, YangAppErrorHolder { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 63 | |
| 64 | private static final long serialVersionUID = 8062016051L; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Ascending list of range interval restriction. If the restriction is a |
| 68 | * single value, the start and end length of the range is same. |
| 69 | */ |
| 70 | private List<YangRangeInterval<T>> ascendingRangeIntervals; |
| 71 | |
| 72 | /** |
| 73 | * Textual reference. |
| 74 | */ |
| 75 | private String reference; |
| 76 | |
| 77 | /** |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 78 | * Textual description. |
| 79 | */ |
| 80 | private String description; |
| 81 | |
| 82 | /** |
rama-huawei | 6c728a9 | 2016-07-11 14:48:12 +0530 | [diff] [blame] | 83 | * YANG application error information. |
| 84 | */ |
| 85 | private YangAppErrorInfo yangAppErrorInfo; |
| 86 | |
| 87 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 88 | * Creates YANG range restriction object. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 89 | */ |
| 90 | public YangRangeRestriction() { |
| 91 | } |
| 92 | |
| 93 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 94 | * Returns the list of range interval restriction in ascending order. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 95 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 96 | * @return list of range interval restriction in ascending order |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 97 | */ |
| 98 | public List<YangRangeInterval<T>> getAscendingRangeIntervals() { |
| 99 | return ascendingRangeIntervals; |
| 100 | } |
| 101 | |
| 102 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 103 | * Sets the list of range interval restriction in ascending order. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 104 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 105 | * @param rangeList list of range interval restriction in ascending order |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 106 | */ |
| 107 | private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) { |
| 108 | ascendingRangeIntervals = rangeList; |
| 109 | } |
| 110 | |
| 111 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 112 | * Returns the minimum valid value as per the restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 113 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 114 | * @return minimum restricted value |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 115 | * @throws DataModelException data model exception for minimum restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 116 | */ |
| 117 | public T getMinRestrictedvalue() throws DataModelException { |
| 118 | if (getAscendingRangeIntervals() == null) { |
| 119 | throw new DataModelException("No range restriction info"); |
| 120 | } |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 121 | if (getAscendingRangeIntervals().isEmpty()) { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 122 | throw new DataModelException("No range interval info"); |
| 123 | } |
| 124 | return getAscendingRangeIntervals().get(0).getStartValue(); |
| 125 | } |
| 126 | |
| 127 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 128 | * Returns the maximum valid value as per the restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 129 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 130 | * @return minimum maximum value |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 131 | * @throws DataModelException data model exception for maximum restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 132 | */ |
| 133 | public T getMaxRestrictedvalue() throws DataModelException { |
| 134 | if (getAscendingRangeIntervals() == null) { |
| 135 | throw new DataModelException("No range restriction info"); |
| 136 | } |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 137 | if (getAscendingRangeIntervals().isEmpty()) { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 138 | throw new DataModelException("No range interval info"); |
| 139 | } |
| 140 | return getAscendingRangeIntervals() |
| 141 | .get(getAscendingRangeIntervals().size() - 1).getEndValue(); |
| 142 | } |
| 143 | |
| 144 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 145 | * Adds new interval to extend its range in the last. i.e. newly added |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 146 | * interval needs to be bigger than the biggest interval in the list. |
| 147 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 148 | * @param newInterval restricted length interval |
| 149 | * @throws DataModelException data model exception for range restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 150 | */ |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 151 | public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 152 | |
| 153 | checkNotNull(newInterval); |
| 154 | checkNotNull(newInterval.getStartValue()); |
| 155 | |
| 156 | if (getAscendingRangeIntervals() == null) { |
| 157 | /* |
| 158 | * First interval that is being added, and it must be the smallest |
| 159 | * interval. |
| 160 | */ |
| 161 | setAscendingRangeIntervals(new LinkedList<YangRangeInterval<T>>()); |
| 162 | getAscendingRangeIntervals().add(newInterval); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | T curMaxvalue = getMaxRestrictedvalue(); |
| 167 | |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 168 | if (newInterval.getStartValue().compareTo(curMaxvalue) < 1) { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 169 | throw new DataModelException( |
| 170 | "New added range interval is lesser than the old interval(s)"); |
| 171 | } |
| 172 | |
| 173 | getAscendingRangeIntervals() |
| 174 | .add(getAscendingRangeIntervals().size(), newInterval); |
| 175 | } |
| 176 | |
| 177 | /** |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 178 | * Validates if the given value is correct as per the restriction. |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 179 | * |
| 180 | * @param valueInString value |
| 181 | * @return true, if the value is confirming to restriction, false otherwise |
| 182 | * @throws DataModelException data model error |
| 183 | */ |
| 184 | public boolean isValidValueString(String valueInString) throws DataModelException { |
| 185 | |
| 186 | if (getAscendingRangeIntervals() == null |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 187 | || getAscendingRangeIntervals().isEmpty()) { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 188 | // Throw exception, At least one default range needs to be set in |
| 189 | // constructor or in linker. |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 190 | throw new DataModelException("Range interval missing in range restriction."); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType(); |
| 195 | YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type); |
| 196 | |
| 197 | for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) { |
| 198 | int rangeStartCompareRes = interval.getStartValue().compareTo((T) value); |
| 199 | int rangeEndCompareRes = interval.getEndValue().compareTo((T) value); |
| 200 | |
| 201 | if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) { |
| 202 | return true; |
| 203 | } |
| 204 | } |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
| 208 | /** |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 209 | * Validates if the given interval is correct as per the restriction. |
| 210 | * |
| 211 | * @param rangeInterval range interval |
| 212 | * @return true, if the interval is confirming to restriction, false otherwise |
| 213 | * @throws DataModelException data model error |
| 214 | */ |
| 215 | public boolean isValidInterval(YangRangeInterval rangeInterval) throws DataModelException { |
| 216 | |
| 217 | if (getAscendingRangeIntervals() == null |
| 218 | || getAscendingRangeIntervals().isEmpty()) { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 219 | // Throw exception, At least one default range needs to be set in |
| 220 | // constructor or in linker. |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 221 | throw new DataModelException("Range interval missing in range restriction."); |
| 222 | } |
| 223 | |
| 224 | for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) { |
| 225 | int rangeStartCompareRes = interval.getStartValue().compareTo((T) rangeInterval.getStartValue()); |
| 226 | int rangeEndCompareRes = interval.getEndValue().compareTo((T) rangeInterval.getEndValue()); |
| 227 | |
| 228 | if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) { |
| 229 | return true; |
| 230 | } |
| 231 | } |
| 232 | throw new DataModelException("Range interval doesn't fall within the referred restriction ranges"); |
| 233 | } |
| 234 | |
| 235 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 236 | * Returns the textual reference of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 237 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 238 | * @return textual reference of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 239 | */ |
| 240 | @Override |
| 241 | public String getReference() { |
| 242 | return reference; |
| 243 | } |
| 244 | |
| 245 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 246 | * Sets the textual reference of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 247 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 248 | * @param ref textual reference of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 249 | */ |
| 250 | @Override |
| 251 | public void setReference(String ref) { |
| 252 | reference = ref; |
| 253 | } |
| 254 | |
| 255 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 256 | * Returns the description of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 257 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 258 | * @return description of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 259 | */ |
| 260 | @Override |
| 261 | public String getDescription() { |
| 262 | return description; |
| 263 | } |
| 264 | |
| 265 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 266 | * Sets the description of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 267 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 268 | * @param desc description of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 269 | */ |
| 270 | @Override |
| 271 | public void setDescription(String desc) { |
| 272 | description = desc; |
| 273 | |
| 274 | } |
| 275 | |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 276 | @Override |
| 277 | public YangConstructType getYangConstructType() { |
| 278 | return YangConstructType.RANGE_DATA; |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public void validateDataOnEntry() throws DataModelException { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 283 | // TODO: implement the method. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void validateDataOnExit() throws DataModelException { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 288 | // TODO: implement the method. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 289 | } |
rama-huawei | 6c728a9 | 2016-07-11 14:48:12 +0530 | [diff] [blame] | 290 | |
| 291 | @Override |
| 292 | public void setAppErrorInfo(YangAppErrorInfo yangAppErrorInfo) { |
| 293 | this.yangAppErrorInfo = yangAppErrorInfo; |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public YangAppErrorInfo getAppErrorInfo() { |
| 298 | return yangAppErrorInfo; |
| 299 | } |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 300 | } |