39fbc37b4d2b2c9345f521b5df172b80d392c12b
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / A1Client.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 package org.oransc.policyagent.clients;
21
22 import java.lang.invoke.MethodHandles;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.json.JSONArray;
27 import org.json.JSONObject;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import reactor.core.publisher.Flux;
32 import reactor.core.publisher.Mono;
33
34 public class A1Client {
35     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
36
37     public String getBaseUrl(final String nearRtRicUrl) {
38         return nearRtRicUrl + "/A1-P/v1";
39     }
40
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);
46     }
47
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));
53     }
54
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);
60     }
61
62     public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
63         logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
64             policyString);
65         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
66         Mono<String> response = client.put("/policies/" + policyId, policyString);
67         return response.flatMap(this::createPolicyMono);
68     }
69
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);
74     }
75
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());
81         }
82         logger.debug("A1 client: policyTypes = {}", policyTypesList);
83         return Flux.fromIterable(policyTypesList);
84     }
85
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());
93             }
94         }
95         logger.debug("A1 client: policies = {}", policiesList);
96         return Flux.fromIterable(policiesList);
97     }
98
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);
104     }
105 }