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.Collection;
26 import java.util.List;
28 import org.json.JSONArray;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Mono;
35 public class A1ClientImpl implements A1Client {
36 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38 private static String getBaseUrl(final String nearRtRicUrl) {
39 return nearRtRicUrl + "/A1-P/v1";
42 public AsyncRestClient createClient(final String nearRtRicUrl) {
43 return new AsyncRestClient(getBaseUrl(nearRtRicUrl));
47 public Mono<Collection<String>> getPolicyTypeIdentities(String nearRtRicUrl) {
48 logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl);
49 AsyncRestClient client = createClient(nearRtRicUrl);
50 return client.get("/policytypes/identities") //
51 .flatMap(this::parseJsonArrayOfString);
55 public Mono<Collection<String>> getPolicyIdentities(String nearRtRicUrl) {
56 logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl);
57 AsyncRestClient client = createClient(nearRtRicUrl);
58 return client.get("/policies/identities") //
59 .flatMap(this::parseJsonArrayOfString);
63 public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
64 logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
65 AsyncRestClient client = createClient(nearRtRicUrl);
66 Mono<String> response = client.get("/policytypes/" + policyTypeId);
67 return response.flatMap(this::createMono);
71 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
72 logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
74 AsyncRestClient client = createClient(nearRtRicUrl);
75 Mono<String> response = client.put("/policies/" + policyId, policyString);
76 return response.flatMap(this::createMono);
80 public Mono<String> deletePolicy(String nearRtRicUrl, String policyId) {
81 logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
82 AsyncRestClient client = createClient(nearRtRicUrl);
83 return client.delete("/policies/" + policyId);
86 private Mono<Collection<String>> parseJsonArrayOfString(String inputString) {
88 List<String> arrayList = new ArrayList<>();
89 JSONArray jsonArray = new JSONArray(inputString);
90 for (int i = 0; i < jsonArray.length(); i++) {
91 arrayList.add(jsonArray.getString(i));
93 logger.debug("A1 client: received list = {}", arrayList);
94 return Mono.just(arrayList);
95 } catch (JSONException ex) { // invalid json
96 return Mono.error(ex);
100 private Mono<String> createMono(String inputString) {
102 JSONObject jsonObject = new JSONObject(inputString);
103 String jsonString = jsonObject.toString();
104 logger.debug("A1 client: received string = {}", jsonString);
105 return Mono.just(jsonString);
106 } catch (JSONException ex) { // invalid json
107 return Mono.error(ex);