Add tests for RefreshConfigTask
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / StdA1Client.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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.util.List;
24
25 import org.oransc.policyagent.configuration.RicConfig;
26 import org.oransc.policyagent.repository.Policy;
27 import org.springframework.web.util.UriComponentsBuilder;
28
29 import reactor.core.publisher.Flux;
30 import reactor.core.publisher.Mono;
31
32 public class StdA1Client implements A1Client {
33     private static final String URL_PREFIX = "/A1-P/v1";
34
35     private static final String POLICY_TYPES_URI = "/policytypes";
36     private static final String POLICY_TYPE_ID = "policyTypeId";
37
38     private static final String POLICIES_URI = "/policies";
39
40     private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
41         UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
42
43     private static final UriComponentsBuilder POLICY_URI =
44         UriComponentsBuilder.fromPath("/policies/{policy-id}").queryParam(POLICY_TYPE_ID, "{policy-type-name}");
45
46     private static final UriComponentsBuilder POLICY_DELETE_URI =
47         UriComponentsBuilder.fromPath("/policies/{policy-id}");
48
49     private static final UriComponentsBuilder POLICY_STATUS_URI =
50         UriComponentsBuilder.fromPath("/policies/{policy-id}/status");
51
52     private final AsyncRestClient restClient;
53
54     public StdA1Client(RicConfig ricConfig) {
55         String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
56         this.restClient = new AsyncRestClient(baseUrl);
57     }
58
59     public StdA1Client(AsyncRestClient restClient) {
60         this.restClient = restClient;
61     }
62
63     @Override
64     public Mono<List<String>> getPolicyIdentities() {
65         return getPolicyIds() //
66             .collectList();
67     }
68
69     @Override
70     public Mono<String> putPolicy(Policy policy) {
71         String uri = POLICY_URI.buildAndExpand(policy.id(), policy.type().name()).toUriString();
72         return restClient.put(uri, policy.json()) //
73             .flatMap(JsonHelper::validateJson);
74     }
75
76     @Override
77     public Mono<List<String>> getPolicyTypeIdentities() {
78         return restClient.get(POLICY_TYPES_URI) //
79             .flatMapMany(JsonHelper::parseJsonArrayOfString) //
80             .collectList();
81     }
82
83     @Override
84     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
85         String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
86         return restClient.get(uri) //
87             .flatMap(JsonHelper::extractPolicySchema);
88     }
89
90     @Override
91     public Mono<String> deletePolicy(Policy policy) {
92         return deletePolicyById(policy.id());
93     }
94
95     @Override
96     public Flux<String> deleteAllPolicies() {
97         return getPolicyIds() //
98             .flatMap(this::deletePolicyById); //
99     }
100
101     @Override
102     public Mono<A1ProtocolType> getProtocolVersion() {
103         return getPolicyTypeIdentities() //
104             .flatMap(x -> Mono.just(A1ProtocolType.STD_V1));
105     }
106
107     @Override
108     public Mono<String> getPolicyStatus(Policy policy) {
109         String uri = POLICY_STATUS_URI.buildAndExpand(policy.id()).toUriString();
110         return restClient.get(uri);
111     }
112
113     private Flux<String> getPolicyIds() {
114         return restClient.get(POLICIES_URI) //
115             .flatMapMany(JsonHelper::parseJsonArrayOfString);
116     }
117
118     private Mono<String> deletePolicyById(String policyId) {
119         String uri = POLICY_DELETE_URI.buildAndExpand(policyId).toUriString();
120         return restClient.delete(uri);
121     }
122 }