blob: 1661d969b11eb2e4beecf4a7cd2c58cbe2cbc8f3 [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;
35import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
36import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.EMPTY;
37import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.ENUMERATION;
38import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.IDENTITYREF;
39import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.LEAFREF;
40import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.STRING;
41import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UNION;
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 }
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530337 } else if ((baseType.getDataType() == LEAFREF) || (baseType.getDataType() == IDENTITYREF)) {
janani be18b5342016-07-13 21:06:41 +0530338 setEffectiveBuiltInType(baseType.getDataType());
339 return RESOLVED;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530340 } else {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530341 setEffectiveBuiltInType(baseType.getDataType());
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530342 /*
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 saraswal96dfef02016-06-16 00:29:12 +0530351 * resolve range/string restriction will throw exception in
352 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530353 */
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 saraswal96dfef02016-06-16 00:29:12 +0530363 * resolve range/string restriction will throw exception in
364 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530365 */
366 return RESOLVED;
367 }
368 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530369 * If the effective built-in type is of type string calls for
370 * string resolution.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530371 */
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 saraswal96dfef02016-06-16 00:29:12 +0530377 * resolve range/string restriction will throw exception in
378 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530379 */
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 saraswal96dfef02016-06-16 00:29:12 +0530389 * resolve range/string restriction will throw exception in
390 * previous function.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530391 */
392 return RESOLVED;
393 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530394 } 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 saraswal96dfef02016-06-16 00:29:12 +0530399 * resolve length restriction will throw exception in
400 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530401 */
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 saraswal96dfef02016-06-16 00:29:12 +0530411 * resolve length restriction will throw exception in
412 * previous function.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530413 */
414 return RESOLVED;
415 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530416 }
417 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530418 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530419 * 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 Agrawalcfa1c412016-05-03 00:41:48 +0530421 */
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 Agrawalcfa1c412016-05-03 00:41:48 +0530431 // 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-Huawei2ee9e7e2016-06-01 14:30:22 +0530441 private void resolveStringRestriction(YangStringRestriction refStringRestriction)
442 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530443 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 saraswalcad0e652016-05-26 23:48:38 +0530451 if (!Strings.isNullOrEmpty(getRangeRestrictionString())) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530452 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 saraswal96dfef02016-06-16 00:29:12 +0530469 * If referred string restriction is not null, take value of length and
470 * pattern restriction and assign.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530471 */
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-Huawei2ee9e7e2016-06-01 14:30:22 +0530540 private YangRangeRestriction resolveLengthRestriction(YangRangeRestriction refLengthRestriction)
Bharat saraswal96dfef02016-06-16 00:29:12 +0530541 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530542
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 saraswal96dfef02016-06-16 00:29:12 +0530570 * Carry out self resolution based with obtained effective built-in type
571 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530572 */
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-Huawei2ee9e7e2016-06-01 14:30:22 +0530590 YangRangeRestriction curRestriction)
591 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530592 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-Huawei2ee9e7e2016-06-01 14:30:22 +0530613 private void resolveRangeRestriction(YangRangeRestriction refRangeRestriction)
614 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530615
616 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530617 * Check that string restriction should be null when built-in type is of
618 * range type.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530619 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530620 if (!Strings.isNullOrEmpty(getLengthRestrictionString()) || getPatternRestriction() != null) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530621 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 saraswal96dfef02016-06-16 00:29:12 +0530657 * Carry out self resolution based with obtained effective built-in type
658 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530659 */
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 saraswalcad0e652016-05-26 23:48:38 +0530675 return dataType == BOOLEAN
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530676 || dataType == ENUMERATION
677 || dataType == BITS
678 || dataType == EMPTY
679 || dataType == UNION
680 || dataType == IDENTITYREF
Bharat saraswalcad0e652016-05-26 23:48:38 +0530681 || dataType == LEAFREF;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530682 }
683}