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