2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Ericsson
4 * Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.model.statements.yang;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
27 import org.oran.smo.yangtools.parser.ParserExecutionContext;
28 import org.oran.smo.yangtools.parser.findings.Finding;
29 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
30 import org.oran.smo.yangtools.parser.model.statements.AbstractStatement;
31 import org.oran.smo.yangtools.parser.model.statements.StatementModuleAndName;
32 import org.oran.smo.yangtools.parser.model.yangdom.YangDomElement;
35 * Type-safe Yang core statement.
37 * @author Mark Hollmann
39 public class YLength extends AbstractStatement {
41 private static final long MIN_VALUE_FOR_LENGTH = 0;
42 private static final long MAX_VALUE_FOR_LENGTH = Long.MAX_VALUE;
44 public YLength(final AbstractStatement parentStatement, final YangDomElement domNode) {
45 super(parentStatement, domNode);
49 public StatementArgumentType getArgumentType() {
50 return StatementArgumentType.VALUE;
54 public StatementModuleAndName getStatementModuleAndName() {
55 return CY.STMT_LENGTH;
58 public String getLengthValue() {
59 return domElement.getTrimmedValueOrEmpty();
62 public YErrorAppTag getErrorAppTag() {
63 return getChild(CY.STMT_ERROR_APP_TAG);
66 public YErrorMessage getErrorMessage() {
67 return getChild(CY.STMT_ERROR_MESSAGE);
70 protected void validate(final ParserExecutionContext context) {
71 if (!validateArgumentNotNullNotEmpty(context)) {
72 /* no point trying to perform more validation */
76 validateBoundaries(context);
79 public void validateBoundaries(final ParserExecutionContext context) {
80 final List<BoundaryPair> boundaries = getBoundaries();
81 checkBoundaries(context, boundaries);
85 * Returns the boundaries for this length. "min" and "max" are zero / Long.MAX respectively.
87 public List<BoundaryPair> getBoundaries() {
91 * "A length range consists of an explicit value, or a lower bound, two
92 * consecutive dots "..", and an upper bound. Multiple values or ranges
93 * can be given, separated by "|". Length-restricting values MUST NOT
94 * be negative. If multiple values or ranges are given, they all MUST
95 * be disjoint and MUST be in ascending order.
97 * Official definition is:
99 * length-boundary = min-keyword / max-keyword / non-negative-integer-value
102 // length-arg = length-part *(optsep "|" optsep length-part)
104 // length-part = length-boundary [optsep ".." optsep length-boundary]
106 // length-boundary = min-keyword / max-keyword / non-negative-integer-value
108 // max-keyword = "max"
109 // min-keyword = "min"
113 final List<BoundaryPair> boundaries = new ArrayList<>();
114 final String stringefiedLengthValues = getLengthValue().trim();
115 if (stringefiedLengthValues.isEmpty() || stringefiedLengthValues.startsWith("|") || stringefiedLengthValues
117 return Collections.<BoundaryPair> emptyList();
120 final String[] lengths = stringefiedLengthValues.contains("|") ?
121 stringefiedLengthValues.split("\\|") :
122 new String[] { stringefiedLengthValues };
123 for (final String oneLength : lengths) {
125 if (oneLength.trim().startsWith("..") || oneLength.trim().endsWith("..")) {
126 return Collections.<BoundaryPair> emptyList();
129 final String[] boundary = oneLength.contains("..") ?
130 oneLength.split("\\.\\.") :
131 new String[] { oneLength, oneLength };
133 final String lowerBoundary = boundary[0].trim();
134 final String upperBoundary = boundary[1].trim();
137 if (lowerBoundary.equals("min")) {
138 lowerValue = MIN_VALUE_FOR_LENGTH;
139 } else if (lowerBoundary.equals("max")) {
140 lowerValue = MAX_VALUE_FOR_LENGTH;
142 lowerValue = Long.parseLong(lowerBoundary); // this may throw
146 if (upperBoundary.equals("min")) {
147 upperValue = MIN_VALUE_FOR_LENGTH;
148 } else if (upperBoundary.equals("max")) {
149 upperValue = MAX_VALUE_FOR_LENGTH;
151 upperValue = Long.parseLong(upperBoundary); // this may throw
154 boundaries.add(new BoundaryPair(lowerValue, upperValue));
159 } catch (final Exception ex) {
164 * If we get here then an issue has occurred somewhere. We return empty boundaries
165 * as these cannot be reliably established.
167 return Collections.<BoundaryPair> emptyList();
170 public void checkBoundaries(final ParserExecutionContext context, final List<BoundaryPair> boundaries) {
172 if (boundaries.isEmpty()) {
173 context.addFinding(new Finding(this, ParserFindingType.P053_INVALID_VALUE,
174 "value '" + getLengthValue() + "' not valid for length."));
180 * - Each value must be >= 0
181 * - In each pair, upper must be >= lower.
182 * - The lower of a pair must be >= the upper of the previous pair.
184 for (int i = 0; i < boundaries.size(); ++i) {
185 boolean allOk = true;
187 if (boundaries.get(i).lower < 0) {
190 if (boundaries.get(i).upper < 0) {
195 if (boundaries.get(i).lower > boundaries.get(i).upper) {
199 if (i > 0 && boundaries.get(i).lower <= boundaries.get(i - 1).upper) {
204 context.addFinding(new Finding(this, ParserFindingType.P053_INVALID_VALUE,
205 "value '" + getLengthValue() + "' not valid for 'length'."));
211 public boolean isWithinLengthBoundaries(final long toCheck) {
212 for (final BoundaryPair boundaryPair : getBoundaries()) {
213 if (toCheck >= boundaryPair.lower && toCheck <= boundaryPair.upper) {
220 public static class BoundaryPair {
221 public final long lower;
222 public final long upper;
224 public BoundaryPair(final long lower, final long upper) {