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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Flux;
34 import reactor.core.publisher.Mono;
36 public class A1ClientImpl implements A1Client {
37 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
39 private static String getBaseUrl(final String nearRtRicUrl) {
40 return nearRtRicUrl + "/A1-P/v1";
44 public Flux<String> getPolicyTypeIdentities(String nearRtRicUrl) {
45 logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl);
46 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
47 Mono<String> response = client.get("/policytypes/identities");
48 return response.flatMapMany(this::createFlux);
52 public Flux<String> getPolicyIdentities(String nearRtRicUrl) {
53 logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl);
54 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
55 Mono<String> response = client.get("/policies/identities");
56 return response.flatMapMany(this::createFlux);
60 public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
61 logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
62 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
63 Mono<String> response = client.get("/policytypes/" + policyTypeId);
64 return response.flatMap(this::createMono);
68 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
69 logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
71 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
72 Mono<String> response = client.put("/policies/" + policyId, policyString);
73 return response.flatMap(this::createMono);
77 public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
78 logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
79 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
80 return client.delete("/policies/" + policyId);
83 private Flux<String> createFlux(String inputString) {
85 List<String> arrayList = new ArrayList<>();
86 JSONArray jsonArray = new JSONArray(inputString);
87 for (int i = 0; i < jsonArray.length(); i++) {
88 arrayList.add(jsonArray.getString(i));
90 logger.debug("A1 client: received list = {}", arrayList);
91 return Flux.fromIterable(arrayList);
92 } catch (JSONException ex) { // invalid json
93 return Flux.error(ex);
97 private Mono<String> createMono(String inputString) {
99 JSONObject jsonObject = new JSONObject(inputString);
100 String jsonString = jsonObject.toString();
101 logger.debug("A1 client: received string = {}", jsonString);
102 return Mono.just(jsonString);
103 } catch (JSONException ex) { // invalid json
104 return Mono.error(ex);