blob: 654ac93fd26081a49ed2345d7aaa2ff4e73df7f1 [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
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053020import java.util.LinkedList;
21import java.util.List;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053023import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053024import org.onosproject.yangutils.datamodel.utils.Parsable;
25import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053026import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053027
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053028import static org.onosproject.yangutils.datamodel.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
Bharat saraswal96dfef02016-06-16 00:29:12 +053029
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053030import static com.google.common.base.Preconditions.checkNotNull;
31
32/*-
33 * Reference RFC 6020.
34 *
35 * The range Statement
36 *
37 * The "range" statement, which is an optional sub-statement to the
38 * "type" statement, takes as an argument a range expression string. It
39 * is used to restrict integer and decimal built-in types, or types
40 * derived from those.
41 *
42 * A range consists of an explicit value, or a lower-inclusive bound,
43 * two consecutive dots "..", and an upper-inclusive bound. Multiple
44 * values or ranges can be given, separated by "|". If multiple values
45 * or ranges are given, they all MUST be disjoint and MUST be in
46 * ascending order. If a range restriction is applied to an already
47 * range-restricted type, the new restriction MUST be equal or more
48 * limiting, that is raising the lower bounds, reducing the upper
49 * bounds, removing explicit values or ranges, or splitting ranges into
50 * multiple ranges with intermediate gaps. Each explicit value and
51 * range boundary value given in the range expression MUST match the
52 * type being restricted, or be one of the special values "min" or
53 * "max". "min" and "max" mean the minimum and maximum value accepted
54 * for the type being restricted, respectively.
55 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053056
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053057/**
Bharat saraswald9822e92016-04-05 15:13:44 +053058 * Represents ascending range restriction information.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053059 *
60 * @param <T> range type (data type)
61 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053062public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
Bharat saraswal96dfef02016-06-16 00:29:12 +053063 implements YangDesc, YangReference, YangAppErrorInfo, Parsable, Serializable {
64
65 private static final long serialVersionUID = 8062016051L;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053066
67 /**
68 * Ascending list of range interval restriction. If the restriction is a
69 * single value, the start and end length of the range is same.
70 */
71 private List<YangRangeInterval<T>> ascendingRangeIntervals;
72
73 /**
74 * Textual reference.
75 */
76 private String reference;
77
78 /**
79 * Application's error message, to be used for data error.
80 */
81 private String errorMessage;
82
83 /**
84 * Application's error tag, to be filled in data validation error response.
85 */
86 private String errorAppTag;
87
88 /**
89 * Textual description.
90 */
91 private String description;
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Creates YANG range restriction object.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053095 */
96 public YangRangeRestriction() {
97 }
98
99 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530100 * Returns the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530101 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530102 * @return list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530103 */
104 public List<YangRangeInterval<T>> getAscendingRangeIntervals() {
105 return ascendingRangeIntervals;
106 }
107
108 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Sets the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530110 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530111 * @param rangeList list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530112 */
113 private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) {
114 ascendingRangeIntervals = rangeList;
115 }
116
117 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530118 * Returns the minimum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530119 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530120 * @return minimum restricted value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530121 * @throws DataModelException data model exception for minimum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530122 */
123 public T getMinRestrictedvalue() throws DataModelException {
124 if (getAscendingRangeIntervals() == null) {
125 throw new DataModelException("No range restriction info");
126 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530127 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530128 throw new DataModelException("No range interval info");
129 }
130 return getAscendingRangeIntervals().get(0).getStartValue();
131 }
132
133 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530134 * Returns the maximum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530135 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530136 * @return minimum maximum value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530137 * @throws DataModelException data model exception for maximum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530138 */
139 public T getMaxRestrictedvalue() throws DataModelException {
140 if (getAscendingRangeIntervals() == null) {
141 throw new DataModelException("No range restriction info");
142 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530143 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530144 throw new DataModelException("No range interval info");
145 }
146 return getAscendingRangeIntervals()
147 .get(getAscendingRangeIntervals().size() - 1).getEndValue();
148 }
149
150 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Adds new interval to extend its range in the last. i.e. newly added
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530152 * interval needs to be bigger than the biggest interval in the list.
153 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530154 * @param newInterval restricted length interval
155 * @throws DataModelException data model exception for range restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530156 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530157 public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530158
159 checkNotNull(newInterval);
160 checkNotNull(newInterval.getStartValue());
161
162 if (getAscendingRangeIntervals() == null) {
163 /*
164 * First interval that is being added, and it must be the smallest
165 * interval.
166 */
167 setAscendingRangeIntervals(new LinkedList<YangRangeInterval<T>>());
168 getAscendingRangeIntervals().add(newInterval);
169 return;
170 }
171
172 T curMaxvalue = getMaxRestrictedvalue();
173
174 if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) {
175 throw new DataModelException(
176 "New added range interval is lesser than the old interval(s)");
177 }
178
179 getAscendingRangeIntervals()
180 .add(getAscendingRangeIntervals().size(), newInterval);
181 }
182
183 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530184 * Validates if the given value is correct as per the restriction.
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530185 *
186 * @param valueInString value
187 * @return true, if the value is confirming to restriction, false otherwise
188 * @throws DataModelException data model error
189 */
190 public boolean isValidValueString(String valueInString) throws DataModelException {
191
192 if (getAscendingRangeIntervals() == null
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530193 || getAscendingRangeIntervals().isEmpty()) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530194 // Throw exception, At least one default range needs to be set in
195 // constructor or in linker.
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530196 throw new DataModelException("Range interval missing in range restriction.");
197
198 }
199
200 YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType();
201 YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type);
202
203 for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) {
204 int rangeStartCompareRes = interval.getStartValue().compareTo((T) value);
205 int rangeEndCompareRes = interval.getEndValue().compareTo((T) value);
206
207 if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) {
208 return true;
209 }
210 }
211
212 return false;
213 }
214
215 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530216 * Validates if the given interval is correct as per the restriction.
217 *
218 * @param rangeInterval range interval
219 * @return true, if the interval is confirming to restriction, false otherwise
220 * @throws DataModelException data model error
221 */
222 public boolean isValidInterval(YangRangeInterval rangeInterval) throws DataModelException {
223
224 if (getAscendingRangeIntervals() == null
225 || getAscendingRangeIntervals().isEmpty()) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530226 // Throw exception, At least one default range needs to be set in
227 // constructor or in linker.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530228 throw new DataModelException("Range interval missing in range restriction.");
229 }
230
231 for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) {
232 int rangeStartCompareRes = interval.getStartValue().compareTo((T) rangeInterval.getStartValue());
233 int rangeEndCompareRes = interval.getEndValue().compareTo((T) rangeInterval.getEndValue());
234
235 if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) {
236 return true;
237 }
238 }
239 throw new DataModelException("Range interval doesn't fall within the referred restriction ranges");
240 }
241
242 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530243 * Returns the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530244 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530245 * @return textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530246 */
247 @Override
248 public String getReference() {
249 return reference;
250 }
251
252 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530253 * Sets the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530254 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530255 * @param ref textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530256 */
257 @Override
258 public void setReference(String ref) {
259 reference = ref;
260 }
261
262 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530263 * Returns the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530264 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530265 * @return description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530266 */
267 @Override
268 public String getDescription() {
269 return description;
270 }
271
272 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530273 * Sets the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530274 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530275 * @param desc description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530276 */
277 @Override
278 public void setDescription(String desc) {
279 description = desc;
280
281 }
282
283 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530284 * Returns application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530285 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530286 * @return Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530287 */
288 @Override
289 public String getGetErrorMessage() {
290 return errorMessage;
291 }
292
293 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530294 * Sets Application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530295 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530296 * @param errMsg Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530297 */
298 @Override
299 public void setErrorMessage(String errMsg) {
300 errorMessage = errMsg;
301
302 }
303
304 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530305 * Returns application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530306 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530307 * @return application's error tag, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530308 */
309 @Override
310 public String getGetErrorAppTag() {
311 return errorAppTag;
312 }
313
314 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530315 * Sets application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530316 *
317 * @param errTag application's error tag, to be used for data error.
318 */
319 @Override
320 public void setErrorAppTag(String errTag) {
321 errorAppTag = errTag;
322 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530323
324 @Override
325 public YangConstructType getYangConstructType() {
326 return YangConstructType.RANGE_DATA;
327 }
328
329 @Override
330 public void validateDataOnEntry() throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530331 // TODO: implement the method.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530332 }
333
334 @Override
335 public void validateDataOnExit() throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530336 // TODO: implement the method.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530337 }
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530338}