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