Generalization of controller
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / StdA1ClientVersion1.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.Arrays;
24 import java.util.List;
25
26 import org.oransc.policyagent.configuration.RicConfig;
27 import org.oransc.policyagent.repository.Policy;
28
29 import reactor.core.publisher.Flux;
30 import reactor.core.publisher.Mono;
31
32 public class StdA1ClientVersion1 implements A1Client {
33     private final AsyncRestClient restClient;
34
35     private final A1UriBuilder uri;
36
37     public StdA1ClientVersion1(RicConfig ricConfig) {
38         this(new AsyncRestClient(""), ricConfig);
39     }
40
41     public StdA1ClientVersion1(AsyncRestClient restClient, RicConfig ricConfig) {
42         this.restClient = restClient;
43         this.uri = new StdA1UriBuilderVersion1(ricConfig);
44     }
45
46     @Override
47     public Mono<List<String>> getPolicyIdentities() {
48         return getPolicyIds() //
49             .collectList();
50     }
51
52     @Override
53     public Mono<String> putPolicy(Policy policy) {
54
55         return restClient.put(uri.createPutPolicyUri(policy), policy.json()) //
56             .flatMap(JsonHelper::validateJson);
57     }
58
59     @Override
60     public Mono<List<String>> getPolicyTypeIdentities() {
61         return Mono.just(Arrays.asList(""));
62     }
63
64     @Override
65     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
66         return Mono.just("{}");
67     }
68
69     @Override
70     public Mono<String> deletePolicy(Policy policy) {
71         return deletePolicyById(policy.id());
72     }
73
74     @Override
75     public Flux<String> deleteAllPolicies() {
76         return getPolicyIds() //
77             .flatMap(this::deletePolicyById); //
78     }
79
80     @Override
81     public Mono<A1ProtocolType> getProtocolVersion() {
82         return getPolicyIdentities() //
83             .flatMap(x -> Mono.just(A1ProtocolType.STD_V1_1));
84     }
85
86     @Override
87     public Mono<String> getPolicyStatus(Policy policy) {
88         return restClient.get(uri.createGetPolicyStatusUri(policy.id()));
89     }
90
91     private Flux<String> getPolicyIds() {
92         return restClient.get(uri.createGetPolicyIdsUri()) //
93             .flatMapMany(JsonHelper::parseJsonArrayOfString);
94     }
95
96     private Mono<String> deletePolicyById(String policyId) {
97         return restClient.delete(uri.createDeleteUri(policyId));
98     }
99 }