Recovery handling
[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.Collection;
26 import java.util.List;
27
28 import org.json.JSONArray;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import reactor.core.publisher.Mono;
34
35 public class A1ClientImpl implements A1Client {
36     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
37
38     private static String getBaseUrl(final String nearRtRicUrl) {
39         return nearRtRicUrl + "/A1-P/v1";
40     }
41
42     public AsyncRestClient createClient(final String nearRtRicUrl) {
43         return new AsyncRestClient(getBaseUrl(nearRtRicUrl));
44     }
45
46     @Override
47     public Mono<Collection<String>> getPolicyTypeIdentities(String nearRtRicUrl) {
48         logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", nearRtRicUrl);
49         AsyncRestClient client = createClient(nearRtRicUrl);
50         return client.get("/policytypes/identities") //
51             .flatMap(this::parseJsonArrayOfString);
52     }
53
54     @Override
55     public Mono<Collection<String>> getPolicyIdentities(String nearRtRicUrl) {
56         logger.debug("getPolicyIdentities nearRtRicUrl = {}", nearRtRicUrl);
57         AsyncRestClient client = createClient(nearRtRicUrl);
58         return client.get("/policies/identities") //
59             .flatMap(this::parseJsonArrayOfString);
60     }
61
62     @Override
63     public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
64         logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
65         AsyncRestClient client = createClient(nearRtRicUrl);
66         Mono<String> response = client.get("/policytypes/" + policyTypeId);
67         return response.flatMap(this::createMono);
68     }
69
70     @Override
71     public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
72         logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
73             policyString);
74         AsyncRestClient client = createClient(nearRtRicUrl);
75         Mono<String> response = client.put("/policies/" + policyId, policyString);
76         return response.flatMap(this::createMono);
77     }
78
79     @Override
80     public Mono<String> deletePolicy(String nearRtRicUrl, String policyId) {
81         logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
82         AsyncRestClient client = createClient(nearRtRicUrl);
83         return client.delete("/policies/" + policyId);
84     }
85
86     private Mono<Collection<String>> parseJsonArrayOfString(String inputString) {
87         try {
88             List<String> arrayList = new ArrayList<>();
89             JSONArray jsonArray = new JSONArray(inputString);
90             for (int i = 0; i < jsonArray.length(); i++) {
91                 arrayList.add(jsonArray.getString(i));
92             }
93             logger.debug("A1 client: received list = {}", arrayList);
94             return Mono.just(arrayList);
95         } catch (JSONException ex) { // invalid json
96             return Mono.error(ex);
97         }
98     }
99
100     private Mono<String> createMono(String inputString) {
101         try {
102             JSONObject jsonObject = new JSONObject(inputString);
103             String jsonString = jsonObject.toString();
104             logger.debug("A1 client: received string = {}", jsonString);
105             return Mono.just(jsonString);
106         } catch (JSONException ex) { // invalid json
107             return Mono.error(ex);
108         }
109     }
110 }