2 * ========================LICENSE_START=================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
22 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
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;
44 import java.util.ArrayList;
45 import java.util.List;
48 @RestController("ricRepositoryControllerV2")
49 @RequiredArgsConstructor
51 name = RicRepositoryController.API_NAME, //
52 description = RicRepositoryController.API_DESCRIPTION //
54 public class RicRepositoryController implements NearRtRicRepositoryApi {
56 public static final String API_NAME = "NearRT-RIC Repository";
57 public static final String API_DESCRIPTION = "";
59 private final Rics rics;
60 final PolicyTypes types;
61 final ObjectMapper objectMapper;
63 private static final Gson gson = new GsonBuilder().create();
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;
72 public Mono<ResponseEntity<RicInfo>> getRic(
73 final String managedElementId, final String ricId, final ServerWebExchange exchange)
76 helper.validateQueryParameters(exchange, Set.of("managed_element_id", "ric_id"));
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));
87 throw new InvalidRequestException("Only one parameter allowed");
91 static final String QUERY_RIC_INFO_DETAILS =
92 "The call returns all Near-RT RICs that supports a given policy type identity";
95 public Mono<ResponseEntity<RicInfoList>> getRics(final String supportingPolicyType, final ServerWebExchange exchange)
97 if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) {
98 throw new EntityNotFoundException("Policy type not found");
101 List<RicInfo> result = new ArrayList<>();
102 for (Ric ric : rics.getRics()) {
103 if (supportingPolicyType == null || ric.isSupportingType(supportingPolicyType)) {
104 result.add(toRicInfo(ric));
108 return Mono.just(new ResponseEntity<>(new RicInfoList().rics(result), HttpStatus.OK));
111 private RicInfo.StateEnum toRicState(Ric.RicState state) {
114 return RicInfo.StateEnum.AVAILABLE;
115 case CONSISTENCY_CHECK:
116 return RicInfo.StateEnum.CONSISTENCY_CHECK;
118 return RicInfo.StateEnum.SYNCHRONIZING;
120 return RicInfo.StateEnum.UNAVAILABLE;
122 return RicInfo.StateEnum.UNAVAILABLE;
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()));