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