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;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.oransc.policyagent.configuration.RicConfig;
30 import org.oransc.policyagent.repository.Policy;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.web.util.UriComponentsBuilder;
34 import reactor.core.publisher.Flux;
35 import reactor.core.publisher.Mono;
37 public class StdA1Client implements A1Client {
38 private static final String URL_PREFIX = "/A1-P/v1";
40 private static final String POLICYTYPES_URI = "/policytypes";
41 private static final String POLICY_TYPE_ID = "policyTypeId";
43 private static final String POLICIES_URI = "/policies";
44 private static final String POLICY_SCHEMA = "policySchema";
46 private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
47 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
49 private static final UriComponentsBuilder POLICY_URI =
50 UriComponentsBuilder.fromPath("/policies/{policy-id}").queryParam(POLICY_TYPE_ID, "{policy-type-name}");
52 private static final UriComponentsBuilder POLICY_DELETE_URI =
53 UriComponentsBuilder.fromPath("/policies/{policy-id}");
55 private static final UriComponentsBuilder POLICY_STATUS_URI =
56 UriComponentsBuilder.fromPath("/policies/{policy-id}/status");
58 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60 private final AsyncRestClient restClient;
62 public StdA1Client(RicConfig ricConfig) {
63 String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
64 this.restClient = new AsyncRestClient(baseUrl);
67 public StdA1Client(AsyncRestClient restClient) {
68 this.restClient = restClient;
72 public Mono<List<String>> getPolicyIdentities() {
73 return restClient.get(POLICIES_URI) //
74 .flatMap(this::parseJsonArrayOfString);
78 public Mono<String> putPolicy(Policy policy) {
79 String uri = POLICY_URI.buildAndExpand(policy.id(), policy.type().name()).toUriString();
80 return restClient.put(uri, policy.json()) //
81 .flatMap(this::validateJson);
85 public Mono<List<String>> getPolicyTypeIdentities() {
86 return restClient.get(POLICYTYPES_URI) //
87 .flatMap(this::parseJsonArrayOfString);
91 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
92 String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
93 return restClient.get(uri) //
94 .flatMap(this::extractPolicySchema);
98 public Mono<String> deletePolicy(Policy policy) {
99 return deletePolicyById(policy.id());
103 public Flux<String> deleteAllPolicies() {
104 return getPolicyIdentities() //
105 .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) // )
106 .flatMap(policyId -> deletePolicyById(policyId)); //
110 public Mono<A1ProtocolType> getProtocolVersion() {
111 return getPolicyTypeIdentities() //
112 .flatMap(x -> Mono.just(A1ProtocolType.STD_V1));
116 public Mono<String> getPolicyStatus(Policy policy) {
117 String uri = POLICY_STATUS_URI.buildAndExpand(policy.id()).toUriString();
118 return restClient.get(uri);
121 private Mono<String> deletePolicyById(String policyId) {
122 String uri = POLICY_DELETE_URI.buildAndExpand(policyId).toUriString();
123 return restClient.delete(uri);
126 private Mono<List<String>> parseJsonArrayOfString(String inputString) {
128 List<String> arrayList = new ArrayList<>();
129 JSONArray jsonArray = new JSONArray(inputString);
130 for (int i = 0; i < jsonArray.length(); i++) {
131 arrayList.add(jsonArray.getString(i));
133 logger.debug("A1 client: received list = {}", arrayList);
134 return Mono.just(arrayList);
135 } catch (JSONException ex) { // invalid json
136 return Mono.error(ex);
140 private Mono<String> extractPolicySchema(String inputString) {
142 JSONObject jsonObject = new JSONObject(inputString);
143 JSONObject schemaObject = jsonObject.getJSONObject(POLICY_SCHEMA);
144 String schemaString = schemaObject.toString();
145 return Mono.just(schemaString);
146 } catch (JSONException ex) { // invalid json
147 return Mono.error(ex);
151 private Mono<String> validateJson(String inputString) {
153 new JSONObject(inputString);
154 return Mono.just(inputString);
155 } catch (JSONException ex) { // invalid json
156 return Mono.error(ex);