2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.clients;
23 import com.google.gson.FieldNamingPolicy;
24 import com.google.gson.GsonBuilder;
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;
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;
43 import reactor.core.publisher.Flux;
44 import reactor.core.publisher.Mono;
47 * Client for accessing the A1 adapter in the SDNC controller in OSC.
49 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
50 public class SdncOscA1Client implements A1Client {
52 static final int CONCURRENCY_RIC = 1; // How may paralell requests that is sent to one NearRT RIC
55 @org.immutables.gson.Gson.TypeAdapters
56 public interface AdapterRequest {
57 public String nearRtRicUrl();
59 public Optional<String> body();
63 @org.immutables.gson.Gson.TypeAdapters
64 public interface AdapterOutput {
65 public Optional<String> body();
67 public int httpStatus();
70 static com.google.gson.Gson gson = new GsonBuilder() //
71 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
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;
82 * Constructor that creates the REST client to use.
84 * @param protocolType the southbound protocol of the controller. Supported protocols are SDNC_OSC_STD_V1_1 and
86 * @param ricConfig the configuration of the Ric to communicate with
87 * @param controllerConfig the configuration of the SDNC controller to use
89 * @throws IllegalArgumentException when the protocolType is wrong.
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);
99 * Constructor where the REST client to use is provided.
101 * @param protocolType the southbound protocol of the controller. Supported protocols are SDNC_OSC_STD_V1_1 and
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
107 * @throws IllegalArgumentException when the protocolType is wrong.
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);
116 this.restClient = restClient;
117 this.ricConfig = ricConfig;
118 this.protocolType = protocolType;
119 this.controllerConfig = controllerConfig;
123 public Mono<List<String>> getPolicyTypeIdentities() {
124 if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) {
125 return Mono.just(Arrays.asList(""));
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) //
137 public Mono<List<String>> getPolicyIdentities() {
138 return getPolicyIds() //
143 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
144 if (this.protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) {
145 return Mono.just("{}");
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));
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()));
164 public Mono<String> deletePolicy(Policy policy) {
165 return deletePolicyById(policy.type().name(), policy.id());
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); //
174 OscA1Client.UriBuilder uriBuilder = new OscA1Client.UriBuilder(ricConfig);
175 return getPolicyTypeIdentities() //
176 .flatMapMany(Flux::fromIterable) //
177 .flatMap(type -> oscDeleteInstancesForType(uriBuilder, type), CONCURRENCY_RIC);
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);
186 private Flux<String> oscDeleteInstancesForType(OscA1Client.UriBuilder uriBuilder, String type) {
187 return oscGetInstancesForType(uriBuilder, type) //
188 .flatMap(instance -> deletePolicyById(type, instance), CONCURRENCY_RIC);
192 public Mono<A1ProtocolType> getProtocolVersion() {
193 return tryStdProtocolVersion() //
194 .onErrorResume(t -> tryOscProtocolVersion());
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());
206 private Mono<A1UriBuilder> getUriBuilder() {
207 if (protocolType == A1ProtocolType.SDNC_OSC_STD_V1_1) {
208 return Mono.just(new StdA1ClientVersion1.UriBuilder(ricConfig));
210 return Mono.just(new OscA1Client.UriBuilder(ricConfig));
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));
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));
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);
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);
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());
249 private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) {
250 AdapterRequest inputParams = ImmutableAdapterRequest.builder() //
251 .nearRtRicUrl(ricUrl) //
254 final String inputJsonString = SdncJsonHelper.createInputJsonString(inputParams);
255 logger.debug("POST inputJsonString = {}", inputJsonString);
258 .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, this.controllerConfig.userName(),
259 this.controllerConfig.password()) //
260 .flatMap(this::extractResponseBody);
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);
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);
275 return Mono.error(responseException);
279 private Mono<String> extractResponseBody(String responseStr) {
280 return SdncJsonHelper.getOutput(responseStr) //
281 .flatMap(this::extractResponse);
284 private String controllerUrl(String rpcName) {
285 return "/A1-ADAPTER-API:" + rpcName;