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