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;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import reactor.core.publisher.Flux;
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";
43 public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
44 logger.debug("getAllPolicyTypes nearRtRicUrl = {}", nearRtRicUrl);
45 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
46 Mono<String> response = client.get("/policytypes");
47 return response.flatMapMany(this::createPolicyTypesFlux);
51 public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
52 logger.debug("getPoliciesForType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
53 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
54 return client.get("/policies") //
55 .flatMapMany(policiesString -> createPoliciesFlux(policiesString, policyTypeId));
59 public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
60 logger.debug("getPolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
61 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
62 Mono<String> response = client.get("/policies/" + policyId);
63 return response.flatMap(this::createPolicyMono);
67 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
68 logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
70 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
71 Mono<String> response = client.put("/policies/" + policyId, policyString);
72 return response.flatMap(this::createPolicyMono);
76 public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
77 logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
78 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
79 return client.delete("/policies/" + policyId);
82 private Flux<String> createPolicyTypesFlux(String policyTypesString) {
84 List<String> policyTypesList = new ArrayList<>();
85 JSONArray policyTypesArray = new JSONArray(policyTypesString);
86 for (int i = 0; i < policyTypesArray.length(); i++) {
87 policyTypesList.add(policyTypesArray.getJSONObject(i).toString());
89 logger.debug("A1 client: policyTypes = {}", policyTypesList);
90 return Flux.fromIterable(policyTypesList);
91 } catch (JSONException ex) { // invalid json
92 return Flux.error(ex);
96 private Flux<String> createPoliciesFlux(String policiesString, String policyTypeId) {
98 List<String> policiesList = new ArrayList<>();
99 JSONArray policiesArray = new JSONArray(policiesString);
100 for (int i = 0; i < policiesArray.length(); i++) {
101 JSONObject policyObject = policiesArray.getJSONObject(i);
102 if (policyObject.get("policyTypeId").equals(policyTypeId)) {
103 policiesList.add(policyObject.toString());
106 logger.debug("A1 client: policies = {}", policiesList);
107 return Flux.fromIterable(policiesList);
108 } catch (JSONException ex) { // invalid json
109 return Flux.error(ex);
113 private Mono<String> createPolicyMono(String policyString) {
115 JSONObject policyObject = new JSONObject(policyString);
116 String policy = policyObject.toString();
117 logger.debug("A1 client: policy = {}", policy);
118 return Mono.just(policy);
119 } catch (JSONException ex) { // invalid json
120 return Mono.error(ex);