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.oransc.policyagent.configuration.RicConfig;
27 import org.oransc.policyagent.repository.Policy;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import reactor.core.publisher.Flux;
32 import reactor.core.publisher.Mono;
35 * Client for accessing OSC A1 REST API
37 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
38 public class OscA1Client implements A1Client {
40 public static class UriBuilder implements A1UriBuilder {
41 private final RicConfig ricConfig;
43 public UriBuilder(RicConfig ricConfig) {
44 this.ricConfig = ricConfig;
48 public String createPutPolicyUri(String type, String policyId) {
49 return createPolicyUri(type, policyId);
53 * /a1-p/policytypes/{policy_type_id}/policies
55 public String createGetPolicyIdsUri(String type) {
56 return createPolicyTypeUri(type) + "/policies";
60 public String createDeleteUri(String type, String policyId) {
61 return createPolicyUri(type, policyId);
65 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
68 public String createGetPolicyStatusUri(String type, String policyId) {
69 return createPolicyUri(type, policyId) + "/status";
75 public String createHealtcheckUri() {
76 return baseUri() + "/healthcheck";
80 * /a1-p/policytypes/{policy_type_id}
82 public String createGetSchemaUri(String type) {
83 return this.createPolicyTypeUri(type);
87 * /a1-p/policytypes/{policy_type_id}
89 public String createPolicyTypesUri() {
90 return baseUri() + "/policytypes";
94 * /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}
96 private String createPolicyUri(String type, String id) {
97 return createPolicyTypeUri(type) + "/policies/" + id;
101 * /a1-p/policytypes/{policy_type_id}
103 private String createPolicyTypeUri(String type) {
104 return createPolicyTypesUri() + "/" + type;
107 private String baseUri() {
108 return ricConfig.baseUrl() + "/a1-p";
112 private static final String TITLE = "title";
113 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
114 private final AsyncRestClient restClient;
115 private final UriBuilder uri;
117 public OscA1Client(RicConfig ricConfig) {
118 this(ricConfig, new AsyncRestClient(""));
121 public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
122 this.restClient = restClient;
123 logger.debug("OscA1Client for ric: {}", ricConfig.name());
125 uri = new UriBuilder(ricConfig);
129 public Mono<List<String>> getPolicyTypeIdentities() {
130 return getPolicyTypeIds() //
135 public Mono<List<String>> getPolicyIdentities() {
136 return getPolicyTypeIds() //
137 .flatMap(this::getPolicyIdentitiesByType) //
142 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
143 String schemaUri = uri.createGetSchemaUri(policyTypeId);
144 return restClient.get(schemaUri) //
145 .flatMap(response -> SdncJsonHelper.getCreateSchema(response, policyTypeId));
149 public Mono<String> putPolicy(Policy policy) {
150 String policyUri = this.uri.createPutPolicyUri(policy.type().name(), policy.id());
151 return restClient.put(policyUri, policy.json());
155 public Mono<String> deletePolicy(Policy policy) {
156 return deletePolicyById(policy.type().name(), policy.id());
160 public Mono<A1ProtocolType> getProtocolVersion() {
161 return restClient.get(uri.createHealtcheckUri()) //
162 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
166 public Flux<String> deleteAllPolicies() {
167 return getPolicyTypeIds() //
168 .flatMap(this::deletePoliciesForType);
172 public Mono<String> getPolicyStatus(Policy policy) {
173 String statusUri = uri.createGetPolicyStatusUri(policy.type().name(), policy.id());
174 return restClient.get(statusUri);
178 private Flux<String> getPolicyTypeIds() {
179 return restClient.get(uri.createPolicyTypesUri()) //
180 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
183 private Flux<String> getPolicyIdentitiesByType(String typeId) {
184 return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
185 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
188 private Mono<String> deletePolicyById(String typeId, String policyId) {
189 String policyUri = uri.createDeleteUri(typeId, policyId);
190 return restClient.delete(policyUri);
193 private Flux<String> deletePoliciesForType(String typeId) {
194 return getPolicyIdentitiesByType(typeId) //
195 .flatMap(policyId -> deletePolicyById(typeId, policyId));