f7872a38967fa1dcb1daa259e42ccc046fa4deb7
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
22
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;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 @RestController("ricRepositoryControllerV2")
46 @RequiredArgsConstructor
47 @Tag( //
48         name = RicRepositoryController.API_NAME, //
49         description = RicRepositoryController.API_DESCRIPTION //
50 )
51 public class RicRepositoryController implements NearRtRicRepositoryApi {
52
53     public static final String API_NAME = "NearRT-RIC Repository";
54     public static final String API_DESCRIPTION = "";
55
56     private final Rics rics;
57     final PolicyTypes types;
58     final ObjectMapper objectMapper;
59
60     private static final Gson gson = new GsonBuilder().create();
61
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).";
66     @Override
67     public Mono<ResponseEntity<RicInfo>> getRic(
68             final String managedElementId, final String ricId, final ServerWebExchange exchange)
69             throws Exception {
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));
78         } else {
79             throw new InvalidRequestException("Give one query parameter");
80         }
81     }
82
83     static final String QUERY_RIC_INFO_DETAILS =
84             "The call returns all Near-RT RICs that supports a given policy type identity";
85
86     @Override
87     public Mono<ResponseEntity<RicInfoList>> getRics(final String supportingPolicyType, final ServerWebExchange exchange)
88             throws Exception {
89         if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) {
90             throw new EntityNotFoundException("Policy type not found");
91         }
92
93         List<RicInfo> result = new ArrayList<>();
94         for (Ric ric : rics.getRics()) {
95             if (supportingPolicyType == null || ric.isSupportingType(supportingPolicyType)) {
96                 result.add(toRicInfo(ric));
97             }
98         }
99
100         return Mono.just(new ResponseEntity<>(new RicInfoList().rics(result), HttpStatus.OK));
101     }
102
103     private RicInfo.StateEnum toRicState(Ric.RicState state) {
104         switch (state) {
105             case AVAILABLE:
106                 return RicInfo.StateEnum.AVAILABLE;
107             case CONSISTENCY_CHECK:
108                 return RicInfo.StateEnum.CONSISTENCY_CHECK;
109             case SYNCHRONIZING:
110                 return RicInfo.StateEnum.SYNCHRONIZING;
111             case UNAVAILABLE:
112                 return RicInfo.StateEnum.UNAVAILABLE;
113             default:
114                 return RicInfo.StateEnum.UNAVAILABLE;
115         }
116     }
117
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()));
123     }
124 }