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;
39 * Client for accessing OSC A1 REST API
41 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
42 public class OscA1Client implements A1Client {
43 static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC
45 public static class UriBuilder implements A1UriBuilder {
46 private final RicConfig ricConfig;
48 public UriBuilder(RicConfig ricConfig) {
49 this.ricConfig = ricConfig;
53 public String createPutPolicyUri(String type, String policyId, String notificationDestinationUri) {
54 return createPolicyUri(type, policyId, notificationDestinationUri);
58 * /a1-p/policytypes/{policy_type_id}/policies
61 public String createGetPolicyIdsUri(String type) {
62 return createPolicyTypeUri(type) + "/policies";
66 public String createDeleteUri(String type, String policyId) {
67 return createPolicyUri(type, policyId, null);
71 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
74 public String createGetPolicyStatusUri(String type, String policyId) {
75 return createPolicyUri(type, policyId, null) + "/status";
81 public String createHealtcheckUri() {
82 return baseUri() + "/healthcheck";
86 * /a1-p/policytypes/{policy_type_id}
89 public String createGetSchemaUri(String type) {
90 return this.createPolicyTypeUri(type);
94 * /a1-p/policytypes/{policy_type_id}
97 public String createPolicyTypesUri() {
98 return baseUri() + "/policytypes";
102 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}
104 private String createPolicyUri(String type, String id, String notificationDestination) {
106 String baseUrl = createPolicyTypeUri(type);
107 String url = baseUrl + "/policies/" + id;
108 if (notificationDestination != null) {
109 url += "?notificationDestination=" +
110 URLEncoder.encode(notificationDestination, StandardCharsets.UTF_8.toString());
112 return new URI(url).toString();
113 } catch (Exception e) {
114 String exceptionString = e.getMessage();
115 logger.error("Unexpected error in policy URI creation for policy type: {}, exception: {}", type,
122 * /a1-p/policytypes/{policy_type_id}
124 private String createPolicyTypeUri(String type) {
125 return createPolicyTypesUri() + "/" + type;
128 private String baseUri() {
129 return ricConfig.getBaseUrl() + "/a1-p";
133 private static final String TITLE = "title";
134 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
135 private final AsyncRestClient restClient;
136 private final UriBuilder uri;
138 public OscA1Client(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
139 this(ricConfig, restClientFactory.createRestClientUseHttpProxy(""));
142 public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
143 this.restClient = restClient;
144 logger.debug("OscA1Client for ric: {}", ricConfig.getRicId());
146 uri = new UriBuilder(ricConfig);
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);