Merge "A1 controler access can be both OSC and ORAN versions of A1"
[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
34     public static class UriBuilder implements A1UriBuilder {
35
36         private final RicConfig ricConfig;
37
38         public UriBuilder(RicConfig ricConfig) {
39             this.ricConfig = ricConfig;
40         }
41
42         @Override
43         public String createPutPolicyUri(String type, String policyId) {
44             return policiesBaseUri() + policyId;
45         }
46
47         public String createGetPolicyIdsUri() {
48             return baseUri() + "/policies";
49         }
50
51         @Override
52         public String createDeleteUri(String type, String policyId) {
53             return policiesBaseUri() + policyId;
54         }
55
56         public String createGetPolicyStatusUri(String type, String policyId) {
57             return policiesBaseUri() + policyId + "/status";
58         }
59
60         private String baseUri() {
61             return ricConfig.baseUrl() + "/A1-P/v1";
62         }
63
64         private String policiesBaseUri() {
65             return createGetPolicyIdsUri() + "/";
66         }
67     }
68
69     private final AsyncRestClient restClient;
70     private final UriBuilder uri;
71
72     public StdA1ClientVersion1(RicConfig ricConfig) {
73         this(new AsyncRestClient(""), ricConfig);
74     }
75
76     public StdA1ClientVersion1(AsyncRestClient restClient, RicConfig ricConfig) {
77         this.restClient = restClient;
78         this.uri = new UriBuilder(ricConfig);
79     }
80
81     @Override
82     public Mono<List<String>> getPolicyIdentities() {
83         return getPolicyIds() //
84             .collectList();
85     }
86
87     @Override
88     public Mono<String> putPolicy(Policy policy) {
89         return restClient.put(uri.createPutPolicyUri(policy.type().name(), policy.id()), policy.json()) //
90             .flatMap(JsonHelper::validateJson);
91     }
92
93     @Override
94     public Mono<List<String>> getPolicyTypeIdentities() {
95         return Mono.just(Arrays.asList(""));
96     }
97
98     @Override
99     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
100         return Mono.just("{}");
101     }
102
103     @Override
104     public Mono<String> deletePolicy(Policy policy) {
105         return deletePolicyById(policy.id());
106     }
107
108     @Override
109     public Flux<String> deleteAllPolicies() {
110         return getPolicyIds() //
111             .flatMap(this::deletePolicyById); //
112     }
113
114     @Override
115     public Mono<A1ProtocolType> getProtocolVersion() {
116         return getPolicyIdentities() //
117             .flatMap(x -> Mono.just(A1ProtocolType.STD_V1_1));
118     }
119
120     @Override
121     public Mono<String> getPolicyStatus(Policy policy) {
122         return restClient.get(uri.createGetPolicyStatusUri(policy.type().name(), policy.id()));
123     }
124
125     private Flux<String> getPolicyIds() {
126         return restClient.get(uri.createGetPolicyIdsUri()) //
127             .flatMapMany(JsonHelper::parseJsonArrayOfString);
128     }
129
130     private Mono<String> deletePolicyById(String policyId) {
131         return restClient.delete(uri.createDeleteUri("", policyId));
132     }
133 }