53abcd71c8334641b0d4c2b79d3fa42b31922607
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2023 Nordix Foundation. All rights reserved.
6  * Modifications Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
7  * ======================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21
22 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import io.swagger.v3.oas.annotations.tags.Tag;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.NearRtRicRepositoryApi;
30 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException;
31 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.InvalidRequestException;
32 import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.RicInfo;
33 import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.RicInfoList;
34 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
35 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
36 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
37 import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.RestController;
41 import org.springframework.web.server.ServerWebExchange;
42 import reactor.core.publisher.Mono;
43
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.Set;
47
48 @RestController("ricRepositoryControllerV2")
49 @RequiredArgsConstructor
50 @Tag( //
51         name = RicRepositoryController.API_NAME, //
52         description = RicRepositoryController.API_DESCRIPTION //
53 )
54 public class RicRepositoryController implements NearRtRicRepositoryApi {
55
56     public static final String API_NAME = "NearRT-RIC Repository";
57     public static final String API_DESCRIPTION = "";
58
59     private final Rics rics;
60     final PolicyTypes types;
61     final ObjectMapper objectMapper;
62
63     private static final Gson gson = new GsonBuilder().create();
64
65     private static final String GET_RIC_BRIEF = "Returns info for one Near-RT RIC";
66     private static final String GET_RIC_DETAILS =
67             "Either a Near-RT RIC identity or a Managed Element identity can be specified.<br>" //
68                     + "The intention with Managed Element identity is the ID used in O1 for accessing the traffical element (such as the ID of CU).";
69     private final Helper helper;
70
71     @Override
72     public Mono<ResponseEntity<RicInfo>> getRic(
73             final String managedElementId, final String ricId, final ServerWebExchange exchange)
74             throws Exception {
75
76         helper.validateQueryParameters(exchange, Set.of("managed_element_id", "ric_id"));
77
78         if (managedElementId != null && ricId != null) {
79             throw new InvalidRequestException("Only one parameter allowed");
80         } else if (managedElementId != null) {
81             Ric ric = this.rics.lookupRicForManagedElement(managedElementId);
82             return Mono.just(new ResponseEntity<>(toRicInfo(ric), HttpStatus.OK));
83         } else if (ricId != null) {
84             RicInfo info = toRicInfo(this.rics.getRic(ricId));
85             return Mono.just(new ResponseEntity<>(info, HttpStatus.OK));
86         } else {
87             throw new InvalidRequestException("Only one parameter allowed");
88         }
89     }
90
91     static final String QUERY_RIC_INFO_DETAILS =
92             "The call returns all Near-RT RICs that supports a given policy type identity";
93
94     @Override
95     public Mono<ResponseEntity<RicInfoList>> getRics(final String supportingPolicyType, final ServerWebExchange exchange)
96             throws Exception {
97         if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) {
98             throw new EntityNotFoundException("Policy type not found");
99         }
100
101         List<RicInfo> result = new ArrayList<>();
102         for (Ric ric : rics.getRics()) {
103             if (supportingPolicyType == null || ric.isSupportingType(supportingPolicyType)) {
104                 result.add(toRicInfo(ric));
105             }
106         }
107
108         return Mono.just(new ResponseEntity<>(new RicInfoList().rics(result), HttpStatus.OK));
109     }
110
111     private RicInfo.StateEnum toRicState(Ric.RicState state) {
112         switch (state) {
113             case AVAILABLE:
114                 return RicInfo.StateEnum.AVAILABLE;
115             case CONSISTENCY_CHECK:
116                 return RicInfo.StateEnum.CONSISTENCY_CHECK;
117             case SYNCHRONIZING:
118                 return RicInfo.StateEnum.SYNCHRONIZING;
119             case UNAVAILABLE:
120                 return RicInfo.StateEnum.UNAVAILABLE;
121             default:
122                 return RicInfo.StateEnum.UNAVAILABLE;
123         }
124     }
125
126     private RicInfo toRicInfo(Ric ric) {
127         return new RicInfo().ricId(ric.id())
128                 .managedElementIds((List<String>) ric.getManagedElementIds())
129                 .policytypeIds((List<String>) ric.getSupportedPolicyTypeNames())
130                 .state(toRicState(ric.getState()));
131     }
132 }