2 * ========================LICENSE_START=================================
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
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.clients;
23 import com.google.gson.FieldNamingPolicy;
24 import com.google.gson.GsonBuilder;
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;
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;
45 import reactor.core.publisher.Flux;
46 import reactor.core.publisher.Mono;
49 * Client for accessing the A1 adapter in the CCSDK in ONAP.
51 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
52 public class CcsdkA1AdapterClient implements A1Client {
54 static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC
57 public static class AdapterRequest {
58 private String nearRtRicUrl = null;
59 private String body = null;
61 public AdapterRequest(String url, String body) {
62 this.nearRtRicUrl = url;
66 public AdapterRequest() {
71 public static class AdapterOutput {
72 private String body = null;
73 private int httpStatus = 0;
75 public AdapterOutput(int status, String body) {
76 this.httpStatus = status;
80 public AdapterOutput() {
84 static com.google.gson.Gson gson = new GsonBuilder() //
85 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
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;
95 * Constructor that creates the REST client to use.
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
103 * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
105 * @throws IllegalArgumentException when the protocolType is wrong.
107 public CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig,
108 AsyncRestClientFactory restClientFactory) {
109 this(protocolType, ricConfig, restClientFactory
110 .createRestClientNoHttpProxy(ricConfig.getControllerConfig().getBaseUrl() + "/rests/operations"));
114 * Constructor where the REST client to use is provided.
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
122 * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
123 * @param restClient the REST client to use
125 * @throws IllegalArgumentException when the protocolType is illegal.
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());
138 logger.error("Not supported protocoltype: {}", protocolType);
139 throw new IllegalArgumentException("Not handeled protocolversion: " + protocolType);
144 public Mono<List<String>> getPolicyTypeIdentities() {
145 if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
146 return Mono.just(Arrays.asList(""));
148 return post(GET_POLICY_RPC, getUriBuilder().createPolicyTypesUri(), Optional.empty()) //
149 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString) //
155 public Mono<List<String>> getPolicyIdentities() {
156 return getPolicyIds() //
161 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
162 if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
163 return Mono.just("{}");
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));
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);
178 return Mono.error(new ServiceException("Not supported " + this.protocolType));
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()));
190 public Mono<String> deletePolicy(Policy policy) {
191 return deletePolicyById(policy.getType().getId(), policy.getId());
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); //
201 A1UriBuilder uriBuilder = this.getUriBuilder();
202 return getPolicyTypeIdentities() //
203 .flatMapMany(Flux::fromIterable) //
204 .flatMap(type -> deleteAllInstancesForType(uriBuilder, type, excludePolicyIds), CONCURRENCY_RIC);
208 private Flux<String> getInstancesForType(A1UriBuilder uriBuilder, String type) {
209 return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty()) //
210 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
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);
220 public Mono<A1ProtocolType> getProtocolVersion() {
221 return tryStdProtocolVersion2() //
222 .onErrorResume(t -> tryStdProtocolVersion1()) //
223 .onErrorResume(t -> tryOscProtocolVersion());
227 public Mono<String> getPolicyStatus(Policy policy) {
228 String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.getType().getId(), policy.getId());
229 return post("getA1PolicyStatus", ricUrl, Optional.empty());
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);
241 logger.error("Not supported protocoltype: {}", protocolType);
242 throw new NullPointerException();
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));
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));
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));
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);
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);
278 private Mono<String> deletePolicyById(String type, String policyId) {
279 String ricUrl = getUriBuilder().createDeleteUri(type, policyId);
280 return post("deleteA1Policy", ricUrl, Optional.empty());
283 private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) {
284 AdapterRequest inputParams = new AdapterRequest(ricUrl, body.isPresent() ? body.get() : null);
286 final String inputJsonString = A1AdapterJsonHelper.createInputJsonString(inputParams);
287 logger.debug("POST inputJsonString = {}", inputJsonString);
288 ControllerConfig controllerConfig = this.ricConfig.getControllerConfig();
290 .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, controllerConfig.getUserName(),
291 controllerConfig.getPassword()) //
292 .flatMap(resp -> extractResponseBody(resp, ricUrl));
295 private Mono<String> extractResponse(JSONObject responseOutput, String ricUrl) {
296 AdapterOutput output = gson.fromJson(responseOutput.toString(), AdapterOutput.class);
298 String body = output.body == null ? "" : output.body;
299 if (HttpStatus.valueOf(output.httpStatus).is2xxSuccessful()) {
300 return Mono.just(body);
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);
308 return Mono.error(responseException);
312 private Mono<String> extractResponseBody(String responseStr, String ricUrl) {
313 return A1AdapterJsonHelper.getOutput(responseStr) //
314 .flatMap(responseOutput -> extractResponse(responseOutput, ricUrl));
317 private String controllerUrl(String rpcName) {
318 return "/A1-ADAPTER-API:" + rpcName;