blob: abbc6c286de564843ce6df1c0ba71b04cd2c8985 [file] [log] [blame]
Vinod Kumar S0c330cd2016-02-23 22:36:57 +05301/*Copyright 2016.year Open Networking Laboratory
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.*/
14package org.onosproject.yangutils.datamodel;
15
16import java.util.LinkedList;
17import java.util.List;
18
19import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
20
21import static com.google.common.base.Preconditions.checkNotNull;
22
23/*-
24 * Reference RFC 6020.
25 *
26 * The range Statement
27 *
28 * The "range" statement, which is an optional sub-statement to the
29 * "type" statement, takes as an argument a range expression string. It
30 * is used to restrict integer and decimal built-in types, or types
31 * derived from those.
32 *
33 * A range consists of an explicit value, or a lower-inclusive bound,
34 * two consecutive dots "..", and an upper-inclusive bound. Multiple
35 * values or ranges can be given, separated by "|". If multiple values
36 * or ranges are given, they all MUST be disjoint and MUST be in
37 * ascending order. If a range restriction is applied to an already
38 * range-restricted type, the new restriction MUST be equal or more
39 * limiting, that is raising the lower bounds, reducing the upper
40 * bounds, removing explicit values or ranges, or splitting ranges into
41 * multiple ranges with intermediate gaps. Each explicit value and
42 * range boundary value given in the range expression MUST match the
43 * type being restricted, or be one of the special values "min" or
44 * "max". "min" and "max" mean the minimum and maximum value accepted
45 * for the type being restricted, respectively.
46 */
47/**
48 * Ascending range restriction information.
49 *
50 * @param <T> range type (data type)
51 */
52public class YangRangeRestriction<T extends Comparable<T>> implements YangDesc, YangReference, YangAppErrorInfo {
53
54 /**
55 * Ascending list of range interval restriction. If the restriction is a
56 * single value, the start and end length of the range is same.
57 */
58 private List<YangRangeInterval<T>> ascendingRangeIntervals;
59
60 /**
61 * Textual reference.
62 */
63 private String reference;
64
65 /**
66 * Application's error message, to be used for data error.
67 */
68 private String errorMessage;
69
70 /**
71 * Application's error tag, to be filled in data validation error response.
72 */
73 private String errorAppTag;
74
75 /**
76 * Textual description.
77 */
78 private String description;
79
80 /**
81 * Default constructor.
82 */
83 public YangRangeRestriction() {
84 }
85
86 /**
87 * Get the list of range interval restriction in ascending order.
88 *
89 * @return list of range interval restriction in ascending order.
90 */
91 public List<YangRangeInterval<T>> getAscendingRangeIntervals() {
92 return ascendingRangeIntervals;
93 }
94
95 /**
96 * Set the list of range interval restriction in ascending order.
97 *
98 * @param rangeList list of range interval restriction in ascending order.
99 */
100 private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) {
101 ascendingRangeIntervals = rangeList;
102 }
103
104 /**
105 * Get the minimum valid value as per the restriction.
106 *
107 * @throws DataModelException data model exception for minimum restriction.
108 *
109 * @return minimum restricted value.
110 */
111 public T getMinRestrictedvalue() throws DataModelException {
112 if (getAscendingRangeIntervals() == null) {
113 throw new DataModelException("No range restriction info");
114 }
115 if (getAscendingRangeIntervals().size() == 0) {
116 throw new DataModelException("No range interval info");
117 }
118 return getAscendingRangeIntervals().get(0).getStartValue();
119 }
120
121 /**
122 * Get the maximum valid value as per the restriction.
123 *
124 * @throws DataModelException data model exception for maximum restriction.
125 *
126 * @return minimum maximum value.
127 */
128 public T getMaxRestrictedvalue() throws DataModelException {
129 if (getAscendingRangeIntervals() == null) {
130 throw new DataModelException("No range restriction info");
131 }
132 if (getAscendingRangeIntervals().size() == 0) {
133 throw new DataModelException("No range interval info");
134 }
135 return getAscendingRangeIntervals()
136 .get(getAscendingRangeIntervals().size() - 1).getEndValue();
137 }
138
139 /**
140 * Add new interval to extend its range in the last. i.e. newly added
141 * interval needs to be bigger than the biggest interval in the list.
142 *
143 * @param newInterval restricted length interval.
144 * @throws DataModelException data model exception for range restriction.
145 */
146 public void addLenghtRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException {
147
148 checkNotNull(newInterval);
149 checkNotNull(newInterval.getStartValue());
150
151 if (getAscendingRangeIntervals() == null) {
152 /*
153 * First interval that is being added, and it must be the smallest
154 * interval.
155 */
156 setAscendingRangeIntervals(new LinkedList<YangRangeInterval<T>>());
157 getAscendingRangeIntervals().add(newInterval);
158 return;
159 }
160
161 T curMaxvalue = getMaxRestrictedvalue();
162
163 if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) {
164 throw new DataModelException(
165 "New added range interval is lesser than the old interval(s)");
166 }
167
168 getAscendingRangeIntervals()
169 .add(getAscendingRangeIntervals().size(), newInterval);
170 }
171
172 /**
173 * Get the textual reference of the length restriction.
174 *
175 * @return textual reference of the length restriction.
176 */
177 @Override
178 public String getReference() {
179 return reference;
180 }
181
182 /**
183 * Set the textual reference of the length restriction.
184 *
185 * @param ref textual reference of the length restriction.
186 */
187 @Override
188 public void setReference(String ref) {
189 reference = ref;
190 }
191
192 /**
193 * Get the description of the length restriction.
194 *
195 * @return description of the length restriction.
196 */
197 @Override
198 public String getDescription() {
199 return description;
200 }
201
202 /**
203 * Set the description of the length restriction.
204 *
205 * @param desc description of the length restriction.
206 */
207 @Override
208 public void setDescription(String desc) {
209 description = desc;
210
211 }
212
213 /**
214 * Get application's error message, to be used for data error.
215 *
216 * @return Application's error message, to be used for data error.
217 */
218 @Override
219 public String getGetErrorMessage() {
220 return errorMessage;
221 }
222
223 /**
224 * Set Application's error message, to be used for data error.
225 *
226 * @param errMsg Application's error message, to be used for data error.
227 */
228 @Override
229 public void setErrorMessage(String errMsg) {
230 errorMessage = errMsg;
231
232 }
233
234 /**
235 * Get application's error tag, to be used for data error.
236 *
237 * @return application's error tag, to be used for data error.
238 */
239 @Override
240 public String getGetErrorAppTag() {
241 return errorAppTag;
242 }
243
244 /**
245 * Set application's error tag, to be used for data error.
246 *
247 * @param errTag application's error tag, to be used for data error.
248 */
249 @Override
250 public void setErrorAppTag(String errTag) {
251 errorAppTag = errTag;
252 }
253}