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