Add Error Handling in A1 Client
[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 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;
32
33 public class A1Client {
34     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
35
36     public String getBaseUrl(final String nearRtRicUrl) {
37         return nearRtRicUrl + "/A1-P/v1";
38     }
39
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);
45     }
46
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));
52     }
53
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);
59     }
60
61     public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
62         logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
63             policyString);
64         try {
65             new JSONObject(policyString);
66         } catch (JSONException ex) { // invalid json
67             return Mono.error(ex);
68         }
69         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
70         Mono<String> response = client.put("/policies/" + policyId, policyString);
71         return response.flatMap(this::createPolicyMono);
72     }
73
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);
78     }
79
80     private Flux<String> createPolicyTypesFlux(String policyTypesString) {
81         try {
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());
86             }
87             logger.debug("A1 client: policyTypes = {}", policyTypesList);
88             return Flux.fromIterable(policyTypesList);
89         } catch (JSONException ex) { // invalid json
90             return Flux.error(ex);
91         }
92     }
93
94     private Flux<String> createPoliciesFlux(String policiesString, String policyTypeId) {
95         try {
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());
102                 }
103             }
104             logger.debug("A1 client: policies = {}", policiesList);
105             return Flux.fromIterable(policiesList);
106         } catch (JSONException ex) { // invalid json
107             return Flux.error(ex);
108         }
109     }
110
111     private Mono<String> createPolicyMono(String policyString) {
112         try {
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);
119         }
120     }
121 }