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.oransc.policyagent.repository.Policy;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
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";
43 protected AsyncRestClient createClient(final String nearRtRicUrl) {
44 return new AsyncRestClient(getBaseUrl(nearRtRicUrl));
48 public Mono<Collection<String>> getPolicyTypeIdentities(String nearRtRicUrl) {
49 logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl);
50 AsyncRestClient client = createClient(nearRtRicUrl);
51 return client.get("/policytypes/identities") //
52 .flatMap(this::parseJsonArrayOfString);
56 public Mono<Collection<String>> getPolicyIdentities(String nearRtRicUrl) {
57 logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl);
58 AsyncRestClient client = createClient(nearRtRicUrl);
59 return client.get("/policies/identities") //
60 .flatMap(this::parseJsonArrayOfString);
64 public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
65 logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
66 AsyncRestClient client = createClient(nearRtRicUrl);
67 Mono<String> response = client.get("/policytypes/" + policyTypeId);
68 return response.flatMap(this::createMono);
72 public Mono<String> putPolicy(Policy policy) {
73 logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", //
74 policy.ric().getConfig().baseUrl(), policy.id(), policy.json());
75 AsyncRestClient client = createClient(policy.ric().getConfig().baseUrl());
76 // TODO update when simulator is updated to include policy type
77 // Mono<String> response = client.put("/policies/" + policy.id() + "?policyTypeId=" + policy.type().name(),
79 Mono<String> response = client.put("/policies/" + policy.id(), policy.json());
81 return response.flatMap(this::createMono);
85 public Mono<String> deletePolicy(String nearRtRicUrl, String policyId) {
86 logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
87 AsyncRestClient client = createClient(nearRtRicUrl);
88 return client.delete("/policies/" + policyId);
91 private Mono<Collection<String>> parseJsonArrayOfString(String inputString) {
93 List<String> arrayList = new ArrayList<>();
94 JSONArray jsonArray = new JSONArray(inputString);
95 for (int i = 0; i < jsonArray.length(); i++) {
96 arrayList.add(jsonArray.getString(i));
98 logger.debug("A1 client: received list = {}", arrayList);
99 return Mono.just(arrayList);
100 } catch (JSONException ex) { // invalid json
101 return Mono.error(ex);
105 private Mono<String> createMono(String inputString) {
107 JSONObject jsonObject = new JSONObject(inputString);
108 String jsonString = jsonObject.toString();
109 logger.debug("A1 client: received string = {}", jsonString);
110 return Mono.just(jsonString);
111 } catch (JSONException ex) { // invalid json
112 return Mono.error(ex);