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