Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 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 | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 16 | |
Vinod Kumar S | 0c330cd | 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 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 51 | * Represents ascending range restriction information. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 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 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 84 | * Creates YANG range restriction object. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 85 | */ |
| 86 | public YangRangeRestriction() { |
| 87 | } |
| 88 | |
| 89 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 90 | * Returns the list of range interval restriction in ascending order. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 91 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 92 | * @return list of range interval restriction in ascending order |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 93 | */ |
| 94 | public List<YangRangeInterval<T>> getAscendingRangeIntervals() { |
| 95 | return ascendingRangeIntervals; |
| 96 | } |
| 97 | |
| 98 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 99 | * Sets the list of range interval restriction in ascending order. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 100 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 101 | * @param rangeList list of range interval restriction in ascending order |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 102 | */ |
| 103 | private void setAscendingRangeIntervals(List<YangRangeInterval<T>> rangeList) { |
| 104 | ascendingRangeIntervals = rangeList; |
| 105 | } |
| 106 | |
| 107 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 108 | * Returns the minimum valid value as per the restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 109 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 110 | * @throws DataModelException data model exception for minimum restriction |
| 111 | * @return minimum restricted value |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 112 | */ |
| 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 saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 124 | * Returns the maximum valid value as per the restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 125 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 126 | * @throws DataModelException data model exception for maximum restriction |
| 127 | * @return minimum maximum value |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 128 | */ |
| 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 saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 141 | * Adds new interval to extend its range in the last. i.e. newly added |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 142 | * interval needs to be bigger than the biggest interval in the list. |
| 143 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 144 | * @param newInterval restricted length interval |
| 145 | * @throws DataModelException data model exception for range restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 146 | */ |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 147 | public void addRangeRestrictionInterval(YangRangeInterval<T> newInterval) throws DataModelException { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 148 | |
| 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 saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 174 | * Returns the textual reference of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 175 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 176 | * @return textual reference of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 177 | */ |
| 178 | @Override |
| 179 | public String getReference() { |
| 180 | return reference; |
| 181 | } |
| 182 | |
| 183 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 184 | * Sets the textual reference of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 185 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 186 | * @param ref textual reference of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 187 | */ |
| 188 | @Override |
| 189 | public void setReference(String ref) { |
| 190 | reference = ref; |
| 191 | } |
| 192 | |
| 193 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 194 | * Returns the description of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 195 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 196 | * @return description of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 197 | */ |
| 198 | @Override |
| 199 | public String getDescription() { |
| 200 | return description; |
| 201 | } |
| 202 | |
| 203 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 204 | * Sets the description of the length restriction. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 205 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 206 | * @param desc description of the length restriction |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 207 | */ |
| 208 | @Override |
| 209 | public void setDescription(String desc) { |
| 210 | description = desc; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 215 | * Returns application's error message, to be used for data error. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 216 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 217 | * @return Application's error message, to be used for data error |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 218 | */ |
| 219 | @Override |
| 220 | public String getGetErrorMessage() { |
| 221 | return errorMessage; |
| 222 | } |
| 223 | |
| 224 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 225 | * Sets Application's error message, to be used for data error. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 226 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 227 | * @param errMsg Application's error message, to be used for data error |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 228 | */ |
| 229 | @Override |
| 230 | public void setErrorMessage(String errMsg) { |
| 231 | errorMessage = errMsg; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 236 | * Returns application's error tag, to be used for data error. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 237 | * |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 238 | * @return application's error tag, to be used for data error |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 239 | */ |
| 240 | @Override |
| 241 | public String getGetErrorAppTag() { |
| 242 | return errorAppTag; |
| 243 | } |
| 244 | |
| 245 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 246 | * Sets application's error tag, to be used for data error. |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 247 | * |
| 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 | } |