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