blob: dee1c988f9bf925d9839185d993a9569a5531133 [file] [log] [blame]
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +05303 *
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
17package org.onosproject.yangutils.datamodel;
18
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
20
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053023import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053024
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053025import com.google.common.base.Strings;
26
janani be18b5342016-07-13 21:06:41 +053027import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.INTRA_FILE_RESOLVED;
28import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
29import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processLengthRestriction;
30import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processRangeRestriction;
31import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053032import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY;
33import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BITS;
34import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BOOLEAN;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053035import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DECIMAL64;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053036import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
37import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.EMPTY;
38import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.ENUMERATION;
39import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.IDENTITYREF;
40import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.LEAFREF;
41import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.STRING;
42import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UNION;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053043
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053044/**
Bharat saraswald9822e92016-04-05 15:13:44 +053045 * Represents the derived information.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053046 *
47 * @param <T> extended information.
48 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053049public class YangDerivedInfo<T>
Bharat saraswal96dfef02016-06-16 00:29:12 +053050 implements LocationInfo, Cloneable, Serializable {
51
52 private static final long serialVersionUID = 806201641L;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053053
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 Agrawalcfa1c412016-05-03 00:41:48 +053067 * Line number of pattern restriction in YANG file.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053068 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053069 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 Agrawald9d6cc82016-03-29 02:17:23 +053099
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 saraswald9822e92016-04-05 15:13:44 +0530110 * Sets the referred typedef reference.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530111 *
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 saraswald9822e92016-04-05 15:13:44 +0530128 * Sets resolved extended information after successful linking.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530129 *
130 * @param resolvedExtendedInfo resolved extended information
131 */
132 public void setResolvedExtendedInfo(T resolvedExtendedInfo) {
133 this.resolvedExtendedInfo = resolvedExtendedInfo;
134 }
135
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530136 @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 Agrawald9d6cc82016-03-29 02:17:23 +0530154 }
155
156 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530157 * Returns the length restriction string.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530158 *
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530159 * @return the length restriction string
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530160 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530161 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-Huawei2ee9e7e2016-06-01 14:30:22 +0530235 public ResolvableStatus resolve()
236 throws DataModelException {
237
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530238 YangType<?> baseType = getReferredTypeDef().getTypeDefBaseType();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530239
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530240 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530241 * 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 Agrawalcfa1c412016-05-03 00:41:48 +0530244 */
245 if (baseType.getDataType() == DERIVED) {
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530246 ResolvableStatus resolvableStatus = resolveTypeDerivedInfo(baseType);
247 if (resolvableStatus != null) {
248 return resolvableStatus;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530249 }
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530250 } else if ((baseType.getDataType() == LEAFREF) || (baseType.getDataType() == IDENTITYREF)) {
janani be18b5342016-07-13 21:06:41 +0530251 setEffectiveBuiltInType(baseType.getDataType());
252 return RESOLVED;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530253 } else {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530254 setEffectiveBuiltInType(baseType.getDataType());
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530255 /*
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 saraswal96dfef02016-06-16 00:29:12 +0530264 * resolve range/string restriction will throw exception in
265 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530266 */
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 saraswal96dfef02016-06-16 00:29:12 +0530276 * resolve range/string restriction will throw exception in
277 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530278 */
279 return RESOLVED;
280 }
281 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530282 * If the effective built-in type is of type string calls for
283 * string resolution.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530284 */
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 saraswal96dfef02016-06-16 00:29:12 +0530290 * resolve range/string restriction will throw exception in
291 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530292 */
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 saraswal96dfef02016-06-16 00:29:12 +0530302 * resolve range/string restriction will throw exception in
303 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530304 */
305 return RESOLVED;
306 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530307 } else if (getEffectiveBuiltInType() == BINARY) {
308 if (baseType.getDataTypeExtendedInfo() == null) {
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530309 resolveBinaryRestriction(null);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530310 /*
311 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530312 * resolve length restriction will throw exception in
313 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530314 */
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 Huawei46fb4db2016-07-14 12:38:17 +0530321 resolveBinaryRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530322 /*
323 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530324 * resolve length restriction will throw exception in
325 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530326 */
327 return RESOLVED;
328 }
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530329 } 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 Agrawalcfa1c412016-05-03 00:41:48 +0530358 }
359 }
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530360
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530361 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530362 * 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 Agrawalcfa1c412016-05-03 00:41:48 +0530364 */
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 Huawei46fb4db2016-07-14 12:38:17 +0530374
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530375 // Throw exception for unsupported types
376 throw new DataModelException("Linker error: Unable to process the derived type.");
377 }
378
379 /**
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530380 * 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) {
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530481 if (refDerivedInfo.getResolvedExtendedInfo() == null) {
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530482 resolveRangeRestriction(null);
483 /*
484 * Return the resolution status as resolved, if it's not;
485 * resolve range restriction will throw exception in
486 * previous function.
487 */
488 return RESOLVED;
489 } else {
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530490 if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) {
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530491 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
492 "type.");
493 }
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530494 resolveRangeRestriction((YangRangeRestriction) refDerivedInfo
495 .getResolvedExtendedInfo());
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530496 /*
497 * Return the resolution status as resolved, if it's not
498 * resolve range/string restriction will throw exception in
499 * previous function.
500 */
501 return RESOLVED;
502 }
503 }
504
505 return null;
506 }
507
508 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530509 * Resolves the string restrictions.
510 *
511 * @param refStringRestriction referred string restriction of typedef
512 * @throws DataModelException a violation in data model rule
513 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530514 private void resolveStringRestriction(YangStringRestriction refStringRestriction)
515 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530516 YangStringRestriction curStringRestriction = null;
517 YangRangeRestriction refRangeRestriction = null;
518 YangPatternRestriction refPatternRestriction = null;
519
520 /*
521 * Check that range restriction should be null when built-in type is
522 * string.
523 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530524 if (!Strings.isNullOrEmpty(getRangeRestrictionString())) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530525 DataModelException dataModelException = new DataModelException("YANG file error: Range restriction " +
526 "should't be present for string data type.");
527 dataModelException.setLine(lineNumber);
528 dataModelException.setCharPosition(charPositionInLine);
529 throw dataModelException;
530 }
531
532 /*
533 * If referred restriction and self restriction both are null, no
534 * resolution is required.
535 */
536 if (refStringRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())
537 && getPatternRestriction() == null) {
538 return;
539 }
540
541 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530542 * If referred string restriction is not null, take value of length and
543 * pattern restriction and assign.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530544 */
545 if (refStringRestriction != null) {
546 refRangeRestriction = refStringRestriction.getLengthRestriction();
547 refPatternRestriction = refStringRestriction.getPatternRestriction();
548 }
549
550 YangRangeRestriction lengthRestriction = resolveLengthRestriction(refRangeRestriction);
551 YangPatternRestriction patternRestriction = resolvePatternRestriction(refPatternRestriction);
552
553 /*
554 * Check if either of length or pattern restriction is present, if yes
555 * create string restriction and assign value.
556 */
557 if (lengthRestriction != null || patternRestriction != null) {
558 curStringRestriction = new YangStringRestriction();
559 curStringRestriction.setLengthRestriction(lengthRestriction);
560 curStringRestriction.setPatternRestriction(patternRestriction);
561 }
562 setResolvedExtendedInfo((T) curStringRestriction);
563 }
564
565 /**
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530566 * Resolves the binary restrictions.
567 *
568 * @param refLengthRestriction referred length restriction of typedef
569 * @throws DataModelException a violation in data model rule
570 */
571 private void resolveBinaryRestriction(YangRangeRestriction refLengthRestriction)
572 throws DataModelException {
573
574 if (rangeRestrictionString != null || patternRestriction != null) {
575 DataModelException dataModelException =
576 new DataModelException("YANG file error: for binary " +
577 "range restriction or pattern restriction is not allowed.");
578 dataModelException.setLine(lineNumber);
579 dataModelException.setCharPosition(charPositionInLine);
580 throw dataModelException;
581 }
582
583 // Get the final resolved length restriction
584 YangRangeRestriction lengthRestriction = resolveLengthRestriction(refLengthRestriction);
585 // Set the lenght restriction.
586 setResolvedExtendedInfo((T) lengthRestriction);
587 }
588
589 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530590 * Resolves pattern restriction.
591 *
592 * @param refPatternRestriction referred pattern restriction of typedef
593 * @return resolved pattern restriction
594 */
595 private YangPatternRestriction resolvePatternRestriction(YangPatternRestriction refPatternRestriction) {
596 /*
597 * If referred restriction and self restriction both are null, no
598 * resolution is required.
599 */
600 if (refPatternRestriction == null && getPatternRestriction() == null) {
601 return null;
602 }
603
604 /*
605 * If self restriction is null, and referred restriction is present
606 * shallow copy the referred to self.
607 */
608 if (getPatternRestriction() == null) {
609 return refPatternRestriction;
610 }
611
612 /*
613 * If referred restriction is null, and self restriction is present
614 * carry out self resolution.
615 */
616 if (refPatternRestriction == null) {
617 return getPatternRestriction();
618 }
619
620 /*
621 * Get patterns of referred type and add it to current pattern
622 * restrictions.
623 */
624 for (String pattern : refPatternRestriction.getPatternList()) {
625 getPatternRestriction().addPattern(pattern);
626 }
627 return getPatternRestriction();
628 }
629
630 /**
631 * Resolves the length restrictions.
632 *
633 * @param refLengthRestriction referred length restriction of typedef
634 * @return resolved length restriction
635 * @throws DataModelException a violation in data model rule
636 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530637 private YangRangeRestriction resolveLengthRestriction(YangRangeRestriction refLengthRestriction)
Bharat saraswal96dfef02016-06-16 00:29:12 +0530638 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530639
640 /*
641 * If referred restriction and self restriction both are null, no
642 * resolution is required.
643 */
644 if (refLengthRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())) {
645 return null;
646 }
647
648 /*
649 * If self restriction is null, and referred restriction is present
650 * shallow copy the referred to self.
651 */
652 if (Strings.isNullOrEmpty(getLengthRestrictionString())) {
653 return refLengthRestriction;
654 }
655
656 /*
657 * If referred restriction is null, and self restriction is present
658 * carry out self resolution.
659 */
660 if (refLengthRestriction == null) {
661 YangRangeRestriction curLengthRestriction = processLengthRestriction(null, lineNumber,
662 charPositionInLine, false, getLengthRestrictionString());
663 return curLengthRestriction;
664 }
665
666 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530667 * Carry out self resolution based with obtained effective built-in type
668 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530669 */
670 YangRangeRestriction curLengthRestriction = processLengthRestriction(refLengthRestriction, lineNumber,
671 charPositionInLine, true, getLengthRestrictionString());
672
673 // Resolve the range with referred typedef's restriction.
674 resolveLengthAndRangeRestriction(refLengthRestriction, curLengthRestriction);
675 return curLengthRestriction;
676 }
677
678 /**
679 * Resolves the length/range self and referred restriction, to check whether
680 * the all the range interval in self restriction is stricter than the
681 * referred typedef's restriction.
682 *
683 * @param refRestriction referred restriction
684 * @param curRestriction self restriction
685 */
686 private void resolveLengthAndRangeRestriction(YangRangeRestriction refRestriction,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530687 YangRangeRestriction curRestriction)
688 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530689 for (Object curInterval : curRestriction.getAscendingRangeIntervals()) {
690 if (!(curInterval instanceof YangRangeInterval)) {
691 throw new DataModelException("Linker error: Current range intervals not processed correctly.");
692 }
693 try {
694 refRestriction.isValidInterval((YangRangeInterval) curInterval);
695 } catch (DataModelException e) {
696 DataModelException dataModelException = new DataModelException(e);
697 dataModelException.setLine(lineNumber);
698 dataModelException.setCharPosition(charPositionInLine);
699 throw dataModelException;
700 }
701 }
702 }
703
704 /**
705 * Resolves the range restrictions.
706 *
707 * @param refRangeRestriction referred range restriction of typedef
708 * @throws DataModelException a violation in data model rule
709 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530710 private void resolveRangeRestriction(YangRangeRestriction refRangeRestriction)
711 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530712
713 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530714 * Check that string restriction should be null when built-in type is of
715 * range type.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530716 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530717 if (!Strings.isNullOrEmpty(getLengthRestrictionString()) || getPatternRestriction() != null) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530718 DataModelException dataModelException = new DataModelException("YANG file error: Length/Pattern " +
719 "restriction should't be present for int/uint/decimal data type.");
720 dataModelException.setLine(lineNumber);
721 dataModelException.setCharPosition(charPositionInLine);
722 throw dataModelException;
723 }
724
725 /*
726 * If referred restriction and self restriction both are null, no
727 * resolution is required.
728 */
729 if (refRangeRestriction == null && Strings.isNullOrEmpty(getRangeRestrictionString())) {
730 return;
731 }
732
733 /*
734 * If self restriction is null, and referred restriction is present
735 * shallow copy the referred to self.
736 */
737 if (Strings.isNullOrEmpty(getRangeRestrictionString())) {
738 setResolvedExtendedInfo((T) refRangeRestriction);
739 return;
740 }
741
742 /*
743 * If referred restriction is null, and self restriction is present
744 * carry out self resolution.
745 */
746 if (refRangeRestriction == null) {
747 YangRangeRestriction curRangeRestriction = processRangeRestriction(null, lineNumber,
748 charPositionInLine, false, getRangeRestrictionString(), getEffectiveBuiltInType());
749 setResolvedExtendedInfo((T) curRangeRestriction);
750 return;
751 }
752
753 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530754 * Carry out self resolution based with obtained effective built-in type
755 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530756 */
757 YangRangeRestriction curRangeRestriction = processRangeRestriction(refRangeRestriction, lineNumber,
758 charPositionInLine, true, getRangeRestrictionString(), getEffectiveBuiltInType());
759
760 // Resolve the range with referred typedef's restriction.
761 resolveLengthAndRangeRestriction(refRangeRestriction, curRangeRestriction);
762 setResolvedExtendedInfo((T) curRangeRestriction);
763 }
764
765 /**
766 * Returns whether the data type is of non restricted type.
767 *
768 * @param dataType data type to be checked
769 * @return true, if data type can't be restricted, false otherwise
770 */
771 private boolean isOfValidNonRestrictedType(YangDataTypes dataType) {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530772 return dataType == BOOLEAN
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530773 || dataType == ENUMERATION
774 || dataType == BITS
775 || dataType == EMPTY
776 || dataType == UNION
777 || dataType == IDENTITYREF
Bharat saraswalcad0e652016-05-26 23:48:38 +0530778 || dataType == LEAFREF;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530779 }
780}