blob: da04b1a37ec9971b172e22e261e72b389ede0777 [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
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053027import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY;
28import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BITS;
29import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BOOLEAN;
30import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
31import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.EMPTY;
32import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.ENUMERATION;
33import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.IDENTITYREF;
34import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.LEAFREF;
35import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.STRING;
36import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UNION;
Bharat saraswal96dfef02016-06-16 00:29:12 +053037import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.INTRA_FILE_RESOLVED;
38import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.RESOLVED;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053039import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053040import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processLengthRestriction;
41import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processRangeRestriction;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053042
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053043/**
Bharat saraswald9822e92016-04-05 15:13:44 +053044 * Represents the derived information.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053045 *
46 * @param <T> extended information.
47 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053048public class YangDerivedInfo<T>
Bharat saraswal96dfef02016-06-16 00:29:12 +053049 implements LocationInfo, Cloneable, Serializable {
50
51 private static final long serialVersionUID = 806201641L;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053052
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 Agrawalcfa1c412016-05-03 00:41:48 +053066 * Line number of pattern restriction in YANG file.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053067 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053068 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 Agrawald9d6cc82016-03-29 02:17:23 +053098
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 saraswald9822e92016-04-05 15:13:44 +0530109 * Sets the referred typedef reference.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530110 *
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 saraswald9822e92016-04-05 15:13:44 +0530127 * Sets resolved extended information after successful linking.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530128 *
129 * @param resolvedExtendedInfo resolved extended information
130 */
131 public void setResolvedExtendedInfo(T resolvedExtendedInfo) {
132 this.resolvedExtendedInfo = resolvedExtendedInfo;
133 }
134
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530135 @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 Agrawald9d6cc82016-03-29 02:17:23 +0530153 }
154
155 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530156 * Returns the length restriction string.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530157 *
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530158 * @return the length restriction string
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530159 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530160 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-Huawei2ee9e7e2016-06-01 14:30:22 +0530234 public ResolvableStatus resolve()
235 throws DataModelException {
236
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530237 YangType<?> baseType = getReferredTypeDef().getTypeDefBaseType();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530238
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530239 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530240 * 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 Agrawalcfa1c412016-05-03 00:41:48 +0530243 */
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 Agrawal0d43bb52016-05-17 18:06:38 +0530249 throw new DataModelException("Linker Error: Referred typedef is not resolved for type.");
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530250 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530251
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530252 /*
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 saraswalcad0e652016-05-26 23:48:38 +0530261 YangDerivedInfo refDerivedInfo = (YangDerivedInfo<?>) baseType.getDataTypeExtendedInfo();
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530262 /*
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 saraswal96dfef02016-06-16 00:29:12 +0530271 * resolve range/string restriction will throw exception in
272 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530273 */
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 saraswal96dfef02016-06-16 00:29:12 +0530283 * resolve range/string restriction will throw exception in
284 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530285 */
286 return RESOLVED;
287 }
288 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530289 * If the effective built-in type is of type string calls for
290 * string resolution.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530291 */
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 saraswal96dfef02016-06-16 00:29:12 +0530297 * resolve range/string restriction will throw exception in
298 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530299 */
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 saraswal96dfef02016-06-16 00:29:12 +0530309 * resolve range/string restriction will throw exception in
310 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530311 */
312 return RESOLVED;
313 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530314 } 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 saraswal96dfef02016-06-16 00:29:12 +0530319 * resolve length restriction will throw exception in
320 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530321 */
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 saraswal96dfef02016-06-16 00:29:12 +0530331 * resolve length restriction will throw exception in
332 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530333 */
334 return RESOLVED;
335 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530336 }
337 } else {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530338 setEffectiveBuiltInType(baseType.getDataType());
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530339 /*
340 * Check whether the effective built-in type can have range
341 * restrictions, if yes call resolution of range.
342 */
343 if (isOfRangeRestrictedType(getEffectiveBuiltInType())) {
344 if (baseType.getDataTypeExtendedInfo() == null) {
345 resolveRangeRestriction(null);
346 /*
347 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530348 * resolve range/string restriction will throw exception in
349 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530350 */
351 return RESOLVED;
352 } else {
353 if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
354 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
355 "type.");
356 }
357 resolveRangeRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
358 /*
359 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530360 * resolve range/string restriction will throw exception in
361 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530362 */
363 return RESOLVED;
364 }
365 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530366 * If the effective built-in type is of type string calls for
367 * string resolution.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530368 */
369 } else if (getEffectiveBuiltInType() == STRING) {
370 if (baseType.getDataTypeExtendedInfo() == null) {
371 resolveStringRestriction(null);
372 /*
373 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530374 * resolve range/string restriction will throw exception in
375 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530376 */
377 return RESOLVED;
378 } else {
379 if (!(baseType.getDataTypeExtendedInfo() instanceof YangStringRestriction)) {
380 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
381 "type.");
382 }
383 resolveStringRestriction((YangStringRestriction) baseType.getDataTypeExtendedInfo());
384 /*
385 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530386 * resolve range/string restriction will throw exception in
387 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530388 */
389 return RESOLVED;
390 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530391 } else if (getEffectiveBuiltInType() == BINARY) {
392 if (baseType.getDataTypeExtendedInfo() == null) {
393 resolveLengthRestriction(null);
394 /*
395 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530396 * resolve length restriction will throw exception in
397 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530398 */
399 return RESOLVED;
400 } else {
401 if (!(baseType.getDataTypeExtendedInfo() instanceof YangRangeRestriction)) {
402 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
403 "type.");
404 }
405 resolveLengthRestriction((YangRangeRestriction) baseType.getDataTypeExtendedInfo());
406 /*
407 * Return the resolution status as resolved, if it's not
Bharat saraswal96dfef02016-06-16 00:29:12 +0530408 * resolve length restriction will throw exception in
409 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530410 */
411 return RESOLVED;
412 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530413 }
414 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530415
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530416 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530417 * Check if the data type is the one which can't be restricted, in this
418 * case check whether no self restrictions should be present.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530419 */
420 if (isOfValidNonRestrictedType(getEffectiveBuiltInType())) {
421 if (Strings.isNullOrEmpty(getLengthRestrictionString())
422 && Strings.isNullOrEmpty(getRangeRestrictionString())
423 && getPatternRestriction() == null) {
424 return RESOLVED;
425 } else {
426 throw new DataModelException("YANG file error: Restrictions can't be applied to a given type");
427 }
428 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530429
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530430 // Throw exception for unsupported types
431 throw new DataModelException("Linker error: Unable to process the derived type.");
432 }
433
434 /**
435 * Resolves the string restrictions.
436 *
437 * @param refStringRestriction referred string restriction of typedef
438 * @throws DataModelException a violation in data model rule
439 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530440 private void resolveStringRestriction(YangStringRestriction refStringRestriction)
441 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530442 YangStringRestriction curStringRestriction = null;
443 YangRangeRestriction refRangeRestriction = null;
444 YangPatternRestriction refPatternRestriction = null;
445
446 /*
447 * Check that range restriction should be null when built-in type is
448 * string.
449 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530450 if (!Strings.isNullOrEmpty(getRangeRestrictionString())) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530451 DataModelException dataModelException = new DataModelException("YANG file error: Range restriction " +
452 "should't be present for string data type.");
453 dataModelException.setLine(lineNumber);
454 dataModelException.setCharPosition(charPositionInLine);
455 throw dataModelException;
456 }
457
458 /*
459 * If referred restriction and self restriction both are null, no
460 * resolution is required.
461 */
462 if (refStringRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())
463 && getPatternRestriction() == null) {
464 return;
465 }
466
467 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530468 * If referred string restriction is not null, take value of length and
469 * pattern restriction and assign.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530470 */
471 if (refStringRestriction != null) {
472 refRangeRestriction = refStringRestriction.getLengthRestriction();
473 refPatternRestriction = refStringRestriction.getPatternRestriction();
474 }
475
476 YangRangeRestriction lengthRestriction = resolveLengthRestriction(refRangeRestriction);
477 YangPatternRestriction patternRestriction = resolvePatternRestriction(refPatternRestriction);
478
479 /*
480 * Check if either of length or pattern restriction is present, if yes
481 * create string restriction and assign value.
482 */
483 if (lengthRestriction != null || patternRestriction != null) {
484 curStringRestriction = new YangStringRestriction();
485 curStringRestriction.setLengthRestriction(lengthRestriction);
486 curStringRestriction.setPatternRestriction(patternRestriction);
487 }
488 setResolvedExtendedInfo((T) curStringRestriction);
489 }
490
491 /**
492 * Resolves pattern restriction.
493 *
494 * @param refPatternRestriction referred pattern restriction of typedef
495 * @return resolved pattern restriction
496 */
497 private YangPatternRestriction resolvePatternRestriction(YangPatternRestriction refPatternRestriction) {
498 /*
499 * If referred restriction and self restriction both are null, no
500 * resolution is required.
501 */
502 if (refPatternRestriction == null && getPatternRestriction() == null) {
503 return null;
504 }
505
506 /*
507 * If self restriction is null, and referred restriction is present
508 * shallow copy the referred to self.
509 */
510 if (getPatternRestriction() == null) {
511 return refPatternRestriction;
512 }
513
514 /*
515 * If referred restriction is null, and self restriction is present
516 * carry out self resolution.
517 */
518 if (refPatternRestriction == null) {
519 return getPatternRestriction();
520 }
521
522 /*
523 * Get patterns of referred type and add it to current pattern
524 * restrictions.
525 */
526 for (String pattern : refPatternRestriction.getPatternList()) {
527 getPatternRestriction().addPattern(pattern);
528 }
529 return getPatternRestriction();
530 }
531
532 /**
533 * Resolves the length restrictions.
534 *
535 * @param refLengthRestriction referred length restriction of typedef
536 * @return resolved length restriction
537 * @throws DataModelException a violation in data model rule
538 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530539 private YangRangeRestriction resolveLengthRestriction(YangRangeRestriction refLengthRestriction)
Bharat saraswal96dfef02016-06-16 00:29:12 +0530540 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530541
542 /*
543 * If referred restriction and self restriction both are null, no
544 * resolution is required.
545 */
546 if (refLengthRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())) {
547 return null;
548 }
549
550 /*
551 * If self restriction is null, and referred restriction is present
552 * shallow copy the referred to self.
553 */
554 if (Strings.isNullOrEmpty(getLengthRestrictionString())) {
555 return refLengthRestriction;
556 }
557
558 /*
559 * If referred restriction is null, and self restriction is present
560 * carry out self resolution.
561 */
562 if (refLengthRestriction == null) {
563 YangRangeRestriction curLengthRestriction = processLengthRestriction(null, lineNumber,
564 charPositionInLine, false, getLengthRestrictionString());
565 return curLengthRestriction;
566 }
567
568 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530569 * Carry out self resolution based with obtained effective built-in type
570 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530571 */
572 YangRangeRestriction curLengthRestriction = processLengthRestriction(refLengthRestriction, lineNumber,
573 charPositionInLine, true, getLengthRestrictionString());
574
575 // Resolve the range with referred typedef's restriction.
576 resolveLengthAndRangeRestriction(refLengthRestriction, curLengthRestriction);
577 return curLengthRestriction;
578 }
579
580 /**
581 * Resolves the length/range self and referred restriction, to check whether
582 * the all the range interval in self restriction is stricter than the
583 * referred typedef's restriction.
584 *
585 * @param refRestriction referred restriction
586 * @param curRestriction self restriction
587 */
588 private void resolveLengthAndRangeRestriction(YangRangeRestriction refRestriction,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530589 YangRangeRestriction curRestriction)
590 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530591 for (Object curInterval : curRestriction.getAscendingRangeIntervals()) {
592 if (!(curInterval instanceof YangRangeInterval)) {
593 throw new DataModelException("Linker error: Current range intervals not processed correctly.");
594 }
595 try {
596 refRestriction.isValidInterval((YangRangeInterval) curInterval);
597 } catch (DataModelException e) {
598 DataModelException dataModelException = new DataModelException(e);
599 dataModelException.setLine(lineNumber);
600 dataModelException.setCharPosition(charPositionInLine);
601 throw dataModelException;
602 }
603 }
604 }
605
606 /**
607 * Resolves the range restrictions.
608 *
609 * @param refRangeRestriction referred range restriction of typedef
610 * @throws DataModelException a violation in data model rule
611 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530612 private void resolveRangeRestriction(YangRangeRestriction refRangeRestriction)
613 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530614
615 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530616 * Check that string restriction should be null when built-in type is of
617 * range type.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530618 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530619 if (!Strings.isNullOrEmpty(getLengthRestrictionString()) || getPatternRestriction() != null) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530620 DataModelException dataModelException = new DataModelException("YANG file error: Length/Pattern " +
621 "restriction should't be present for int/uint/decimal data type.");
622 dataModelException.setLine(lineNumber);
623 dataModelException.setCharPosition(charPositionInLine);
624 throw dataModelException;
625 }
626
627 /*
628 * If referred restriction and self restriction both are null, no
629 * resolution is required.
630 */
631 if (refRangeRestriction == null && Strings.isNullOrEmpty(getRangeRestrictionString())) {
632 return;
633 }
634
635 /*
636 * If self restriction is null, and referred restriction is present
637 * shallow copy the referred to self.
638 */
639 if (Strings.isNullOrEmpty(getRangeRestrictionString())) {
640 setResolvedExtendedInfo((T) refRangeRestriction);
641 return;
642 }
643
644 /*
645 * If referred restriction is null, and self restriction is present
646 * carry out self resolution.
647 */
648 if (refRangeRestriction == null) {
649 YangRangeRestriction curRangeRestriction = processRangeRestriction(null, lineNumber,
650 charPositionInLine, false, getRangeRestrictionString(), getEffectiveBuiltInType());
651 setResolvedExtendedInfo((T) curRangeRestriction);
652 return;
653 }
654
655 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530656 * Carry out self resolution based with obtained effective built-in type
657 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530658 */
659 YangRangeRestriction curRangeRestriction = processRangeRestriction(refRangeRestriction, lineNumber,
660 charPositionInLine, true, getRangeRestrictionString(), getEffectiveBuiltInType());
661
662 // Resolve the range with referred typedef's restriction.
663 resolveLengthAndRangeRestriction(refRangeRestriction, curRangeRestriction);
664 setResolvedExtendedInfo((T) curRangeRestriction);
665 }
666
667 /**
668 * Returns whether the data type is of non restricted type.
669 *
670 * @param dataType data type to be checked
671 * @return true, if data type can't be restricted, false otherwise
672 */
673 private boolean isOfValidNonRestrictedType(YangDataTypes dataType) {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530674 return dataType == BOOLEAN
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530675 || dataType == ENUMERATION
676 || dataType == BITS
677 || dataType == EMPTY
678 || dataType == UNION
679 || dataType == IDENTITYREF
Bharat saraswalcad0e652016-05-26 23:48:38 +0530680 || dataType == LEAFREF;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530681 }
682}