225d2f719db1da0c8a3bc4f4a96f7312bb82cdd5
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020-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.clients;
22
23 import com.google.gson.FieldNamingPolicy;
24 import com.google.gson.GsonBuilder;
25
26 import java.lang.invoke.MethodHandles;
27 import java.nio.charset.StandardCharsets;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.Set;
32
33 import lombok.Getter;
34
35 import org.json.JSONObject;
36 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
37 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
38 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.web.reactive.function.client.WebClientResponseException;
44
45 import reactor.core.publisher.Flux;
46 import reactor.core.publisher.Mono;
47
48 /**
49  * Client for accessing the A1 adapter in the CCSDK in ONAP.
50  */
51 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
52 public class CcsdkA1AdapterClient implements A1Client {
53
54     static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC
55
56     @Getter
57     public static class AdapterRequest {
58         private String nearRtRicUrl = null;
59         private String body = null;
60
61         public AdapterRequest(String url, String body) {
62             this.nearRtRicUrl = url;
63             this.body = body;
64         }
65
66         public AdapterRequest() {}
67     }
68
69     @Getter
70     public static class AdapterOutput {
71         private String body = null;
72         private int httpStatus = 0;
73
74         public AdapterOutput(int status, String body) {
75             this.httpStatus = status;
76             this.body = body;
77         }
78
79         public AdapterOutput() {}
80     }
81
82     static com.google.gson.Gson gson = new GsonBuilder() //
83             .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
84             .create(); //
85
86     private static final String GET_POLICY_RPC = "getA1Policy";
87     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
88     private final ControllerConfig controllerConfig;
89     private final AsyncRestClient restClient;
90     private final RicConfig ricConfig;
91     private final A1ProtocolType protocolType;
92
93     /**
94      * Constructor that creates the REST client to use.
95      *
96      * @param protocolType the southbound protocol of the controller. Supported
97      *        protocols are CCSDK_A1_ADAPTER_STD_V1_1,
98      *        CCSDK_A1_ADAPTER_OSC_V1 and
99      *        CCSDK_A1_ADAPTER_STD_V2_0_0 with
100      * @param ricConfig the configuration of the Near-RT RIC to communicate
101      *        with
102      * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
103      *
104      * @throws IllegalArgumentException when the protocolType is wrong.
105      */
106     public CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
107             AsyncRestClientFactory restClientFactory) {
108         this(protocolType, ricConfig, controllerConfig,
109                 restClientFactory.createRestClientNoHttpProxy(controllerConfig.getBaseUrl() + "/rests/operations"));
110     }
111
112     /**
113      * Constructor where the REST client to use is provided.
114      *
115      * @param protocolType the southbound protocol of the controller. Supported
116      *        protocols are CCSDK_A1_ADAPTER_STD_V1_1,
117      *        CCSDK_A1_ADAPTER_OSC_V1 and
118      *        CCSDK_A1_ADAPTER_STD_V2_0_0 with
119      * @param ricConfig the configuration of the Near-RT RIC to communicate
120      *        with
121      * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
122      * @param restClient the REST client to use
123      *
124      * @throws IllegalArgumentException when the protocolType is illegal.
125      */
126     CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
127             AsyncRestClient restClient) {
128         if (A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1.equals(protocolType) //
129                 || A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1.equals(protocolType) //
130                 || A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0.equals(protocolType)) {
131             this.restClient = restClient;
132             this.ricConfig = ricConfig;
133             this.protocolType = protocolType;
134             this.controllerConfig = controllerConfig;
135             logger.debug("CcsdkA1AdapterClient for ric: {}, a1Controller: {}", ricConfig.getRicId(), controllerConfig);
136         } else {
137             logger.error("Not supported protocoltype: {}", protocolType);
138             throw new IllegalArgumentException("Not handeled protocolversion: " + protocolType);
139         }
140     }
141
142     @Override
143     public Mono<List<String>> getPolicyTypeIdentities() {
144         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
145             return Mono.just(Arrays.asList(""));
146         } else {
147             return post(GET_POLICY_RPC, getUriBuilder().createPolicyTypesUri(), Optional.empty()) //
148                     .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString) //
149                     .collectList();
150         }
151     }
152
153     @Override
154     public Mono<List<String>> getPolicyIdentities() {
155         return getPolicyIds() //
156                 .collectList();
157     }
158
159     @Override
160     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
161         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
162             return Mono.just("{}");
163         } else {
164             A1UriBuilder uri = this.getUriBuilder();
165             final String ricUrl = uri.createGetSchemaUri(policyTypeId);
166             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
167                     .flatMap(response -> extractCreateSchema(response, policyTypeId));
168         }
169     }
170
171     private Mono<String> extractCreateSchema(String controllerResponse, String policyTypeId) {
172         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
173             return OscA1Client.extractCreateSchema(controllerResponse, policyTypeId);
174         } else if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
175             return StdA1ClientVersion2.extractPolicySchema(controllerResponse, policyTypeId);
176         } else {
177             return Mono.error(new ServiceException("Not supported " + this.protocolType));
178         }
179     }
180
181     @Override
182     public Mono<String> putPolicy(Policy policy) {
183         String ricUrl = getUriBuilder().createPutPolicyUri(policy.getType().getId(), policy.getId(),
184                 policy.getStatusNotificationUri());
185         return post("putA1Policy", ricUrl, Optional.of(policy.getJson()));
186     }
187
188     @Override
189     public Mono<String> deletePolicy(Policy policy) {
190         return deletePolicyById(policy.getType().getId(), policy.getId());
191     }
192
193     @Override
194     public Flux<String> deleteAllPolicies(Set<String> excludePolicyIds) {
195         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
196             return getPolicyIds() //
197                     .filter(policyId -> !excludePolicyIds.contains(policyId)) //
198                     .flatMap(policyId -> deletePolicyById("", policyId), CONCURRENCY_RIC); //
199         } else {
200             A1UriBuilder uriBuilder = this.getUriBuilder();
201             return getPolicyTypeIdentities() //
202                     .flatMapMany(Flux::fromIterable) //
203                     .flatMap(type -> deleteAllInstancesForType(uriBuilder, type, excludePolicyIds), CONCURRENCY_RIC);
204         }
205     }
206
207     private Flux<String> getInstancesForType(A1UriBuilder uriBuilder, String type) {
208         return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty()) //
209                 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
210     }
211
212     private Flux<String> deleteAllInstancesForType(A1UriBuilder uriBuilder, String type, Set<String> excludePolicyIds) {
213         return getInstancesForType(uriBuilder, type) //
214                 .filter(policyId -> !excludePolicyIds.contains(policyId)) //
215                 .flatMap(policyId -> deletePolicyById(type, policyId), CONCURRENCY_RIC);
216     }
217
218     @Override
219     public Mono<A1ProtocolType> getProtocolVersion() {
220         return tryStdProtocolVersion2() //
221                 .onErrorResume(t -> tryStdProtocolVersion1()) //
222                 .onErrorResume(t -> tryOscProtocolVersion());
223     }
224
225     @Override
226     public Mono<String> getPolicyStatus(Policy policy) {
227         String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.getType().getId(), policy.getId());
228         return post("getA1PolicyStatus", ricUrl, Optional.empty());
229
230     }
231
232     private A1UriBuilder getUriBuilder() {
233         if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
234             return new StdA1ClientVersion1.UriBuilder(ricConfig);
235         } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
236             return new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
237         } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
238             return new OscA1Client.UriBuilder(ricConfig);
239         }
240         logger.error("Not supported protocoltype: {}", protocolType);
241         throw new NullPointerException();
242     }
243
244     private Mono<A1ProtocolType> tryOscProtocolVersion() {
245         OscA1Client.UriBuilder oscApiuriBuilder = new OscA1Client.UriBuilder(ricConfig);
246         return post(GET_POLICY_RPC, oscApiuriBuilder.createHealtcheckUri(), Optional.empty()) //
247                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1));
248     }
249
250     private Mono<A1ProtocolType> tryStdProtocolVersion1() {
251         StdA1ClientVersion1.UriBuilder uriBuilder = new StdA1ClientVersion1.UriBuilder(ricConfig);
252         return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(""), Optional.empty()) //
253                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1));
254     }
255
256     private Mono<A1ProtocolType> tryStdProtocolVersion2() {
257         StdA1ClientVersion2.OranV2UriBuilder uriBuilder = new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
258         return post(GET_POLICY_RPC, uriBuilder.createPolicyTypesUri(), Optional.empty()) //
259                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0));
260     }
261
262     private Flux<String> getPolicyIds() {
263         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
264             StdA1ClientVersion1.UriBuilder uri = new StdA1ClientVersion1.UriBuilder(ricConfig);
265             final String ricUrl = uri.createGetPolicyIdsUri("");
266             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
267                     .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
268         } else {
269             A1UriBuilder uri = this.getUriBuilder();
270             return getPolicyTypeIdentities() //
271                     .flatMapMany(Flux::fromIterable)
272                     .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) //
273                     .flatMap(A1AdapterJsonHelper::parseJsonArrayOfString);
274         }
275     }
276
277     private Mono<String> deletePolicyById(String type, String policyId) {
278         String ricUrl = getUriBuilder().createDeleteUri(type, policyId);
279         return post("deleteA1Policy", ricUrl, Optional.empty());
280     }
281
282     private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) {
283         AdapterRequest inputParams = new AdapterRequest(ricUrl, body.isPresent() ? body.get() : null);
284
285         final String inputJsonString = A1AdapterJsonHelper.createInputJsonString(inputParams);
286         logger.debug("POST inputJsonString = {}", inputJsonString);
287
288         return restClient
289                 .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, this.controllerConfig.getUserName(),
290                         this.controllerConfig.getPassword()) //
291                 .flatMap(resp -> extractResponseBody(resp, ricUrl));
292     }
293
294     private Mono<String> extractResponse(JSONObject responseOutput, String ricUrl) {
295         AdapterOutput output = gson.fromJson(responseOutput.toString(), AdapterOutput.class);
296
297         String body = output.body == null ? "" : output.body;
298         if (HttpStatus.valueOf(output.httpStatus).is2xxSuccessful()) {
299             return Mono.just(body);
300         } else {
301             logger.debug("Error response: {} {}, from: {}", output.httpStatus, body, ricUrl);
302             byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8);
303             HttpStatus httpStatus = HttpStatus.valueOf(output.httpStatus);
304             WebClientResponseException responseException = new WebClientResponseException(httpStatus.value(),
305                     httpStatus.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null);
306
307             return Mono.error(responseException);
308         }
309     }
310
311     private Mono<String> extractResponseBody(String responseStr, String ricUrl) {
312         return A1AdapterJsonHelper.getOutput(responseStr) //
313                 .flatMap(responseOutput -> extractResponse(responseOutput, ricUrl));
314     }
315
316     private String controllerUrl(String rpcName) {
317         return "/A1-ADAPTER-API:" + rpcName;
318     }
319 }