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;
24 import java.util.List;
27 import org.json.JSONObject;
28 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Flux;
34 import reactor.core.publisher.Mono;
35 import org.apache.http.client.utils.URIBuilder;
37 * Client for accessing OSC A1 REST API
39 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
40 public class OscA1Client implements A1Client {
41 static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC
43 public static class UriBuilder implements A1UriBuilder {
44 private final RicConfig ricConfig;
46 public UriBuilder(RicConfig ricConfig) {
47 this.ricConfig = ricConfig;
51 public String createPutPolicyUri(String type, String policyId, String notificationDestinationUri) {
52 return createPolicyUri(type, policyId, notificationDestinationUri);
56 * /a1-p/policytypes/{policy_type_id}/policies
59 public String createGetPolicyIdsUri(String type) {
60 return createPolicyTypeUri(type) + "/policies";
64 public String createDeleteUri(String type, String policyId) {
65 return createPolicyUri(type, policyId, null);
69 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
72 public String createGetPolicyStatusUri(String type, String policyId) {
73 return createPolicyUri(type, policyId, null) + "/status";
79 public String createHealtcheckUri() {
80 return baseUri() + "/healthcheck";
84 * /a1-p/policytypes/{policy_type_id}
87 public String createGetSchemaUri(String type) {
88 return this.createPolicyTypeUri(type);
92 * /a1-p/policytypes/{policy_type_id}
95 public String createPolicyTypesUri() {
96 return baseUri() + "/policytypes";
100 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}
102 private String createPolicyUri(String type, String id, String notificationDestination) {
104 URIBuilder ub = null;
106 ub = new URIBuilder(createPolicyTypeUri(type) + "/policies/" + id);
107 if(notificationDestination != null) {
108 ub.addParameter("notificationDestination", notificationDestination);
113 String exceptionString = e.toString();
114 logger.error("Unexpected error in policy URI creation for policy type: {}, exception: {}", type, exceptionString);
120 * /a1-p/policytypes/{policy_type_id}
122 private String createPolicyTypeUri(String type) {
123 return createPolicyTypesUri() + "/" + type;
126 private String baseUri() {
127 return ricConfig.getBaseUrl() + "/a1-p";
131 private static final String TITLE = "title";
132 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
133 private final AsyncRestClient restClient;
134 private final UriBuilder uri;
136 public OscA1Client(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
137 this(ricConfig, restClientFactory.createRestClientUseHttpProxy(""));
140 public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
141 this.restClient = restClient;
142 logger.debug("OscA1Client for ric: {}", ricConfig.getRicId());
144 uri = new UriBuilder(ricConfig);
147 public static Mono<String> extractCreateSchema(String policyTypeResponse, String policyTypeId) {
149 JSONObject obj = new JSONObject(policyTypeResponse);
150 JSONObject schemaObj = obj.getJSONObject("create_schema");
151 schemaObj.put(TITLE, policyTypeId);
152 return Mono.just(schemaObj.toString());
153 } catch (Exception e) {
154 String exceptionString = e.toString();
155 logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
156 return Mono.error(e);
161 public Mono<List<String>> getPolicyTypeIdentities() {
162 return getPolicyTypeIds() //
167 public Mono<List<String>> getPolicyIdentities() {
168 return getPolicyTypeIds() //
169 .flatMap(this::getPolicyIdentitiesByType) //
174 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
175 String schemaUri = uri.createGetSchemaUri(policyTypeId);
176 return restClient.get(schemaUri) //
177 .flatMap(response -> extractCreateSchema(response, policyTypeId));
181 public Mono<String> putPolicy(Policy policy) {
182 String policyUri = this.uri.createPutPolicyUri(policy.getType().getId(), policy.getId(),
183 policy.getStatusNotificationUri());
184 return restClient.put(policyUri, policy.getJson());
188 public Mono<String> deletePolicy(Policy policy) {
189 return deletePolicyById(policy.getType().getId(), policy.getId());
193 public Mono<A1ProtocolType> getProtocolVersion() {
194 return restClient.get(uri.createHealtcheckUri()) //
195 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
199 public Flux<String> deleteAllPolicies(Set<String> excludePolicyIds) {
200 return getPolicyTypeIds() //
201 .flatMap(typeId -> deletePoliciesForType(typeId, excludePolicyIds), CONCURRENCY_RIC);
205 public Mono<String> getPolicyStatus(Policy policy) {
206 String statusUri = uri.createGetPolicyStatusUri(policy.getType().getId(), policy.getId());
207 return restClient.get(statusUri);
211 private Flux<String> getPolicyTypeIds() {
212 return restClient.get(uri.createPolicyTypesUri()) //
213 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
216 private Flux<String> getPolicyIdentitiesByType(String typeId) {
217 return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
218 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
221 private Mono<String> deletePolicyById(String typeId, String policyId) {
222 String policyUri = uri.createDeleteUri(typeId, policyId);
223 return restClient.delete(policyUri);
226 private Flux<String> deletePoliciesForType(String typeId, Set<String> excludePolicyIds) {
227 return getPolicyIdentitiesByType(typeId) //
228 .filter(policyId -> !excludePolicyIds.contains(policyId)) //
229 .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);