2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 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.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;
34 import org.json.JSONObject;
35 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
36 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
37 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
38 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.web.reactive.function.client.WebClientResponseException;
44 import reactor.core.publisher.Flux;
45 import reactor.core.publisher.Mono;
48 * Client for accessing the A1 adapter in the CCSDK in ONAP.
50 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
51 public class CcsdkA1AdapterClient implements A1Client {
53 static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC
56 public static class AdapterRequest {
57 private String nearRtRicUrl = null;
58 private String body = null;
60 public AdapterRequest(String url, String body) {
61 this.nearRtRicUrl = url;
65 public AdapterRequest() {}
69 public static class AdapterOutput {
70 private String body = null;
71 private int httpStatus = 0;
73 public AdapterOutput(int status, String body) {
74 this.httpStatus = status;
78 public AdapterOutput() {}
81 static com.google.gson.Gson gson = new GsonBuilder() //
82 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
85 private static final String GET_POLICY_RPC = "getA1Policy";
86 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
87 private final ControllerConfig controllerConfig;
88 private final AsyncRestClient restClient;
89 private final RicConfig ricConfig;
90 private final A1ProtocolType protocolType;
93 * Constructor that creates the REST client to use.
95 * @param protocolType the southbound protocol of the controller. Supported
96 * protocols are CCSDK_A1_ADAPTER_STD_V1_1,
97 * CCSDK_A1_ADAPTER_OSC_V1 and
98 * CCSDK_A1_ADAPTER_STD_V2_0_0 with
99 * @param ricConfig the configuration of the Near-RT RIC to communicate
101 * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
103 * @throws IllegalArgumentException when the protocolType is wrong.
105 public CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
106 AsyncRestClientFactory restClientFactory) {
107 this(protocolType, ricConfig, controllerConfig,
108 restClientFactory.createRestClientNoHttpProxy(controllerConfig.getBaseUrl() + "/restconf/operations"));
112 * Constructor where the REST client to use is provided.
114 * @param protocolType the southbound protocol of the controller. Supported
115 * protocols are CCSDK_A1_ADAPTER_STD_V1_1,
116 * CCSDK_A1_ADAPTER_OSC_V1 and
117 * CCSDK_A1_ADAPTER_STD_V2_0_0 with
118 * @param ricConfig the configuration of the Near-RT RIC to communicate
120 * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
121 * @param restClient the REST client to use
123 * @throws IllegalArgumentException when the protocolType is illegal.
125 CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
126 AsyncRestClient restClient) {
127 if (A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1.equals(protocolType) //
128 || A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1.equals(protocolType) //
129 || A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0.equals(protocolType)) {
130 this.restClient = restClient;
131 this.ricConfig = ricConfig;
132 this.protocolType = protocolType;
133 this.controllerConfig = controllerConfig;
134 logger.debug("CcsdkA1AdapterClient for ric: {}, a1Controller: {}", ricConfig.getRicId(), controllerConfig);
136 logger.error("Not supported protocoltype: {}", protocolType);
137 throw new IllegalArgumentException("Not handeled protocolversion: " + protocolType);
142 public Mono<List<String>> getPolicyTypeIdentities() {
143 if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
144 return Mono.just(Arrays.asList(""));
146 return post(GET_POLICY_RPC, getUriBuilder().createPolicyTypesUri(), Optional.empty()) //
147 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString) //
153 public Mono<List<String>> getPolicyIdentities() {
154 return getPolicyIds() //
159 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
160 if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
161 return Mono.just("{}");
163 A1UriBuilder uri = this.getUriBuilder();
164 final String ricUrl = uri.createGetSchemaUri(policyTypeId);
165 return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
166 .flatMap(response -> extractCreateSchema(response, policyTypeId));
170 private Mono<String> extractCreateSchema(String controllerResponse, String policyTypeId) {
171 if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
172 return OscA1Client.extractCreateSchema(controllerResponse, policyTypeId);
173 } else if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
174 return StdA1ClientVersion2.extractPolicySchema(controllerResponse, policyTypeId);
176 return Mono.error(new ServiceException("Not supported " + this.protocolType));
181 public Mono<String> putPolicy(Policy policy) {
182 String ricUrl = getUriBuilder().createPutPolicyUri(policy.getType().getId(), policy.getId(),
183 policy.getStatusNotificationUri());
184 return post("putA1Policy", ricUrl, Optional.of(policy.getJson()));
188 public Mono<String> deletePolicy(Policy policy) {
189 return deletePolicyById(policy.getType().getId(), policy.getId());
193 public Flux<String> deleteAllPolicies() {
194 if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
195 return getPolicyIds() //
196 .flatMap(policyId -> deletePolicyById("", policyId), CONCURRENCY_RIC); //
198 A1UriBuilder uriBuilder = this.getUriBuilder();
199 return getPolicyTypeIdentities() //
200 .flatMapMany(Flux::fromIterable) //
201 .flatMap(type -> deleteAllInstancesForType(uriBuilder, type), CONCURRENCY_RIC);
205 private Flux<String> getInstancesForType(A1UriBuilder uriBuilder, String type) {
206 return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty()) //
207 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
210 private Flux<String> deleteAllInstancesForType(A1UriBuilder uriBuilder, String type) {
211 return getInstancesForType(uriBuilder, type) //
212 .flatMap(instance -> deletePolicyById(type, instance), CONCURRENCY_RIC);
216 public Mono<A1ProtocolType> getProtocolVersion() {
217 return tryStdProtocolVersion2() //
218 .onErrorResume(t -> tryStdProtocolVersion1()) //
219 .onErrorResume(t -> tryOscProtocolVersion());
223 public Mono<String> getPolicyStatus(Policy policy) {
224 String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.getType().getId(), policy.getId());
225 return post("getA1PolicyStatus", ricUrl, Optional.empty());
229 private A1UriBuilder getUriBuilder() {
230 if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
231 return new StdA1ClientVersion1.UriBuilder(ricConfig);
232 } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
233 return new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
234 } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
235 return new OscA1Client.UriBuilder(ricConfig);
237 logger.error("Not supported protocoltype: {}", protocolType);
238 throw new NullPointerException();
241 private Mono<A1ProtocolType> tryOscProtocolVersion() {
242 OscA1Client.UriBuilder oscApiuriBuilder = new OscA1Client.UriBuilder(ricConfig);
243 return post(GET_POLICY_RPC, oscApiuriBuilder.createHealtcheckUri(), Optional.empty()) //
244 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1));
247 private Mono<A1ProtocolType> tryStdProtocolVersion1() {
248 StdA1ClientVersion1.UriBuilder uriBuilder = new StdA1ClientVersion1.UriBuilder(ricConfig);
249 return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(""), Optional.empty()) //
250 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1));
253 private Mono<A1ProtocolType> tryStdProtocolVersion2() {
254 StdA1ClientVersion2.OranV2UriBuilder uriBuilder = new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
255 return post(GET_POLICY_RPC, uriBuilder.createPolicyTypesUri(), Optional.empty()) //
256 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0));
259 private Flux<String> getPolicyIds() {
260 if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
261 StdA1ClientVersion1.UriBuilder uri = new StdA1ClientVersion1.UriBuilder(ricConfig);
262 final String ricUrl = uri.createGetPolicyIdsUri("");
263 return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
264 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
266 A1UriBuilder uri = this.getUriBuilder();
267 return getPolicyTypeIdentities() //
268 .flatMapMany(Flux::fromIterable)
269 .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) //
270 .flatMap(A1AdapterJsonHelper::parseJsonArrayOfString);
274 private Mono<String> deletePolicyById(String type, String policyId) {
275 String ricUrl = getUriBuilder().createDeleteUri(type, policyId);
276 return post("deleteA1Policy", ricUrl, Optional.empty());
279 private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) {
280 AdapterRequest inputParams = new AdapterRequest(ricUrl, body.isPresent() ? body.get() : null);
282 final String inputJsonString = A1AdapterJsonHelper.createInputJsonString(inputParams);
283 logger.debug("POST inputJsonString = {}", inputJsonString);
286 .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, this.controllerConfig.getUserName(),
287 this.controllerConfig.getPassword()) //
288 .flatMap(resp -> extractResponseBody(resp, ricUrl));
291 private Mono<String> extractResponse(JSONObject responseOutput, String ricUrl) {
292 AdapterOutput output = gson.fromJson(responseOutput.toString(), AdapterOutput.class);
294 String body = output.body == null ? "" : output.body;
295 if (HttpStatus.valueOf(output.httpStatus).is2xxSuccessful()) {
296 return Mono.just(body);
298 logger.debug("Error response: {} {}, from: {}", output.httpStatus, body, ricUrl);
299 byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8);
300 HttpStatus httpStatus = HttpStatus.valueOf(output.httpStatus);
301 WebClientResponseException responseException = new WebClientResponseException(httpStatus.value(),
302 httpStatus.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null);
304 return Mono.error(responseException);
308 private Mono<String> extractResponseBody(String responseStr, String ricUrl) {
309 return A1AdapterJsonHelper.getOutput(responseStr) //
310 .flatMap(responseOutput -> extractResponse(responseOutput, ricUrl));
313 private String controllerUrl(String rpcName) {
314 return "/A1-ADAPTER-API:" + rpcName;