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;
25 import org.json.JSONObject;
26 import org.oransc.policyagent.configuration.RicConfig;
27 import org.oransc.policyagent.repository.Policy;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.web.util.UriComponentsBuilder;
31 import reactor.core.publisher.Flux;
32 import reactor.core.publisher.Mono;
34 public class OscA1Client implements A1Client {
35 private static final String URL_PREFIX = "/a1-p";
37 private static final String POLICY_TYPES = "/policytypes";
38 private static final String CREATE_SCHEMA = "create_schema";
39 private static final String TITLE = "title";
41 private static final String HEALTHCHECK = "/healthcheck";
43 private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
44 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
46 private static final UriComponentsBuilder POLICY_URI =
47 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}");
49 private static final UriComponentsBuilder POLICY_IDS_URI =
50 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies");
52 private static final UriComponentsBuilder POLICY_STATUS_URI =
53 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}/status");
55 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57 private final AsyncRestClient restClient;
59 public OscA1Client(RicConfig ricConfig) {
60 String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
61 this.restClient = new AsyncRestClient(baseUrl);
62 if (logger.isDebugEnabled()) {
63 logger.debug("OscA1Client for ric: {}", ricConfig.name());
67 public OscA1Client(AsyncRestClient restClient) {
68 this.restClient = restClient;
72 public Mono<List<String>> getPolicyTypeIdentities() {
73 return getPolicyTypeIds() //
78 public Mono<List<String>> getPolicyIdentities() {
79 return getPolicyTypeIds() //
80 .flatMap(this::getPolicyIdentitiesByType) //
85 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
86 String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
87 return restClient.get(uri) //
88 .flatMap(response -> getCreateSchema(response, policyTypeId));
92 public Mono<String> putPolicy(Policy policy) {
93 String uri = POLICY_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
94 return restClient.put(uri, policy.json());
98 public Mono<String> deletePolicy(Policy policy) {
99 return deletePolicyById(policy.type().name(), policy.id());
103 public Mono<A1ProtocolType> getProtocolVersion() {
104 return restClient.get(HEALTHCHECK) //
105 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
109 public Flux<String> deleteAllPolicies() {
110 return getPolicyTypeIds() //
111 .flatMap(this::deletePoliciesForType);
115 public Mono<String> getPolicyStatus(Policy policy) {
116 String uri = POLICY_STATUS_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
117 return restClient.get(uri);
121 private Flux<String> getPolicyTypeIds() {
122 return restClient.get(POLICY_TYPES) //
123 .flatMapMany(JsonHelper::parseJsonArrayOfString);
126 private Flux<String> getPolicyIdentitiesByType(String typeId) {
127 return restClient.get(POLICY_IDS_URI.buildAndExpand(typeId).toUriString()) //
128 .flatMapMany(JsonHelper::parseJsonArrayOfString);
131 private Mono<String> getCreateSchema(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 logger.error("Unexcpected response for policy type: {}", policyTypeResponse, e);
139 return Mono.error(e);
143 private Mono<String> deletePolicyById(String typeId, String policyId) {
144 String uri = POLICY_URI.buildAndExpand(typeId, policyId).toUriString();
145 return restClient.delete(uri);
148 private Flux<String> deletePoliciesForType(String typeId) {
149 return getPolicyIdentitiesByType(typeId) //
150 .flatMap(policyId -> deletePolicyById(typeId, policyId));