Add getJobs and adapt getProducers
[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 java.lang.invoke.MethodHandles;
23
24 import org.json.JSONArray;
25 import org.json.JSONObject;
26 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
27 import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Component;
33
34 @Component("EiProducerApi")
35 public class EiProducerApiImpl implements EiProducerApi {
36     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
37
38     private static final String EI_TYPES = "/eitypes";
39     private static final String EI_PRODUCERS = "/eiproducers";
40     private static final String EI_JOBS = "/eijobs";
41     private static final String STATUS = "/status";
42
43     private final AsyncRestClient webClient;
44
45     @Autowired
46     public EiProducerApiImpl(
47         @org.springframework.beans.factory.annotation.Value("${enrichmentcontroller.url.prefix}") final String urlPrefix) {
48         this(new AsyncRestClient(urlPrefix));
49         logger.debug("enrichment controller url prefix '{}'", urlPrefix);
50     }
51
52     public EiProducerApiImpl(AsyncRestClient webClient) {
53         this.webClient = webClient;
54     }
55
56     @Override
57     public ResponseEntity<String> getAllEiTypeIds() {
58         return getResponseArray(EI_TYPES);
59     }
60
61     @Override
62     public ResponseEntity<String> getEiType(String eiTypeId) {
63         return getResponseObject(EI_TYPES + "/" + eiTypeId);
64     }
65
66     @Override
67     public ResponseEntity<String> getAllEiProducerIds() {
68         return getResponseArray(EI_PRODUCERS);
69     }
70
71     @Override
72     public ResponseEntity<String> getEiProducer(String eiProducerId) {
73         return getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
74     }
75
76     @Override
77     public ResponseEntity<String> getEiJobsForOneEiProducer(String eiProducerId) {
78         return getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
79     }
80
81     @Override
82     public ResponseEntity<String> getEiProducerStatus(String eiProducerId) {
83         return getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
84     }
85
86     private ResponseEntity<String> getResponseArray(String url) {
87         try {
88             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
89             if (!rsp.getStatusCode().is2xxSuccessful()) {
90                 return rsp;
91             }
92             return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
93         } catch (Exception e) {
94             return ErrorResponseHandler.handleException(e);
95         }
96     }
97
98     private ResponseEntity<String> getResponseObject(String url) {
99         try {
100             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
101             if (!rsp.getStatusCode().is2xxSuccessful()) {
102                 return rsp;
103             }
104             return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
105         } catch (Exception e) {
106             return ErrorResponseHandler.handleException(e);
107         }
108     }
109 }