blob: dcd0f1d03237c52dbbbacd38eff90bc62bde14b8 [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
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053020import java.math.BigInteger;
21import java.util.ListIterator;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022
Vidyashree Rama1db15562016-05-17 16:16:15 +053023import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053024import org.onosproject.yangutils.datamodel.utils.Parsable;
25import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053026import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053027
28/*-
29 * Reference RFC 6020.
30 *
31 * A string can be restricted with the "length" and "pattern" statements.
32 *
33 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053034
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053035/**
Bharat saraswald9822e92016-04-05 15:13:44 +053036 * Represents the restriction for string data type.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053037 */
rama-huawei6c728a92016-07-11 14:48:12 +053038public class YangStringRestriction implements YangDesc, YangReference, Parsable, Serializable {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053039
40 /*-
41 * Reference RFC 6020.
42 * The length Statement
43 *
44 * The "length" statement, which is an optional sub-statement to the
45 * "type" statement, takes as an argument a length expression string.
46 * It is used to restrict the built-in type "string", or types derived
47 * from "string".
48 * A "length" statement restricts the number of unicode characters in
49 * the string.
50 * A length range consists of an explicit value, or a lower bound, two
51 * consecutive dots "..", and an upper bound. Multiple values or ranges
52 * can be given, separated by "|". Length-restricting values MUST NOT
53 * be negative. If multiple values or ranges are given, they all MUST
54 * be disjoint and MUST be in ascending order. If a length restriction
55 * is applied to an already length-restricted type, the new restriction
56 * MUST be equal or more limiting, that is, raising the lower bounds,
57 * reducing the upper bounds, removing explicit length values or ranges,
58 * or splitting ranges into multiple ranges with intermediate gaps. A
59 * length value is a non-negative integer, or one of the special values
60 * "min" or "max". "min" and "max" mean the minimum and maximum length
61 * accepted for the type being restricted, respectively. An
62 * implementation is not required to support a length value larger than
63 * 18446744073709551615.
64 * The length's sub-statements
65 *
66 * +---------------+---------+-------------+-----------------+
67 * | substatement | section | cardinality | mapped data type|
68 * +---------------+---------+-------------+-----------------+
69 * | description | 7.19.3 | 0..1 | string |
70 * | error-app-tag | 7.5.4.2 | 0..1 | string |
71 * | error-message | 7.5.4.1 | 0..1 | string |
72 * | reference | 7.19.4 | 0..1 | string |
73 * +---------------+---------+-------------+-----------------+
74 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053075
Bharat saraswal96dfef02016-06-16 00:29:12 +053076 private static final long serialVersionUID = 8062016053L;
77
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053078 /**
79 * Length restriction information.
80 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053081 private YangRangeRestriction<YangUint64> lengthRestriction;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053082
83 /**
84 * Effective pattern restriction for the type.
85 */
86 private YangPatternRestriction patternRestriction;
87
88 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +053089 * Textual reference.
90 */
91 private String reference;
92
93 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +053094 * Textual description.
95 */
96 private String description;
97
98 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053099 * Creates a YANG string restriction object.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530100 */
101 public YangStringRestriction() {
102 }
103
104 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * Returns the length restriction on the string data.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530106 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530107 * @return length restriction on the string data
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530108 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530109 public YangRangeRestriction<YangUint64> getLengthRestriction() {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530110 return lengthRestriction;
111 }
112
113 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530114 * Sets the length restriction on the string data.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530115 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530116 * @param lengthRestriction length restriction on the string data
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 */
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530118 public void setLengthRestriction(YangRangeRestriction<YangUint64> lengthRestriction) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530119 this.lengthRestriction = lengthRestriction;
120 }
121
122 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530123 * Returns the pattern restriction for the type.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530124 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530125 * @return pattern restriction for the type
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530126 */
127 public YangPatternRestriction getPatternRestriction() {
128 return patternRestriction;
129 }
130
131 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530132 * Sets the pattern restriction for the type.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530133 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530134 * @param patternRestriction pattern restriction for the type
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530135 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530136 public void setPatternRestriction(YangPatternRestriction patternRestriction) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530137 this.patternRestriction = patternRestriction;
138 }
Vinod Kumar S71cba682016-02-25 15:52:16 +0530139
140 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530141 * Adds a new pattern restriction for the type.
Vinod Kumar S71cba682016-02-25 15:52:16 +0530142 *
Bharat saraswald9822e92016-04-05 15:13:44 +0530143 * @param newPattern new pattern restriction for the type
Vinod Kumar S71cba682016-02-25 15:52:16 +0530144 */
145 public void addPattern(String newPattern) {
146 if (getPatternRestriction() == null) {
147 setPatternRestriction(new YangPatternRestriction());
148 }
149 getPatternRestriction().addPattern(newPattern);
150 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530151
152 /**
153 * Returns the textual reference of the string restriction.
154 *
155 * @return textual reference of the string restriction
156 */
157 @Override
158 public String getReference() {
159 return reference;
160 }
161
162 /**
163 * Sets the textual reference of the string restriction.
164 *
165 * @param ref textual reference of the string restriction
166 */
167 @Override
168 public void setReference(String ref) {
169 reference = ref;
170 }
171
172 /**
173 * Returns the description of the string restriction.
174 *
175 * @return description of the string restriction
176 */
177 @Override
178 public String getDescription() {
179 return description;
180 }
181
182 /**
183 * Sets the description of the string restriction.
184 *
185 * @param desc description of the string restriction
186 */
187 @Override
188 public void setDescription(String desc) {
189 description = desc;
190
191 }
192
Vidyashree Rama1db15562016-05-17 16:16:15 +0530193 @Override
194 public YangConstructType getYangConstructType() {
195 return YangConstructType.PATTERN_DATA;
196 }
197
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530198 /**
199 * Validates if the given value is correct as per the length restriction.
200 *
201 * @param valueInString value
202 * @return true, if the value is confirming to length restriction, false otherwise
203 */
204 public boolean isValidStringOnLengthRestriction(String valueInString) {
205 if (lengthRestriction == null || lengthRestriction.getAscendingRangeIntervals() == null
206 || lengthRestriction.getAscendingRangeIntervals().isEmpty()) {
207 // Length restriction is optional
208 return true;
209 }
210
211 ListIterator<YangRangeInterval<YangUint64>> rangeListIterator = lengthRestriction.getAscendingRangeIntervals()
212 .listIterator();
213 boolean isMatched = false;
214 while (rangeListIterator.hasNext()) {
215 YangRangeInterval rangeInterval = rangeListIterator.next();
216 BigInteger startValue = ((YangUint64) rangeInterval.getStartValue()).getValue();
217 BigInteger endValue = ((YangUint64) rangeInterval.getEndValue()).getValue();
218 if ((valueInString.length() >= startValue.intValue()) &&
219 (valueInString.length() <= endValue.intValue())) {
220 isMatched = true;
221 break;
222 }
223 }
224
225 return isMatched;
226 }
227
228 /**
229 * Validates if the given value is correct as per the pattern restriction.
230 *
231 * @param valueInString value
232 * @return true, if the value is confirming to pattern restriction, false otherwise
233 */
234 public boolean isValidStringOnPatternRestriction(String valueInString) {
235 if (patternRestriction == null
236 || patternRestriction.getPatternList().isEmpty()) {
237 // Pattern restriction is optional
238 return true;
239 }
240
241 ListIterator<String> patternListIterator = patternRestriction.getPatternList().listIterator();
242 boolean isMatched = false;
243 while (patternListIterator.hasNext()) {
244 if (valueInString.matches(patternListIterator.next())) {
245 isMatched = true;
246 break;
247 }
248 }
249
250 return isMatched;
251 }
252
Vidyashree Rama1db15562016-05-17 16:16:15 +0530253 @Override
254 public void validateDataOnEntry() throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530255 // TODO: implement the method.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530256 }
257
258 @Override
259 public void validateDataOnExit() throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530260 // TODO: implement the method.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530261 }
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530262}