3b581b63b7f2eb18712b785171b056731388e3e6
[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, CCSDK_A1_ADAPTER_OSC_V1 and
85      *        CCSDK_A1_ADAPTER_STD_V2_0_0 with
86      * @param ricConfig the configuration of the Near-RT RIC to communicate
87      *        with
88      * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
89      *
90      * @throws IllegalArgumentException when the protocolType is wrong.
91      */
92     public CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
93             AsyncRestClientFactory restClientFactory) {
94         this(protocolType, ricConfig, controllerConfig,
95                 restClientFactory.createRestClient(controllerConfig.baseUrl() + "/restconf/operations"));
96     }
97
98     /**
99      * Constructor where the REST client to use is provided.
100      *
101      * @param protocolType the southbound protocol of the controller. Supported
102      *        protocols are CCSDK_A1_ADAPTER_STD_V1_1, CCSDK_A1_ADAPTER_OSC_V1 and
103      *        CCSDK_A1_ADAPTER_STD_V2_0_0 with
104      * @param ricConfig the configuration of the Near-RT RIC to communicate
105      *        with
106      * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
107      * @param restClient the REST client to use
108      *
109      * @throws IllegalArgumentException when the protocolType is illegal.
110      */
111     public CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
112             AsyncRestClient restClient) {
113         if (A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1.equals(protocolType) //
114                 || A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1.equals(protocolType) //
115                 || A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0.equals(protocolType)) {
116             this.restClient = restClient;
117             this.ricConfig = ricConfig;
118             this.protocolType = protocolType;
119             this.controllerConfig = controllerConfig;
120             logger.debug("CcsdkA1AdapterClient for ric: {}, a1Controller: {}", ricConfig.ricId(), controllerConfig);
121         } else {
122             throw new IllegalArgumentException("Not handeled protocolversion: " + protocolType);
123         }
124
125     }
126
127     @Override
128     public Mono<List<String>> getPolicyTypeIdentities() {
129         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
130             return Mono.just(Arrays.asList(""));
131         } else {
132             return post(GET_POLICY_RPC, getUriBuilder().createPolicyTypesUri(), Optional.empty()) //
133                     .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString) //
134                     .collectList();
135         }
136
137     }
138
139     @Override
140     public Mono<List<String>> getPolicyIdentities() {
141         return getPolicyIds() //
142                 .collectList();
143     }
144
145     @Override
146     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
147         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
148             return Mono.just("{}");
149         } else {
150             A1UriBuilder uri = this.getUriBuilder();
151             final String ricUrl = uri.createGetSchemaUri(policyTypeId);
152             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
153                     .flatMap(response -> extractCreateSchema(response, policyTypeId));
154         }
155     }
156
157     private Mono<String> extractCreateSchema(String controllerResponse, String policyTypeId) {
158         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
159             return OscA1Client.extractCreateSchema(controllerResponse, policyTypeId);
160         } else if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
161             return StdA1ClientVersion2.extractPolicySchema(controllerResponse, policyTypeId);
162         } else {
163             throw new NullPointerException("Not supported");
164         }
165     }
166
167     @Override
168     public Mono<String> putPolicy(Policy policy) {
169         String ricUrl =
170                 getUriBuilder().createPutPolicyUri(policy.type().id(), policy.id(), policy.statusNotificationUri());
171         return post("putA1Policy", ricUrl, Optional.of(policy.json()));
172     }
173
174     @Override
175     public Mono<String> deletePolicy(Policy policy) {
176         return deletePolicyById(policy.type().id(), policy.id());
177     }
178
179     @Override
180     public Flux<String> deleteAllPolicies() {
181         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
182             return getPolicyIds() //
183                     .flatMap(policyId -> deletePolicyById("", policyId), CONCURRENCY_RIC); //
184         } else {
185             A1UriBuilder uriBuilder = this.getUriBuilder();
186             return getPolicyTypeIdentities() //
187                     .flatMapMany(Flux::fromIterable) //
188                     .flatMap(type -> deleteAllInstancesForType(uriBuilder, type), CONCURRENCY_RIC);
189         }
190     }
191
192     private Flux<String> getInstancesForType(A1UriBuilder uriBuilder, String type) {
193         return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty()) //
194                 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
195     }
196
197     private Flux<String> deleteAllInstancesForType(A1UriBuilder uriBuilder, String type) {
198         return getInstancesForType(uriBuilder, type) //
199                 .flatMap(instance -> deletePolicyById(type, instance), CONCURRENCY_RIC);
200     }
201
202     @Override
203     public Mono<A1ProtocolType> getProtocolVersion() {
204         return tryStdProtocolVersion2() //
205                 .onErrorResume(t -> tryStdProtocolVersion1()) //
206                 .onErrorResume(t -> tryOscProtocolVersion());
207     }
208
209     @Override
210     public Mono<String> getPolicyStatus(Policy policy) {
211         String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.type().id(), policy.id());
212         return post("getA1PolicyStatus", ricUrl, Optional.empty());
213
214     }
215
216     private A1UriBuilder getUriBuilder() {
217         if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
218             return new StdA1ClientVersion1.UriBuilder(ricConfig);
219         } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
220             return new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
221         } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
222             return new OscA1Client.UriBuilder(ricConfig);
223         }
224         throw new NullPointerException();
225     }
226
227     private Mono<A1ProtocolType> tryOscProtocolVersion() {
228         OscA1Client.UriBuilder oscApiuriBuilder = new OscA1Client.UriBuilder(ricConfig);
229         return post(GET_POLICY_RPC, oscApiuriBuilder.createHealtcheckUri(), Optional.empty()) //
230                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1));
231     }
232
233     private Mono<A1ProtocolType> tryStdProtocolVersion1() {
234         StdA1ClientVersion1.UriBuilder uriBuilder = new StdA1ClientVersion1.UriBuilder(ricConfig);
235         return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(""), Optional.empty()) //
236                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1));
237     }
238
239     private Mono<A1ProtocolType> tryStdProtocolVersion2() {
240         StdA1ClientVersion2.OranV2UriBuilder uriBuilder = new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
241         return post(GET_POLICY_RPC, uriBuilder.createPolicyTypesUri(), Optional.empty()) //
242                 .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0));
243     }
244
245     private Flux<String> getPolicyIds() {
246         if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
247             StdA1ClientVersion1.UriBuilder uri = new StdA1ClientVersion1.UriBuilder(ricConfig);
248             final String ricUrl = uri.createGetPolicyIdsUri("");
249             return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
250                     .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
251         } else {
252             A1UriBuilder uri = this.getUriBuilder();
253             return getPolicyTypeIdentities() //
254                     .flatMapMany(Flux::fromIterable)
255                     .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) //
256                     .flatMap(A1AdapterJsonHelper::parseJsonArrayOfString);
257         }
258     }
259
260     private Mono<String> deletePolicyById(String type, String policyId) {
261         String ricUrl = getUriBuilder().createDeleteUri(type, policyId);
262         return post("deleteA1Policy", ricUrl, Optional.empty());
263     }
264
265     private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) {
266         AdapterRequest inputParams = ImmutableAdapterRequest.builder() //
267                 .nearRtRicUrl(ricUrl) //
268                 .body(body) //
269                 .build();
270         final String inputJsonString = A1AdapterJsonHelper.createInputJsonString(inputParams);
271         logger.debug("POST inputJsonString = {}", inputJsonString);
272
273         return restClient
274                 .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, this.controllerConfig.userName(),
275                         this.controllerConfig.password()) //
276                 .flatMap(this::extractResponseBody);
277     }
278
279     private Mono<String> extractResponse(JSONObject responseOutput) {
280         AdapterOutput output = gson.fromJson(responseOutput.toString(), ImmutableAdapterOutput.class);
281         Optional<String> optionalBody = output.body();
282         String body = optionalBody.isPresent() ? optionalBody.get() : "";
283         if (HttpStatus.valueOf(output.httpStatus()).is2xxSuccessful()) {
284             return Mono.just(body);
285         } else {
286             logger.debug("Error response: {} {}", output.httpStatus(), body);
287             byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8);
288             HttpStatus httpStatus = HttpStatus.valueOf(output.httpStatus());
289             WebClientResponseException responseException = new WebClientResponseException(httpStatus.value(),
290                     httpStatus.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null);
291
292             return Mono.error(responseException);
293         }
294     }
295
296     private Mono<String> extractResponseBody(String responseStr) {
297         return A1AdapterJsonHelper.getOutput(responseStr) //
298                 .flatMap(this::extractResponse);
299     }
300
301     private String controllerUrl(String rpcName) {
302         return "/A1-ADAPTER-API:" + rpcName;
303     }
304 }