Add unit test for A1 client
[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     public AsyncRestClient createClient(final String nearRtRicUrl) {
44         return new AsyncRestClient(getBaseUrl(nearRtRicUrl));
45     }
46
47     @Override
48     public Flux<String> getPolicyTypeIdentities(String nearRtRicUrl) {
49         logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl);
50         AsyncRestClient client = createClient(nearRtRicUrl);
51         Mono<String> response = client.get("/policytypes/identities");
52         return response.flatMapMany(this::createFlux);
53     }
54
55     @Override
56     public Flux<String> getPolicyIdentities(String nearRtRicUrl) {
57         logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl);
58         AsyncRestClient client = createClient(nearRtRicUrl);
59         Mono<String> response = client.get("/policies/identities");
60         return response.flatMapMany(this::createFlux);
61     }
62
63     @Override
64     public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
65         logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
66         AsyncRestClient client = createClient(nearRtRicUrl);
67         Mono<String> response = client.get("/policytypes/" + policyTypeId);
68         return response.flatMap(this::createMono);
69     }
70
71     @Override
72     public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
73         logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
74             policyString);
75         AsyncRestClient client = createClient(nearRtRicUrl);
76         Mono<String> response = client.put("/policies/" + policyId, policyString);
77         return response.flatMap(this::createMono);
78     }
79
80     @Override
81     public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
82         logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
83         AsyncRestClient client = createClient(nearRtRicUrl);
84         return client.delete("/policies/" + policyId);
85     }
86
87     private Flux<String> createFlux(String inputString) {
88         try {
89             List<String> arrayList = new ArrayList<>();
90             JSONArray jsonArray = new JSONArray(inputString);
91             for (int i = 0; i < jsonArray.length(); i++) {
92                 arrayList.add(jsonArray.getString(i));
93             }
94             logger.debug("A1 client: received list = {}", arrayList);
95             return Flux.fromIterable(arrayList);
96         } catch (JSONException ex) { // invalid json
97             return Flux.error(ex);
98         }
99     }
100
101     private Mono<String> createMono(String inputString) {
102         try {
103             JSONObject jsonObject = new JSONObject(inputString);
104             String jsonString = jsonObject.toString();
105             logger.debug("A1 client: received string = {}", jsonString);
106             return Mono.just(jsonString);
107         } catch (JSONException ex) { // invalid json
108             return Mono.error(ex);
109         }
110     }
111 }