aa3445795d469e9fcc89f0ef1484a340f037c663
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Modifications Copyright (C) 2025 OpenInfra Foundation Europe
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.oran.smo.ncmp_to_teiv_adapter.models;
21
22 import com.fasterxml.jackson.annotation.JsonAnySetter;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import lombok.Getter;
25 import lombok.extern.slf4j.Slf4j;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30
31 @Slf4j
32 public class ManagedElement extends AbstractEntity {
33
34     @Getter
35     private List<AbstractFunction> ranFunctions = new ArrayList<>();
36
37     @Override
38     @JsonAnySetter
39     public void setAdditionalProperty(String name, Object value) {
40         ObjectMapper mapper = new ObjectMapper();
41
42         try {
43             if ("_3gpp-nr-nrm-gnbdufunction:GNBDUFunction".equals(name)) {
44                 List<Object> list = (List<Object>) value;
45                 for (Object item : list) {
46                     GNBDUFunction du = mapper.convertValue(item, GNBDUFunction.class);
47                     ranFunctions.add(du);
48                 }
49             } else if ("_3gpp-nr-nrm-gnbcucpfunction:GNBCUCPFunction".equals(name)) {
50                 List<Object> list = (List<Object>) value;
51                 for (Object item : list) {
52                     GNBCUCPFunction cu = mapper.convertValue(item, GNBCUCPFunction.class);
53                     ranFunctions.add(cu);
54                 }
55             } else {
56                 getAdditionalProperties().put(name, value);
57             }
58         } catch (Exception e) {
59             log.error("{}", e);
60         }
61     }
62
63     @Override
64     public Map<String, Object> addTeivEntitiesAndRelationships(Map<String, List<Object>> entityMap,
65             Map<String, List<Object>> relationshipMap, String parenId) {
66         for (AbstractFunction ranFunction : getRanFunctions()) {
67             addRanFunctionEntitiesAndRelationships(ranFunction, entityMap, relationshipMap);
68             addRelationshipWithRanFunction(ranFunction, relationshipMap);
69         }
70         String managedElementId = "urn:oran:smo:teiv:" + getId();
71         return Map.of("id", managedElementId, "sourceIds", List.of(managedElementId));
72     }
73
74     private void addRanFunctionEntitiesAndRelationships(AbstractFunction ranFunction, Map<String, List<Object>> entityMap,
75             Map<String, List<Object>> relationshipMap) {
76         String ranFunctionType = ranFunction.getTeivEntityType();
77         if (!entityMap.containsKey(ranFunctionType)) {
78             entityMap.put(ranFunctionType, new ArrayList<>());
79         }
80         entityMap.get(ranFunctionType).add(ranFunction.addTeivEntitiesAndRelationships(entityMap, relationshipMap,
81                 getId()));
82     }
83
84     private void addRelationshipWithRanFunction(AbstractFunction ranFunction, Map<String, List<Object>> relationshipMap) {
85         String ranFunctionRelType = ranFunction.getTeivRelationshipWithManagedElement();
86         if (!relationshipMap.containsKey(ranFunctionRelType)) {
87             relationshipMap.put(ranFunctionRelType, new ArrayList<>());
88         }
89         relationshipMap.get(ranFunctionRelType).add(ranFunction.createRelationshipWithManagedElement(getId()));
90     }
91
92     @Override
93     public String getTeivEntityType() {
94         return "o-ran-smo-teiv-oam:ManagedElement";
95     }
96 }