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 {
41 public static class UriBuilder implements A1UriBuilder {
42 private final RicConfig ricConfig;
44 public UriBuilder(RicConfig ricConfig) {
45 this.ricConfig = ricConfig;
49 public String createPutPolicyUri(String type, String policyId) {
50 return createPolicyUri(type, policyId);
54 * /a1-p/policytypes/{policy_type_id}/policies
56 public String createGetPolicyIdsUri(String type) {
57 return createPolicyTypeUri(type) + "/policies";
61 public String createDeleteUri(String type, String policyId) {
62 return createPolicyUri(type, policyId);
66 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
69 public String createGetPolicyStatusUri(String type, String policyId) {
70 return createPolicyUri(type, policyId) + "/status";
76 public String createHealtcheckUri() {
77 return baseUri() + "/healthcheck";
81 * /a1-p/policytypes/{policy_type_id}
83 public String createGetSchemaUri(String type) {
84 return this.createPolicyTypeUri(type);
88 * /a1-p/policytypes/{policy_type_id}
90 public String createPolicyTypesUri() {
91 return baseUri() + "/policytypes";
95 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}
97 private String createPolicyUri(String type, String id) {
98 return createPolicyTypeUri(type) + "/policies/" + id;
102 * /a1-p/policytypes/{policy_type_id}
104 private String createPolicyTypeUri(String type) {
105 return createPolicyTypesUri() + "/" + type;
108 private String baseUri() {
109 return ricConfig.baseUrl() + "/a1-p";
113 private static final String TITLE = "title";
114 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
115 private final AsyncRestClient restClient;
116 private final UriBuilder uri;
118 public OscA1Client(RicConfig ricConfig) {
119 this(ricConfig, new AsyncRestClient(""));
122 public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
123 this.restClient = restClient;
124 logger.debug("OscA1Client for ric: {}", ricConfig.name());
126 uri = new UriBuilder(ricConfig);
129 public static Mono<String> extractCreateSchema(String policyTypeResponse, String policyTypeId) {
131 JSONObject obj = new JSONObject(policyTypeResponse);
132 JSONObject schemaObj = obj.getJSONObject("create_schema");
133 schemaObj.put(TITLE, policyTypeId);
134 return Mono.just(schemaObj.toString());
135 } catch (Exception e) {
136 String exceptionString = e.toString();
137 logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
138 return Mono.error(e);
143 public Mono<List<String>> getPolicyTypeIdentities() {
144 return getPolicyTypeIds() //
149 public Mono<List<String>> getPolicyIdentities() {
150 return getPolicyTypeIds() //
151 .flatMap(this::getPolicyIdentitiesByType) //
156 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
157 String schemaUri = uri.createGetSchemaUri(policyTypeId);
158 return restClient.get(schemaUri) //
159 .flatMap(response -> extractCreateSchema(response, policyTypeId));
163 public Mono<String> putPolicy(Policy policy) {
164 String policyUri = this.uri.createPutPolicyUri(policy.type().name(), policy.id());
165 return restClient.put(policyUri, policy.json());
169 public Mono<String> deletePolicy(Policy policy) {
170 return deletePolicyById(policy.type().name(), policy.id());
174 public Mono<A1ProtocolType> getProtocolVersion() {
175 return restClient.get(uri.createHealtcheckUri()) //
176 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
180 public Flux<String> deleteAllPolicies() {
181 return getPolicyTypeIds() //
182 .flatMap(this::deletePoliciesForType);
186 public Mono<String> getPolicyStatus(Policy policy) {
187 String statusUri = uri.createGetPolicyStatusUri(policy.type().name(), policy.id());
188 return restClient.get(statusUri);
192 private Flux<String> getPolicyTypeIds() {
193 return restClient.get(uri.createPolicyTypesUri()) //
194 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
197 private Flux<String> getPolicyIdentitiesByType(String typeId) {
198 return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
199 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
202 private Mono<String> deletePolicyById(String typeId, String policyId) {
203 String policyUri = uri.createDeleteUri(typeId, policyId);
204 return restClient.delete(policyUri);
207 private Flux<String> deletePoliciesForType(String typeId) {
208 return getPolicyIdentitiesByType(typeId) //
209 .flatMap(policyId -> deletePolicyById(typeId, policyId));