a388267e344dbe362e4446591262c09bfbdbc016
[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) 2020 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.List;
25
26 import org.json.JSONObject;
27 import org.oransc.policyagent.configuration.RicConfig;
28 import org.oransc.policyagent.configuration.WebClientConfig;
29 import org.oransc.policyagent.repository.Policy;
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 /**
37  * Client for accessing OSC A1 REST API
38  */
39 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
40 public class OscA1Client implements A1Client {
41     static final int CONCURRENCY_RIC = 1; // How may paralell requests that is sent to one NearRT RIC
42
43     public static class UriBuilder implements A1UriBuilder {
44         private final RicConfig ricConfig;
45
46         public UriBuilder(RicConfig ricConfig) {
47             this.ricConfig = ricConfig;
48         }
49
50         @Override
51         public String createPutPolicyUri(String type, String policyId) {
52             return createPolicyUri(type, policyId);
53         }
54
55         /**
56          * /a1-p/policytypes/{policy_type_id}/policies
57          */
58         public String createGetPolicyIdsUri(String type) {
59             return createPolicyTypeUri(type) + "/policies";
60         }
61
62         @Override
63         public String createDeleteUri(String type, String policyId) {
64             return createPolicyUri(type, policyId);
65         }
66
67         /**
68          * ​/a1-p​/policytypes​/{policy_type_id}​/policies​/{policy_instance_id}​/status
69          */
70         @Override
71         public String createGetPolicyStatusUri(String type, String policyId) {
72             return createPolicyUri(type, policyId) + "/status";
73         }
74
75         /**
76          * ​/a1-p​/healthcheck
77          */
78         public String createHealtcheckUri() {
79             return baseUri() + "/healthcheck";
80         }
81
82         /**
83          * /a1-p/policytypes/{policy_type_id}
84          */
85         public String createGetSchemaUri(String type) {
86             return this.createPolicyTypeUri(type);
87         }
88
89         /**
90          * ​/a1-p​/policytypes​/{policy_type_id}
91          */
92         public String createPolicyTypesUri() {
93             return baseUri() + "/policytypes";
94         }
95
96         /**
97          * ​/a1-p​/policytypes​/{policy_type_id}​/policies​/{policy_instance_id}
98          */
99         private String createPolicyUri(String type, String id) {
100             return createPolicyTypeUri(type) + "/policies/" + id;
101         }
102
103         /**
104          * /a1-p/policytypes/{policy_type_id}
105          */
106         private String createPolicyTypeUri(String type) {
107             return createPolicyTypesUri() + "/" + type;
108         }
109
110         private String baseUri() {
111             return ricConfig.baseUrl() + "/a1-p";
112         }
113     }
114
115     private static final String TITLE = "title";
116     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
117     private final AsyncRestClient restClient;
118     private final UriBuilder uri;
119
120     public OscA1Client(RicConfig ricConfig, WebClientConfig clientConfig) {
121         this(ricConfig, new AsyncRestClient("", clientConfig));
122     }
123
124     public OscA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
125         this.restClient = restClient;
126         logger.debug("OscA1Client for ric: {}", ricConfig.name());
127
128         uri = new UriBuilder(ricConfig);
129     }
130
131     public static Mono<String> extractCreateSchema(String policyTypeResponse, String policyTypeId) {
132         try {
133             JSONObject obj = new JSONObject(policyTypeResponse);
134             JSONObject schemaObj = obj.getJSONObject("create_schema");
135             schemaObj.put(TITLE, policyTypeId);
136             return Mono.just(schemaObj.toString());
137         } catch (Exception e) {
138             String exceptionString = e.toString();
139             logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
140             return Mono.error(e);
141         }
142     }
143
144     @Override
145     public Mono<List<String>> getPolicyTypeIdentities() {
146         return getPolicyTypeIds() //
147             .collectList();
148     }
149
150     @Override
151     public Mono<List<String>> getPolicyIdentities() {
152         return getPolicyTypeIds() //
153             .flatMap(this::getPolicyIdentitiesByType) //
154             .collectList();
155     }
156
157     @Override
158     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
159         String schemaUri = uri.createGetSchemaUri(policyTypeId);
160         return restClient.get(schemaUri) //
161             .flatMap(response -> extractCreateSchema(response, policyTypeId));
162     }
163
164     @Override
165     public Mono<String> putPolicy(Policy policy) {
166         String policyUri = this.uri.createPutPolicyUri(policy.type().name(), policy.id());
167         return restClient.put(policyUri, policy.json());
168     }
169
170     @Override
171     public Mono<String> deletePolicy(Policy policy) {
172         return deletePolicyById(policy.type().name(), policy.id());
173     }
174
175     @Override
176     public Mono<A1ProtocolType> getProtocolVersion() {
177         return restClient.get(uri.createHealtcheckUri()) //
178             .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
179     }
180
181     @Override
182     public Flux<String> deleteAllPolicies() {
183         return getPolicyTypeIds() //
184             .flatMap(this::deletePoliciesForType, CONCURRENCY_RIC);
185     }
186
187     @Override
188     public Mono<String> getPolicyStatus(Policy policy) {
189         String statusUri = uri.createGetPolicyStatusUri(policy.type().name(), policy.id());
190         return restClient.get(statusUri);
191
192     }
193
194     private Flux<String> getPolicyTypeIds() {
195         return restClient.get(uri.createPolicyTypesUri()) //
196             .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
197     }
198
199     private Flux<String> getPolicyIdentitiesByType(String typeId) {
200         return restClient.get(uri.createGetPolicyIdsUri(typeId)) //
201             .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
202     }
203
204     private Mono<String> deletePolicyById(String typeId, String policyId) {
205         String policyUri = uri.createDeleteUri(typeId, policyId);
206         return restClient.delete(policyUri);
207     }
208
209     private Flux<String> deletePoliciesForType(String typeId) {
210         return getPolicyIdentitiesByType(typeId) //
211             .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);
212     }
213 }