blob: 39c2cb5d420af157e56be87731abc3d398765ac9 [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;
21
22import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053023import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053024
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053025import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053026import static com.google.common.base.Preconditions.checkNotNull;
27
28/*-
29 * Reference RFC 6020.
30 *
31 * The range Statement
32 *
33 * The "range" statement, which is an optional sub-statement to the
34 * "type" statement, takes as an argument a range expression string. It
35 * is used to restrict integer and decimal built-in types, or types
36 * derived from those.
37 *
38 * A range consists of an explicit value, or a lower-inclusive bound,
39 * two consecutive dots "..", and an upper-inclusive bound. Multiple
40 * values or ranges can be given, separated by "|". If multiple values
41 * or ranges are given, they all MUST be disjoint and MUST be in
42 * ascending order. If a range restriction is applied to an already
43 * range-restricted type, the new restriction MUST be equal or more
44 * limiting, that is raising the lower bounds, reducing the upper
45 * bounds, removing explicit values or ranges, or splitting ranges into
46 * multiple ranges with intermediate gaps. Each explicit value and
47 * range boundary value given in the range expression MUST match the
48 * type being restricted, or be one of the special values "min" or
49 * "max". "min" and "max" mean the minimum and maximum value accepted
50 * for the type being restricted, respectively.
51 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053052
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053053/**
Bharat saraswald9822e92016-04-05 15:13:44 +053054 * Represents ascending range restriction information.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053055 *
56 * @param <T> range type (data type)
57 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053058public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
59 implements YangDesc, YangReference, YangAppErrorInfo {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053060
61 /**
62 * Ascending list of range interval restriction. If the restriction is a
63 * single value, the start and end length of the range is same.
64 */
65 private List<YangRangeInterval<T>> ascendingRangeIntervals;
66
67 /**
68 * Textual reference.
69 */
70 private String reference;
71
72 /**
73 * Application's error message, to be used for data error.
74 */
75 private String errorMessage;
76
77 /**
78 * Application's error tag, to be filled in data validation error response.
79 */
80 private String errorAppTag;
81
82 /**
83 * Textual description.
84 */
85 private String description;
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 * @throws DataModelException data model exception for minimum restriction
115 * @return minimum restricted value
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 * @throws DataModelException data model exception for maximum restriction
131 * @return minimum maximum value
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
168 if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) {
169 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 /**
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530178 * Check if the given value is correct as per the restriction.
179 *
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()) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530188 // Throw exception, At least one default range needs to be set in constructor or in linker.
189 throw new DataModelException("Range interval missing in range restriction.");
190
191 }
192
193 YangDataTypes type = getAscendingRangeIntervals().get(0).getStartValue().getYangType();
194 YangBuiltInDataTypeInfo<?> value = getDataObjectFromString(valueInString, type);
195
196 for (YangRangeInterval<T> interval : getAscendingRangeIntervals()) {
197 int rangeStartCompareRes = interval.getStartValue().compareTo((T) value);
198 int rangeEndCompareRes = interval.getEndValue().compareTo((T) value);
199
200 if (rangeStartCompareRes <= 0 && rangeEndCompareRes >= 0) {
201 return true;
202 }
203 }
204
205 return false;
206 }
207
208 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530209 * Returns the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530210 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530211 * @return textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530212 */
213 @Override
214 public String getReference() {
215 return reference;
216 }
217
218 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530219 * Sets the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530220 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530221 * @param ref textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530222 */
223 @Override
224 public void setReference(String ref) {
225 reference = ref;
226 }
227
228 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530229 * Returns the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530230 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530231 * @return description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530232 */
233 @Override
234 public String getDescription() {
235 return description;
236 }
237
238 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530239 * Sets the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530240 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530241 * @param desc description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530242 */
243 @Override
244 public void setDescription(String desc) {
245 description = desc;
246
247 }
248
249 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530250 * Returns application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530251 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530252 * @return Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530253 */
254 @Override
255 public String getGetErrorMessage() {
256 return errorMessage;
257 }
258
259 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530260 * Sets Application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530261 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530262 * @param errMsg Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530263 */
264 @Override
265 public void setErrorMessage(String errMsg) {
266 errorMessage = errMsg;
267
268 }
269
270 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530271 * Returns application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530272 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530273 * @return application's error tag, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530274 */
275 @Override
276 public String getGetErrorAppTag() {
277 return errorAppTag;
278 }
279
280 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530281 * Sets application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530282 *
283 * @param errTag application's error tag, to be used for data error.
284 */
285 @Override
286 public void setErrorAppTag(String errTag) {
287 errorAppTag = errTag;
288 }
289}