Infrastructure for having mutiple RIC APIs
[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.Collection;
26 import java.util.List;
27
28 import org.json.JSONArray;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.oransc.policyagent.configuration.RicConfig;
32 import org.oransc.policyagent.repository.Policy;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import reactor.core.publisher.Mono;
36
37 public class StdA1Client implements A1Client {
38     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
39
40     private final RicConfig ricConfig;
41     private final AsyncRestClient restClient;
42
43     public StdA1Client(RicConfig ricConfig) {
44         this.ricConfig = ricConfig;
45         this.restClient = new AsyncRestClient(getBaseUrl());
46     }
47
48     public StdA1Client(RicConfig ricConfig, AsyncRestClient restClient) {
49         this.ricConfig = ricConfig;
50         this.restClient = restClient;
51     }
52
53     @Override
54     public Mono<Collection<String>> getPolicyTypeIdentities() {
55         logger.debug("getPolicyTypeIdentities nearRtRicUrl = {}", ricConfig.baseUrl());
56         return restClient.get("/policytypes/identities") //
57             .flatMap(this::parseJsonArrayOfString);
58     }
59
60     @Override
61     public Mono<Collection<String>> getPolicyIdentities() {
62         logger.debug("getPolicyIdentities nearRtRicUrl = {}", ricConfig.baseUrl());
63         return restClient.get("/policies/identities") //
64             .flatMap(this::parseJsonArrayOfString);
65     }
66
67     @Override
68     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
69         logger.debug("getPolicyType nearRtRicUrl = {}, policyTypeId = {}", ricConfig.baseUrl(), policyTypeId);
70         Mono<String> response = restClient.get("/policytypes/" + policyTypeId);
71         return response.flatMap(this::createMono);
72     }
73
74     @Override
75     public Mono<String> putPolicy(Policy policy) {
76         logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", //
77             policy.ric().getConfig().baseUrl(), policy.id(), policy.json());
78         // TODO update when simulator is updated to include policy type
79         // Mono<String> response = client.put("/policies/" + policy.id() + "?policyTypeId=" + policy.type().name(),
80         // policy.json());
81         Mono<String> response = restClient.put("/policies/" + policy.id(), policy.json());
82
83         return response.flatMap(this::createMono);
84     }
85
86     @Override
87     public Mono<String> deletePolicy(String policyId) {
88         logger.debug("deletePolicy nearRtRicUrl = {}, policyId = {}", ricConfig.baseUrl(), policyId);
89         return restClient.delete("/policies/" + policyId);
90     }
91
92     @Override
93     public Mono<A1ProtocolType> getProtocolVersion() {
94         return getPolicyTypeIdentities() //
95             .flatMap(x -> Mono.just(A1ProtocolType.STD_V1));
96     }
97
98     private String getBaseUrl() {
99         return ricConfig.baseUrl() + "/A1-P/v1";
100     }
101
102     private Mono<Collection<String>> parseJsonArrayOfString(String inputString) {
103         try {
104             List<String> arrayList = new ArrayList<>();
105             JSONArray jsonArray = new JSONArray(inputString);
106             for (int i = 0; i < jsonArray.length(); i++) {
107                 arrayList.add(jsonArray.getString(i));
108             }
109             logger.debug("A1 client: received list = {}", arrayList);
110             return Mono.just(arrayList);
111         } catch (JSONException ex) { // invalid json
112             return Mono.error(ex);
113         }
114     }
115
116     private Mono<String> createMono(String inputString) {
117         try {
118             JSONObject jsonObject = new JSONObject(inputString);
119             String jsonString = jsonObject.toString();
120             logger.debug("A1 client: received string = {}", jsonString);
121             return Mono.just(jsonString);
122         } catch (JSONException ex) { // invalid json
123             return Mono.error(ex);
124         }
125     }
126
127 }