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;
31 import org.springframework.web.util.UriComponentsBuilder;
33 import reactor.core.publisher.Flux;
34 import reactor.core.publisher.Mono;
36 public class OscA1Client implements A1Client {
37 private static final String URL_PREFIX = "/a1-p";
39 private static final String POLICY_TYPES = "/policytypes";
40 private static final String CREATE_SCHEMA = "create_schema";
41 private static final String TITLE = "title";
43 private static final String HEALTHCHECK = "/healthcheck";
45 private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
46 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
48 private static final UriComponentsBuilder POLICY_URI =
49 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}");
51 private static final UriComponentsBuilder POLICY_IDS_URI =
52 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies");
54 private static final UriComponentsBuilder POLICY_STATUS_URI =
55 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}/status");
57 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 private final AsyncRestClient restClient;
61 public OscA1Client(RicConfig ricConfig) {
62 String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
63 this.restClient = new AsyncRestClient(baseUrl);
64 if (logger.isDebugEnabled()) {
65 logger.debug("OscA1Client for ric: {}", ricConfig.name());
69 public OscA1Client(AsyncRestClient restClient) {
70 this.restClient = restClient;
74 public Mono<List<String>> getPolicyTypeIdentities() {
75 return getPolicyTypeIds() //
80 public Mono<List<String>> getPolicyIdentities() {
81 return getPolicyTypeIds() //
82 .flatMap(this::getPolicyIdentitiesByType) //
87 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
88 String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
89 return restClient.get(uri) //
90 .flatMap(response -> getCreateSchema(response, policyTypeId));
94 public Mono<String> putPolicy(Policy policy) {
95 String uri = POLICY_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
96 return restClient.put(uri, policy.json());
100 public Mono<String> deletePolicy(Policy policy) {
101 return deletePolicyById(policy.type().name(), policy.id());
105 public Mono<A1ProtocolType> getProtocolVersion() {
106 return restClient.get(HEALTHCHECK) //
107 .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
111 public Flux<String> deleteAllPolicies() {
112 return getPolicyTypeIds() //
113 .flatMap(this::deletePoliciesForType);
117 public Mono<String> getPolicyStatus(Policy policy) {
118 String uri = POLICY_STATUS_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
119 return restClient.get(uri);
123 private Flux<String> getPolicyTypeIds() {
124 return restClient.get(POLICY_TYPES) //
125 .flatMapMany(JsonHelper::parseJsonArrayOfString);
128 private Flux<String> getPolicyIdentitiesByType(String typeId) {
129 return restClient.get(POLICY_IDS_URI.buildAndExpand(typeId).toUriString()) //
130 .flatMapMany(JsonHelper::parseJsonArrayOfString);
133 private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
135 JSONObject obj = new JSONObject(policyTypeResponse);
136 JSONObject schemaObj = obj.getJSONObject(CREATE_SCHEMA);
137 schemaObj.put(TITLE, policyTypeId);
138 return Mono.just(schemaObj.toString());
139 } catch (Exception e) {
140 String exceptionString = e.toString();
141 logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
142 return Mono.error(e);
146 private Mono<String> deletePolicyById(String typeId, String policyId) {
147 String uri = POLICY_URI.buildAndExpand(typeId, policyId).toUriString();
148 return restClient.delete(uri);
151 private Flux<String> deletePoliciesForType(String typeId) {
152 return getPolicyIdentitiesByType(typeId) //
153 .flatMap(policyId -> deletePolicyById(typeId, policyId));