Uplift from master
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / eiproducerapi / EiProducerApiImpl.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 package org.oransc.portal.nonrtric.controlpanel.eiproducerapi;
21
22 import com.google.gson.GsonBuilder;
23 import com.google.gson.JsonSyntaxException;
24
25 import java.lang.invoke.MethodHandles;
26 import java.util.List;
27
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.oransc.portal.nonrtric.controlpanel.model.JobInfo;
31 import org.oransc.portal.nonrtric.controlpanel.model.ProducerRegistrationInfo;
32 import org.oransc.portal.nonrtric.controlpanel.model.ProducerStatusInfo;
33 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
34 import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler;
35 import org.oransc.portal.nonrtric.controlpanel.util.JsonArrayHandler;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Component;
41
42 @Component("EiProducerApi")
43 public class EiProducerApiImpl implements EiProducerApi {
44     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
45
46     private static final String EI_PRODUCERS = "/eiproducers";
47     private static final String EI_JOBS = "/eijobs";
48     private static final String STATUS = "/status";
49
50     private final AsyncRestClient webClient;
51     private static com.google.gson.Gson gson = new GsonBuilder().create();
52
53     @Autowired
54     public EiProducerApiImpl(
55         @org.springframework.beans.factory.annotation.Value("${enrichmentcontroller.url.prefix}") final String urlPrefix) {
56         this(new AsyncRestClient(urlPrefix));
57         logger.debug("enrichment controller url prefix '{}'", urlPrefix);
58     }
59
60     public EiProducerApiImpl(AsyncRestClient webClient) {
61         this.webClient = webClient;
62     }
63
64     @Override
65     public ResponseEntity<String> getAllEiProducerIds() {
66         return getResponseArray(EI_PRODUCERS);
67     }
68
69     @Override
70     public ResponseEntity<ProducerRegistrationInfo> getEiProducer(String eiProducerId) throws JsonSyntaxException {
71         ResponseEntity<String> resp = getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
72         ProducerRegistrationInfo info = gson.fromJson(resp.getBody(), ProducerRegistrationInfo.class);
73         return new ResponseEntity<>(info, resp.getStatusCode());
74     }
75
76     @Override
77     public ResponseEntity<List<JobInfo>> getEiJobsForOneEiProducer(String eiProducerId)
78         throws JsonSyntaxException, IllegalStateException {
79         ResponseEntity<String> resp = getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
80         List<JobInfo> jobs = JsonArrayHandler.parseJsonArray(resp.getBody(), JobInfo.class);
81         return new ResponseEntity<>(jobs, resp.getStatusCode());
82     }
83
84     @Override
85     public ResponseEntity<ProducerStatusInfo> getEiProducerStatus(String eiProducerId) throws JsonSyntaxException {
86         ResponseEntity<String> resp = getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
87         ProducerStatusInfo status = gson.fromJson(resp.getBody(), ProducerStatusInfo.class);
88         return new ResponseEntity<>(status, resp.getStatusCode());
89     }
90
91     private ResponseEntity<String> getResponseArray(String url) {
92         try {
93             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
94             if (!rsp.getStatusCode().is2xxSuccessful()) {
95                 return rsp;
96             }
97             return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
98         } catch (Exception e) {
99             return ErrorResponseHandler.handleException(e);
100         }
101     }
102
103     private ResponseEntity<String> getResponseObject(String url) {
104         try {
105             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
106             if (!rsp.getStatusCode().is2xxSuccessful()) {
107                 return rsp;
108             }
109             return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
110         } catch (Exception e) {
111             return ErrorResponseHandler.handleException(e);
112         }
113     }
114 }