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