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);
130 public Mono<List<String>> getPolicyTypeIdentities() {
131 return getPolicyTypeIds() //
136 public Mono<List<String>> getPolicyIdentities() {
137 return getPolicyTypeIds() //
138 .flatMap(this::getPolicyIdentitiesByType) //
143 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
144 String schemaUri = uri.createGetSchemaUri(policyTypeId);
145 return restClient.get(schemaUri) //
146 .flatMap(response -> getCreateSchema(response, policyTypeId));
150 public Mono<String> putPolicy(Policy policy) {
151 String policyUri = this.uri.createPutPolicyUri(policy.type().name(), policy.id());
152 return restClient.put(policyUri, policy.json());
156 public Mono<String> deletePolicy(Policy policy) {
157 return deletePolicyById(policy.type().name(), policy.id());
161 public Mono<A1ProtocolType> getProtocolVersion() {
162 return restClient.get(uri.createHealtcheckUri()) //
163 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
167 public Flux<String> deleteAllPolicies() {
168 return getPolicyTypeIds() //
169 .flatMap(this::deletePoliciesForType);
173 public Mono<String> getPolicyStatus(Policy policy) {
174 String statusUri = uri.createGetPolicyStatusUri(policy.type().name(), policy.id());
175 return restClient.get(statusUri);
179 private Flux<String> getPolicyTypeIds() {
180 return restClient.get(uri.createPolicyTypesUri()) //
181 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
184 private Flux<String> getPolicyIdentitiesByType(String typeId) {
185 return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
186 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
189 private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
191 JSONObject obj = new JSONObject(policyTypeResponse);
192 JSONObject schemaObj = obj.getJSONObject("create_schema");
193 schemaObj.put(TITLE, policyTypeId);
194 return Mono.just(schemaObj.toString());
195 } catch (Exception e) {
196 String exceptionString = e.toString();
197 logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
198 return Mono.error(e);
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));