9045b2d352e84e6461cb181b586a2a81929bfab6
[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.threegpp;
22
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.oran.smo.yangtools.parser.ParserExecutionContext;
27 import org.oran.smo.yangtools.parser.model.statements.AbstractStatement;
28 import org.oran.smo.yangtools.parser.model.statements.ExtensionStatement;
29 import org.oran.smo.yangtools.parser.model.statements.StatementModuleAndName;
30 import org.oran.smo.yangtools.parser.model.statements.yang.CY;
31 import org.oran.smo.yangtools.parser.model.yangdom.YangDomElement;
32
33 /**
34  * Type-safe 3GPP statement.
35  *
36  * @author Mark Hollmann
37  */
38 public class Y3gppInitialValue extends ExtensionStatement {
39
40     public Y3gppInitialValue(final AbstractStatement parentStatement, final YangDomElement domNode) {
41         super(parentStatement, domNode);
42     }
43
44     @Override
45     public StatementArgumentType getArgumentType() {
46         return StatementArgumentType.VALUE;
47     }
48
49     @Override
50     public StatementModuleAndName getStatementModuleAndName() {
51         return C3GPP.THREEGPP_COMMON_YANG_EXTENSIONS__INITIAL_VALUE;
52     }
53
54     @Override
55     public boolean argumentIsMandatory() {
56         return true;
57     }
58
59     @Override
60     public boolean orderUnderParentMatters() {
61         return true;
62     }
63
64     public String getInitialValue() {
65         return getValue() != null ? getValue() : "";
66     }
67
68     @Override
69     public MaxCardinality getMaxCardinalityUnderParent() {
70         return getParentStatement().is(CY.STMT_LEAF) ? MaxCardinality.ONE : MaxCardinality.MULTIPLE;
71     }
72
73     private static final List<StatementModuleAndName> REQUIRED_PARENTS = Arrays.asList(CY.STMT_LEAF, CY.STMT_LEAF_LIST);
74
75     @Override
76     public boolean canBeChildOf(final StatementModuleAndName parentSman) {
77         return REQUIRED_PARENTS.contains(parentSman);
78     }
79
80     protected void validate(final ParserExecutionContext context) {
81         /*
82          * From module _3gpp-common-yang-extensions:
83          *
84          * Specifies a value that the system will set for a leaf
85          * leaf-list if a value is not specified for it when its parent list
86          * or container is created. The value has no effect in any other
87          * modification e.g. changing or removing the value.
88          *
89          * The description statement of the parent statement SHOULD contain
90          * the label 'Initial-value: ' followed by the text from the argument.
91          *
92          * The statement MUST only be a substatement of a leaf or leaf-list.
93          * The statement MUST NOT be present if the leaf or the leaf-list
94          * has a default statement or the type used for the data node
95          * has a default value.
96          * The statement MUST NOT be used for config=false data or in an
97          * action, rpc or notification.
98          * Zero or one initial-value statements are allowed for a leaf parent
99          * statement. Zero or more initial-value statements are allowed for a
100          * leaf-list parent statement. If the leaf-list is ordered-by user, the
101          * initial values are stored in the order they appear in the YANG definition.
102          * NO substatements are allowed.
103          *
104          * Always consider using a YANG-default statement instead.
105          *
106          * Modification of the initial-value is a non-backwards-compatible change.
107          *
108          * The argument specifies a single initial value for a leaf or leaf-list.
109          * The value MUST be part of the valuespace of the leaf/leaf-list.
110          * It follows the same rules as the argument of the default statement.
111          */
112         validateArgumentNotNull(context);
113         checkParentAlsoAllowDeviateOrRefine(context);
114
115         if (getParentStatement().is(CY.STMT_LEAF)) {
116             checkCardinalityUnderParent(context, 1);
117         }
118     }
119 }