c475b22f3bfd8a0fe044a16b7559fd1f4e1b6ab5
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 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
26 import org.json.JSONObject;
27 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
28 import org.onap.ccsdk.oran.a1policymanagementservice.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 ORAN A1-P Vesion 2.0 REST API
37  */
38 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
39 public class StdA1ClientVersion2 implements A1Client {
40     static final int CONCURRENCY_RIC = 1; // How many 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/v2​/policytypes/{policy_type_id}/policies
56          */
57         @Override
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/v2​/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/v2/policytypes/{policy_type_id}
77          */
78         @Override
79         public String createGetSchemaUri(String type) {
80             return this.createPolicyTypeUri(type);
81         }
82
83         /**
84          * ​/A1-P/v2​/policytypes​/{policy_type_id}
85          */
86         @Override
87         public String createPolicyTypesUri() {
88             return baseUri() + "/policytypes";
89         }
90
91         /**
92          * ​/A1-P/v2​/policytypes​/{policy_type_id}​/policies​/{policy_instance_id}
93          */
94         private String createPolicyUri(String type, String id) {
95             return createPolicyTypeUri(type) + "/policies/" + id;
96         }
97
98         /**
99          * /A1-P/v2/policytypes/{policy_type_id}
100          */
101         private String createPolicyTypeUri(String type) {
102             return createPolicyTypesUri() + "/" + type;
103         }
104
105         private String baseUri() {
106             return ricConfig.baseUrl() + "/A1-P/v2";
107         }
108     }
109
110     private static final String TITLE = "title";
111     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
112     private final AsyncRestClient restClient;
113     private final UriBuilder uriBuiler;
114
115     public StdA1ClientVersion2(RicConfig ricConfig, AsyncRestClientFactory restClientFactory) {
116         this(ricConfig, restClientFactory.createRestClient(""));
117     }
118
119     public StdA1ClientVersion2(RicConfig ricConfig, AsyncRestClient restClient) {
120         this.restClient = restClient;
121         logger.debug("OscA1Client for ric: {}", ricConfig.ricId());
122
123         uriBuiler = new UriBuilder(ricConfig);
124     }
125
126     public static Mono<String> extractPolicySchema(String policyTypeResponse, String policyTypeId) {
127         try {
128             JSONObject obj = new JSONObject(policyTypeResponse);
129             JSONObject schemaObj = obj.getJSONObject("policySchema");
130             schemaObj.put(TITLE, policyTypeId);
131             return Mono.just(schemaObj.toString());
132         } catch (Exception e) {
133             String exceptionString = e.toString();
134             logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
135             return Mono.error(e);
136         }
137     }
138
139     @Override
140     public Mono<List<String>> getPolicyTypeIdentities() {
141         return getPolicyTypeIds() //
142                 .collectList();
143     }
144
145     @Override
146     public Mono<List<String>> getPolicyIdentities() {
147         return getPolicyTypeIds() //
148                 .flatMap(this::getPolicyIdentitiesByType) //
149                 .collectList();
150     }
151
152     @Override
153     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
154         String schemaUri = uriBuiler.createGetSchemaUri(policyTypeId);
155         return restClient.get(schemaUri) //
156                 .flatMap(response -> extractPolicySchema(response, policyTypeId));
157     }
158
159     @Override
160     public Mono<String> putPolicy(Policy policy) {
161         String policyUri = this.uriBuiler.createPutPolicyUri(policy.type().id(), policy.id());
162         return restClient.put(policyUri, policy.json());
163     }
164
165     @Override
166     public Mono<String> deletePolicy(Policy policy) {
167         return deletePolicyById(policy.type().id(), policy.id());
168     }
169
170     @Override
171     public Mono<A1ProtocolType> getProtocolVersion() {
172         return restClient.get(uriBuiler.createPolicyTypesUri()) //
173                 .flatMap(notUsed -> Mono.just(A1ProtocolType.STD_V2_0_0));
174     }
175
176     @Override
177     public Flux<String> deleteAllPolicies() {
178         return getPolicyTypeIds() //
179                 .flatMap(this::deletePoliciesForType, CONCURRENCY_RIC);
180     }
181
182     @Override
183     public Mono<String> getPolicyStatus(Policy policy) {
184         String statusUri = uriBuiler.createGetPolicyStatusUri(policy.type().id(), policy.id());
185         return restClient.get(statusUri);
186
187     }
188
189     private Flux<String> getPolicyTypeIds() {
190         return restClient.get(uriBuiler.createPolicyTypesUri()) //
191                 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
192     }
193
194     private Flux<String> getPolicyIdentitiesByType(String typeId) {
195         return restClient.get(uriBuiler.createGetPolicyIdsUri(typeId)) //
196                 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
197     }
198
199     private Mono<String> deletePolicyById(String typeId, String policyId) {
200         String policyUri = uriBuiler.createDeleteUri(typeId, policyId);
201         return restClient.delete(policyUri);
202     }
203
204     private Flux<String> deletePoliciesForType(String typeId) {
205         return getPolicyIdentitiesByType(typeId) //
206                 .flatMap(policyId -> deletePolicyById(typeId, policyId), CONCURRENCY_RIC);
207     }
208 }