blob: 29322e604fd07de71e3a52c720a5c5980bc11889 [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>>
rama-huawei6c728a92016-07-11 14:48:12 +053062 implements YangDesc, YangReference, Parsable, Serializable, YangAppErrorHolder {
Bharat saraswal96dfef02016-06-16 00:29:12 +053063
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 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053078 * Textual description.
79 */
80 private String description;
81
82 /**
rama-huawei6c728a92016-07-11 14:48:12 +053083 * YANG application error information.
84 */
85 private YangAppErrorInfo yangAppErrorInfo;
86
87 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053088 * Creates YANG range restriction object.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053089 */
90 public YangRangeRestriction() {
91 }
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Returns the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053095 *
Bharat saraswald9822e92016-04-05 15:13:44 +053096 * @return list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053097 */
98 public List<YangRangeInterval<T>> getAscendingRangeIntervals() {
99 return ascendingRangeIntervals;
100 }
101
102 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530103 * Sets the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530104 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * @param rangeList list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530106 */
107 private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) {
108 ascendingRangeIntervals = rangeList;
109 }
110
111 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530112 * Returns the minimum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530113 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530114 * @return minimum restricted value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530115 * @throws DataModelException data model exception for minimum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530116 */
117 public T getMinRestrictedvalue() throws DataModelException {
118 if (getAscendingRangeIntervals() == null) {
119 throw new DataModelException("No range restriction info");
120 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530121 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530122 throw new DataModelException("No range interval info");
123 }
124 return getAscendingRangeIntervals().get(0).getStartValue();
125 }
126
127 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530128 * Returns the maximum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530129 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530130 * @return minimum maximum value
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530131 * @throws DataModelException data model exception for maximum restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530132 */
133 public T getMaxRestrictedvalue() throws DataModelException {
134 if (getAscendingRangeIntervals() == null) {
135 throw new DataModelException("No range restriction info");
136 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530137 if (getAscendingRangeIntervals().isEmpty()) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530138 throw new DataModelException("No range interval info");
139 }
140 return getAscendingRangeIntervals()
141 .get(getAscendingRangeIntervals().size() - 1).getEndValue();
142 }
143
144 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530145 * Adds new interval to extend its range in the last. i.e. newly added
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530146 * interval needs to be bigger than the biggest interval in the list.
147 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530148 * @param newInterval restricted length interval
149 * @throws DataModelException data model exception for range restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530150 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530151 public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530152
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
Mahesh Poojary S98675a92016-07-27 15:36:42 +0530168 if (newInterval.getStartValue().compareTo(curMaxvalue) < 1) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530169 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 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530178 * Validates if the given value is correct as per the restriction.
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530179 *
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
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530187 || getAscendingRangeIntervals().isEmpty()) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530188 // Throw exception, At least one default range needs to be set in
189 // constructor or in linker.
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530190 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 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530205 return false;
206 }
207
208 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530209 * Validates if the given interval is correct as per the restriction.
210 *
211 * @param rangeInterval range interval
212 * @return true, if the interval is confirming to restriction, false otherwise
213 * @throws DataModelException data model error
214 */
215 public boolean isValidInterval(YangRangeInterval rangeInterval) throws DataModelException {
216
217 if (getAscendingRangeIntervals() == null
218 || getAscendingRangeIntervals().isEmpty()) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530219 // Throw exception, At least one default range needs to be set in
220 // constructor or in linker.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530221 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
Vidyashree Rama1db15562016-05-17 16:16:15 +0530276 @Override
277 public YangConstructType getYangConstructType() {
278 return YangConstructType.RANGE_DATA;
279 }
280
281 @Override
282 public void validateDataOnEntry() throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530283 // TODO: implement the method.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530284 }
285
286 @Override
287 public void validateDataOnExit() throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530288 // TODO: implement the method.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530289 }
rama-huawei6c728a92016-07-11 14:48:12 +0530290
291 @Override
292 public void setAppErrorInfo(YangAppErrorInfo yangAppErrorInfo) {
293 this.yangAppErrorInfo = yangAppErrorInfo;
294 }
295
296 @Override
297 public YangAppErrorInfo getAppErrorInfo() {
298 return yangAppErrorInfo;
299 }
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530300}