04ecf045e4524d293ddacc9ebe07b15d95c7daa8
[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
70     @Getter
71     public static class AdapterOutput {
72         private String body = null;
73         private int httpStatus = 0;
74
75         public AdapterOutput(int status, String body) {
76             this.httpStatus = status;
77             this.body = body;
78         }
79
80         public AdapterOutput() {
81         }
82     }
83
84     static com.google.gson.Gson gson = new GsonBuilder() //
85             .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
86             .create(); //
87
88     private static final String GET_POLICY_RPC = "getA1Policy";
89     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
90     private final AsyncRestClient restClient;
91     private final RicConfig ricConfig;
92     private final A1ProtocolType protocolType;
93
94     /**
95      * Constructor that creates the REST client to use.
96      *
97      * @param protocolType     the southbound protocol of the controller. Supported
98      *                         protocols are CCSDK_A1_ADAPTER_STD_V1_1,
99      *                         CCSDK_A1_ADAPTER_OSC_V1 and
100      *                         CCSDK_A1_ADAPTER_STD_V2_0_0 with
101      * @param ricConfig        the configuration of the Near-RT RIC to communicate
102      *                         with
103      * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
104      *
105      * @throws IllegalArgumentException when the protocolType is wrong.
106      */
107     public CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig,
108             AsyncRestClientFactory restClientFactory) {
109         this(protocolType, ricConfig, restClientFactory
110                 .createRestClientNoHttpProxy(ricConfig.getControllerConfig().getBaseUrl() + "/rests/operations"));
111     }
112
113     /**
114      * Constructor where the REST client to use is provided.
115      *
116      * @param protocolType     the southbound protocol of the controller. Supported
117      *                         protocols are CCSDK_A1_ADAPTER_STD_V1_1,
118      *                         CCSDK_A1_ADAPTER_OSC_V1 and
119      *                         CCSDK_A1_ADAPTER_STD_V2_0_0 with
120      * @param ricConfig        the configuration of the Near-RT RIC to communicate
121      *                         with
122      * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
123      * @param restClient       the REST client to use
124      *
125      * @throws IllegalArgumentException when the protocolType is illegal.
126      */
127     CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, 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             logger.debug("A1Client (" + getClass().getTypeName() + ") created for ric: {}, a1Controller: {}",
135                     ricConfig.getRicId(), ricConfig.getControllerConfig());
136
137         } else {
138             logger.error("Not supported protocoltype: {}", protocolType);
139             throw new IllegalArgumentException("Not handeled protocolversion: " + protocolType);
140         }
141     }
142
143     @Override
144     public Mono<List<String>> getPolicyTypeIdentities() {
145         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
146             return Mono.just(Arrays.asList(""));
147         } else {
148             return post(GET_POLICY_RPC, getUriBuilder().createPolicyTypesUri(), Optional.empty()) //
149                     .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString) //
150                     .collectList();
151         }
152     }
153
154     @Override
155     public Mono<List<String>> getPolicyIdentities() {
156         return getPolicyIds() //
157                 .collectList();
158     }
159
160     @Override
161     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
162         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
163             return Mono.just("{}");
164         } else {
165             A1UriBuilder uri = this.getUriBuilder();
166             final String ricUrl = uri.createGetSchemaUri(policyTypeId);
167             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
168                     .flatMap(response -> extractCreateSchema(response, policyTypeId));
169         }
170     }
171
172     private Mono<String> extractCreateSchema(String controllerResponse, String policyTypeId) {
173         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
174             return OscA1Client.extractCreateSchema(controllerResponse, policyTypeId);
175         } else if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
176             return StdA1ClientVersion2.extractPolicySchema(controllerResponse, policyTypeId);
177         } else {
178             return Mono.error(new ServiceException("Not supported " + this.protocolType));
179         }
180     }
181
182     @Override
183     public Mono<String> putPolicy(Policy policy) {
184         String ricUrl = getUriBuilder().createPutPolicyUri(policy.getType().getId(), policy.getId(),
185                 policy.getStatusNotificationUri());
186         return post("putA1Policy", ricUrl, Optional.of(policy.getJson()));
187     }
188
189     @Override
190     public Mono<String> deletePolicy(Policy policy) {
191         return deletePolicyById(policy.getType().getId(), policy.getId());
192     }
193
194     @Override
195     public Flux<String> deleteAllPolicies(Set<String> excludePolicyIds) {
196         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
197             return getPolicyIds() //
198                     .filter(policyId -> !excludePolicyIds.contains(policyId)) //
199                     .flatMap(policyId -> deletePolicyById("", policyId), CONCURRENCY_RIC); //
200         } else {
201             A1UriBuilder uriBuilder = this.getUriBuilder();
202             return getPolicyTypeIdentities() //
203                     .flatMapMany(Flux::fromIterable) //
204                     .flatMap(type -> deleteAllInstancesForType(uriBuilder, type, excludePolicyIds), CONCURRENCY_RIC);
205         }
206     }
207
208     private Flux<String> getInstancesForType(A1UriBuilder uriBuilder, String type) {
209         return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty()) //
210                 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
211     }
212
213     private Flux<String> deleteAllInstancesForType(A1UriBuilder uriBuilder, String type, Set<String> excludePolicyIds) {
214         return getInstancesForType(uriBuilder, type) //
215                 .filter(policyId -> !excludePolicyIds.contains(policyId)) //
216                 .flatMap(policyId -> deletePolicyById(type, policyId), CONCURRENCY_RIC);
217     }
218
219     @Override
220     public Mono<A1ProtocolType> getProtocolVersion() {
221         return tryStdProtocolVersion2() //
222                 .onErrorResume(t -> tryStdProtocolVersion1()) //
223                 .onErrorResume(t -> tryOscProtocolVersion());
224     }
225
226     @Override
227     public Mono<String> getPolicyStatus(Policy policy) {
228         String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.getType().getId(), policy.getId());
229         return post("getA1PolicyStatus", ricUrl, Optional.empty());
230
231     }
232
233     private A1UriBuilder getUriBuilder() {
234         if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
235             return new StdA1ClientVersion1.UriBuilder(ricConfig);
236         } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
237             return new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
238         } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
239             return new OscA1Client.UriBuilder(ricConfig);
240         }
241         logger.error("Not supported protocoltype: {}", protocolType);
242         throw new NullPointerException();
243     }
244
245     private Mono<A1ProtocolType> tryOscProtocolVersion() {
246         OscA1Client.UriBuilder oscApiuriBuilder = new OscA1Client.UriBuilder(ricConfig);
247         return post(GET_POLICY_RPC, oscApiuriBuilder.createHealtcheckUri(), Optional.empty()) //
248                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1));
249     }
250
251     private Mono<A1ProtocolType> tryStdProtocolVersion1() {
252         StdA1ClientVersion1.UriBuilder uriBuilder = new StdA1ClientVersion1.UriBuilder(ricConfig);
253         return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(""), Optional.empty()) //
254                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1));
255     }
256
257     private Mono<A1ProtocolType> tryStdProtocolVersion2() {
258         StdA1ClientVersion2.OranV2UriBuilder uriBuilder = new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
259         return post(GET_POLICY_RPC, uriBuilder.createPolicyTypesUri(), Optional.empty()) //
260                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0));
261     }
262
263     private Flux<String> getPolicyIds() {
264         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
265             StdA1ClientVersion1.UriBuilder uri = new StdA1ClientVersion1.UriBuilder(ricConfig);
266             final String ricUrl = uri.createGetPolicyIdsUri("");
267             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
268                     .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
269         } else {
270             A1UriBuilder uri = this.getUriBuilder();
271             return getPolicyTypeIdentities() //
272                     .flatMapMany(Flux::fromIterable)
273                     .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) //
274                     .flatMap(A1AdapterJsonHelper::parseJsonArrayOfString);
275         }
276     }
277
278     private Mono<String> deletePolicyById(String type, String policyId) {
279         String ricUrl = getUriBuilder().createDeleteUri(type, policyId);
280         return post("deleteA1Policy", ricUrl, Optional.empty());
281     }
282
283     private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) {
284         AdapterRequest inputParams = new AdapterRequest(ricUrl, body.isPresent() ? body.get() : null);
285
286         final String inputJsonString = A1AdapterJsonHelper.createInputJsonString(inputParams);
287         logger.debug("POST inputJsonString = {}", inputJsonString);
288         ControllerConfig controllerConfig = this.ricConfig.getControllerConfig();
289         return restClient
290                 .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, controllerConfig.getUserName(),
291                         controllerConfig.getPassword()) //
292                 .flatMap(resp -> extractResponseBody(resp, ricUrl));
293     }
294
295     private Mono<String> extractResponse(JSONObject responseOutput, String ricUrl) {
296         AdapterOutput output = gson.fromJson(responseOutput.toString(), AdapterOutput.class);
297
298         String body = output.body == null ? "" : output.body;
299         if (HttpStatus.valueOf(output.httpStatus).is2xxSuccessful()) {
300             return Mono.just(body);
301         } else {
302             logger.debug("Error response: {} {}, from: {}", output.httpStatus, body, ricUrl);
303             byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8);
304             HttpStatus httpStatus = HttpStatus.valueOf(output.httpStatus);
305             WebClientResponseException responseException = new WebClientResponseException(httpStatus.value(),
306                     httpStatus.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null);
307
308             return Mono.error(responseException);
309         }
310     }
311
312     private Mono<String> extractResponseBody(String responseStr, String ricUrl) {
313         return A1AdapterJsonHelper.getOutput(responseStr) //
314                 .flatMap(responseOutput -> extractResponse(responseOutput, ricUrl));
315     }
316
317     private String controllerUrl(String rpcName) {
318         return "/A1-ADAPTER-API:" + rpcName;
319     }
320 }