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