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