0b2b42ebde7428f8ff303c21e2499ab6f4f0fd8c
[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
26 import org.oran.smo.yangtools.parser.ParserExecutionContext;
27 import org.oran.smo.yangtools.parser.findings.Finding;
28 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
29 import org.oran.smo.yangtools.parser.model.statements.AbstractStatement;
30 import org.oran.smo.yangtools.parser.model.statements.ExtensionStatement;
31 import org.oran.smo.yangtools.parser.model.statements.StatementModuleAndName;
32 import org.oran.smo.yangtools.parser.model.statements.yang.CY;
33 import org.oran.smo.yangtools.parser.model.yangdom.YangDomElement;
34
35 public class YOranSmoTeivBiDirectionalTopologyRelationship extends ExtensionStatement {
36
37     public YOranSmoTeivBiDirectionalTopologyRelationship(final AbstractStatement parentStatement,
38             final YangDomElement domNode) {
39         super(parentStatement, domNode);
40     }
41
42     @Override
43     public StatementArgumentType getArgumentType() {
44         return StatementArgumentType.NAME;
45     }
46
47     @Override
48     public StatementModuleAndName getStatementModuleAndName() {
49         return CORAN.ORAN_SMO_TEIV_COMMON_YANG_EXTENSIONS__BI_DIRECTIONAL_TOPOLOGY_RELATIONSHIP;
50     }
51
52     @Override
53     public boolean argumentIsMandatory() {
54         return true;
55     }
56
57     @Override
58     public MaxCardinality getMaxCardinalityUnderParent() {
59         return MaxCardinality.MULTIPLE;
60     }
61
62     public String getRelationshipName() {
63         return getValue() != null ? getValue() : "";
64     }
65
66     private static final List<StatementModuleAndName> REQUIRED_PARENTS = Arrays.asList(CY.STMT_MODULE);
67
68     @Override
69     public boolean canBeChildOf(final StatementModuleAndName parentSman) {
70         return REQUIRED_PARENTS.contains(parentSman);
71     }
72
73     @Override
74     protected void validate(final ParserExecutionContext context) {
75         /*
76          * From o-ran-smo-teiv-common-yang-extensions:
77          *
78          * Defines a bi-directional relationship in the topology.
79          *
80          * A bi-directional-association (UDA) is a relationship comprising of an
81          * A-side and a B-side. The A-side is considered the originating side of
82          * the relationship; the B-side is considered the terminating side of the
83          * relationship. The order of A-side and B-side is of importance and MUST
84          * NOT be changed once defined.
85          *
86          * Both A-side and B-side are defined on a type, and are given a role. A
87          * type may have multiple originating and/or terminating sides of a
88          * relationship, all distinguished by role name.
89          *
90          * The statement MUST only be a substatement of the 'module' statement.
91          * Multiple 'bi-directional-topology-relationship' statements are allowed
92          * per parent statement.
93          *
94          * Substatements to the 'bi-directional-topology-relationship' define the
95          * A-side and the B-side, respectively, and optionally properties of the
96          * relationship. Data nodes of types 'leaf' and 'leaf-list' are used for
97          * this purpose. One of the data nodes MUST be annotated with the 'aSide'
98          * extension; another data node MUST be annotated with the 'bSide'
99          * extension. Other data nodes define properties of the relationship.
100          *
101          * The argument is the name of the relationship. The relationship name is
102          * scoped to the namespace of the declaring module and MUST be unique
103          * within the scope.";
104          */
105         validateArgumentNotNullNotEmpty(context);
106
107         checkParent(context);
108     }
109
110     @Override
111     protected void subtreeProcessed(final ParserExecutionContext context) {
112         /*
113          * We are checking that we have an a-side and a b-side.
114          */
115         final long aSides = getChildStatements().stream().filter(stmt -> stmt.is(CY.STMT_LEAF) || stmt.is(
116                 CY.STMT_LEAF_LIST)).filter(stmt -> stmt.hasAtLeastOneChildOf(
117                         CORAN.ORAN_SMO_TEIV_COMMON_YANG_EXTENSIONS__A_SIDE)).count();
118         final long bSides = getChildStatements().stream().filter(stmt -> stmt.is(CY.STMT_LEAF) || stmt.is(
119                 CY.STMT_LEAF_LIST)).filter(stmt -> stmt.hasAtLeastOneChildOf(
120                         CORAN.ORAN_SMO_TEIV_COMMON_YANG_EXTENSIONS__B_SIDE)).count();
121
122         if (aSides != 1) {
123             context.addFinding(new Finding(this, ParserFindingType.P025_INVALID_EXTENSION,
124                     "A 'bi-directional relationship' must have as child exactly a single leaf or leaf-list annotated with 'aSide'."));
125         }
126         if (bSides != 1) {
127             context.addFinding(new Finding(this, ParserFindingType.P025_INVALID_EXTENSION,
128                     "A 'bi-directional relationship' must have as child exactly a single leaf or leaf-list annotated with 'bSide'."));
129         }
130     }
131 }