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