Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +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 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.datamodel; |
| 18 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 20 | import org.onosproject.yangutils.linker.ResolvableStatus; |
| 21 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 22 | import com.google.common.base.Strings; |
| 23 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 24 | import static org.onosproject.yangutils.datamodel.YangDataTypes.BINARY; |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 25 | import static org.onosproject.yangutils.datamodel.YangDataTypes.BITS; |
| 26 | import static org.onosproject.yangutils.datamodel.YangDataTypes.BOOLEAN; |
| 27 | import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED; |
| 28 | import static org.onosproject.yangutils.datamodel.YangDataTypes.EMPTY; |
| 29 | import static org.onosproject.yangutils.datamodel.YangDataTypes.ENUMERATION; |
| 30 | import static org.onosproject.yangutils.datamodel.YangDataTypes.IDENTITYREF; |
| 31 | import static org.onosproject.yangutils.datamodel.YangDataTypes.LEAFREF; |
| 32 | import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING; |
| 33 | import static org.onosproject.yangutils.datamodel.YangDataTypes.UNION; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 34 | import static org.onosproject.yangutils.linker.ResolvableStatus.INTRA_FILE_RESOLVED; |
| 35 | import static org.onosproject.yangutils.linker.ResolvableStatus.RESOLVED; |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.RestrictionResolver.isOfRangeRestrictedType; |
| 37 | import static org.onosproject.yangutils.utils.RestrictionResolver.processLengthRestriction; |
| 38 | import static org.onosproject.yangutils.utils.RestrictionResolver.processRangeRestriction; |
| 39 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 40 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 41 | * Represents the derived information. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 42 | * |
| 43 | * @param <T> extended information. |
| 44 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 45 | public class YangDerivedInfo<T> |
| 46 | implements LocationInfo, Cloneable { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * YANG typedef reference. |
| 50 | */ |
| 51 | private YangTypeDef referredTypeDef; |
| 52 | |
| 53 | /** |
| 54 | * Resolved additional information about data type after linking, example |
| 55 | * restriction info, named values, etc. The extra information is based |
| 56 | * on the data type. Based on the data type, the extended info can vary. |
| 57 | */ |
| 58 | private T resolvedExtendedInfo; |
| 59 | |
| 60 | /** |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 61 | * Line number of pattern restriction in YANG file. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 62 | */ |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 63 | private int lineNumber; |
| 64 | |
| 65 | /** |
| 66 | * Position of pattern restriction in line. |
| 67 | */ |
| 68 | private int charPositionInLine; |
| 69 | |
| 70 | /** |
| 71 | * Effective built-in type, requried in case type of typedef is again a |
| 72 | * derived type. This information is to be added during linking. |
| 73 | */ |
| 74 | private YangDataTypes effectiveBuiltInType; |
| 75 | |
| 76 | /** |
| 77 | * Length restriction string to temporary store the length restriction when the type |
| 78 | * is derived. |
| 79 | */ |
| 80 | private String lengthRestrictionString; |
| 81 | |
| 82 | /** |
| 83 | * Range restriction string to temporary store the range restriction when the type |
| 84 | * is derived. |
| 85 | */ |
| 86 | private String rangeRestrictionString; |
| 87 | |
| 88 | /** |
| 89 | * Pattern restriction string to temporary store the pattern restriction when the type |
| 90 | * is derived. |
| 91 | */ |
| 92 | private YangPatternRestriction patternRestriction; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Returns the referred typedef reference. |
| 96 | * |
| 97 | * @return referred typedef reference |
| 98 | */ |
| 99 | public YangTypeDef getReferredTypeDef() { |
| 100 | return referredTypeDef; |
| 101 | } |
| 102 | |
| 103 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 104 | * Sets the referred typedef reference. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 105 | * |
| 106 | * @param referredTypeDef referred typedef reference |
| 107 | */ |
| 108 | public void setReferredTypeDef(YangTypeDef referredTypeDef) { |
| 109 | this.referredTypeDef = referredTypeDef; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns resolved extended information after successful linking. |
| 114 | * |
| 115 | * @return resolved extended information |
| 116 | */ |
| 117 | public T getResolvedExtendedInfo() { |
| 118 | return resolvedExtendedInfo; |
| 119 | } |
| 120 | |
| 121 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 122 | * Sets resolved extended information after successful linking. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 123 | * |
| 124 | * @param resolvedExtendedInfo resolved extended information |
| 125 | */ |
| 126 | public void setResolvedExtendedInfo(T resolvedExtendedInfo) { |
| 127 | this.resolvedExtendedInfo = resolvedExtendedInfo; |
| 128 | } |
| 129 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 130 | @Override |
| 131 | public int getLineNumber() { |
| 132 | return lineNumber; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public int getCharPosition() { |
| 137 | return charPositionInLine; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void setLineNumber(int lineNumber) { |
| 142 | this.lineNumber = lineNumber; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void setCharPosition(int charPositionInLine) { |
| 147 | this.charPositionInLine = charPositionInLine; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 151 | * Returns the length restriction string. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 152 | * |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 153 | * @return the length restriction string |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 154 | */ |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 155 | public String getLengthRestrictionString() { |
| 156 | return lengthRestrictionString; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets the length restriction string. |
| 161 | * |
| 162 | * @param lengthRestrictionString the length restriction string |
| 163 | */ |
| 164 | public void setLengthRestrictionString(String lengthRestrictionString) { |
| 165 | this.lengthRestrictionString = lengthRestrictionString; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns the range restriction string. |
| 170 | * |
| 171 | * @return the range restriction string |
| 172 | */ |
| 173 | public String getRangeRestrictionString() { |
| 174 | return rangeRestrictionString; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Sets the range restriction string. |
| 179 | * |
| 180 | * @param rangeRestrictionString the range restriction string |
| 181 | */ |
| 182 | public void setRangeRestrictionString(String rangeRestrictionString) { |
| 183 | this.rangeRestrictionString = rangeRestrictionString; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns the pattern restriction. |
| 188 | * |
| 189 | * @return the pattern restriction |
| 190 | */ |
| 191 | public YangPatternRestriction getPatternRestriction() { |
| 192 | return patternRestriction; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Sets the pattern restriction. |
| 197 | * |
| 198 | * @param patternRestriction the pattern restriction |
| 199 | */ |
| 200 | public void setPatternRestriction(YangPatternRestriction patternRestriction) { |
| 201 | this.patternRestriction = patternRestriction; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns effective built-in type. |
| 206 | * |
| 207 | * @return effective built-in type |
| 208 | */ |
| 209 | public YangDataTypes getEffectiveBuiltInType() { |
| 210 | return effectiveBuiltInType; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Sets effective built-in type. |
| 215 | * |
| 216 | * @param effectiveBuiltInType effective built-in type |
| 217 | */ |
| 218 | public void setEffectiveBuiltInType(YangDataTypes effectiveBuiltInType) { |
| 219 | this.effectiveBuiltInType = effectiveBuiltInType; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Resolves the type derived info, by obtaining the effective built-in type |
| 224 | * and resolving the restrictions. |
| 225 | * |
| 226 | * @return resolution status |
| 227 | * @throws DataModelException a violation in data mode rule |
| 228 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 229 | public ResolvableStatus resolve() |
| 230 | throws DataModelException { |
| 231 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 232 | YangType<?> baseType = getReferredTypeDef().getTypeDefBaseType(); |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 233 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 234 | /* |
| 235 | * Checks the data type of the referred typedef, if it's derived, |
| 236 | * obtain effective built-in type and restrictions from it's derived |
| 237 | * info, otherwise take from the base type of type itself. |
| 238 | */ |
| 239 | if (baseType.getDataType() == DERIVED) { |
| 240 | /* |
| 241 | * Check whether the referred typedef is resolved. |
| 242 | */ |
| 243 | if (baseType.getResolvableStatus() != INTRA_FILE_RESOLVED && baseType.getResolvableStatus() != RESOLVED) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 244 | throw new DataModelException("Linker Error: Referred typedef is not resolved for type."); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 245 | } |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 246 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 247 | /* |
| 248 | * Check if the referred typedef is intra file resolved, if yes sets |
| 249 | * current status also to intra file resolved . |
| 250 | */ |
| 251 | if (getReferredTypeDef().getTypeDefBaseType().getResolvableStatus() == INTRA_FILE_RESOLVED) { |
| 252 | return INTRA_FILE_RESOLVED; |
| 253 | } |
| 254 | setEffectiveBuiltInType(((YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo()) |
| 255 | .getEffectiveBuiltInType()); |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 256 | YangDerivedInfo refDerivedInfo = (YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo(); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 257 | /* |
| 258 | * Check whether the effective built-in type can have range |
| 259 | * restrictions, if yes call resolution of range. |
| 260 | */ |
| 261 | if (isOfRangeRestrictedType(getEffectiveBuiltInType())) { |
| 262 | if (refDerivedInfo.getResolvedExtendedInfo() == null) { |
| 263 | resolveRangeRestriction(null); |
| 264 | /* |
| 265 | * Return the resolution status as resolved, if it's not |
| 266 | * resolve range/string restriction will throw exception |
| 267 | * in previous function. |
| 268 | */ |
| 269 | return RESOLVED; |
| 270 | } else { |
| 271 | if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) { |
| 272 | throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " + |
| 273 | "type."); |
| 274 | } |
| 275 | resolveRangeRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo()); |
| 276 | /* |
| 277 | * Return the resolution status as resolved, if it's not |
| 278 | * resolve range/string restriction will throw exception |
| 279 | * in previous function. |
| 280 | */ |
| 281 | return RESOLVED; |
| 282 | } |
| 283 | /* |
| 284 | * If the effective built-in type is of type string calls |
| 285 | * for string resolution. |
| 286 | */ |
| 287 | } else if (getEffectiveBuiltInType() == STRING) { |
| 288 | if (refDerivedInfo.getResolvedExtendedInfo() == null) { |
| 289 | resolveStringRestriction(null); |
| 290 | /* |
| 291 | * Return the resolution status as resolved, if it's not |
| 292 | * resolve range/string restriction will throw exception |
| 293 | * in previous function. |
| 294 | */ |
| 295 | return RESOLVED; |
| 296 | } else { |
| 297 | if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangStringRestriction)) { |
| 298 | throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " + |
| 299 | "type."); |
| 300 | } |
| 301 | resolveStringRestriction((YangStringRestriction) refDerivedInfo.getResolvedExtendedInfo()); |
| 302 | /* |
| 303 | * Return the resolution status as resolved, if it's not |
| 304 | * resolve range/string restriction will throw exception |
| 305 | * in previous function. |
| 306 | */ |
| 307 | return RESOLVED; |
| 308 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 309 | } else if (getEffectiveBuiltInType() == BINARY) { |
| 310 | if (refDerivedInfo.getResolvedExtendedInfo() == null) { |
| 311 | resolveLengthRestriction(null); |
| 312 | /* |
| 313 | * Return the resolution status as resolved, if it's not |
| 314 | * resolve length restriction will throw exception |
| 315 | * in previous function. |
| 316 | */ |
| 317 | return RESOLVED; |
| 318 | } else { |
| 319 | if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) { |
| 320 | throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " + |
| 321 | "type."); |
| 322 | } |
| 323 | resolveLengthRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo()); |
| 324 | /* |
| 325 | * Return the resolution status as resolved, if it's not |
| 326 | * resolve length restriction will throw exception |
| 327 | * in previous function. |
| 328 | */ |
| 329 | return RESOLVED; |
| 330 | } |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 331 | } |
| 332 | } else { |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 333 | setEffectiveBuiltInType(baseType.getDataType()); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 334 | /* |
| 335 | * Check whether the effective built-in type can have range |
| 336 | * restrictions, if yes call resolution of range. |
| 337 | */ |
| 338 | if (isOfRangeRestrictedType(getEffectiveBuiltInType())) { |
| 339 | if (baseType.getDataTypeExtendedInfo() == null) { |
| 340 | resolveRangeRestriction(null); |
| 341 | /* |
| 342 | * Return the resolution status as resolved, if it's not |
| 343 | * resolve range/string restriction will throw exception |
| 344 | * in previous function. |
| 345 | */ |
| 346 | return RESOLVED; |
| 347 | } else { |
| 348 | if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) { |
| 349 | throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " + |
| 350 | "type."); |
| 351 | } |
| 352 | resolveRangeRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo()); |
| 353 | /* |
| 354 | * Return the resolution status as resolved, if it's not |
| 355 | * resolve range/string restriction will throw exception |
| 356 | * in previous function. |
| 357 | */ |
| 358 | return RESOLVED; |
| 359 | } |
| 360 | /* |
| 361 | * If the effective built-in type is of type string calls |
| 362 | * for string resolution. |
| 363 | */ |
| 364 | } else if (getEffectiveBuiltInType() == STRING) { |
| 365 | if (baseType.getDataTypeExtendedInfo() == null) { |
| 366 | resolveStringRestriction(null); |
| 367 | /* |
| 368 | * Return the resolution status as resolved, if it's not |
| 369 | * resolve range/string restriction will throw exception |
| 370 | * in previous function. |
| 371 | */ |
| 372 | return RESOLVED; |
| 373 | } else { |
| 374 | if (!(baseType.getDataTypeExtendedInfo() instanceof YangStringRestriction)) { |
| 375 | throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " + |
| 376 | "type."); |
| 377 | } |
| 378 | resolveStringRestriction((YangStringRestriction) baseType.getDataTypeExtendedInfo()); |
| 379 | /* |
| 380 | * Return the resolution status as resolved, if it's not |
| 381 | * resolve range/string restriction will throw exception |
| 382 | * in previous function. |
| 383 | */ |
| 384 | return RESOLVED; |
| 385 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 386 | } else if (getEffectiveBuiltInType() == BINARY) { |
| 387 | if (baseType.getDataTypeExtendedInfo() == null) { |
| 388 | resolveLengthRestriction(null); |
| 389 | /* |
| 390 | * Return the resolution status as resolved, if it's not |
| 391 | * resolve length restriction will throw exception |
| 392 | * in previous function. |
| 393 | */ |
| 394 | return RESOLVED; |
| 395 | } else { |
| 396 | if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) { |
| 397 | throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " + |
| 398 | "type."); |
| 399 | } |
| 400 | resolveLengthRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo()); |
| 401 | /* |
| 402 | * Return the resolution status as resolved, if it's not |
| 403 | * resolve length restriction will throw exception |
| 404 | * in previous function. |
| 405 | */ |
| 406 | return RESOLVED; |
| 407 | } |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 408 | } |
| 409 | } |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 410 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 411 | /* |
| 412 | * Check if the data type is the one which can't be restricted, in |
| 413 | * this case check whether no self restrictions should be present. |
| 414 | */ |
| 415 | if (isOfValidNonRestrictedType(getEffectiveBuiltInType())) { |
| 416 | if (Strings.isNullOrEmpty(getLengthRestrictionString()) |
| 417 | && Strings.isNullOrEmpty(getRangeRestrictionString()) |
| 418 | && getPatternRestriction() == null) { |
| 419 | return RESOLVED; |
| 420 | } else { |
| 421 | throw new DataModelException("YANG file error: Restrictions can't be applied to a given type"); |
| 422 | } |
| 423 | } |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 424 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 425 | // Throw exception for unsupported types |
| 426 | throw new DataModelException("Linker error: Unable to process the derived type."); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Resolves the string restrictions. |
| 431 | * |
| 432 | * @param refStringRestriction referred string restriction of typedef |
| 433 | * @throws DataModelException a violation in data model rule |
| 434 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 435 | private void resolveStringRestriction(YangStringRestriction refStringRestriction) |
| 436 | throws DataModelException { |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 437 | YangStringRestriction curStringRestriction = null; |
| 438 | YangRangeRestriction refRangeRestriction = null; |
| 439 | YangPatternRestriction refPatternRestriction = null; |
| 440 | |
| 441 | /* |
| 442 | * Check that range restriction should be null when built-in type is |
| 443 | * string. |
| 444 | */ |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 445 | if (!Strings.isNullOrEmpty(getRangeRestrictionString())) { |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 446 | DataModelException dataModelException = new DataModelException("YANG file error: Range restriction " + |
| 447 | "should't be present for string data type."); |
| 448 | dataModelException.setLine(lineNumber); |
| 449 | dataModelException.setCharPosition(charPositionInLine); |
| 450 | throw dataModelException; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * If referred restriction and self restriction both are null, no |
| 455 | * resolution is required. |
| 456 | */ |
| 457 | if (refStringRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString()) |
| 458 | && getPatternRestriction() == null) { |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * If referred string restriction is not null, take value of length |
| 464 | * and pattern restriction and assign. |
| 465 | */ |
| 466 | if (refStringRestriction != null) { |
| 467 | refRangeRestriction = refStringRestriction.getLengthRestriction(); |
| 468 | refPatternRestriction = refStringRestriction.getPatternRestriction(); |
| 469 | } |
| 470 | |
| 471 | YangRangeRestriction lengthRestriction = resolveLengthRestriction(refRangeRestriction); |
| 472 | YangPatternRestriction patternRestriction = resolvePatternRestriction(refPatternRestriction); |
| 473 | |
| 474 | /* |
| 475 | * Check if either of length or pattern restriction is present, if yes |
| 476 | * create string restriction and assign value. |
| 477 | */ |
| 478 | if (lengthRestriction != null || patternRestriction != null) { |
| 479 | curStringRestriction = new YangStringRestriction(); |
| 480 | curStringRestriction.setLengthRestriction(lengthRestriction); |
| 481 | curStringRestriction.setPatternRestriction(patternRestriction); |
| 482 | } |
| 483 | setResolvedExtendedInfo((T) curStringRestriction); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Resolves pattern restriction. |
| 488 | * |
| 489 | * @param refPatternRestriction referred pattern restriction of typedef |
| 490 | * @return resolved pattern restriction |
| 491 | */ |
| 492 | private YangPatternRestriction resolvePatternRestriction(YangPatternRestriction refPatternRestriction) { |
| 493 | /* |
| 494 | * If referred restriction and self restriction both are null, no |
| 495 | * resolution is required. |
| 496 | */ |
| 497 | if (refPatternRestriction == null && getPatternRestriction() == null) { |
| 498 | return null; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * If self restriction is null, and referred restriction is present |
| 503 | * shallow copy the referred to self. |
| 504 | */ |
| 505 | if (getPatternRestriction() == null) { |
| 506 | return refPatternRestriction; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * If referred restriction is null, and self restriction is present |
| 511 | * carry out self resolution. |
| 512 | */ |
| 513 | if (refPatternRestriction == null) { |
| 514 | return getPatternRestriction(); |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Get patterns of referred type and add it to current pattern |
| 519 | * restrictions. |
| 520 | */ |
| 521 | for (String pattern : refPatternRestriction.getPatternList()) { |
| 522 | getPatternRestriction().addPattern(pattern); |
| 523 | } |
| 524 | return getPatternRestriction(); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Resolves the length restrictions. |
| 529 | * |
| 530 | * @param refLengthRestriction referred length restriction of typedef |
| 531 | * @return resolved length restriction |
| 532 | * @throws DataModelException a violation in data model rule |
| 533 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 534 | private YangRangeRestriction resolveLengthRestriction(YangRangeRestriction refLengthRestriction) |
| 535 | throws |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 536 | DataModelException { |
| 537 | |
| 538 | /* |
| 539 | * If referred restriction and self restriction both are null, no |
| 540 | * resolution is required. |
| 541 | */ |
| 542 | if (refLengthRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())) { |
| 543 | return null; |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * If self restriction is null, and referred restriction is present |
| 548 | * shallow copy the referred to self. |
| 549 | */ |
| 550 | if (Strings.isNullOrEmpty(getLengthRestrictionString())) { |
| 551 | return refLengthRestriction; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * If referred restriction is null, and self restriction is present |
| 556 | * carry out self resolution. |
| 557 | */ |
| 558 | if (refLengthRestriction == null) { |
| 559 | YangRangeRestriction curLengthRestriction = processLengthRestriction(null, lineNumber, |
| 560 | charPositionInLine, false, getLengthRestrictionString()); |
| 561 | return curLengthRestriction; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Carry out self resolution based with obtained effective built-in |
| 566 | * type and MIN/MAX values as per the referred typedef's values. |
| 567 | */ |
| 568 | YangRangeRestriction curLengthRestriction = processLengthRestriction(refLengthRestriction, lineNumber, |
| 569 | charPositionInLine, true, getLengthRestrictionString()); |
| 570 | |
| 571 | // Resolve the range with referred typedef's restriction. |
| 572 | resolveLengthAndRangeRestriction(refLengthRestriction, curLengthRestriction); |
| 573 | return curLengthRestriction; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Resolves the length/range self and referred restriction, to check whether |
| 578 | * the all the range interval in self restriction is stricter than the |
| 579 | * referred typedef's restriction. |
| 580 | * |
| 581 | * @param refRestriction referred restriction |
| 582 | * @param curRestriction self restriction |
| 583 | */ |
| 584 | private void resolveLengthAndRangeRestriction(YangRangeRestriction refRestriction, |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 585 | YangRangeRestriction curRestriction) |
| 586 | throws DataModelException { |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 587 | for (Object curInterval : curRestriction.getAscendingRangeIntervals()) { |
| 588 | if (!(curInterval instanceof YangRangeInterval)) { |
| 589 | throw new DataModelException("Linker error: Current range intervals not processed correctly."); |
| 590 | } |
| 591 | try { |
| 592 | refRestriction.isValidInterval((YangRangeInterval) curInterval); |
| 593 | } catch (DataModelException e) { |
| 594 | DataModelException dataModelException = new DataModelException(e); |
| 595 | dataModelException.setLine(lineNumber); |
| 596 | dataModelException.setCharPosition(charPositionInLine); |
| 597 | throw dataModelException; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Resolves the range restrictions. |
| 604 | * |
| 605 | * @param refRangeRestriction referred range restriction of typedef |
| 606 | * @throws DataModelException a violation in data model rule |
| 607 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 608 | private void resolveRangeRestriction(YangRangeRestriction refRangeRestriction) |
| 609 | throws DataModelException { |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 610 | |
| 611 | /* |
| 612 | * Check that string restriction should be null when built-in type is |
| 613 | * of range type. |
| 614 | */ |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 615 | if (!Strings.isNullOrEmpty(getLengthRestrictionString()) || getPatternRestriction() != null) { |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 616 | DataModelException dataModelException = new DataModelException("YANG file error: Length/Pattern " + |
| 617 | "restriction should't be present for int/uint/decimal data type."); |
| 618 | dataModelException.setLine(lineNumber); |
| 619 | dataModelException.setCharPosition(charPositionInLine); |
| 620 | throw dataModelException; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * If referred restriction and self restriction both are null, no |
| 625 | * resolution is required. |
| 626 | */ |
| 627 | if (refRangeRestriction == null && Strings.isNullOrEmpty(getRangeRestrictionString())) { |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * If self restriction is null, and referred restriction is present |
| 633 | * shallow copy the referred to self. |
| 634 | */ |
| 635 | if (Strings.isNullOrEmpty(getRangeRestrictionString())) { |
| 636 | setResolvedExtendedInfo((T) refRangeRestriction); |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * If referred restriction is null, and self restriction is present |
| 642 | * carry out self resolution. |
| 643 | */ |
| 644 | if (refRangeRestriction == null) { |
| 645 | YangRangeRestriction curRangeRestriction = processRangeRestriction(null, lineNumber, |
| 646 | charPositionInLine, false, getRangeRestrictionString(), getEffectiveBuiltInType()); |
| 647 | setResolvedExtendedInfo((T) curRangeRestriction); |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Carry out self resolution based with obtained effective built-in |
| 653 | * type and MIN/MAX values as per the referred typedef's values. |
| 654 | */ |
| 655 | YangRangeRestriction curRangeRestriction = processRangeRestriction(refRangeRestriction, lineNumber, |
| 656 | charPositionInLine, true, getRangeRestrictionString(), getEffectiveBuiltInType()); |
| 657 | |
| 658 | // Resolve the range with referred typedef's restriction. |
| 659 | resolveLengthAndRangeRestriction(refRangeRestriction, curRangeRestriction); |
| 660 | setResolvedExtendedInfo((T) curRangeRestriction); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Returns whether the data type is of non restricted type. |
| 665 | * |
| 666 | * @param dataType data type to be checked |
| 667 | * @return true, if data type can't be restricted, false otherwise |
| 668 | */ |
| 669 | private boolean isOfValidNonRestrictedType(YangDataTypes dataType) { |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 670 | return dataType == BOOLEAN |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 671 | || dataType == ENUMERATION |
| 672 | || dataType == BITS |
| 673 | || dataType == EMPTY |
| 674 | || dataType == UNION |
| 675 | || dataType == IDENTITYREF |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 676 | || dataType == LEAFREF; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 677 | } |
| 678 | } |