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; |
| 21 | |
| 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 24 | |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 25 | import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString; |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /*- |
| 29 | * Reference RFC 6020. |
| 30 | * |
| 31 | * The range Statement |
| 32 | * |
| 33 | * The "range" statement, which is an optional sub-statement to the |
| 34 | * "type" statement, takes as an argument a range expression string. It |
| 35 | * is used to restrict integer and decimal built-in types, or types |
| 36 | * derived from those. |
| 37 | * |
| 38 | * A range consists of an explicit value, or a lower-inclusive bound, |
| 39 | * two consecutive dots "..", and an upper-inclusive bound. Multiple |
| 40 | * values or ranges can be given, separated by "|". If multiple values |
| 41 | * or ranges are given, they all MUST be disjoint and MUST be in |
| 42 | * ascending order. If a range restriction is applied to an already |
| 43 | * range-restricted type, the new restriction MUST be equal or more |
| 44 | * limiting, that is raising the lower bounds, reducing the upper |
| 45 | * bounds, removing explicit values or ranges, or splitting ranges into |
| 46 | * multiple ranges with intermediate gaps. Each explicit value and |
| 47 | * range boundary value given in the range expression MUST match the |
| 48 | * type being restricted, or be one of the special values "min" or |
| 49 | * "max". "min" and "max" mean the minimum and maximum value accepted |
| 50 | * for the type being restricted, respectively. |
| 51 | */ |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 52 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 53 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 54 | * Represents ascending range restriction information. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 55 | * |
| 56 | * @param <T> range type (data type) |
| 57 | */ |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 58 | public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>> |
| 59 | implements YangDesc, YangReference, YangAppErrorInfo { |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Ascending list of range interval restriction. If the restriction is a |
| 63 | * single value, the start and end length of the range is same. |
| 64 | */ |
| 65 | private List<YangRangeInterval<T>> ascendingRangeIntervals; |
| 66 | |
| 67 | /** |
| 68 | * Textual reference. |
| 69 | */ |
| 70 | private String reference; |
| 71 | |
| 72 | /** |
| 73 | * Application's error message, to be used for data error. |
| 74 | */ |
| 75 | private String errorMessage; |
| 76 | |
| 77 | /** |
| 78 | * Application's error tag, to be filled in data validation error response. |
| 79 | */ |
| 80 | private String errorAppTag; |
| 81 | |
| 82 | /** |
| 83 | * Textual description. |
| 84 | */ |
| 85 | private String description; |
| 86 | |
| 87 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 88 | * Creates YANG range restriction object. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 89 | */ |
| 90 | public YangRangeRestriction() { |
| 91 | } |
| 92 | |
| 93 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 94 | * Returns the list of range interval restriction in ascending order. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 95 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 96 | * @return list of range interval restriction in ascending order |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 97 | */ |
| 98 | public List<YangRangeInterval<T>> getAscendingRangeIntervals() { |
| 99 | return ascendingRangeIntervals; |
| 100 | } |
| 101 | |
| 102 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 103 | * Sets the list of range interval restriction in ascending order. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 104 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 105 | * @param rangeList list of range interval restriction in ascending order |
Vinod Kumar S | c26bf19 | 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 | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 112 | * Returns the minimum valid value as per the restriction. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 113 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 114 | * @throws DataModelException data model exception for minimum restriction |
| 115 | * @return minimum restricted value |
Vinod Kumar S | c26bf19 | 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 | } |
| 121 | if (getAscendingRangeIntervals().size() == 0) { |
| 122 | throw new DataModelException("No range interval info"); |
| 123 | } |
| 124 | return getAscendingRangeIntervals().get(0).getStartValue(); |
| 125 | } |
| 126 | |
| 127 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 128 | * Returns the maximum valid value as per the restriction. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 129 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 130 | * @throws DataModelException data model exception for maximum restriction |
| 131 | * @return minimum maximum value |
Vinod Kumar S | c26bf19 | 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 | } |
| 137 | if (getAscendingRangeIntervals().size() == 0) { |
| 138 | throw new DataModelException("No range interval info"); |
| 139 | } |
| 140 | return getAscendingRangeIntervals() |
| 141 | .get(getAscendingRangeIntervals().size() - 1).getEndValue(); |
| 142 | } |
| 143 | |
| 144 | /** |
Bharat saraswal | 63f26fb | 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 | c26bf19 | 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 | 63f26fb | 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 | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 150 | */ |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 151 | public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException { |
Vinod Kumar S | c26bf19 | 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 | |
| 168 | if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) { |
| 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 | /** |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 178 | * Check if the given value is correct as per the restriction. |
| 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 |
| 187 | || getAscendingRangeIntervals().size() == 0) { |
| 188 | // Throw exception, At least one default range needs to be set in constructor or in linker. |
| 189 | throw new DataModelException("Range interval missing in range restriction."); |
| 190 | |
| 191 | } |
| 192 | |
| 193 | YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType(); |
| 194 | YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type); |
| 195 | |
| 196 | for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) { |
| 197 | int rangeStartCompareRes = interval.getStartValue().compareTo((T) value); |
| 198 | int rangeEndCompareRes = interval.getEndValue().compareTo((T) value); |
| 199 | |
| 200 | if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) { |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 209 | * Returns the textual reference of the length restriction. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 210 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 211 | * @return textual reference of the length restriction |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 212 | */ |
| 213 | @Override |
| 214 | public String getReference() { |
| 215 | return reference; |
| 216 | } |
| 217 | |
| 218 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 219 | * Sets the textual reference of the length restriction. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 220 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 221 | * @param ref textual reference of the length restriction |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 222 | */ |
| 223 | @Override |
| 224 | public void setReference(String ref) { |
| 225 | reference = ref; |
| 226 | } |
| 227 | |
| 228 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 229 | * Returns the description of the length restriction. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 230 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 231 | * @return description of the length restriction |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 232 | */ |
| 233 | @Override |
| 234 | public String getDescription() { |
| 235 | return description; |
| 236 | } |
| 237 | |
| 238 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 239 | * Sets the description of the length restriction. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 240 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 241 | * @param desc description of the length restriction |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 242 | */ |
| 243 | @Override |
| 244 | public void setDescription(String desc) { |
| 245 | description = desc; |
| 246 | |
| 247 | } |
| 248 | |
| 249 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 250 | * Returns application's error message, to be used for data error. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 251 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 252 | * @return Application's error message, to be used for data error |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 253 | */ |
| 254 | @Override |
| 255 | public String getGetErrorMessage() { |
| 256 | return errorMessage; |
| 257 | } |
| 258 | |
| 259 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 260 | * Sets Application's error message, to be used for data error. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 261 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 262 | * @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] | 263 | */ |
| 264 | @Override |
| 265 | public void setErrorMessage(String errMsg) { |
| 266 | errorMessage = errMsg; |
| 267 | |
| 268 | } |
| 269 | |
| 270 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 271 | * Returns application's error tag, to be used for data error. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 272 | * |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 273 | * @return application's error tag, to be used for data error |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 274 | */ |
| 275 | @Override |
| 276 | public String getGetErrorAppTag() { |
| 277 | return errorAppTag; |
| 278 | } |
| 279 | |
| 280 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 281 | * Sets application's error tag, to be used for data error. |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 282 | * |
| 283 | * @param errTag application's error tag, to be used for data error. |
| 284 | */ |
| 285 | @Override |
| 286 | public void setErrorAppTag(String errTag) { |
| 287 | errorAppTag = errTag; |
| 288 | } |
| 289 | } |