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