2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 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 org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.NearRtRicRepositoryApi;
28 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException;
29 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.InvalidRequestException;
30 import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.RicInfo;
31 import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.RicInfoList;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
33 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
34 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
35 import org.springframework.beans.factory.annotation.Autowired;
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")
47 name = RicRepositoryController.API_NAME, //
48 description = RicRepositoryController.API_DESCRIPTION //
50 public class RicRepositoryController implements NearRtRicRepositoryApi {
52 public static final String API_NAME = "NearRT-RIC Repository";
53 public static final String API_DESCRIPTION = "";
62 ObjectMapper objectMapper;
64 private static Gson gson = new GsonBuilder() //
67 private static final String GET_RIC_BRIEF = "Returns info for one Near-RT RIC";
68 private static final String GET_RIC_DETAILS =
69 "Either a Near-RT RIC identity or a Managed Element identity can be specified.<br>" //
70 + "The intention with Managed Element identity is the ID used in O1 for accessing the traffical element (such as the ID of CU).";
72 public Mono<ResponseEntity<Object>> getRic(
73 final String managedElementId, final String ricId, final ServerWebExchange exchange)
75 if (managedElementId != null && ricId != null) {
76 throw new InvalidRequestException("Give one query parameter");
77 } else if (managedElementId != null) {
78 Ric ric = this.rics.lookupRicForManagedElement(managedElementId);
79 return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(toRicInfo(ric)), HttpStatus.OK));
80 } else if (ricId != null) {
81 RicInfo info = toRicInfo(this.rics.getRic(ricId));
82 return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(info), HttpStatus.OK));
84 throw new InvalidRequestException("Give one query parameter");
88 static final String QUERY_RIC_INFO_DETAILS =
89 "The call returns all Near-RT RICs that supports a given policy type identity";
92 public Mono<ResponseEntity<Object>> getRics(final String supportingPolicyType, final ServerWebExchange exchange)
94 if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) {
95 throw new EntityNotFoundException("Policy type not found");
98 List<RicInfo> result = new ArrayList<>();
99 for (Ric ric : rics.getRics()) {
100 if (supportingPolicyType == null || ric.isSupportingType(supportingPolicyType)) {
101 result.add(toRicInfo(ric));
105 return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(new RicInfoList().rics(result)), HttpStatus.OK));
108 private RicInfo.StateEnum toRicState(Ric.RicState state) {
111 return RicInfo.StateEnum.AVAILABLE;
112 case CONSISTENCY_CHECK:
113 return RicInfo.StateEnum.CONSISTENCY_CHECK;
115 return RicInfo.StateEnum.SYNCHRONIZING;
117 return RicInfo.StateEnum.UNAVAILABLE;
119 return RicInfo.StateEnum.UNAVAILABLE;
123 private RicInfo toRicInfo(Ric ric) {
124 return new RicInfo().ricId(ric.id())
125 .managedElementIds((List<String>) ric.getManagedElementIds())
126 .policytypeIds((List<String>) ric.getSupportedPolicyTypeNames())
127 .state(toRicState(ric.getState()));