blob: d645010b73ef637d739a73a3d5c86db31b856236 [file] [log] [blame]
Vinod Kumar Sc4216002016-03-03 19:55:30 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar Sc4216002016-03-03 19:55:30 +05303 *
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 S0c330cd2016-02-23 22:36:57 +053016
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053017package org.onosproject.yangutils.datamodel;
18
19import java.util.LinkedList;
20import java.util.List;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Rama1db15562016-05-17 16:16:15 +053022import org.onosproject.yangutils.parser.Parsable;
23import org.onosproject.yangutils.utils.YangConstructType;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053024import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053026import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053027import 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 Ramaa2f73982016-04-12 23:33:33 +053053
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053054/**
Bharat saraswald9822e92016-04-05 15:13:44 +053055 * Represents ascending range restriction information.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053056 *
57 * @param <T> range type (data type)
58 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053059public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
Vidyashree Rama1db15562016-05-17 16:16:15 +053060 implements YangDesc, YangReference, YangAppErrorInfo, Parsable {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053061
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 saraswald9822e92016-04-05 15:13:44 +053089 * Creates YANG range restriction object.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053090 */
91 public YangRangeRestriction() {
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Returns the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053096 *
Bharat saraswald9822e92016-04-05 15:13:44 +053097 * @return list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053098 */
99 public List<YangRangeInterval<T>> getAscendingRangeIntervals() {
100 return ascendingRangeIntervals;
101 }
102
103 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 * Sets the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530105 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530106 * @param rangeList list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530107 */
108 private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) {
109 ascendingRangeIntervals = rangeList;
110 }
111
112 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530113 * Returns the minimum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530114 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530115 * @return minimum restricted value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530116 * @throws DataModelException data model exception for minimum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 */
118 public T getMinRestrictedvalue() throws DataModelException {
119 if (getAscendingRangeIntervals() == null) {
120 throw new DataModelException("No range restriction info");
121 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530122 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530123 throw new DataModelException("No range interval info");
124 }
125 return getAscendingRangeIntervals().get(0).getStartValue();
126 }
127
128 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530129 * Returns the maximum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530130 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530131 * @return minimum maximum value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530132 * @throws DataModelException data model exception for maximum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530133 */
134 public T getMaxRestrictedvalue() throws DataModelException {
135 if (getAscendingRangeIntervals() == null) {
136 throw new DataModelException("No range restriction info");
137 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530138 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530139 throw new DataModelException("No range interval info");
140 }
141 return getAscendingRangeIntervals()
142 .get(getAscendingRangeIntervals().size() - 1).getEndValue();
143 }
144
145 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530146 * Adds new interval to extend its range in the last. i.e. newly added
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530147 * interval needs to be bigger than the biggest interval in the list.
148 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530149 * @param newInterval restricted length interval
150 * @throws DataModelException data model exception for range restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530151 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530152 public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530153
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 Agrawalcfa1c412016-05-03 00:41:48 +0530179 * Validates if the given value is correct as per the restriction.
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530180 *
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 Rama7142d9c2016-04-26 15:06:06 +0530188 || getAscendingRangeIntervals().isEmpty()) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530189 // 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 Agrawalcfa1c412016-05-03 00:41:48 +0530210 * 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 saraswald9822e92016-04-05 15:13:44 +0530236 * Returns the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530237 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530238 * @return textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530239 */
240 @Override
241 public String getReference() {
242 return reference;
243 }
244
245 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * Sets the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530247 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530248 * @param ref textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530249 */
250 @Override
251 public void setReference(String ref) {
252 reference = ref;
253 }
254
255 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530256 * Returns the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530257 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530258 * @return description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530259 */
260 @Override
261 public String getDescription() {
262 return description;
263 }
264
265 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530266 * Sets the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530267 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530268 * @param desc description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530269 */
270 @Override
271 public void setDescription(String desc) {
272 description = desc;
273
274 }
275
276 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530277 * Returns application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530278 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530279 * @return Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530280 */
281 @Override
282 public String getGetErrorMessage() {
283 return errorMessage;
284 }
285
286 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530287 * Sets Application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530288 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530289 * @param errMsg Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530290 */
291 @Override
292 public void setErrorMessage(String errMsg) {
293 errorMessage = errMsg;
294
295 }
296
297 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530298 * Returns application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530299 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530300 * @return application's error tag, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530301 */
302 @Override
303 public String getGetErrorAppTag() {
304 return errorAppTag;
305 }
306
307 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530308 * Sets application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530309 *
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 Rama1db15562016-05-17 16:16:15 +0530316
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 S0c330cd2016-02-23 22:36:57 +0530331}