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