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