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; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 22 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 24 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 25 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 27 | |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 29 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /*- |
| 33 | * Reference RFC 6020. |
| 34 | * |
| 35 | * The range Statement |
| 36 | * |
| 37 | * The "range" statement, which is an optional sub-statement to the |
| 38 | * "type" statement, takes as an argument a range expression string. It |
| 39 | * is used to restrict integer and decimal built-in types, or types |
| 40 | * derived from those. |
| 41 | * |
| 42 | * A range consists of an explicit value, or a lower-inclusive bound, |
| 43 | * two consecutive dots "..", and an upper-inclusive bound. Multiple |
| 44 | * values or ranges can be given, separated by "|". If multiple values |
| 45 | * or ranges are given, they all MUST be disjoint and MUST be in |
| 46 | * ascending order. If a range restriction is applied to an already |
| 47 | * range-restricted type, the new restriction MUST be equal or more |
| 48 | * limiting, that is raising the lower bounds, reducing the upper |
| 49 | * bounds, removing explicit values or ranges, or splitting ranges into |
| 50 | * multiple ranges with intermediate gaps. Each explicit value and |
| 51 | * range boundary value given in the range expression MUST match the |
| 52 | * type being restricted, or be one of the special values "min" or |
| 53 | * "max". "min" and "max" mean the minimum and maximum value accepted |
| 54 | * for the type being restricted, respectively. |
| 55 | */ |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 56 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 57 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 58 | * Represents ascending range restriction information. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 59 | * |
| 60 | * @param <T> range type (data type) |
| 61 | */ |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 62 | public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>> |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 63 | implements YangDesc, YangReference, YangAppErrorInfo, Parsable, Serializable { |
| 64 | |
| 65 | private static final long serialVersionUID = 8062016051L; |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Ascending list of range interval restriction. If the restriction is a |
| 69 | * single value, the start and end length of the range is same. |
| 70 | */ |
| 71 | private List<YangRangeInterval<T>> ascendingRangeIntervals; |
| 72 | |
| 73 | /** |
| 74 | * Textual reference. |
| 75 | */ |
| 76 | private String reference; |
| 77 | |
| 78 | /** |
| 79 | * Application's error message, to be used for data error. |
| 80 | */ |
| 81 | private String errorMessage; |
| 82 | |
| 83 | /** |
| 84 | * Application's error tag, to be filled in data validation error response. |
| 85 | */ |
| 86 | private String errorAppTag; |
| 87 | |
| 88 | /** |
| 89 | * Textual description. |
| 90 | */ |
| 91 | private String description; |
| 92 | |
| 93 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 94 | * Creates YANG range restriction object. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 95 | */ |
| 96 | public YangRangeRestriction() { |
| 97 | } |
| 98 | |
| 99 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 100 | * Returns the list of range interval restriction in ascending order. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 101 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 102 | * @return list of range interval restriction in ascending order |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 103 | */ |
| 104 | public List<YangRangeInterval<T>> getAscendingRangeIntervals() { |
| 105 | return ascendingRangeIntervals; |
| 106 | } |
| 107 | |
| 108 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 109 | * Sets the list of range interval restriction in ascending order. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 110 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 111 | * @param rangeList list of range interval restriction in ascending order |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 112 | */ |
| 113 | private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) { |
| 114 | ascendingRangeIntervals = rangeList; |
| 115 | } |
| 116 | |
| 117 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 118 | * Returns the minimum valid value as per the restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 119 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 120 | * @return minimum restricted value |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 121 | * @throws DataModelException data model exception for minimum restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 122 | */ |
| 123 | public T getMinRestrictedvalue() throws DataModelException { |
| 124 | if (getAscendingRangeIntervals() == null) { |
| 125 | throw new DataModelException("No range restriction info"); |
| 126 | } |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 127 | if (getAscendingRangeIntervals().isEmpty()) { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 128 | throw new DataModelException("No range interval info"); |
| 129 | } |
| 130 | return getAscendingRangeIntervals().get(0).getStartValue(); |
| 131 | } |
| 132 | |
| 133 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 134 | * Returns the maximum valid value as per the restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 135 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 136 | * @return minimum maximum value |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 137 | * @throws DataModelException data model exception for maximum restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 138 | */ |
| 139 | public T getMaxRestrictedvalue() throws DataModelException { |
| 140 | if (getAscendingRangeIntervals() == null) { |
| 141 | throw new DataModelException("No range restriction info"); |
| 142 | } |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 143 | if (getAscendingRangeIntervals().isEmpty()) { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 144 | throw new DataModelException("No range interval info"); |
| 145 | } |
| 146 | return getAscendingRangeIntervals() |
| 147 | .get(getAscendingRangeIntervals().size() - 1).getEndValue(); |
| 148 | } |
| 149 | |
| 150 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 151 | * 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] | 152 | * interval needs to be bigger than the biggest interval in the list. |
| 153 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 154 | * @param newInterval restricted length interval |
| 155 | * @throws DataModelException data model exception for range restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 156 | */ |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 157 | public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 158 | |
| 159 | checkNotNull(newInterval); |
| 160 | checkNotNull(newInterval.getStartValue()); |
| 161 | |
| 162 | if (getAscendingRangeIntervals() == null) { |
| 163 | /* |
| 164 | * First interval that is being added, and it must be the smallest |
| 165 | * interval. |
| 166 | */ |
| 167 | setAscendingRangeIntervals(new LinkedList<YangRangeInterval<T>>()); |
| 168 | getAscendingRangeIntervals().add(newInterval); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | T curMaxvalue = getMaxRestrictedvalue(); |
| 173 | |
| 174 | if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) { |
| 175 | throw new DataModelException( |
| 176 | "New added range interval is lesser than the old interval(s)"); |
| 177 | } |
| 178 | |
| 179 | getAscendingRangeIntervals() |
| 180 | .add(getAscendingRangeIntervals().size(), newInterval); |
| 181 | } |
| 182 | |
| 183 | /** |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 184 | * Validates if the given value is correct as per the restriction. |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 185 | * |
| 186 | * @param valueInString value |
| 187 | * @return true, if the value is confirming to restriction, false otherwise |
| 188 | * @throws DataModelException data model error |
| 189 | */ |
| 190 | public boolean isValidValueString(String valueInString) throws DataModelException { |
| 191 | |
| 192 | if (getAscendingRangeIntervals() == null |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 193 | || getAscendingRangeIntervals().isEmpty()) { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 194 | // Throw exception, At least one default range needs to be set in |
| 195 | // constructor or in linker. |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 196 | throw new DataModelException("Range interval missing in range restriction."); |
| 197 | |
| 198 | } |
| 199 | |
| 200 | YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType(); |
| 201 | YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type); |
| 202 | |
| 203 | for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) { |
| 204 | int rangeStartCompareRes = interval.getStartValue().compareTo((T) value); |
| 205 | int rangeEndCompareRes = interval.getEndValue().compareTo((T) value); |
| 206 | |
| 207 | if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) { |
| 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | /** |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 216 | * Validates if the given interval is correct as per the restriction. |
| 217 | * |
| 218 | * @param rangeInterval range interval |
| 219 | * @return true, if the interval is confirming to restriction, false otherwise |
| 220 | * @throws DataModelException data model error |
| 221 | */ |
| 222 | public boolean isValidInterval(YangRangeInterval rangeInterval) throws DataModelException { |
| 223 | |
| 224 | if (getAscendingRangeIntervals() == null |
| 225 | || getAscendingRangeIntervals().isEmpty()) { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 226 | // Throw exception, At least one default range needs to be set in |
| 227 | // constructor or in linker. |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 228 | throw new DataModelException("Range interval missing in range restriction."); |
| 229 | } |
| 230 | |
| 231 | for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) { |
| 232 | int rangeStartCompareRes = interval.getStartValue().compareTo((T) rangeInterval.getStartValue()); |
| 233 | int rangeEndCompareRes = interval.getEndValue().compareTo((T) rangeInterval.getEndValue()); |
| 234 | |
| 235 | if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) { |
| 236 | return true; |
| 237 | } |
| 238 | } |
| 239 | throw new DataModelException("Range interval doesn't fall within the referred restriction ranges"); |
| 240 | } |
| 241 | |
| 242 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 243 | * Returns the textual reference of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 244 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 245 | * @return textual reference of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 246 | */ |
| 247 | @Override |
| 248 | public String getReference() { |
| 249 | return reference; |
| 250 | } |
| 251 | |
| 252 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 253 | * Sets the textual reference of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 254 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 255 | * @param ref textual reference of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 256 | */ |
| 257 | @Override |
| 258 | public void setReference(String ref) { |
| 259 | reference = ref; |
| 260 | } |
| 261 | |
| 262 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 263 | * Returns the description of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 264 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 265 | * @return description of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 266 | */ |
| 267 | @Override |
| 268 | public String getDescription() { |
| 269 | return description; |
| 270 | } |
| 271 | |
| 272 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 273 | * Sets the description of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 274 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 275 | * @param desc description of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 276 | */ |
| 277 | @Override |
| 278 | public void setDescription(String desc) { |
| 279 | description = desc; |
| 280 | |
| 281 | } |
| 282 | |
| 283 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 284 | * Returns application's error message, to be used for data error. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 285 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 286 | * @return Application's error message, to be used for data error |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 287 | */ |
| 288 | @Override |
| 289 | public String getGetErrorMessage() { |
| 290 | return errorMessage; |
| 291 | } |
| 292 | |
| 293 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 294 | * Sets Application's error message, to be used for data error. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 295 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 296 | * @param errMsg Application's error message, to be used for data error |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 297 | */ |
| 298 | @Override |
| 299 | public void setErrorMessage(String errMsg) { |
| 300 | errorMessage = errMsg; |
| 301 | |
| 302 | } |
| 303 | |
| 304 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 305 | * Returns application's error tag, to be used for data error. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 306 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 307 | * @return application's error tag, to be used for data error |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 308 | */ |
| 309 | @Override |
| 310 | public String getGetErrorAppTag() { |
| 311 | return errorAppTag; |
| 312 | } |
| 313 | |
| 314 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 315 | * Sets application's error tag, to be used for data error. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 316 | * |
| 317 | * @param errTag application's error tag, to be used for data error. |
| 318 | */ |
| 319 | @Override |
| 320 | public void setErrorAppTag(String errTag) { |
| 321 | errorAppTag = errTag; |
| 322 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 323 | |
| 324 | @Override |
| 325 | public YangConstructType getYangConstructType() { |
| 326 | return YangConstructType.RANGE_DATA; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void validateDataOnEntry() throws DataModelException { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 331 | // TODO: implement the method. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void validateDataOnExit() throws DataModelException { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 336 | // TODO: implement the method. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 337 | } |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 338 | } |