2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2020-2023 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
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.onap.ccsdk.oran.a1policymanagementservice.clients;
23 import java.lang.invoke.MethodHandles;
25 import java.net.URLEncoder;
26 import java.nio.charset.StandardCharsets;
27 import java.util.List;
30 import org.json.JSONObject;
31 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
40 * Client for accessing OSC A1 REST API
42 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
43 public class OscA1Client implements A1Client {
44 static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC
46 public static class UriBuilder implements A1UriBuilder {
47 private final RicConfig ricConfig;
49 public UriBuilder(RicConfig ricConfig) {
50 this.ricConfig = ricConfig;
54 public String createPutPolicyUri(String type, String policyId, String notificationDestinationUri) {
55 return createPolicyUri(type, policyId, notificationDestinationUri);
59 * /a1-p/policytypes/{policy_type_id}/policies
62 public String createGetPolicyIdsUri(String type) {
63 return createPolicyTypeUri(type) + "/policies";
67 public String createDeleteUri(String type, String policyId) {
68 return createPolicyUri(type, policyId, null);
72 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
75 public String createGetPolicyStatusUri(String type, String policyId) {
76 return createPolicyUri(type, policyId, null) + "/status";
82 public String createHealtcheckUri() {
83 return baseUri() + "/healthcheck";
87 * /a1-p/policytypes/{policy_type_id}
90 public String createGetSchemaUri(String type) {
91 return this.createPolicyTypeUri(type);
95 * /a1-p/policytypes/{policy_type_id}
98 public String createPolicyTypesUri() {
99 return baseUri() + "/policytypes";
103 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}
105 private String createPolicyUri(String type, String id, String notificationDestination) {
107 String baseUrl = createPolicyTypeUri(type);
108 String url = baseUrl + "/policies/" + id;
109 if (notificationDestination != null) {
110 url += "?notificationDestination=" +
111 URLEncoder.encode(notificationDestination, StandardCharsets.UTF_8.toString());
113 return new URI(url).toString();
114 } catch (Exception e) {
115 String exceptionString = e.getMessage();
116 logger.error("Unexpected error in policy URI creation for policy type: {}, exception: {}", type,
123 * /a1-p/policytypes/{policy_type_id}
125 private String createPolicyTypeUri(String type) {
126 return createPolicyTypesUri() + "/" + type;
129 private String baseUri() {
130 return ricConfig.getBaseUrl() + "/a1-p";
134 private static final String TITLE = "title";
135 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
136 private final AsyncRestClient restClient;
137 private final UriBuilder uri;
139 public OscA1Client(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
140 this(ricConfig, restClientFactory.createRestClientUseHttpProxy(""));
143 public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
144 this.restClient = restClient;
145 uri = new UriBuilder(ricConfig);
146 logger.debug("A1Client (" + getClass().getTypeName() + ") created for ric: {}", ricConfig.getRicId());
149 public static Mono<String> extractCreateSchema(String policyTypeResponse, String policyTypeId) {
151 JSONObject obj = new JSONObject(policyTypeResponse);
152 JSONObject schemaObj = obj.getJSONObject("create_schema");
153 schemaObj.put(TITLE, policyTypeId);
154 return Mono.just(schemaObj.toString());
155 } catch (Exception e) {
156 String exceptionString = e.toString();
157 logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
158 return Mono.error(e);
163 public Mono<List<String>> getPolicyTypeIdentities() {
164 return getPolicyTypeIds() //
169 public Mono<List<String>> getPolicyIdentities() {
170 return getPolicyTypeIds() //
171 .flatMap(this::getPolicyIdentitiesByType) //
176 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
177 String schemaUri = uri.createGetSchemaUri(policyTypeId);
178 return restClient.get(schemaUri) //
179 .flatMap(response -> extractCreateSchema(response, policyTypeId));
183 public Mono<String> putPolicy(Policy policy) {
184 String policyUri = this.uri.createPutPolicyUri(policy.getType().getId(), policy.getId(),
185 policy.getStatusNotificationUri());
186 return restClient.put(policyUri, policy.getJson());
190 public Mono<String> deletePolicy(Policy policy) {
191 return deletePolicyById(policy.getType().getId(), policy.getId());
195 public Mono<A1ProtocolType> getProtocolVersion() {
196 return restClient.get(uri.createHealtcheckUri()) //
197 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
201 public Flux<String> deleteAllPolicies(Set<String> excludePolicyIds) {
202 return getPolicyTypeIds() //
203 .flatMap(typeId -> deletePoliciesForType(typeId, excludePolicyIds), CONCURRENCY_RIC);
207 public Mono<String> getPolicyStatus(Policy policy) {
208 String statusUri = uri.createGetPolicyStatusUri(policy.getType().getId(), policy.getId());
209 return restClient.get(statusUri);
213 private Flux<String> getPolicyTypeIds() {
214 return restClient.get(uri.createPolicyTypesUri()) //
215 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
218 private Flux<String> getPolicyIdentitiesByType(String typeId) {
219 return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
220 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
223 private Mono<String> deletePolicyById(String typeId, String policyId) {
224 String policyUri = uri.createDeleteUri(typeId, policyId);
225 return restClient.delete(policyUri);
228 private Flux<String> deletePoliciesForType(String typeId, Set<String> excludePolicyIds) {
229 return getPolicyIdentitiesByType(typeId) //
230 .filter(policyId -> !excludePolicyIds.contains(policyId)) //
231 .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);