2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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.ArrayList;
25 import java.util.List;
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.oransc.policyagent.configuration.RicConfig;
31 import org.oransc.policyagent.repository.Policy;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.web.util.UriComponentsBuilder;
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
38 public class OscA1Client implements A1Client {
39 private static final String URL_PREFIX = "/a1-p";
41 private static final String POLICYTYPES = "/policytypes";
42 private static final String CREATE_SCHEMA = "create_schema";
43 private static final String TITLE = "title";
45 private static final String HEALTHCHECK = "/healthcheck";
47 private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
48 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
50 private static final UriComponentsBuilder POLICY_URI =
51 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}");
53 private static final UriComponentsBuilder POLICY_IDS_URI =
54 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies");
56 private static final UriComponentsBuilder POLICY_STATUS_URI =
57 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}/status");
59 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61 private final AsyncRestClient restClient;
63 public OscA1Client(RicConfig ricConfig) {
64 String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
65 this.restClient = new AsyncRestClient(baseUrl);
66 if (logger.isDebugEnabled()) {
67 logger.debug("OscA1Client for ric: {}", ricConfig.name());
71 public OscA1Client(AsyncRestClient restClient) {
72 this.restClient = restClient;
76 public Mono<List<String>> getPolicyTypeIdentities() {
77 return restClient.get(POLICYTYPES) //
78 .flatMap(this::parseJsonArrayOfString);
82 public Mono<List<String>> getPolicyIdentities() {
83 return getPolicyTypeIdentities() //
84 .flatMapMany(types -> Flux.fromIterable(types)) //
85 .flatMap(type -> getPolicyIdentitiesById(type)) //
86 .flatMap(policyIds -> Flux.fromIterable(policyIds)) //
91 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
92 String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
93 return restClient.get(uri) //
94 .flatMap(response -> getCreateSchema(response, policyTypeId));
98 public Mono<String> putPolicy(Policy policy) {
99 String uri = POLICY_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
100 return restClient.put(uri, policy.json());
104 public Mono<String> deletePolicy(Policy policy) {
105 return deletePolicyById(policy.type().name(), policy.id());
109 public Mono<A1ProtocolType> getProtocolVersion() {
110 return restClient.get(HEALTHCHECK) //
111 .flatMap(resp -> Mono.just(A1ProtocolType.OSC_V1));
115 public Flux<String> deleteAllPolicies() {
116 return getPolicyTypeIdentities() //
117 .flatMapMany(types -> Flux.fromIterable(types)) //
118 .flatMap(typeId -> deletePoliciesForType(typeId)); //
122 public Mono<String> getPolicyStatus(Policy policy) {
123 String uri = POLICY_STATUS_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
124 return restClient.get(uri);
128 private Mono<List<String>> getPolicyIdentitiesById(String typeId) {
129 String uri = POLICY_IDS_URI.buildAndExpand(typeId).toUriString();
130 return restClient.get(uri) //
131 .flatMap(this::parseJsonArrayOfString);
134 private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
136 JSONObject obj = new JSONObject(policyTypeResponse);
137 JSONObject schemaObj = obj.getJSONObject(CREATE_SCHEMA);
138 schemaObj.put(TITLE, policyTypeId);
139 return Mono.just(schemaObj.toString());
140 } catch (Exception e) {
141 logger.error("Unexcpected response for policy type: {}", policyTypeResponse, e);
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 getPolicyIdentitiesById(typeId) //
153 .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) //
154 .flatMap(policyId -> deletePolicyById(typeId, policyId)); //
157 private Mono<List<String>> parseJsonArrayOfString(String inputString) {
159 List<String> arrayList = new ArrayList<>();
160 JSONArray jsonArray = new JSONArray(inputString);
161 for (int i = 0; i < jsonArray.length(); i++) {
162 arrayList.add(jsonArray.getString(i));
164 logger.debug("A1 client: received list = {}", arrayList);
165 return Mono.just(arrayList);
166 } catch (JSONException ex) { // invalid json
167 return Mono.error(ex);