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