53c60d1e7088c203e0affe3e623a93947c095621
[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.oran;
22
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.regex.Pattern;
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.ExtensionStatement;
32 import org.oran.smo.yangtools.parser.model.statements.StatementModuleAndName;
33 import org.oran.smo.yangtools.parser.model.statements.yang.CY;
34 import org.oran.smo.yangtools.parser.model.yangdom.YangDomElement;
35
36 public class YOranSmoTeivLabel extends ExtensionStatement {
37
38     private int version;
39     private int release;
40     private int correction;
41
42     public YOranSmoTeivLabel(final AbstractStatement parentStatement, final YangDomElement domNode) {
43         super(parentStatement, domNode);
44     }
45
46     @Override
47     public StatementArgumentType getArgumentType() {
48         return StatementArgumentType.VALUE;
49     }
50
51     @Override
52     public StatementModuleAndName getStatementModuleAndName() {
53         return CORAN.ORAN_SMO_TEIV_COMMON_YANG_EXTENSIONS__LABEL;
54     }
55
56     @Override
57     public boolean argumentIsMandatory() {
58         return true;
59     }
60
61     public String getLabel() {
62         return getValue() != null ? getValue() : "";
63     }
64
65     public int getVersion() {
66         return version;
67     }
68
69     public int getRelease() {
70         return release;
71     }
72
73     public int getCorrection() {
74         return correction;
75     }
76
77     private static final List<StatementModuleAndName> REQUIRED_PARENTS = Arrays.asList(CY.STMT_REVISION);
78
79     @Override
80     public boolean canBeChildOf(final StatementModuleAndName parentSman) {
81         return REQUIRED_PARENTS.contains(parentSman);
82     }
83
84     private static final Pattern VALID_PATTERN = Pattern.compile("[0-9]+\\.[0-9]+\\.[0-9]+");
85
86     @Override
87     protected void validate(final ParserExecutionContext context) {
88         /*
89          * From o-ran-smo-teiv-common-yang-extensions:
90          *
91          * The label can be used to give modules and submodules a semantic version, in addition to their revision.
92          *
93          * The format of the label is 'x.y.z' – expressed as pattern, it is [0-9]+\.[0-9]+\.[0-9]+
94          *
95          * The statement MUST only be a substatement of the revision statement.  Zero or one revision label statements
96          * per parent statement are allowed.
97          *
98          * Revision labels MUST be unique amongst all revisions of a module or submodule.
99          */
100         validateArgumentNotNullNotEmpty(context);
101
102         final String label = getLabel();
103         if (!VALID_PATTERN.matcher(label).matches()) {
104             context.addFinding(new Finding(this, ParserFindingType.P025_INVALID_EXTENSION,
105                     "Label value must be in 'version.release.correction' format."));
106         } else {
107             version = Integer.parseInt(label.substring(0, label.indexOf('.')));
108             release = Integer.parseInt(label.substring(label.indexOf('.') + 1, label.lastIndexOf('.')));
109             correction = Integer.parseInt(label.substring(label.lastIndexOf('.') + 1));
110         }
111
112         checkParent(context);
113         checkCardinalityUnderParent(context, 1);
114     }
115 }