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.oran;
23 import java.util.Arrays;
24 import java.util.List;
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;
35 public class YOranSmoTeivBiDirectionalTopologyRelationship extends ExtensionStatement {
37 public YOranSmoTeivBiDirectionalTopologyRelationship(final AbstractStatement parentStatement,
38 final YangDomElement domNode) {
39 super(parentStatement, domNode);
43 public StatementArgumentType getArgumentType() {
44 return StatementArgumentType.NAME;
48 public StatementModuleAndName getStatementModuleAndName() {
49 return CORAN.ORAN_SMO_TEIV_COMMON_YANG_EXTENSIONS__BI_DIRECTIONAL_TOPOLOGY_RELATIONSHIP;
53 public boolean argumentIsMandatory() {
58 public MaxCardinality getMaxCardinalityUnderParent() {
59 return MaxCardinality.MULTIPLE;
62 public String getRelationshipName() {
63 return getValue() != null ? getValue() : "";
66 private static final List<StatementModuleAndName> REQUIRED_PARENTS = Arrays.asList(CY.STMT_MODULE);
69 public boolean canBeChildOf(final StatementModuleAndName parentSman) {
70 return REQUIRED_PARENTS.contains(parentSman);
74 protected void validate(final ParserExecutionContext context) {
76 * From o-ran-smo-teiv-common-yang-extensions:
78 * Defines a bi-directional relationship in the topology.
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.
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.
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.
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.
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.";
105 validateArgumentNotNullNotEmpty(context);
107 checkParent(context);
111 protected void subtreeProcessed(final ParserExecutionContext context) {
113 * We are checking that we have an a-side and a b-side.
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();
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'."));
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'."));