blob: d66f17030b5679b1fc4f1ace3d51789464ea021e [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 Ramaa2f73982016-04-12 23:33:33 +053022import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053023
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053024import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025import 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 Ramaa2f73982016-04-12 23:33:33 +053051
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053052/**
Bharat saraswald9822e92016-04-05 15:13:44 +053053 * Represents ascending range restriction information.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053054 *
55 * @param <T> range type (data type)
56 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053057public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
58 implements YangDesc, YangReference, YangAppErrorInfo {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053059
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 saraswald9822e92016-04-05 15:13:44 +053087 * Creates YANG range restriction object.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053088 */
89 public YangRangeRestriction() {
90 }
91
92 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053093 * Returns the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053094 *
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * @return list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053096 */
97 public List<YangRangeInterval<T>> getAscendingRangeIntervals() {
98 return ascendingRangeIntervals;
99 }
100
101 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530102 * Sets the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530103 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 * @param rangeList list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530105 */
106 private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) {
107 ascendingRangeIntervals = rangeList;
108 }
109
110 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530111 * Returns the minimum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530112 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530113 * @return minimum restricted value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530114 * @throws DataModelException data model exception for minimum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530115 */
116 public T getMinRestrictedvalue() throws DataModelException {
117 if (getAscendingRangeIntervals() == null) {
118 throw new DataModelException("No range restriction info");
119 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530120 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530121 throw new DataModelException("No range interval info");
122 }
123 return getAscendingRangeIntervals().get(0).getStartValue();
124 }
125
126 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530127 * Returns the maximum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530128 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530129 * @return minimum maximum value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530130 * @throws DataModelException data model exception for maximum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530131 */
132 public T getMaxRestrictedvalue() throws DataModelException {
133 if (getAscendingRangeIntervals() == null) {
134 throw new DataModelException("No range restriction info");
135 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530136 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530137 throw new DataModelException("No range interval info");
138 }
139 return getAscendingRangeIntervals()
140 .get(getAscendingRangeIntervals().size() - 1).getEndValue();
141 }
142
143 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530144 * Adds new interval to extend its range in the last. i.e. newly added
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530145 * interval needs to be bigger than the biggest interval in the list.
146 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530147 * @param newInterval restricted length interval
148 * @throws DataModelException data model exception for range restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530149 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530150 public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530151
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 Agrawalcfa1c412016-05-03 00:41:48 +0530177 * Validates if the given value is correct as per the restriction.
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530178 *
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 Rama7142d9c2016-04-26 15:06:06 +0530186 || getAscendingRangeIntervals().isEmpty()) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530187 // 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 Agrawalcfa1c412016-05-03 00:41:48 +0530208 * 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 saraswald9822e92016-04-05 15:13:44 +0530234 * Returns the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530235 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530236 * @return textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530237 */
238 @Override
239 public String getReference() {
240 return reference;
241 }
242
243 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530244 * Sets the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530245 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * @param ref textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530247 */
248 @Override
249 public void setReference(String ref) {
250 reference = ref;
251 }
252
253 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530254 * Returns the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530255 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530256 * @return description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530257 */
258 @Override
259 public String getDescription() {
260 return description;
261 }
262
263 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530264 * Sets the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530265 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530266 * @param desc description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530267 */
268 @Override
269 public void setDescription(String desc) {
270 description = desc;
271
272 }
273
274 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530275 * Returns application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530276 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530277 * @return Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530278 */
279 @Override
280 public String getGetErrorMessage() {
281 return errorMessage;
282 }
283
284 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530285 * Sets Application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530286 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530287 * @param errMsg Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530288 */
289 @Override
290 public void setErrorMessage(String errMsg) {
291 errorMessage = errMsg;
292
293 }
294
295 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530296 * Returns application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530297 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530298 * @return application's error tag, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530299 */
300 @Override
301 public String getGetErrorAppTag() {
302 return errorAppTag;
303 }
304
305 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530306 * Sets application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530307 *
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}