blob: 09eb79637b9fedc5e9af4d7a1d5966aa14d2831b [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;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/*-
27 * Reference RFC 6020.
28 *
29 * The range Statement
30 *
31 * The "range" statement, which is an optional sub-statement to the
32 * "type" statement, takes as an argument a range expression string. It
33 * is used to restrict integer and decimal built-in types, or types
34 * derived from those.
35 *
36 * A range consists of an explicit value, or a lower-inclusive bound,
37 * two consecutive dots "..", and an upper-inclusive bound. Multiple
38 * values or ranges can be given, separated by "|". If multiple values
39 * or ranges are given, they all MUST be disjoint and MUST be in
40 * ascending order. If a range restriction is applied to an already
41 * range-restricted type, the new restriction MUST be equal or more
42 * limiting, that is raising the lower bounds, reducing the upper
43 * bounds, removing explicit values or ranges, or splitting ranges into
44 * multiple ranges with intermediate gaps. Each explicit value and
45 * range boundary value given in the range expression MUST match the
46 * type being restricted, or be one of the special values "min" or
47 * "max". "min" and "max" mean the minimum and maximum value accepted
48 * for the type being restricted, respectively.
49 */
50/**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Represents ascending range restriction information.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053052 *
53 * @param <T> range type (data type)
54 */
55public class YangRangeRestriction<T extends Comparable<T>> implements YangDesc, YangReference, YangAppErrorInfo {
56
57 /**
58 * Ascending list of range interval restriction. If the restriction is a
59 * single value, the start and end length of the range is same.
60 */
61 private List<YangRangeInterval<T>> ascendingRangeIntervals;
62
63 /**
64 * Textual reference.
65 */
66 private String reference;
67
68 /**
69 * Application's error message, to be used for data error.
70 */
71 private String errorMessage;
72
73 /**
74 * Application's error tag, to be filled in data validation error response.
75 */
76 private String errorAppTag;
77
78 /**
79 * Textual description.
80 */
81 private String description;
82
83 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053084 * Creates YANG range restriction object.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053085 */
86 public YangRangeRestriction() {
87 }
88
89 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053090 * Returns the list of range interval restriction in ascending order.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053091 *
Bharat saraswald9822e92016-04-05 15:13:44 +053092 * @return list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053093 */
94 public List<YangRangeInterval<T>> getAscendingRangeIntervals() {
95 return ascendingRangeIntervals;
96 }
97
98 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053099 * Sets 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 * @param rangeList list of range interval restriction in ascending order
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530102 */
103 private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) {
104 ascendingRangeIntervals = rangeList;
105 }
106
107 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530108 * Returns the minimum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530109 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530110 * @throws DataModelException data model exception for minimum restriction
111 * @return minimum restricted value
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530112 */
113 public T getMinRestrictedvalue() throws DataModelException {
114 if (getAscendingRangeIntervals() == null) {
115 throw new DataModelException("No range restriction info");
116 }
117 if (getAscendingRangeIntervals().size() == 0) {
118 throw new DataModelException("No range interval info");
119 }
120 return getAscendingRangeIntervals().get(0).getStartValue();
121 }
122
123 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530124 * Returns the maximum valid value as per the restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530125 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530126 * @throws DataModelException data model exception for maximum restriction
127 * @return minimum maximum value
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530128 */
129 public T getMaxRestrictedvalue() throws DataModelException {
130 if (getAscendingRangeIntervals() == null) {
131 throw new DataModelException("No range restriction info");
132 }
133 if (getAscendingRangeIntervals().size() == 0) {
134 throw new DataModelException("No range interval info");
135 }
136 return getAscendingRangeIntervals()
137 .get(getAscendingRangeIntervals().size() - 1).getEndValue();
138 }
139
140 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530141 * Adds new interval to extend its range in the last. i.e. newly added
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530142 * interval needs to be bigger than the biggest interval in the list.
143 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530144 * @param newInterval restricted length interval
145 * @throws DataModelException data model exception for range restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530146 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530147 public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530148
149 checkNotNull(newInterval);
150 checkNotNull(newInterval.getStartValue());
151
152 if (getAscendingRangeIntervals() == null) {
153 /*
154 * First interval that is being added, and it must be the smallest
155 * interval.
156 */
157 setAscendingRangeIntervals(new LinkedList<YangRangeInterval<T>>());
158 getAscendingRangeIntervals().add(newInterval);
159 return;
160 }
161
162 T curMaxvalue = getMaxRestrictedvalue();
163
164 if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) {
165 throw new DataModelException(
166 "New added range interval is lesser than the old interval(s)");
167 }
168
169 getAscendingRangeIntervals()
170 .add(getAscendingRangeIntervals().size(), newInterval);
171 }
172
173 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530174 * Returns the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530175 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530176 * @return textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530177 */
178 @Override
179 public String getReference() {
180 return reference;
181 }
182
183 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530184 * Sets the textual reference of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530185 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530186 * @param ref textual reference of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530187 */
188 @Override
189 public void setReference(String ref) {
190 reference = ref;
191 }
192
193 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530194 * Returns the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530195 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530196 * @return description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530197 */
198 @Override
199 public String getDescription() {
200 return description;
201 }
202
203 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530204 * Sets the description of the length restriction.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530205 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530206 * @param desc description of the length restriction
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530207 */
208 @Override
209 public void setDescription(String desc) {
210 description = desc;
211
212 }
213
214 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530215 * Returns application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530216 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530217 * @return Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530218 */
219 @Override
220 public String getGetErrorMessage() {
221 return errorMessage;
222 }
223
224 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530225 * Sets Application's error message, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530226 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530227 * @param errMsg Application's error message, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530228 */
229 @Override
230 public void setErrorMessage(String errMsg) {
231 errorMessage = errMsg;
232
233 }
234
235 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530236 * Returns application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530237 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530238 * @return application's error tag, to be used for data error
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530239 */
240 @Override
241 public String getGetErrorAppTag() {
242 return errorAppTag;
243 }
244
245 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * Sets application's error tag, to be used for data error.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530247 *
248 * @param errTag application's error tag, to be used for data error.
249 */
250 @Override
251 public void setErrorAppTag(String errTag) {
252 errorAppTag = errTag;
253 }
254}