Merge "Add unit test for StdA1Client"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / OscA1Client.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.oransc.policyagent.configuration.RicConfig;
31 import org.oransc.policyagent.repository.Policy;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
37
38 public class OscA1Client implements A1Client {
39     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40
41     private final AsyncRestClient restClient;
42
43     public OscA1Client(RicConfig ricConfig) {
44         String baseUrl = ricConfig.baseUrl() + "/a1-p";
45         this.restClient = new AsyncRestClient(baseUrl);
46         logger.debug("OscA1Client for ric: {}", ricConfig.name());
47     }
48
49     public OscA1Client(AsyncRestClient restClient) {
50         this.restClient = restClient;
51     }
52
53     @Override
54     public Mono<List<String>> getPolicyTypeIdentities() {
55         return restClient.get("/policytypes") //
56             .flatMap(this::parseJsonArrayOfString);
57     }
58
59     @Override
60     public Mono<List<String>> getPolicyIdentities() {
61         return getPolicyTypeIdentities() //
62             .flatMapMany(types -> Flux.fromIterable(types)) //
63             .flatMap(type -> getPolicyIdentities(type)) //
64             .flatMap(policyIds -> Flux.fromIterable(policyIds)) //
65             .collectList();
66     }
67
68     private Mono<List<String>> getPolicyIdentities(String typeId) {
69         return restClient.get("/policytypes/" + typeId + "/policies") //
70             .flatMap(this::parseJsonArrayOfString);
71     }
72
73     @Override
74     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
75         return restClient.get("/policytypes/" + policyTypeId) //
76             .flatMap(response -> getCreateSchema(response, policyTypeId));
77     }
78
79     private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
80         try {
81             JSONObject obj = new JSONObject(policyTypeResponse);
82             JSONObject schemaObj = obj.getJSONObject("create_schema");
83             schemaObj.put("title", policyTypeId);
84             return Mono.just(schemaObj.toString());
85         } catch (Exception e) {
86             logger.error("Unexcpected response for policy type: {}", policyTypeResponse, e);
87             return Mono.error(e);
88         }
89     }
90
91     @Override
92     public Mono<String> putPolicy(Policy policy) {
93         return restClient.put("/policytypes/" + policy.type().name() + "/policies/" + policy.id(), policy.json());
94     }
95
96     @Override
97     public Mono<String> deletePolicy(Policy policy) {
98         return deletePolicy(policy.type().name(), policy.id());
99     }
100
101     private Mono<String> deletePolicy(String typeId, String policyId) {
102         return restClient.delete("/policytypes/" + typeId + "/policies/" + policyId);
103     }
104
105     @Override
106     public Mono<A1ProtocolType> getProtocolVersion() {
107         return restClient.get("/healthcheck") //
108             .flatMap(resp -> Mono.just(A1ProtocolType.OSC_V1));
109     }
110
111     @Override
112     public Flux<String> deleteAllPolicies() {
113         return getPolicyTypeIdentities() //
114             .flatMapMany(types -> Flux.fromIterable(types)) //
115             .flatMap(typeId -> deletePoliciesForType(typeId)); //
116     }
117
118     private Flux<String> deletePoliciesForType(String typeId) {
119         return getPolicyIdentities(typeId) //
120             .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) //
121             .flatMap(policyId -> deletePolicy(typeId, policyId)); //
122     }
123
124     private Mono<List<String>> parseJsonArrayOfString(String inputString) {
125         try {
126             List<String> arrayList = new ArrayList<>();
127             JSONArray jsonArray = new JSONArray(inputString);
128             for (int i = 0; i < jsonArray.length(); i++) {
129                 arrayList.add(jsonArray.getString(i));
130             }
131             logger.debug("A1 client: received list = {}", arrayList);
132             return Mono.just(arrayList);
133         } catch (JSONException ex) { // invalid json
134             return Mono.error(ex);
135         }
136     }
137
138     @Override
139     public Mono<String> getPolicyStatus(Policy policy) {
140         // /a1-p/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
141         return restClient.get("/policytypes/" + policy.type().name() + "/policies/" + policy.id() + "/status");
142
143     }
144 }