Integrating A1 client with service supervision
[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 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.JSONException;
28 import org.json.JSONObject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import reactor.core.publisher.Flux;
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     @Override
43     public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
44         logger.debug("getAllPolicyTypes nearRtRicUrl = {}", nearRtRicUrl);
45         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
46         Mono<String> response = client.get("/policytypes");
47         return response.flatMapMany(this::createPolicyTypesFlux);
48     }
49
50     @Override
51     public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
52         logger.debug("getPoliciesForType nearRtRicUrl = {}, policyTypeId = {}", nearRtRicUrl, policyTypeId);
53         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
54         return client.get("/policies") //
55             .flatMapMany(policiesString -> createPoliciesFlux(policiesString, policyTypeId));
56     }
57
58     @Override
59     public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
60         logger.debug("getPolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
61         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
62         Mono<String> response = client.get("/policies/" + policyId);
63         return response.flatMap(this::createPolicyMono);
64     }
65
66     @Override
67     public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
68         logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
69             policyString);
70         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
71         Mono<String> response = client.put("/policies/" + policyId, policyString);
72         return response.flatMap(this::createPolicyMono);
73     }
74
75     @Override
76     public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
77         logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", nearRtRicUrl, policyId);
78         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
79         return client.delete("/policies/" + policyId);
80     }
81
82     private Flux<String> createPolicyTypesFlux(String policyTypesString) {
83         try {
84             List<String> policyTypesList = new ArrayList<>();
85             JSONArray policyTypesArray = new JSONArray(policyTypesString);
86             for (int i = 0; i < policyTypesArray.length(); i++) {
87                 policyTypesList.add(policyTypesArray.getJSONObject(i).toString());
88             }
89             logger.debug("A1 client: policyTypes = {}", policyTypesList);
90             return Flux.fromIterable(policyTypesList);
91         } catch (JSONException ex) { // invalid json
92             return Flux.error(ex);
93         }
94     }
95
96     private Flux<String> createPoliciesFlux(String policiesString, String policyTypeId) {
97         try {
98             List<String> policiesList = new ArrayList<>();
99             JSONArray policiesArray = new JSONArray(policiesString);
100             for (int i = 0; i < policiesArray.length(); i++) {
101                 JSONObject policyObject = policiesArray.getJSONObject(i);
102                 if (policyObject.get("policyTypeId").equals(policyTypeId)) {
103                     policiesList.add(policyObject.toString());
104                 }
105             }
106             logger.debug("A1 client: policies = {}", policiesList);
107             return Flux.fromIterable(policiesList);
108         } catch (JSONException ex) { // invalid json
109             return Flux.error(ex);
110         }
111     }
112
113     private Mono<String> createPolicyMono(String policyString) {
114         try {
115             JSONObject policyObject = new JSONObject(policyString);
116             String policy = policyObject.toString();
117             logger.debug("A1 client: policy = {}", policy);
118             return Mono.just(policy);
119         } catch (JSONException ex) { // invalid json
120             return Mono.error(ex);
121
122         }
123     }
124 }