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.repository.Policy;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import reactor.core.publisher.Flux;
33 import reactor.core.publisher.Mono;
36 * Client for accessing OSC A1 REST API
38 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
39 public class OscA1Client implements A1Client {
40 static final int CONCURRENCY_RIC = 1; // How may paralell requests that is sent to one NearRT RIC
42 public static class UriBuilder implements A1UriBuilder {
43 private final RicConfig ricConfig;
45 public UriBuilder(RicConfig ricConfig) {
46 this.ricConfig = ricConfig;
50 public String createPutPolicyUri(String type, String policyId) {
51 return createPolicyUri(type, policyId);
55 * /a1-p/policytypes/{policy_type_id}/policies
57 public String createGetPolicyIdsUri(String type) {
58 return createPolicyTypeUri(type) + "/policies";
62 public String createDeleteUri(String type, String policyId) {
63 return createPolicyUri(type, policyId);
67 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
70 public String createGetPolicyStatusUri(String type, String policyId) {
71 return createPolicyUri(type, policyId) + "/status";
77 public String createHealtcheckUri() {
78 return baseUri() + "/healthcheck";
82 * /a1-p/policytypes/{policy_type_id}
84 public String createGetSchemaUri(String type) {
85 return this.createPolicyTypeUri(type);
89 * /a1-p/policytypes/{policy_type_id}
91 public String createPolicyTypesUri() {
92 return baseUri() + "/policytypes";
96 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}
98 private String createPolicyUri(String type, String id) {
99 return createPolicyTypeUri(type) + "/policies/" + id;
103 * /a1-p/policytypes/{policy_type_id}
105 private String createPolicyTypeUri(String type) {
106 return createPolicyTypesUri() + "/" + type;
109 private String baseUri() {
110 return ricConfig.baseUrl() + "/a1-p";
114 private static final String TITLE = "title";
115 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
116 private final AsyncRestClient restClient;
117 private final UriBuilder uri;
119 public OscA1Client(RicConfig ricConfig) {
120 this(ricConfig, new AsyncRestClient(""));
123 public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
124 this.restClient = restClient;
125 logger.debug("OscA1Client for ric: {}", ricConfig.name());
127 uri = new UriBuilder(ricConfig);
130 public static Mono<String> extractCreateSchema(String policyTypeResponse, String policyTypeId) {
132 JSONObject obj = new JSONObject(policyTypeResponse);
133 JSONObject schemaObj = obj.getJSONObject("create_schema");
134 schemaObj.put(TITLE, policyTypeId);
135 return Mono.just(schemaObj.toString());
136 } catch (Exception e) {
137 String exceptionString = e.toString();
138 logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
139 return Mono.error(e);
144 public Mono<List<String>> getPolicyTypeIdentities() {
145 return getPolicyTypeIds() //
150 public Mono<List<String>> getPolicyIdentities() {
151 return getPolicyTypeIds() //
152 .flatMap(this::getPolicyIdentitiesByType) //
157 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
158 String schemaUri = uri.createGetSchemaUri(policyTypeId);
159 return restClient.get(schemaUri) //
160 .flatMap(response -> extractCreateSchema(response, policyTypeId));
164 public Mono<String> putPolicy(Policy policy) {
165 String policyUri = this.uri.createPutPolicyUri(policy.type().name(), policy.id());
166 return restClient.put(policyUri, policy.json());
170 public Mono<String> deletePolicy(Policy policy) {
171 return deletePolicyById(policy.type().name(), policy.id());
175 public Mono<A1ProtocolType> getProtocolVersion() {
176 return restClient.get(uri.createHealtcheckUri()) //
177 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
181 public Flux<String> deleteAllPolicies() {
182 return getPolicyTypeIds() //
183 .flatMap(this::deletePoliciesForType, CONCURRENCY_RIC);
187 public Mono<String> getPolicyStatus(Policy policy) {
188 String statusUri = uri.createGetPolicyStatusUri(policy.type().name(), policy.id());
189 return restClient.get(statusUri);
193 private Flux<String> getPolicyTypeIds() {
194 return restClient.get(uri.createPolicyTypesUri()) //
195 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
198 private Flux<String> getPolicyIdentitiesByType(String typeId) {
199 return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
200 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
203 private Mono<String> deletePolicyById(String typeId, String policyId) {
204 String policyUri = uri.createDeleteUri(typeId, policyId);
205 return restClient.delete(policyUri);
208 private Flux<String> deletePoliciesForType(String typeId) {
209 return getPolicyIdentitiesByType(typeId) //
210 .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);