blob: bedeead5fc13be0a8764868df56988964e40fc10 [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;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053023
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053024import com.google.common.base.Strings;
25
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053026import static org.onosproject.yangutils.datamodel.YangDataTypes.BINARY;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053027import static org.onosproject.yangutils.datamodel.YangDataTypes.BITS;
28import static org.onosproject.yangutils.datamodel.YangDataTypes.BOOLEAN;
29import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
30import static org.onosproject.yangutils.datamodel.YangDataTypes.EMPTY;
31import static org.onosproject.yangutils.datamodel.YangDataTypes.ENUMERATION;
32import static org.onosproject.yangutils.datamodel.YangDataTypes.IDENTITYREF;
33import static org.onosproject.yangutils.datamodel.YangDataTypes.LEAFREF;
34import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
35import static org.onosproject.yangutils.datamodel.YangDataTypes.UNION;
Bharat saraswal96dfef02016-06-16 00:29:12 +053036import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.INTRA_FILE_RESOLVED;
37import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053038import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.isOfRangeRestrictedType;
39import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processLengthRestriction;
40import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processRangeRestriction;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053041
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053042/**
Bharat saraswald9822e92016-04-05 15:13:44 +053043 * Represents the derived information.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053044 *
45 * @param <T> extended information.
46 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053047public class YangDerivedInfo<T>
Bharat saraswal96dfef02016-06-16 00:29:12 +053048 implements LocationInfo, Cloneable, Serializable {
49
50 private static final long serialVersionUID = 806201641L;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053051
52 /**
53 * YANG typedef reference.
54 */
55 private YangTypeDef referredTypeDef;
56
57 /**
58 * Resolved additional information about data type after linking, example
59 * restriction info, named values, etc. The extra information is based
60 * on the data type. Based on the data type, the extended info can vary.
61 */
62 private T resolvedExtendedInfo;
63
64 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053065 * Line number of pattern restriction in YANG file.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053066 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053067 private int lineNumber;
68
69 /**
70 * Position of pattern restriction in line.
71 */
72 private int charPositionInLine;
73
74 /**
75 * Effective built-in type, requried in case type of typedef is again a
76 * derived type. This information is to be added during linking.
77 */
78 private YangDataTypes effectiveBuiltInType;
79
80 /**
81 * Length restriction string to temporary store the length restriction when the type
82 * is derived.
83 */
84 private String lengthRestrictionString;
85
86 /**
87 * Range restriction string to temporary store the range restriction when the type
88 * is derived.
89 */
90 private String rangeRestrictionString;
91
92 /**
93 * Pattern restriction string to temporary store the pattern restriction when the type
94 * is derived.
95 */
96 private YangPatternRestriction patternRestriction;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053097
98 /**
99 * Returns the referred typedef reference.
100 *
101 * @return referred typedef reference
102 */
103 public YangTypeDef getReferredTypeDef() {
104 return referredTypeDef;
105 }
106
107 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530108 * Sets the referred typedef reference.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530109 *
110 * @param referredTypeDef referred typedef reference
111 */
112 public void setReferredTypeDef(YangTypeDef referredTypeDef) {
113 this.referredTypeDef = referredTypeDef;
114 }
115
116 /**
117 * Returns resolved extended information after successful linking.
118 *
119 * @return resolved extended information
120 */
121 public T getResolvedExtendedInfo() {
122 return resolvedExtendedInfo;
123 }
124
125 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530126 * Sets resolved extended information after successful linking.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530127 *
128 * @param resolvedExtendedInfo resolved extended information
129 */
130 public void setResolvedExtendedInfo(T resolvedExtendedInfo) {
131 this.resolvedExtendedInfo = resolvedExtendedInfo;
132 }
133
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530134 @Override
135 public int getLineNumber() {
136 return lineNumber;
137 }
138
139 @Override
140 public int getCharPosition() {
141 return charPositionInLine;
142 }
143
144 @Override
145 public void setLineNumber(int lineNumber) {
146 this.lineNumber = lineNumber;
147 }
148
149 @Override
150 public void setCharPosition(int charPositionInLine) {
151 this.charPositionInLine = charPositionInLine;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530152 }
153
154 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530155 * Returns the length restriction string.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530156 *
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530157 * @return the length restriction string
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530158 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530159 public String getLengthRestrictionString() {
160 return lengthRestrictionString;
161 }
162
163 /**
164 * Sets the length restriction string.
165 *
166 * @param lengthRestrictionString the length restriction string
167 */
168 public void setLengthRestrictionString(String lengthRestrictionString) {
169 this.lengthRestrictionString = lengthRestrictionString;
170 }
171
172 /**
173 * Returns the range restriction string.
174 *
175 * @return the range restriction string
176 */
177 public String getRangeRestrictionString() {
178 return rangeRestrictionString;
179 }
180
181 /**
182 * Sets the range restriction string.
183 *
184 * @param rangeRestrictionString the range restriction string
185 */
186 public void setRangeRestrictionString(String rangeRestrictionString) {
187 this.rangeRestrictionString = rangeRestrictionString;
188 }
189
190 /**
191 * Returns the pattern restriction.
192 *
193 * @return the pattern restriction
194 */
195 public YangPatternRestriction getPatternRestriction() {
196 return patternRestriction;
197 }
198
199 /**
200 * Sets the pattern restriction.
201 *
202 * @param patternRestriction the pattern restriction
203 */
204 public void setPatternRestriction(YangPatternRestriction patternRestriction) {
205 this.patternRestriction = patternRestriction;
206 }
207
208 /**
209 * Returns effective built-in type.
210 *
211 * @return effective built-in type
212 */
213 public YangDataTypes getEffectiveBuiltInType() {
214 return effectiveBuiltInType;
215 }
216
217 /**
218 * Sets effective built-in type.
219 *
220 * @param effectiveBuiltInType effective built-in type
221 */
222 public void setEffectiveBuiltInType(YangDataTypes effectiveBuiltInType) {
223 this.effectiveBuiltInType = effectiveBuiltInType;
224 }
225
226 /**
227 * Resolves the type derived info, by obtaining the effective built-in type
228 * and resolving the restrictions.
229 *
230 * @return resolution status
231 * @throws DataModelException a violation in data mode rule
232 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530233 public ResolvableStatus resolve()
234 throws DataModelException {
235
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530236 YangType<?> baseType = getReferredTypeDef().getTypeDefBaseType();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530237
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530238 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530239 * Checks the data type of the referred typedef, if it's derived, obtain
240 * effective built-in type and restrictions from it's derived info,
241 * otherwise take from the base type of type itself.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530242 */
243 if (baseType.getDataType() == DERIVED) {
244 /*
245 * Check whether the referred typedef is resolved.
246 */
247 if (baseType.getResolvableStatus() != INTRA_FILE_RESOLVED && baseType.getResolvableStatus() != RESOLVED) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530248 throw new DataModelException("Linker Error: Referred typedef is not resolved for type.");
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530249 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530250
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530251 /*
252 * Check if the referred typedef is intra file resolved, if yes sets
253 * current status also to intra file resolved .
254 */
255 if (getReferredTypeDef().getTypeDefBaseType().getResolvableStatus() == INTRA_FILE_RESOLVED) {
256 return INTRA_FILE_RESOLVED;
257 }
258 setEffectiveBuiltInType(((YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo())
259 .getEffectiveBuiltInType());
Bharat saraswalcad0e652016-05-26 23:48:38 +0530260 YangDerivedInfo refDerivedInfo = (YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo();
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530261 /*
262 * Check whether the effective built-in type can have range
263 * restrictions, if yes call resolution of range.
264 */
265 if (isOfRangeRestrictedType(getEffectiveBuiltInType())) {
266 if (refDerivedInfo.getResolvedExtendedInfo() == null) {
267 resolveRangeRestriction(null);
268 /*
269 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530270 * resolve range/string restriction will throw exception in
271 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530272 */
273 return RESOLVED;
274 } else {
275 if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) {
276 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
277 "type.");
278 }
279 resolveRangeRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo());
280 /*
281 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530282 * resolve range/string restriction will throw exception in
283 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530284 */
285 return RESOLVED;
286 }
287 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530288 * If the effective built-in type is of type string calls for
289 * string resolution.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530290 */
291 } else if (getEffectiveBuiltInType() == STRING) {
292 if (refDerivedInfo.getResolvedExtendedInfo() == null) {
293 resolveStringRestriction(null);
294 /*
295 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530296 * resolve range/string restriction will throw exception in
297 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530298 */
299 return RESOLVED;
300 } else {
301 if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangStringRestriction)) {
302 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
303 "type.");
304 }
305 resolveStringRestriction((YangStringRestriction) refDerivedInfo.getResolvedExtendedInfo());
306 /*
307 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530308 * resolve range/string restriction will throw exception in
309 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530310 */
311 return RESOLVED;
312 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530313 } else if (getEffectiveBuiltInType() == BINARY) {
314 if (refDerivedInfo.getResolvedExtendedInfo() == null) {
315 resolveLengthRestriction(null);
316 /*
317 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530318 * resolve length restriction will throw exception in
319 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530320 */
321 return RESOLVED;
322 } else {
323 if (!(refDerivedInfo.getResolvedExtendedInfo() instanceof YangRangeRestriction)) {
324 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
325 "type.");
326 }
327 resolveLengthRestriction((YangRangeRestriction) refDerivedInfo.getResolvedExtendedInfo());
328 /*
329 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530330 * resolve length restriction will throw exception in
331 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530332 */
333 return RESOLVED;
334 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530335 }
336 } else {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530337 setEffectiveBuiltInType(baseType.getDataType());
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530338 /*
339 * Check whether the effective built-in type can have range
340 * restrictions, if yes call resolution of range.
341 */
342 if (isOfRangeRestrictedType(getEffectiveBuiltInType())) {
343 if (baseType.getDataTypeExtendedInfo() == null) {
344 resolveRangeRestriction(null);
345 /*
346 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530347 * resolve range/string restriction will throw exception in
348 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530349 */
350 return RESOLVED;
351 } else {
352 if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
353 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
354 "type.");
355 }
356 resolveRangeRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
357 /*
358 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530359 * resolve range/string restriction will throw exception in
360 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530361 */
362 return RESOLVED;
363 }
364 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530365 * If the effective built-in type is of type string calls for
366 * string resolution.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530367 */
368 } else if (getEffectiveBuiltInType() == STRING) {
369 if (baseType.getDataTypeExtendedInfo() == null) {
370 resolveStringRestriction(null);
371 /*
372 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530373 * resolve range/string restriction will throw exception in
374 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530375 */
376 return RESOLVED;
377 } else {
378 if (!(baseType.getDataTypeExtendedInfo() instanceof YangStringRestriction)) {
379 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
380 "type.");
381 }
382 resolveStringRestriction((YangStringRestriction) baseType.getDataTypeExtendedInfo());
383 /*
384 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530385 * resolve range/string restriction will throw exception in
386 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530387 */
388 return RESOLVED;
389 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530390 } else if (getEffectiveBuiltInType() == BINARY) {
391 if (baseType.getDataTypeExtendedInfo() == null) {
392 resolveLengthRestriction(null);
393 /*
394 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530395 * resolve length restriction will throw exception in
396 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530397 */
398 return RESOLVED;
399 } else {
400 if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
401 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
402 "type.");
403 }
404 resolveLengthRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
405 /*
406 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530407 * resolve length restriction will throw exception in
408 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530409 */
410 return RESOLVED;
411 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530412 }
413 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530414
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530415 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530416 * Check if the data type is the one which can't be restricted, in this
417 * case check whether no self restrictions should be present.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530418 */
419 if (isOfValidNonRestrictedType(getEffectiveBuiltInType())) {
420 if (Strings.isNullOrEmpty(getLengthRestrictionString())
421 && Strings.isNullOrEmpty(getRangeRestrictionString())
422 && getPatternRestriction() == null) {
423 return RESOLVED;
424 } else {
425 throw new DataModelException("YANG file error: Restrictions can't be applied to a given type");
426 }
427 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530428
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530429 // Throw exception for unsupported types
430 throw new DataModelException("Linker error: Unable to process the derived type.");
431 }
432
433 /**
434 * Resolves the string restrictions.
435 *
436 * @param refStringRestriction referred string restriction of typedef
437 * @throws DataModelException a violation in data model rule
438 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530439 private void resolveStringRestriction(YangStringRestriction refStringRestriction)
440 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530441 YangStringRestriction curStringRestriction = null;
442 YangRangeRestriction refRangeRestriction = null;
443 YangPatternRestriction refPatternRestriction = null;
444
445 /*
446 * Check that range restriction should be null when built-in type is
447 * string.
448 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530449 if (!Strings.isNullOrEmpty(getRangeRestrictionString())) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530450 DataModelException dataModelException = new DataModelException("YANG file error: Range restriction " +
451 "should't be present for string data type.");
452 dataModelException.setLine(lineNumber);
453 dataModelException.setCharPosition(charPositionInLine);
454 throw dataModelException;
455 }
456
457 /*
458 * If referred restriction and self restriction both are null, no
459 * resolution is required.
460 */
461 if (refStringRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())
462 && getPatternRestriction() == null) {
463 return;
464 }
465
466 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530467 * If referred string restriction is not null, take value of length and
468 * pattern restriction and assign.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530469 */
470 if (refStringRestriction != null) {
471 refRangeRestriction = refStringRestriction.getLengthRestriction();
472 refPatternRestriction = refStringRestriction.getPatternRestriction();
473 }
474
475 YangRangeRestriction lengthRestriction = resolveLengthRestriction(refRangeRestriction);
476 YangPatternRestriction patternRestriction = resolvePatternRestriction(refPatternRestriction);
477
478 /*
479 * Check if either of length or pattern restriction is present, if yes
480 * create string restriction and assign value.
481 */
482 if (lengthRestriction != null || patternRestriction != null) {
483 curStringRestriction = new YangStringRestriction();
484 curStringRestriction.setLengthRestriction(lengthRestriction);
485 curStringRestriction.setPatternRestriction(patternRestriction);
486 }
487 setResolvedExtendedInfo((T) curStringRestriction);
488 }
489
490 /**
491 * Resolves pattern restriction.
492 *
493 * @param refPatternRestriction referred pattern restriction of typedef
494 * @return resolved pattern restriction
495 */
496 private YangPatternRestriction resolvePatternRestriction(YangPatternRestriction refPatternRestriction) {
497 /*
498 * If referred restriction and self restriction both are null, no
499 * resolution is required.
500 */
501 if (refPatternRestriction == null && getPatternRestriction() == null) {
502 return null;
503 }
504
505 /*
506 * If self restriction is null, and referred restriction is present
507 * shallow copy the referred to self.
508 */
509 if (getPatternRestriction() == null) {
510 return refPatternRestriction;
511 }
512
513 /*
514 * If referred restriction is null, and self restriction is present
515 * carry out self resolution.
516 */
517 if (refPatternRestriction == null) {
518 return getPatternRestriction();
519 }
520
521 /*
522 * Get patterns of referred type and add it to current pattern
523 * restrictions.
524 */
525 for (String pattern : refPatternRestriction.getPatternList()) {
526 getPatternRestriction().addPattern(pattern);
527 }
528 return getPatternRestriction();
529 }
530
531 /**
532 * Resolves the length restrictions.
533 *
534 * @param refLengthRestriction referred length restriction of typedef
535 * @return resolved length restriction
536 * @throws DataModelException a violation in data model rule
537 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530538 private YangRangeRestriction resolveLengthRestriction(YangRangeRestriction refLengthRestriction)
Bharat saraswal96dfef02016-06-16 00:29:12 +0530539 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530540
541 /*
542 * If referred restriction and self restriction both are null, no
543 * resolution is required.
544 */
545 if (refLengthRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())) {
546 return null;
547 }
548
549 /*
550 * If self restriction is null, and referred restriction is present
551 * shallow copy the referred to self.
552 */
553 if (Strings.isNullOrEmpty(getLengthRestrictionString())) {
554 return refLengthRestriction;
555 }
556
557 /*
558 * If referred restriction is null, and self restriction is present
559 * carry out self resolution.
560 */
561 if (refLengthRestriction == null) {
562 YangRangeRestriction curLengthRestriction = processLengthRestriction(null, lineNumber,
563 charPositionInLine, false, getLengthRestrictionString());
564 return curLengthRestriction;
565 }
566
567 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530568 * Carry out self resolution based with obtained effective built-in type
569 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530570 */
571 YangRangeRestriction curLengthRestriction = processLengthRestriction(refLengthRestriction, lineNumber,
572 charPositionInLine, true, getLengthRestrictionString());
573
574 // Resolve the range with referred typedef's restriction.
575 resolveLengthAndRangeRestriction(refLengthRestriction, curLengthRestriction);
576 return curLengthRestriction;
577 }
578
579 /**
580 * Resolves the length/range self and referred restriction, to check whether
581 * the all the range interval in self restriction is stricter than the
582 * referred typedef's restriction.
583 *
584 * @param refRestriction referred restriction
585 * @param curRestriction self restriction
586 */
587 private void resolveLengthAndRangeRestriction(YangRangeRestriction refRestriction,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530588 YangRangeRestriction curRestriction)
589 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530590 for (Object curInterval : curRestriction.getAscendingRangeIntervals()) {
591 if (!(curInterval instanceof YangRangeInterval)) {
592 throw new DataModelException("Linker error: Current range intervals not processed correctly.");
593 }
594 try {
595 refRestriction.isValidInterval((YangRangeInterval) curInterval);
596 } catch (DataModelException e) {
597 DataModelException dataModelException = new DataModelException(e);
598 dataModelException.setLine(lineNumber);
599 dataModelException.setCharPosition(charPositionInLine);
600 throw dataModelException;
601 }
602 }
603 }
604
605 /**
606 * Resolves the range restrictions.
607 *
608 * @param refRangeRestriction referred range restriction of typedef
609 * @throws DataModelException a violation in data model rule
610 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530611 private void resolveRangeRestriction(YangRangeRestriction refRangeRestriction)
612 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530613
614 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530615 * Check that string restriction should be null when built-in type is of
616 * range type.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530617 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530618 if (!Strings.isNullOrEmpty(getLengthRestrictionString()) || getPatternRestriction() != null) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530619 DataModelException dataModelException = new DataModelException("YANG file error: Length/Pattern " +
620 "restriction should't be present for int/uint/decimal data type.");
621 dataModelException.setLine(lineNumber);
622 dataModelException.setCharPosition(charPositionInLine);
623 throw dataModelException;
624 }
625
626 /*
627 * If referred restriction and self restriction both are null, no
628 * resolution is required.
629 */
630 if (refRangeRestriction == null && Strings.isNullOrEmpty(getRangeRestrictionString())) {
631 return;
632 }
633
634 /*
635 * If self restriction is null, and referred restriction is present
636 * shallow copy the referred to self.
637 */
638 if (Strings.isNullOrEmpty(getRangeRestrictionString())) {
639 setResolvedExtendedInfo((T) refRangeRestriction);
640 return;
641 }
642
643 /*
644 * If referred restriction is null, and self restriction is present
645 * carry out self resolution.
646 */
647 if (refRangeRestriction == null) {
648 YangRangeRestriction curRangeRestriction = processRangeRestriction(null, lineNumber,
649 charPositionInLine, false, getRangeRestrictionString(), getEffectiveBuiltInType());
650 setResolvedExtendedInfo((T) curRangeRestriction);
651 return;
652 }
653
654 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530655 * Carry out self resolution based with obtained effective built-in type
656 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530657 */
658 YangRangeRestriction curRangeRestriction = processRangeRestriction(refRangeRestriction, lineNumber,
659 charPositionInLine, true, getRangeRestrictionString(), getEffectiveBuiltInType());
660
661 // Resolve the range with referred typedef's restriction.
662 resolveLengthAndRangeRestriction(refRangeRestriction, curRangeRestriction);
663 setResolvedExtendedInfo((T) curRangeRestriction);
664 }
665
666 /**
667 * Returns whether the data type is of non restricted type.
668 *
669 * @param dataType data type to be checked
670 * @return true, if data type can't be restricted, false otherwise
671 */
672 private boolean isOfValidNonRestrictedType(YangDataTypes dataType) {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530673 return dataType == BOOLEAN
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530674 || dataType == ENUMERATION
675 || dataType == BITS
676 || dataType == EMPTY
677 || dataType == UNION
678 || dataType == IDENTITYREF
Bharat saraswalcad0e652016-05-26 23:48:38 +0530679 || dataType == LEAFREF;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530680 }
681}