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;
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
38 public class StdA1Client implements A1Client {
39 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41 private final AsyncRestClient restClient;
43 public StdA1Client(RicConfig ricConfig) {
44 String baseUrl = ricConfig.baseUrl() + "/A1-P/v1";
45 this.restClient = new AsyncRestClient(baseUrl);
48 public StdA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
49 this.restClient = restClient;
53 public Mono<List<String>> getPolicyIdentities() {
54 return restClient.get("/policies") //
55 .flatMap(this::parseJsonArrayOfString);
59 public Mono<String> putPolicy(Policy policy) {
60 String url = "/policies/" + policy.id() + "?policyTypeId=" + policy.type().name();
61 return restClient.put(url, policy.json()) //
62 .flatMap(this::validateJson);
66 public Mono<List<String>> getPolicyTypeIdentities() {
67 return restClient.get("/policytypes") //
68 .flatMap(this::parseJsonArrayOfString);
72 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
73 return restClient.get("/policytypes/" + policyTypeId) //
74 .flatMap(this::extractPolicySchema);
78 public Mono<String> deletePolicy(Policy policy) {
79 return deletePolicy(policy.id());
83 public Flux<String> deleteAllPolicies() {
84 return getPolicyIdentities() //
85 .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) // )
86 .flatMap(policyId -> deletePolicy(policyId)); //
90 public Mono<A1ProtocolType> getProtocolVersion() {
91 return getPolicyTypeIdentities() //
92 .flatMap(x -> Mono.just(A1ProtocolType.STD_V1));
95 private Mono<String> deletePolicy(String policyId) {
96 return restClient.delete("/policies/" + policyId);
99 private Mono<List<String>> parseJsonArrayOfString(String inputString) {
101 List<String> arrayList = new ArrayList<>();
102 JSONArray jsonArray = new JSONArray(inputString);
103 for (int i = 0; i < jsonArray.length(); i++) {
104 arrayList.add(jsonArray.getString(i));
106 logger.debug("A1 client: received list = {}", arrayList);
107 return Mono.just(arrayList);
108 } catch (JSONException ex) { // invalid json
109 return Mono.error(ex);
113 private Mono<String> extractPolicySchema(String inputString) {
115 JSONObject jsonObject = new JSONObject(inputString);
116 JSONObject schemaObject = jsonObject.getJSONObject("policySchema");
117 String schemaString = schemaObject.toString();
118 return Mono.just(schemaString);
119 } catch (JSONException ex) { // invalid json
120 return Mono.error(ex);
124 private Mono<String> validateJson(String inputString) {
126 new JSONObject(inputString);
127 return Mono.just(inputString);
128 } catch (JSONException ex) { // invalid json
129 return Mono.error(ex);