5a16bc270cf746819ef0f3bd8cf86fbe8b599ce9
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / A1ClientImpl.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
21 package org.oransc.policyagent.clients;
22
23 import java.lang.invoke.MethodHandles;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import reactor.core.publisher.Flux;
34 import reactor.core.publisher.Mono;
35
36 public class A1ClientImpl implements A1Client {
37     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38
39     private static String getBaseUrl(final String nearRtRicUrl) {
40         return nearRtRicUrl + "/A1-P/v1";
41     }
42
43     @Override
44     public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
45         logger.debug("getAllPolicyTypes nearRtRicUrl = {}", nearRtRicUrl);
46         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
47         Mono<String> response = client.get("/policytypes");
48         return response.flatMapMany(this::createPolicyTypesFlux);
49     }
50
51     @Override
52     public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
53         logger.debug("getPoliciesForType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
54         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
55         return client.get("/policies") //
56             .flatMapMany(policiesString -> createPoliciesFlux(policiesString, policyTypeId));
57     }
58
59     @Override
60     public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
61         logger.debug("getPolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
62         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
63         Mono<String> response = client.get("/policies/" + policyId);
64         return response.flatMap(this::createPolicyMono);
65     }
66
67     @Override
68     public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
69         logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
70             policyString);
71         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
72         Mono<String> response = client.put("/policies/" + policyId, policyString);
73         return response.flatMap(this::createPolicyMono);
74     }
75
76     @Override
77     public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
78         logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
79         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
80         return client.delete("/policies/" + policyId);
81     }
82
83     private Flux<String> createPolicyTypesFlux(String policyTypesString) {
84         try {
85             List<String> policyTypesList = new ArrayList<>();
86             JSONArray policyTypesArray = new JSONArray(policyTypesString);
87             for (int i = 0; i < policyTypesArray.length(); i++) {
88                 policyTypesList.add(policyTypesArray.getJSONObject(i).toString());
89             }
90             logger.debug("A1 client: policyTypes = {}", policyTypesList);
91             return Flux.fromIterable(policyTypesList);
92         } catch (JSONException ex) { // invalid json
93             return Flux.error(ex);
94         }
95     }
96
97     private Flux<String> createPoliciesFlux(String policiesString, String policyTypeId) {
98         try {
99             List<String> policiesList = new ArrayList<>();
100             JSONArray policiesArray = new JSONArray(policiesString);
101             for (int i = 0; i < policiesArray.length(); i++) {
102                 JSONObject policyObject = policiesArray.getJSONObject(i);
103                 if (policyObject.get("policyTypeId").equals(policyTypeId)) {
104                     policiesList.add(policyObject.toString());
105                 }
106             }
107             logger.debug("A1 client: policies = {}", policiesList);
108             return Flux.fromIterable(policiesList);
109         } catch (JSONException ex) { // invalid json
110             return Flux.error(ex);
111         }
112     }
113
114     private Mono<String> createPolicyMono(String policyString) {
115         try {
116             JSONObject policyObject = new JSONObject(policyString);
117             String policy = policyObject.toString();
118             logger.debug("A1 client: policy = {}", policy);
119             return Mono.just(policy);
120         } catch (JSONException ex) { // invalid json
121             return Mono.error(ex);
122
123         }
124     }
125 }