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