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===================================
20 package org.oransc.policyagent.clients;
22 import java.lang.invoke.MethodHandles;
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.json.JSONArray;
26 import org.json.JSONException;
27 import org.json.JSONObject;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import reactor.core.publisher.Flux;
31 import reactor.core.publisher.Mono;
33 public class A1Client {
34 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
36 public String getBaseUrl(final String nearRtRicUrl) {
37 return nearRtRicUrl + "/A1-P/v1";
40 public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
41 logger.debug("getAllPolicyTypes nearRtRicUrl = {}", nearRtRicUrl);
42 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
43 Mono<String> response = client.get("/policytypes");
44 return response.flatMapMany(this::createPolicyTypesFlux);
47 public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
48 logger.debug("getPoliciesForType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
49 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
50 Mono<String> response = client.get("/policies");
51 return response.flatMapMany(policiesString -> createPoliciesFlux(policiesString, policyTypeId));
54 public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
55 logger.debug("getPolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
56 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
57 Mono<String> response = client.get("/policies/" + policyId);
58 return response.flatMap(this::createPolicyMono);
61 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
62 logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
65 new JSONObject(policyString);
66 } catch (JSONException ex) { // invalid json
67 return Mono.error(ex);
69 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
70 Mono<String> response = client.put("/policies/" + policyId, policyString);
71 return response.flatMap(this::createPolicyMono);
74 public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
75 logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
76 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
77 return client.delete("/policies/" + policyId);
80 private Flux<String> createPolicyTypesFlux(String policyTypesString) {
82 List<String> policyTypesList = new ArrayList<>();
83 JSONArray policyTypesArray = new JSONArray(policyTypesString);
84 for (int i = 0; i < policyTypesArray.length(); i++) {
85 policyTypesList.add(policyTypesArray.getJSONObject(i).toString());
87 logger.debug("A1 client: policyTypes = {}", policyTypesList);
88 return Flux.fromIterable(policyTypesList);
89 } catch (JSONException ex) { // invalid json
90 return Flux.error(ex);
94 private Flux<String> createPoliciesFlux(String policiesString, String policyTypeId) {
96 List<String> policiesList = new ArrayList<>();
97 JSONArray policiesArray = new JSONArray(policiesString);
98 for (int i = 0; i < policiesArray.length(); i++) {
99 JSONObject policyObject = policiesArray.getJSONObject(i);
100 if (policyObject.get("policyTypeId").equals(policyTypeId)) {
101 policiesList.add(policyObject.toString());
104 logger.debug("A1 client: policies = {}", policiesList);
105 return Flux.fromIterable(policiesList);
106 } catch (JSONException ex) { // invalid json
107 return Flux.error(ex);
111 private Mono<String> createPolicyMono(String policyString) {
113 JSONObject policyObject = new JSONObject(policyString);
114 String policy = policyObject.toString();
115 logger.debug("A1 client: policy = {}", policy);
116 return Mono.just(policy);
117 } catch (JSONException ex) { // invalid json
118 return Mono.error(ex);