d433e4a7754f4b8bea6161a26b1a3cbf5ad75fb0
[smo/teiv.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.model.statements.yang;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26
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;
33
34 /**
35  * Type-safe Yang core statement.
36  *
37  * @author Mark Hollmann
38  */
39 public class YLength extends AbstractStatement {
40
41     private static final long MIN_VALUE_FOR_LENGTH = 0;
42     private static final long MAX_VALUE_FOR_LENGTH = Long.MAX_VALUE;
43
44     public YLength(final AbstractStatement parentStatement, final YangDomElement domNode) {
45         super(parentStatement, domNode);
46     }
47
48     @Override
49     public StatementArgumentType getArgumentType() {
50         return StatementArgumentType.VALUE;
51     }
52
53     @Override
54     public StatementModuleAndName getStatementModuleAndName() {
55         return CY.STMT_LENGTH;
56     }
57
58     public String getLengthValue() {
59         return domElement.getTrimmedValueOrEmpty();
60     }
61
62     public YErrorAppTag getErrorAppTag() {
63         return getChild(CY.STMT_ERROR_APP_TAG);
64     }
65
66     public YErrorMessage getErrorMessage() {
67         return getChild(CY.STMT_ERROR_MESSAGE);
68     }
69
70     protected void validate(final ParserExecutionContext context) {
71         if (!validateArgumentNotNullNotEmpty(context)) {
72             /* no point trying to perform more validation */
73             return;
74         }
75
76         validateBoundaries(context);
77     }
78
79     public void validateBoundaries(final ParserExecutionContext context) {
80         final List<BoundaryPair> boundaries = getBoundaries();
81         checkBoundaries(context, boundaries);
82     }
83
84     /**
85      * Returns the boundaries for this length. "min" and "max" are zero / Long.MAX respectively.
86      */
87     public List<BoundaryPair> getBoundaries() {
88         /*
89          * From the RFC:
90          *
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.
96          *
97          * Official definition is:
98          *
99          * length-boundary = min-keyword / max-keyword / non-negative-integer-value
100         // From the RFC:
101         //
102         // length-arg = length-part *(optsep "|" optsep length-part)
103         //
104         // length-part = length-boundary [optsep ".." optsep length-boundary]
105         //
106         // length-boundary = min-keyword / max-keyword / non-negative-integer-value
107         //
108         // max-keyword = "max"
109         // min-keyword = "min"
110          */
111
112         try {
113             final List<BoundaryPair> boundaries = new ArrayList<>();
114             final String stringefiedLengthValues = getLengthValue().trim();
115             if (stringefiedLengthValues.isEmpty() || stringefiedLengthValues.startsWith("|") || stringefiedLengthValues
116                     .endsWith("|")) {
117                 return Collections.<BoundaryPair> emptyList();
118             }
119
120             final String[] lengths = stringefiedLengthValues.contains("|") ?
121                     stringefiedLengthValues.split("\\|") :
122                     new String[] { stringefiedLengthValues };
123             for (final String oneLength : lengths) {
124
125                 if (oneLength.trim().startsWith("..") || oneLength.trim().endsWith("..")) {
126                     return Collections.<BoundaryPair> emptyList();
127                 }
128
129                 final String[] boundary = oneLength.contains("..") ?
130                         oneLength.split("\\.\\.") :
131                         new String[] { oneLength, oneLength };
132
133                 final String lowerBoundary = boundary[0].trim();
134                 final String upperBoundary = boundary[1].trim();
135
136                 long lowerValue = 0;
137                 if (lowerBoundary.equals("min")) {
138                     lowerValue = MIN_VALUE_FOR_LENGTH;
139                 } else if (lowerBoundary.equals("max")) {
140                     lowerValue = MAX_VALUE_FOR_LENGTH;
141                 } else {
142                     lowerValue = Long.parseLong(lowerBoundary);         // this may throw
143                 }
144
145                 long upperValue = 0;
146                 if (upperBoundary.equals("min")) {
147                     upperValue = MIN_VALUE_FOR_LENGTH;
148                 } else if (upperBoundary.equals("max")) {
149                     upperValue = MAX_VALUE_FOR_LENGTH;
150                 } else {
151                     upperValue = Long.parseLong(upperBoundary);         // this may throw
152                 }
153
154                 boundaries.add(new BoundaryPair(lowerValue, upperValue));
155             }
156
157             return boundaries;
158
159         } catch (final Exception ex) {
160             /* no-op */
161         }
162
163         /*
164          * If we get here then an issue has occurred somewhere. We return empty boundaries
165          * as these cannot be reliably established.
166          */
167         return Collections.<BoundaryPair> emptyList();
168     }
169
170     public void checkBoundaries(final ParserExecutionContext context, final List<BoundaryPair> boundaries) {
171
172         if (boundaries.isEmpty()) {
173             context.addFinding(new Finding(this, ParserFindingType.P053_INVALID_VALUE,
174                     "value '" + getLengthValue() + "' not valid for length."));
175             return;
176         }
177
178         /*
179          * Rules:
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.
183          */
184         for (int i = 0; i < boundaries.size(); ++i) {
185             boolean allOk = true;
186
187             if (boundaries.get(i).lower < 0) {
188                 allOk = false;
189             }
190             if (boundaries.get(i).upper < 0) {
191                 allOk = false;
192             }
193
194             // lower <= upper ?
195             if (boundaries.get(i).lower > boundaries.get(i).upper) {
196                 allOk = false;
197             }
198
199             if (i > 0 && boundaries.get(i).lower <= boundaries.get(i - 1).upper) {
200                 allOk = false;
201             }
202
203             if (!allOk) {
204                 context.addFinding(new Finding(this, ParserFindingType.P053_INVALID_VALUE,
205                         "value '" + getLengthValue() + "' not valid for 'length'."));
206                 return;
207             }
208         }
209     }
210
211     public boolean isWithinLengthBoundaries(final long toCheck) {
212         for (final BoundaryPair boundaryPair : getBoundaries()) {
213             if (toCheck >= boundaryPair.lower && toCheck <= boundaryPair.upper) {
214                 return true;
215             }
216         }
217         return false;
218     }
219
220     public static class BoundaryPair {
221         public final long lower;
222         public final long upper;
223
224         public BoundaryPair(final long lower, final long upper) {
225             this.lower = lower;
226             this.upper = upper;
227         }
228     }
229 }