blob: b49dda6abae122233f2b1c7a92177b29feabebae [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;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053022import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
24import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangBuiltInDataTypeInfo;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053026import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053027
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053028import static org.onosproject.yangutils.datamodel.BuiltInTypeObjectFactory.getDataObjectFromString;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053029import static com.google.common.base.Preconditions.checkNotNull;
30
31/*-
32 * Reference RFC 6020.
33 *
34 * The range Statement
35 *
36 * The "range" statement, which is an optional sub-statement to the
37 * "type" statement, takes as an argument a range expression string. It
38 * is used to restrict integer and decimal built-in types, or types
39 * derived from those.
40 *
41 * A range consists of an explicit value, or a lower-inclusive bound,
42 * two consecutive dots "..", and an upper-inclusive bound. Multiple
43 * values or ranges can be given, separated by "|". If multiple values
44 * or ranges are given, they all MUST be disjoint and MUST be in
45 * ascending order. If a range restriction is applied to an already
46 * range-restricted type, the new restriction MUST be equal or more
47 * limiting, that is raising the lower bounds, reducing the upper
48 * bounds, removing explicit values or ranges, or splitting ranges into
49 * multiple ranges with intermediate gaps. Each explicit value and
50 * range boundary value given in the range expression MUST match the
51 * type being restricted, or be one of the special values "min" or
52 * "max". "min" and "max" mean the minimum and maximum value accepted
53 * for the type being restricted, respectively.
54 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053055
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053056/**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Represents ascending range restriction information.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053058 *
59 * @param <T> range type (data type)
60 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053061public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
Bharat saraswal96dfef02016-06-16 00:29:12 +053062 implements YangDesc, YangReference, YangAppErrorInfo, Parsable, Serializable {
63
64 private static final long serialVersionUID = 8062016051L;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053065
66 /**
67 * Ascending list of range interval restriction. If the restriction is a
68 * single value, the start and end length of the range is same.
69 */
70 private List<YangRangeInterval<T>> ascendingRangeIntervals;
71
72 /**
73 * Textual reference.
74 */
75 private String reference;
76
77 /**
78 * Application's error message, to be used for data error.
79 */
80 private String errorMessage;
81
82 /**
83 * Application's error tag, to be filled in data validation error response.
84 */
85 private String errorAppTag;
86
87 /**
88 * Textual description.
89 */
90 private String description;
91
92 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053093 * Creates YANG range restriction object.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053094 */
95 public YangRangeRestriction() {
96 }
97
98 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053099 * Returns the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530100 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530101 * @return list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530102 */
103 public List<YangRangeInterval<T>> getAscendingRangeIntervals() {
104 return ascendingRangeIntervals;
105 }
106
107 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530108 * Sets the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530109 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530110 * @param rangeList list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530111 */
112 private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) {
113 ascendingRangeIntervals = rangeList;
114 }
115
116 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530117 * Returns the minimum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530118 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530119 * @return minimum restricted value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530120 * @throws DataModelException data model exception for minimum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530121 */
122 public T getMinRestrictedvalue() throws DataModelException {
123 if (getAscendingRangeIntervals() == null) {
124 throw new DataModelException("No range restriction info");
125 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530126 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530127 throw new DataModelException("No range interval info");
128 }
129 return getAscendingRangeIntervals().get(0).getStartValue();
130 }
131
132 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530133 * Returns the maximum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530134 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530135 * @return minimum maximum value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530136 * @throws DataModelException data model exception for maximum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530137 */
138 public T getMaxRestrictedvalue() throws DataModelException {
139 if (getAscendingRangeIntervals() == null) {
140 throw new DataModelException("No range restriction info");
141 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530142 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530143 throw new DataModelException("No range interval info");
144 }
145 return getAscendingRangeIntervals()
146 .get(getAscendingRangeIntervals().size() - 1).getEndValue();
147 }
148
149 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530150 * Adds new interval to extend its range in the last. i.e. newly added
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530151 * interval needs to be bigger than the biggest interval in the list.
152 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530153 * @param newInterval restricted length interval
154 * @throws DataModelException data model exception for range restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530155 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530156 public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530157
158 checkNotNull(newInterval);
159 checkNotNull(newInterval.getStartValue());
160
161 if (getAscendingRangeIntervals() == null) {
162 /*
163 * First interval that is being added, and it must be the smallest
164 * interval.
165 */
166 setAscendingRangeIntervals(new LinkedList<YangRangeInterval<T>>());
167 getAscendingRangeIntervals().add(newInterval);
168 return;
169 }
170
171 T curMaxvalue = getMaxRestrictedvalue();
172
173 if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) {
174 throw new DataModelException(
175 "New added range interval is lesser than the old interval(s)");
176 }
177
178 getAscendingRangeIntervals()
179 .add(getAscendingRangeIntervals().size(), newInterval);
180 }
181
182 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530183 * Validates if the given value is correct as per the restriction.
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530184 *
185 * @param valueInString value
186 * @return true, if the value is confirming to restriction, false otherwise
187 * @throws DataModelException data model error
188 */
189 public boolean isValidValueString(String valueInString) throws DataModelException {
190
191 if (getAscendingRangeIntervals() == null
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530192 || getAscendingRangeIntervals().isEmpty()) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530193 // Throw exception, At least one default range needs to be set in
194 // constructor or in linker.
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530195 throw new DataModelException("Range interval missing in range restriction.");
196
197 }
198
199 YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType();
200 YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type);
201
202 for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) {
203 int rangeStartCompareRes = interval.getStartValue().compareTo((T) value);
204 int rangeEndCompareRes = interval.getEndValue().compareTo((T) value);
205
206 if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) {
207 return true;
208 }
209 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530210 return false;
211 }
212
213 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530214 * Validates if the given interval is correct as per the restriction.
215 *
216 * @param rangeInterval range interval
217 * @return true, if the interval is confirming to restriction, false otherwise
218 * @throws DataModelException data model error
219 */
220 public boolean isValidInterval(YangRangeInterval rangeInterval) throws DataModelException {
221
222 if (getAscendingRangeIntervals() == null
223 || getAscendingRangeIntervals().isEmpty()) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530224 // Throw exception, At least one default range needs to be set in
225 // constructor or in linker.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530226 throw new DataModelException("Range interval missing in range restriction.");
227 }
228
229 for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) {
230 int rangeStartCompareRes = interval.getStartValue().compareTo((T) rangeInterval.getStartValue());
231 int rangeEndCompareRes = interval.getEndValue().compareTo((T) rangeInterval.getEndValue());
232
233 if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) {
234 return true;
235 }
236 }
237 throw new DataModelException("Range interval doesn't fall within the referred restriction ranges");
238 }
239
240 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530241 * Returns the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530242 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530243 * @return textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530244 */
245 @Override
246 public String getReference() {
247 return reference;
248 }
249
250 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530251 * Sets the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530252 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530253 * @param ref textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530254 */
255 @Override
256 public void setReference(String ref) {
257 reference = ref;
258 }
259
260 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530261 * Returns the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530262 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530263 * @return description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530264 */
265 @Override
266 public String getDescription() {
267 return description;
268 }
269
270 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530271 * Sets the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530272 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530273 * @param desc description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530274 */
275 @Override
276 public void setDescription(String desc) {
277 description = desc;
278
279 }
280
281 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530282 * Returns application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530283 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530284 * @return Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530285 */
286 @Override
287 public String getGetErrorMessage() {
288 return errorMessage;
289 }
290
291 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530292 * Sets Application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530293 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530294 * @param errMsg Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530295 */
296 @Override
297 public void setErrorMessage(String errMsg) {
298 errorMessage = errMsg;
299
300 }
301
302 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530303 * Returns application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530304 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530305 * @return application's error tag, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530306 */
307 @Override
308 public String getGetErrorAppTag() {
309 return errorAppTag;
310 }
311
312 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530313 * Sets application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530314 *
315 * @param errTag application's error tag, to be used for data error.
316 */
317 @Override
318 public void setErrorAppTag(String errTag) {
319 errorAppTag = errTag;
320 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530321
322 @Override
323 public YangConstructType getYangConstructType() {
324 return YangConstructType.RANGE_DATA;
325 }
326
327 @Override
328 public void validateDataOnEntry() throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530329 // TODO: implement the method.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530330 }
331
332 @Override
333 public void validateDataOnExit() throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530334 // TODO: implement the method.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530335 }
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530336}