Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 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 S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 16 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 17 | package org.onosproject.yangutils.datamodel; |
| 18 | |
| 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 23 | |
| 24 | import 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 | /** |
| 51 | * Ascending range restriction information. |
| 52 | * |
| 53 | * @param <T> range type (data type) |
| 54 | */ |
| 55 | public 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 | /** |
| 84 | * Default constructor. |
| 85 | */ |
| 86 | public YangRangeRestriction() { |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get the list of range interval restriction in ascending order. |
| 91 | * |
| 92 | * @return list of range interval restriction in ascending order. |
| 93 | */ |
| 94 | public List<YangRangeInterval<T>> getAscendingRangeIntervals() { |
| 95 | return ascendingRangeIntervals; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Set the list of range interval restriction in ascending order. |
| 100 | * |
| 101 | * @param rangeList list of range interval restriction in ascending order. |
| 102 | */ |
| 103 | private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) { |
| 104 | ascendingRangeIntervals = rangeList; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the minimum valid value as per the restriction. |
| 109 | * |
| 110 | * @throws DataModelException data model exception for minimum restriction. |
| 111 | * |
| 112 | * @return minimum restricted value. |
| 113 | */ |
| 114 | public T getMinRestrictedvalue() throws DataModelException { |
| 115 | if (getAscendingRangeIntervals() == null) { |
| 116 | throw new DataModelException("No range restriction info"); |
| 117 | } |
| 118 | if (getAscendingRangeIntervals().size() == 0) { |
| 119 | throw new DataModelException("No range interval info"); |
| 120 | } |
| 121 | return getAscendingRangeIntervals().get(0).getStartValue(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the maximum valid value as per the restriction. |
| 126 | * |
| 127 | * @throws DataModelException data model exception for maximum restriction. |
| 128 | * |
| 129 | * @return minimum maximum value. |
| 130 | */ |
| 131 | public T getMaxRestrictedvalue() throws DataModelException { |
| 132 | if (getAscendingRangeIntervals() == null) { |
| 133 | throw new DataModelException("No range restriction info"); |
| 134 | } |
| 135 | if (getAscendingRangeIntervals().size() == 0) { |
| 136 | throw new DataModelException("No range interval info"); |
| 137 | } |
| 138 | return getAscendingRangeIntervals() |
| 139 | .get(getAscendingRangeIntervals().size() - 1).getEndValue(); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Add new interval to extend its range in the last. i.e. newly added |
| 144 | * interval needs to be bigger than the biggest interval in the list. |
| 145 | * |
| 146 | * @param newInterval restricted length interval. |
| 147 | * @throws DataModelException data model exception for range restriction. |
| 148 | */ |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 149 | public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException { |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 150 | |
| 151 | checkNotNull(newInterval); |
| 152 | checkNotNull(newInterval.getStartValue()); |
| 153 | |
| 154 | if (getAscendingRangeIntervals() == null) { |
| 155 | /* |
| 156 | * First interval that is being added, and it must be the smallest |
| 157 | * interval. |
| 158 | */ |
| 159 | setAscendingRangeIntervals(new LinkedList<YangRangeInterval<T>>()); |
| 160 | getAscendingRangeIntervals().add(newInterval); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | T curMaxvalue = getMaxRestrictedvalue(); |
| 165 | |
| 166 | if (newInterval.getStartValue().compareTo(curMaxvalue) != 1) { |
| 167 | throw new DataModelException( |
| 168 | "New added range interval is lesser than the old interval(s)"); |
| 169 | } |
| 170 | |
| 171 | getAscendingRangeIntervals() |
| 172 | .add(getAscendingRangeIntervals().size(), newInterval); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the textual reference of the length restriction. |
| 177 | * |
| 178 | * @return textual reference of the length restriction. |
| 179 | */ |
| 180 | @Override |
| 181 | public String getReference() { |
| 182 | return reference; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Set the textual reference of the length restriction. |
| 187 | * |
| 188 | * @param ref textual reference of the length restriction. |
| 189 | */ |
| 190 | @Override |
| 191 | public void setReference(String ref) { |
| 192 | reference = ref; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Get the description of the length restriction. |
| 197 | * |
| 198 | * @return description of the length restriction. |
| 199 | */ |
| 200 | @Override |
| 201 | public String getDescription() { |
| 202 | return description; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Set the description of the length restriction. |
| 207 | * |
| 208 | * @param desc description of the length restriction. |
| 209 | */ |
| 210 | @Override |
| 211 | public void setDescription(String desc) { |
| 212 | description = desc; |
| 213 | |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Get application's error message, to be used for data error. |
| 218 | * |
| 219 | * @return Application's error message, to be used for data error. |
| 220 | */ |
| 221 | @Override |
| 222 | public String getGetErrorMessage() { |
| 223 | return errorMessage; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Set Application's error message, to be used for data error. |
| 228 | * |
| 229 | * @param errMsg Application's error message, to be used for data error. |
| 230 | */ |
| 231 | @Override |
| 232 | public void setErrorMessage(String errMsg) { |
| 233 | errorMessage = errMsg; |
| 234 | |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get application's error tag, to be used for data error. |
| 239 | * |
| 240 | * @return application's error tag, to be used for data error. |
| 241 | */ |
| 242 | @Override |
| 243 | public String getGetErrorAppTag() { |
| 244 | return errorAppTag; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Set application's error tag, to be used for data error. |
| 249 | * |
| 250 | * @param errTag application's error tag, to be used for data error. |
| 251 | */ |
| 252 | @Override |
| 253 | public void setErrorAppTag(String errTag) { |
| 254 | errorAppTag = errTag; |
| 255 | } |
| 256 | } |