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