blob: 7a7439f53be38233d4f7af527aed09112004dd78 [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) {
481 if ((refDerivedInfo.getResolvedExtendedInfo() == null) ||
482 (((YangDecimal64) refDerivedInfo.getResolvedExtendedInfo())
483 .getRangeRestrictedExtendedInfo() == null)) {
484 resolveRangeRestriction(null);
485 /*
486 * Return the resolution status as resolved, if it's not;
487 * resolve range restriction will throw exception in
488 * previous function.
489 */
490 return RESOLVED;
491 } else {
492 if (!(((YangDecimal64) refDerivedInfo.getResolvedExtendedInfo())
493 .getRangeRestrictedExtendedInfo() instanceof YangRangeRestriction)) {
494 throw new DataModelException("Linker error: Referred typedef restriction info is of invalid " +
495 "type.");
496 }
497 resolveRangeRestriction((YangRangeRestriction) ((YangDecimal64) refDerivedInfo
498 .getResolvedExtendedInfo()).getRangeRestrictedExtendedInfo());
499 /*
500 * Return the resolution status as resolved, if it's not
501 * resolve range/string restriction will throw exception in
502 * previous function.
503 */
504 return RESOLVED;
505 }
506 }
507
508 return null;
509 }
510
511 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530512 * Resolves the string restrictions.
513 *
514 * @param refStringRestriction referred string restriction of typedef
515 * @throws DataModelException a violation in data model rule
516 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530517 private void resolveStringRestriction(YangStringRestriction refStringRestriction)
518 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530519 YangStringRestriction curStringRestriction = null;
520 YangRangeRestriction refRangeRestriction = null;
521 YangPatternRestriction refPatternRestriction = null;
522
523 /*
524 * Check that range restriction should be null when built-in type is
525 * string.
526 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530527 if (!Strings.isNullOrEmpty(getRangeRestrictionString())) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530528 DataModelException dataModelException = new DataModelException("YANG file error: Range restriction " +
529 "should't be present for string data type.");
530 dataModelException.setLine(lineNumber);
531 dataModelException.setCharPosition(charPositionInLine);
532 throw dataModelException;
533 }
534
535 /*
536 * If referred restriction and self restriction both are null, no
537 * resolution is required.
538 */
539 if (refStringRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())
540 && getPatternRestriction() == null) {
541 return;
542 }
543
544 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530545 * If referred string restriction is not null, take value of length and
546 * pattern restriction and assign.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530547 */
548 if (refStringRestriction != null) {
549 refRangeRestriction = refStringRestriction.getLengthRestriction();
550 refPatternRestriction = refStringRestriction.getPatternRestriction();
551 }
552
553 YangRangeRestriction lengthRestriction = resolveLengthRestriction(refRangeRestriction);
554 YangPatternRestriction patternRestriction = resolvePatternRestriction(refPatternRestriction);
555
556 /*
557 * Check if either of length or pattern restriction is present, if yes
558 * create string restriction and assign value.
559 */
560 if (lengthRestriction != null || patternRestriction != null) {
561 curStringRestriction = new YangStringRestriction();
562 curStringRestriction.setLengthRestriction(lengthRestriction);
563 curStringRestriction.setPatternRestriction(patternRestriction);
564 }
565 setResolvedExtendedInfo((T) curStringRestriction);
566 }
567
568 /**
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530569 * Resolves the binary restrictions.
570 *
571 * @param refLengthRestriction referred length restriction of typedef
572 * @throws DataModelException a violation in data model rule
573 */
574 private void resolveBinaryRestriction(YangRangeRestriction refLengthRestriction)
575 throws DataModelException {
576
577 if (rangeRestrictionString != null || patternRestriction != null) {
578 DataModelException dataModelException =
579 new DataModelException("YANG file error: for binary " +
580 "range restriction or pattern restriction is not allowed.");
581 dataModelException.setLine(lineNumber);
582 dataModelException.setCharPosition(charPositionInLine);
583 throw dataModelException;
584 }
585
586 // Get the final resolved length restriction
587 YangRangeRestriction lengthRestriction = resolveLengthRestriction(refLengthRestriction);
588 // Set the lenght restriction.
589 setResolvedExtendedInfo((T) lengthRestriction);
590 }
591
592 /**
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530593 * Resolves pattern restriction.
594 *
595 * @param refPatternRestriction referred pattern restriction of typedef
596 * @return resolved pattern restriction
597 */
598 private YangPatternRestriction resolvePatternRestriction(YangPatternRestriction refPatternRestriction) {
599 /*
600 * If referred restriction and self restriction both are null, no
601 * resolution is required.
602 */
603 if (refPatternRestriction == null && getPatternRestriction() == null) {
604 return null;
605 }
606
607 /*
608 * If self restriction is null, and referred restriction is present
609 * shallow copy the referred to self.
610 */
611 if (getPatternRestriction() == null) {
612 return refPatternRestriction;
613 }
614
615 /*
616 * If referred restriction is null, and self restriction is present
617 * carry out self resolution.
618 */
619 if (refPatternRestriction == null) {
620 return getPatternRestriction();
621 }
622
623 /*
624 * Get patterns of referred type and add it to current pattern
625 * restrictions.
626 */
627 for (String pattern : refPatternRestriction.getPatternList()) {
628 getPatternRestriction().addPattern(pattern);
629 }
630 return getPatternRestriction();
631 }
632
633 /**
634 * Resolves the length restrictions.
635 *
636 * @param refLengthRestriction referred length restriction of typedef
637 * @return resolved length restriction
638 * @throws DataModelException a violation in data model rule
639 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530640 private YangRangeRestriction resolveLengthRestriction(YangRangeRestriction refLengthRestriction)
Bharat saraswal96dfef02016-06-16 00:29:12 +0530641 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530642
643 /*
644 * If referred restriction and self restriction both are null, no
645 * resolution is required.
646 */
647 if (refLengthRestriction == null && Strings.isNullOrEmpty(getLengthRestrictionString())) {
648 return null;
649 }
650
651 /*
652 * If self restriction is null, and referred restriction is present
653 * shallow copy the referred to self.
654 */
655 if (Strings.isNullOrEmpty(getLengthRestrictionString())) {
656 return refLengthRestriction;
657 }
658
659 /*
660 * If referred restriction is null, and self restriction is present
661 * carry out self resolution.
662 */
663 if (refLengthRestriction == null) {
664 YangRangeRestriction curLengthRestriction = processLengthRestriction(null, lineNumber,
665 charPositionInLine, false, getLengthRestrictionString());
666 return curLengthRestriction;
667 }
668
669 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530670 * Carry out self resolution based with obtained effective built-in type
671 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530672 */
673 YangRangeRestriction curLengthRestriction = processLengthRestriction(refLengthRestriction, lineNumber,
674 charPositionInLine, true, getLengthRestrictionString());
675
676 // Resolve the range with referred typedef's restriction.
677 resolveLengthAndRangeRestriction(refLengthRestriction, curLengthRestriction);
678 return curLengthRestriction;
679 }
680
681 /**
682 * Resolves the length/range self and referred restriction, to check whether
683 * the all the range interval in self restriction is stricter than the
684 * referred typedef's restriction.
685 *
686 * @param refRestriction referred restriction
687 * @param curRestriction self restriction
688 */
689 private void resolveLengthAndRangeRestriction(YangRangeRestriction refRestriction,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530690 YangRangeRestriction curRestriction)
691 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530692 for (Object curInterval : curRestriction.getAscendingRangeIntervals()) {
693 if (!(curInterval instanceof YangRangeInterval)) {
694 throw new DataModelException("Linker error: Current range intervals not processed correctly.");
695 }
696 try {
697 refRestriction.isValidInterval((YangRangeInterval) curInterval);
698 } catch (DataModelException e) {
699 DataModelException dataModelException = new DataModelException(e);
700 dataModelException.setLine(lineNumber);
701 dataModelException.setCharPosition(charPositionInLine);
702 throw dataModelException;
703 }
704 }
705 }
706
707 /**
708 * Resolves the range restrictions.
709 *
710 * @param refRangeRestriction referred range restriction of typedef
711 * @throws DataModelException a violation in data model rule
712 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530713 private void resolveRangeRestriction(YangRangeRestriction refRangeRestriction)
714 throws DataModelException {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530715
716 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530717 * Check that string restriction should be null when built-in type is of
718 * range type.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530719 */
Bharat saraswalcad0e652016-05-26 23:48:38 +0530720 if (!Strings.isNullOrEmpty(getLengthRestrictionString()) || getPatternRestriction() != null) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530721 DataModelException dataModelException = new DataModelException("YANG file error: Length/Pattern " +
722 "restriction should't be present for int/uint/decimal data type.");
723 dataModelException.setLine(lineNumber);
724 dataModelException.setCharPosition(charPositionInLine);
725 throw dataModelException;
726 }
727
728 /*
729 * If referred restriction and self restriction both are null, no
730 * resolution is required.
731 */
732 if (refRangeRestriction == null && Strings.isNullOrEmpty(getRangeRestrictionString())) {
733 return;
734 }
735
736 /*
737 * If self restriction is null, and referred restriction is present
738 * shallow copy the referred to self.
739 */
740 if (Strings.isNullOrEmpty(getRangeRestrictionString())) {
741 setResolvedExtendedInfo((T) refRangeRestriction);
742 return;
743 }
744
745 /*
746 * If referred restriction is null, and self restriction is present
747 * carry out self resolution.
748 */
749 if (refRangeRestriction == null) {
750 YangRangeRestriction curRangeRestriction = processRangeRestriction(null, lineNumber,
751 charPositionInLine, false, getRangeRestrictionString(), getEffectiveBuiltInType());
752 setResolvedExtendedInfo((T) curRangeRestriction);
753 return;
754 }
755
756 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530757 * Carry out self resolution based with obtained effective built-in type
758 * and MIN/MAX values as per the referred typedef's values.
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530759 */
760 YangRangeRestriction curRangeRestriction = processRangeRestriction(refRangeRestriction, lineNumber,
761 charPositionInLine, true, getRangeRestrictionString(), getEffectiveBuiltInType());
762
763 // Resolve the range with referred typedef's restriction.
764 resolveLengthAndRangeRestriction(refRangeRestriction, curRangeRestriction);
765 setResolvedExtendedInfo((T) curRangeRestriction);
766 }
767
768 /**
769 * Returns whether the data type is of non restricted type.
770 *
771 * @param dataType data type to be checked
772 * @return true, if data type can't be restricted, false otherwise
773 */
774 private boolean isOfValidNonRestrictedType(YangDataTypes dataType) {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530775 return dataType == BOOLEAN
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530776 || dataType == ENUMERATION
777 || dataType == BITS
778 || dataType == EMPTY
779 || dataType == UNION
780 || dataType == IDENTITYREF
Bharat saraswalcad0e652016-05-26 23:48:38 +0530781 || dataType == LEAFREF;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530782 }
783}