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.JSONObject;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import reactor.core.publisher.Flux;
32 import reactor.core.publisher.Mono;
34 public class A1Client {
35 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
37 public String getBaseUrl(final String nearRtRicUrl) {
38 return nearRtRicUrl + "/A1-P/v1";
41 public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
42 logger.debug("getAllPolicyTypes nearRtRicUrl = {}", nearRtRicUrl);
43 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
44 Mono<String> response = client.get("/policytypes");
45 return response.flatMapMany(this::createPolicyTypesFlux);
48 public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
49 logger.debug("getPoliciesForType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
50 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
51 Mono<String> response = client.get("/policies");
52 return response.flatMapMany(policiesString -> createPoliciesFlux(policiesString, policyTypeId));
55 public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
56 logger.debug("getPolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
57 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
58 Mono<String> response = client.get("/policies/" + policyId);
59 return response.flatMap(this::createPolicyMono);
62 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
63 logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
65 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
66 Mono<String> response = client.put("/policies/" + policyId, policyString);
67 return response.flatMap(this::createPolicyMono);
70 public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
71 logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
72 AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
73 return client.delete("/policies/" + policyId);
76 private Flux<String> createPolicyTypesFlux(String policyTypesString) {
77 List<String> policyTypesList = new ArrayList<>();
78 JSONArray policyTypesArray = new JSONArray(policyTypesString);
79 for (int i = 0; i < policyTypesArray.length(); i++) {
80 policyTypesList.add(policyTypesArray.getJSONObject(i).toString());
82 logger.debug("A1 client: policyTypes = {}", policyTypesList);
83 return Flux.fromIterable(policyTypesList);
86 private Flux<String> createPoliciesFlux(String policiesString, String policyTypeId) {
87 List<String> policiesList = new ArrayList<>();
88 JSONArray policiesArray = new JSONArray(policiesString);
89 for (int i = 0; i < policiesArray.length(); i++) {
90 JSONObject policyObject = policiesArray.getJSONObject(i);
91 if (policyObject.get("policyTypeId").equals(policyTypeId)) {
92 policiesList.add(policyObject.toString());
95 logger.debug("A1 client: policies = {}", policiesList);
96 return Flux.fromIterable(policiesList);
99 private Mono<String> createPolicyMono(String policyString) {
100 // remove white-spaces
101 policyString = policyString.replaceAll("\\s+", "");
102 logger.debug("A1 client: policy = {}", policyString);
103 return Mono.just(policyString);