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 java.lang.invoke.MethodHandles;
24 import java.util.List;
26 import org.json.JSONObject;
27 import org.oransc.policyagent.configuration.RicConfig;
28 import org.oransc.policyagent.configuration.WebClientConfig;
29 import org.oransc.policyagent.repository.Policy;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Flux;
34 import reactor.core.publisher.Mono;
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 may 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) {
52 return createPolicyUri(type, policyId);
56 * /a1-p/policytypes/{policy_type_id}/policies
58 public String createGetPolicyIdsUri(String type) {
59 return createPolicyTypeUri(type) + "/policies";
63 public String createDeleteUri(String type, String policyId) {
64 return createPolicyUri(type, policyId);
68 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
71 public String createGetPolicyStatusUri(String type, String policyId) {
72 return createPolicyUri(type, policyId) + "/status";
78 public String createHealtcheckUri() {
79 return baseUri() + "/healthcheck";
83 * /a1-p/policytypes/{policy_type_id}
85 public String createGetSchemaUri(String type) {
86 return this.createPolicyTypeUri(type);
90 * /a1-p/policytypes/{policy_type_id}
92 public String createPolicyTypesUri() {
93 return baseUri() + "/policytypes";
97 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}
99 private String createPolicyUri(String type, String id) {
100 return createPolicyTypeUri(type) + "/policies/" + id;
104 * /a1-p/policytypes/{policy_type_id}
106 private String createPolicyTypeUri(String type) {
107 return createPolicyTypesUri() + "/" + type;
110 private String baseUri() {
111 return ricConfig.baseUrl() + "/a1-p";
115 private static final String TITLE = "title";
116 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
117 private final AsyncRestClient restClient;
118 private final UriBuilder uri;
120 public OscA1Client(RicConfig ricConfig, WebClientConfig clientConfig) {
121 this(ricConfig, new AsyncRestClient("", clientConfig));
124 public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
125 this.restClient = restClient;
126 logger.debug("OscA1Client for ric: {}", ricConfig.name());
128 uri = new UriBuilder(ricConfig);
131 public static Mono<String> extractCreateSchema(String policyTypeResponse, String policyTypeId) {
133 JSONObject obj = new JSONObject(policyTypeResponse);
134 JSONObject schemaObj = obj.getJSONObject("create_schema");
135 schemaObj.put(TITLE, policyTypeId);
136 return Mono.just(schemaObj.toString());
137 } catch (Exception e) {
138 String exceptionString = e.toString();
139 logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
140 return Mono.error(e);
145 public Mono<List<String>> getPolicyTypeIdentities() {
146 return getPolicyTypeIds() //
151 public Mono<List<String>> getPolicyIdentities() {
152 return getPolicyTypeIds() //
153 .flatMap(this::getPolicyIdentitiesByType) //
158 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
159 String schemaUri = uri.createGetSchemaUri(policyTypeId);
160 return restClient.get(schemaUri) //
161 .flatMap(response -> extractCreateSchema(response, policyTypeId));
165 public Mono<String> putPolicy(Policy policy) {
166 String policyUri = this.uri.createPutPolicyUri(policy.type().name(), policy.id());
167 return restClient.put(policyUri, policy.json());
171 public Mono<String> deletePolicy(Policy policy) {
172 return deletePolicyById(policy.type().name(), policy.id());
176 public Mono<A1ProtocolType> getProtocolVersion() {
177 return restClient.get(uri.createHealtcheckUri()) //
178 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
182 public Flux<String> deleteAllPolicies() {
183 return getPolicyTypeIds() //
184 .flatMap(this::deletePoliciesForType, CONCURRENCY_RIC);
188 public Mono<String> getPolicyStatus(Policy policy) {
189 String statusUri = uri.createGetPolicyStatusUri(policy.type().name(), policy.id());
190 return restClient.get(statusUri);
194 private Flux<String> getPolicyTypeIds() {
195 return restClient.get(uri.createPolicyTypesUri()) //
196 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
199 private Flux<String> getPolicyIdentitiesByType(String typeId) {
200 return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
201 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
204 private Mono<String> deletePolicyById(String typeId, String policyId) {
205 String policyUri = uri.createDeleteUri(typeId, policyId);
206 return restClient.delete(policyUri);
209 private Flux<String> deletePoliciesForType(String typeId) {
210 return getPolicyIdentitiesByType(typeId) //
211 .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);