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