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