8f4334c86792cbb8ecb255951dd5ffb12cf3542b
[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.lang.invoke.MethodHandles;
24 import java.util.ArrayList;
25 import java.util.List;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.oransc.policyagent.configuration.RicConfig;
30 import org.oransc.policyagent.repository.Policy;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.web.util.UriComponentsBuilder;
34 import reactor.core.publisher.Flux;
35 import reactor.core.publisher.Mono;
36
37 public class StdA1Client implements A1Client {
38     private static final String URL_PREFIX = "/A1-P/v1";
39
40     private static final String POLICY_TYPES_URI = "/policytypes";
41     private static final String POLICY_TYPE_ID = "policyTypeId";
42
43     private static final String POLICIES_URI = "/policies";
44     private static final String POLICY_SCHEMA = "policySchema";
45
46     private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
47         UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
48
49     private static final UriComponentsBuilder POLICY_URI =
50         UriComponentsBuilder.fromPath("/policies/{policy-id}").queryParam(POLICY_TYPE_ID, "{policy-type-name}");
51
52     private static final UriComponentsBuilder POLICY_DELETE_URI =
53         UriComponentsBuilder.fromPath("/policies/{policy-id}");
54
55     private static final UriComponentsBuilder POLICY_STATUS_URI =
56         UriComponentsBuilder.fromPath("/policies/{policy-id}/status");
57
58     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60     private final AsyncRestClient restClient;
61
62     public StdA1Client(RicConfig ricConfig) {
63         String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
64         this.restClient = new AsyncRestClient(baseUrl);
65     }
66
67     public StdA1Client(AsyncRestClient restClient) {
68         this.restClient = restClient;
69     }
70
71     @Override
72     public Mono<List<String>> getPolicyIdentities() {
73         return getPolicyIds() //
74             .collectList();
75     }
76
77     @Override
78     public Mono<String> putPolicy(Policy policy) {
79         String uri = POLICY_URI.buildAndExpand(policy.id(), policy.type().name()).toUriString();
80         return restClient.put(uri, policy.json()) //
81             .flatMap(this::validateJson);
82     }
83
84     @Override
85     public Mono<List<String>> getPolicyTypeIdentities() {
86         return restClient.get(POLICY_TYPES_URI) //
87             .flatMapMany(this::parseJsonArrayOfString) //
88             .collectList();
89     }
90
91     @Override
92     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
93         String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
94         return restClient.get(uri) //
95             .flatMap(this::extractPolicySchema);
96     }
97
98     @Override
99     public Mono<String> deletePolicy(Policy policy) {
100         return deletePolicyById(policy.id());
101     }
102
103     @Override
104     public Flux<String> deleteAllPolicies() {
105         return getPolicyIds() //
106             .flatMap(this::deletePolicyById); //
107     }
108
109     @Override
110     public Mono<A1ProtocolType> getProtocolVersion() {
111         return getPolicyTypeIdentities() //
112             .flatMap(x -> Mono.just(A1ProtocolType.STD_V1));
113     }
114
115     @Override
116     public Mono<String> getPolicyStatus(Policy policy) {
117         String uri = POLICY_STATUS_URI.buildAndExpand(policy.id()).toUriString();
118         return restClient.get(uri);
119     }
120
121     private Flux<String> getPolicyIds() {
122         return restClient.get(POLICIES_URI) //
123             .flatMapMany(this::parseJsonArrayOfString);
124     }
125
126     private Mono<String> deletePolicyById(String policyId) {
127         String uri = POLICY_DELETE_URI.buildAndExpand(policyId).toUriString();
128         return restClient.delete(uri);
129     }
130
131     private Flux<String> parseJsonArrayOfString(String inputString) {
132         try {
133             List<String> arrayList = new ArrayList<>();
134             JSONArray jsonArray = new JSONArray(inputString);
135             for (int i = 0; i < jsonArray.length(); i++) {
136                 arrayList.add(jsonArray.getString(i));
137             }
138             logger.debug("A1 client: received list = {}", arrayList);
139             return Flux.fromIterable(arrayList);
140         } catch (JSONException ex) { // invalid json
141             return Flux.error(ex);
142         }
143     }
144
145     private Mono<String> extractPolicySchema(String inputString) {
146         try {
147             JSONObject jsonObject = new JSONObject(inputString);
148             JSONObject schemaObject = jsonObject.getJSONObject(POLICY_SCHEMA);
149             String schemaString = schemaObject.toString();
150             return Mono.just(schemaString);
151         } catch (JSONException ex) { // invalid json
152             return Mono.error(ex);
153         }
154     }
155
156     private Mono<String> validateJson(String inputString) {
157         try {
158             new JSONObject(inputString);
159             return Mono.just(inputString);
160         } catch (JSONException ex) { // invalid json
161             return Mono.error(ex);
162         }
163     }
164
165 }