4ebc25c679adf54b55bc82939da19d86000907d6
[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.configuration.WebClientConfig;
28 import org.oransc.policyagent.repository.Policy;
29
30 import reactor.core.publisher.Flux;
31 import reactor.core.publisher.Mono;
32
33 /**
34  * Client for accessing standard A1 REST API version 1.1
35  */
36 public class StdA1ClientVersion1 implements A1Client {
37
38     public static class UriBuilder implements A1UriBuilder {
39
40         private final RicConfig ricConfig;
41
42         public UriBuilder(RicConfig ricConfig) {
43             this.ricConfig = ricConfig;
44         }
45
46         /**
47          * /A1-P/v1/policies/{policyId}
48          */
49         @Override
50         public String createPutPolicyUri(String type, String policyId) {
51             return policiesBaseUri() + policyId;
52         }
53
54         /**
55          * /A1-P/v1/policies
56          */
57         public String createGetPolicyIdsUri() {
58             return baseUri() + "/policies";
59         }
60
61         /**
62          * /A1-P/v1/policies/{policyId}
63          */
64         @Override
65         public String createDeleteUri(String type, String policyId) {
66             return policiesBaseUri() + policyId;
67         }
68
69         /**
70          * /A1-P/v1/policies/{policyId}/status
71          */
72         public String createGetPolicyStatusUri(String type, String policyId) {
73             return policiesBaseUri() + policyId + "/status";
74         }
75
76         private String baseUri() {
77             return ricConfig.baseUrl() + "/A1-P/v1";
78         }
79
80         private String policiesBaseUri() {
81             return createGetPolicyIdsUri() + "/";
82         }
83     }
84
85     private final AsyncRestClient restClient;
86     private final UriBuilder uri;
87
88     public StdA1ClientVersion1(RicConfig ricConfig, WebClientConfig webClientConfig) {
89         this(new AsyncRestClient("", webClientConfig), ricConfig);
90     }
91
92     public StdA1ClientVersion1(AsyncRestClient restClient, RicConfig ricConfig) {
93         this.restClient = restClient;
94         this.uri = new UriBuilder(ricConfig);
95     }
96
97     @Override
98     public Mono<List<String>> getPolicyIdentities() {
99         return getPolicyIds() //
100             .collectList();
101     }
102
103     @Override
104     public Mono<String> putPolicy(Policy policy) {
105         return restClient.put(uri.createPutPolicyUri(policy.type().name(), policy.id()), policy.json());
106     }
107
108     @Override
109     public Mono<List<String>> getPolicyTypeIdentities() {
110         return Mono.just(Arrays.asList(""));
111     }
112
113     @Override
114     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
115         return Mono.just("{}");
116     }
117
118     @Override
119     public Mono<String> deletePolicy(Policy policy) {
120         return deletePolicyById(policy.id());
121     }
122
123     @Override
124     public Flux<String> deleteAllPolicies() {
125         return getPolicyIds() //
126             .flatMap(this::deletePolicyById); //
127     }
128
129     @Override
130     public Mono<A1ProtocolType> getProtocolVersion() {
131         return getPolicyIdentities() //
132             .flatMap(x -> Mono.just(A1ProtocolType.STD_V1_1));
133     }
134
135     @Override
136     public Mono<String> getPolicyStatus(Policy policy) {
137         return restClient.get(uri.createGetPolicyStatusUri(policy.type().name(), policy.id()));
138     }
139
140     private Flux<String> getPolicyIds() {
141         return restClient.get(uri.createGetPolicyIdsUri()) //
142             .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
143     }
144
145     private Mono<String> deletePolicyById(String policyId) {
146         return restClient.delete(uri.createDeleteUri("", policyId));
147     }
148 }