3362b7907c1cef665d4000a1fd83965339ef437a
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.clients;
22
23 import java.lang.invoke.MethodHandles;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.json.JSONObject;
28 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.web.util.DefaultUriBuilderFactory;
33 import org.springframework.web.util.UriBuilderFactory;
34
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
37
38 /**
39  * Client for accessing ORAN A1-P Version 2.0 REST API
40  */
41 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
42 public class StdA1ClientVersion2 implements A1Client {
43     static final int CONCURRENCY_RIC = 1; // How many parallel requests that is sent to one NearRT RIC
44
45     public static class Factory implements A1Client.Factory {
46         @Override
47         public A1Client create(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
48             return new StdA1ClientVersion2(ricConfig, restClientFactory);
49         }
50     }
51
52     public static class OranV2UriBuilder implements A1UriBuilder {
53         private final RicConfig ricConfig;
54
55         public OranV2UriBuilder(RicConfig ricConfig) {
56             this.ricConfig = ricConfig;
57         }
58
59         @Override
60         public String createPutPolicyUri(String type, String policyId, String statusNotificationUri) {
61             String policyUri = createPolicyUri(type, policyId);
62             if (statusNotificationUri.isEmpty()) {
63                 return policyUri;
64             }
65             UriBuilderFactory builderFactory = new DefaultUriBuilderFactory(policyUri);
66             return builderFactory.builder() //
67                     .queryParam("notificationDestination", statusNotificationUri) //
68                     .build() //
69                     .normalize() //
70                     .toASCIIString();
71         }
72
73         /**
74          * /A1-P/v2/policytypes/{policy_type_id}/policies
75          */
76         @Override
77         public String createGetPolicyIdsUri(String type) {
78             return createPolicyTypeUri(type) + "/policies";
79         }
80
81         @Override
82         public String createDeleteUri(String type, String policyId) {
83             return createPolicyUri(type, policyId);
84         }
85
86         /**
87          * /A1-P/v2/policytypes/{policy_type_id}/policies/{policy_instance_id}/status
88          */
89         @Override
90         public String createGetPolicyStatusUri(String type, String policyId) {
91             return createPolicyUri(type, policyId) + "/status";
92         }
93
94         /**
95          * /A1-P/v2/policytypes/{policy_type_id}
96          */
97         @Override
98         public String createGetSchemaUri(String type) {
99             return this.createPolicyTypeUri(type);
100         }
101
102         /**
103          * /A1-P/v2/policytypes/{policy_type_id}
104          */
105         @Override
106         public String createPolicyTypesUri() {
107             return baseUri() + "/policytypes";
108         }
109
110         /**
111          * /A1-P/v2/policytypes/{policy_type_id}/policies/{policy_instance_id}
112          */
113         private String createPolicyUri(String type, String id) {
114             return createPolicyTypeUri(type) + "/policies/" + id;
115         }
116
117         /**
118          * /A1-P/v2/policytypes/{policy_type_id}
119          */
120         private String createPolicyTypeUri(String type) {
121             return createPolicyTypesUri() + "/" + type;
122         }
123
124         private String baseUri() {
125             return ricConfig.getBaseUrl() + "/A1-P/v2";
126         }
127     }
128
129     private static final String TITLE = "title";
130     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
131     private final AsyncRestClient restClient;
132     private final OranV2UriBuilder uriBuiler;
133
134     public StdA1ClientVersion2(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
135         this(ricConfig, restClientFactory.createRestClientUseHttpProxy(""));
136     }
137
138     public StdA1ClientVersion2(RicConfig ricConfig, AsyncRestClient restClient) {
139         this.restClient = restClient;
140         uriBuiler = new OranV2UriBuilder(ricConfig);
141         logger.debug("A1Client \"{}\"  created for ric: {}", getClass().getTypeName(), ricConfig.getRicId());
142     }
143
144     public static Mono<String> extractPolicySchema(String policyTypeResponse, String policyTypeId) {
145         try {
146             JSONObject obj = new JSONObject(policyTypeResponse);
147             JSONObject schemaObj = obj.getJSONObject("policySchema");
148             schemaObj.put(TITLE, policyTypeId);
149             return Mono.just(schemaObj.toString());
150         } catch (Exception e) {
151             String exceptionString = e.toString();
152             logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
153             return Mono.error(e);
154         }
155     }
156
157     @Override
158     public Mono<List<String>> getPolicyTypeIdentities() {
159         return getPolicyTypeIds() //
160                 .collectList();
161     }
162
163     @Override
164     public Mono<List<String>> getPolicyIdentities() {
165         return getPolicyTypeIds() //
166                 .flatMap(this::getPolicyIdentitiesByType) //
167                 .collectList();
168     }
169
170     @Override
171     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
172         String schemaUri = uriBuiler.createGetSchemaUri(policyTypeId);
173         return restClient.get(schemaUri) //
174                 .flatMap(response -> extractPolicySchema(response, policyTypeId));
175     }
176
177     @Override
178     public Mono<String> putPolicy(Policy policy) {
179         String policyUri = this.uriBuiler.createPutPolicyUri(policy.getType().getId(), policy.getId(),
180                 policy.getStatusNotificationUri());
181         return restClient.put(policyUri, policy.getJson());
182     }
183
184     @Override
185     public Mono<String> deletePolicy(Policy policy) {
186         return deletePolicyById(policy.getType().getId(), policy.getId());
187     }
188
189     @Override
190     public Mono<A1ProtocolType> getProtocolVersion() {
191         return restClient.get(uriBuiler.createPolicyTypesUri()) //
192                 .flatMap(notUsed -> Mono.just(A1ProtocolType.STD_V2_0_0));
193     }
194
195     @Override
196     public Flux<String> deleteAllPolicies(Set<String> excludePolicyIds) {
197         return getPolicyTypeIds() //
198                 .flatMap(typeId -> deleteAllPoliciesForType(typeId, excludePolicyIds), CONCURRENCY_RIC);
199     }
200
201     @Override
202     public Mono<String> getPolicyStatus(Policy policy) {
203         String statusUri = uriBuiler.createGetPolicyStatusUri(policy.getType().getId(), policy.getId());
204         return restClient.get(statusUri);
205     }
206
207     private Flux<String> getPolicyTypeIds() {
208         return restClient.get(uriBuiler.createPolicyTypesUri()) //
209                 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
210     }
211
212     private Flux<String> getPolicyIdentitiesByType(String typeId) {
213         return restClient.get(uriBuiler.createGetPolicyIdsUri(typeId)) //
214                 .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
215     }
216
217     private Mono<String> deletePolicyById(String typeId, String policyId) {
218         String policyUri = uriBuiler.createDeleteUri(typeId, policyId);
219         return restClient.delete(policyUri);
220     }
221
222     private Flux<String> deleteAllPoliciesForType(String typeId, Set<String> excludePolicyIds) {
223         return getPolicyIdentitiesByType(typeId) //
224                 .filter(policyId -> !excludePolicyIds.contains(policyId)) //
225                 .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);
226     }
227 }