3216a48d570c0d07a55285cdc0758ad177d11196
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / OscA1Client.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 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.List;
25 import org.json.JSONObject;
26 import org.oransc.policyagent.configuration.RicConfig;
27 import org.oransc.policyagent.repository.Policy;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.web.util.UriComponentsBuilder;
31 import reactor.core.publisher.Flux;
32 import reactor.core.publisher.Mono;
33
34 public class OscA1Client implements A1Client {
35     private static final String URL_PREFIX = "/a1-p";
36
37     private static final String POLICY_TYPES = "/policytypes";
38     private static final String CREATE_SCHEMA = "create_schema";
39     private static final String TITLE = "title";
40
41     private static final String HEALTHCHECK = "/healthcheck";
42
43     private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
44         UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
45
46     private static final UriComponentsBuilder POLICY_URI =
47         UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}");
48
49     private static final UriComponentsBuilder POLICY_IDS_URI =
50         UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies");
51
52     private static final UriComponentsBuilder POLICY_STATUS_URI =
53         UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}/policies/{policy-id}/status");
54
55     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56
57     private final AsyncRestClient restClient;
58
59     public OscA1Client(RicConfig ricConfig) {
60         String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
61         this.restClient = new AsyncRestClient(baseUrl);
62         if (logger.isDebugEnabled()) {
63             logger.debug("OscA1Client for ric: {}", ricConfig.name());
64         }
65     }
66
67     public OscA1Client(AsyncRestClient restClient) {
68         this.restClient = restClient;
69     }
70
71     @Override
72     public Mono<List<String>> getPolicyTypeIdentities() {
73         return getPolicyTypeIds() //
74             .collectList();
75     }
76
77     @Override
78     public Mono<List<String>> getPolicyIdentities() {
79         return getPolicyTypeIds() //
80             .flatMap(this::getPolicyIdentitiesByType) //
81             .collectList();
82     }
83
84     @Override
85     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
86         String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
87         return restClient.get(uri) //
88             .flatMap(response -> getCreateSchema(response, policyTypeId));
89     }
90
91     @Override
92     public Mono<String> putPolicy(Policy policy) {
93         String uri = POLICY_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
94         return restClient.put(uri, policy.json());
95     }
96
97     @Override
98     public Mono<String> deletePolicy(Policy policy) {
99         return deletePolicyById(policy.type().name(), policy.id());
100     }
101
102     @Override
103     public Mono<A1ProtocolType> getProtocolVersion() {
104         return restClient.get(HEALTHCHECK) //
105             .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
106     }
107
108     @Override
109     public Flux<String> deleteAllPolicies() {
110         return getPolicyTypeIds() //
111             .flatMap(this::deletePoliciesForType);
112     }
113
114     @Override
115     public Mono<String> getPolicyStatus(Policy policy) {
116         String uri = POLICY_STATUS_URI.buildAndExpand(policy.type().name(), policy.id()).toUriString();
117         return restClient.get(uri);
118
119     }
120
121     private Flux<String> getPolicyTypeIds() {
122         return restClient.get(POLICY_TYPES) //
123             .flatMapMany(JsonHelper::parseJsonArrayOfString);
124     }
125
126     private Flux<String> getPolicyIdentitiesByType(String typeId) {
127         return restClient.get(POLICY_IDS_URI.buildAndExpand(typeId).toUriString()) //
128             .flatMapMany(JsonHelper::parseJsonArrayOfString);
129     }
130
131     private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
132         try {
133             JSONObject obj = new JSONObject(policyTypeResponse);
134             JSONObject schemaObj = obj.getJSONObject(CREATE_SCHEMA);
135             schemaObj.put(TITLE, policyTypeId);
136             return Mono.just(schemaObj.toString());
137         } catch (Exception e) {
138             logger.error("Unexcpected response for policy type: {}", policyTypeResponse, e);
139             return Mono.error(e);
140         }
141     }
142
143     private Mono<String> deletePolicyById(String typeId, String policyId) {
144         String uri = POLICY_URI.buildAndExpand(typeId, policyId).toUriString();
145         return restClient.delete(uri);
146     }
147
148     private Flux<String> deletePoliciesForType(String typeId) {
149         return getPolicyIdentitiesByType(typeId) //
150             .flatMap(policyId -> deletePolicyById(typeId, policyId));
151     }
152 }