ca45e272bc9e1bf17272a804ccca3c0f1665dbbe
[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 java.lang.invoke.MethodHandles;
24 import javax.net.ssl.SSLException;
25 import org.json.JSONArray;
26 import org.json.JSONObject;
27 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Component;
34 import org.springframework.web.client.HttpClientErrorException;
35 import org.springframework.web.client.HttpServerErrorException;
36
37 @Component("EiProducerApi")
38 public class EiProducerApiImpl implements EiProducerApi {
39     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40
41     private static final String EI_TYPES = "/eitypes";
42     private static final String EI_PRODUCERS = "/eiproducers";
43     private static final String EI_JOBS = "/eijobs";
44     private static final String STATUS = "/status";
45
46     private final AsyncRestClient webClient;
47
48     private static com.google.gson.Gson gson = new GsonBuilder() //
49         .serializeNulls() //
50         .create(); //
51
52     @Autowired
53     public EiProducerApiImpl(
54         @org.springframework.beans.factory.annotation.Value("${enrichmentcontroller.url.prefix}") final String urlPrefix) {
55         this(new AsyncRestClient(urlPrefix));
56         logger.debug("enrichment controller url prefix '{}'", urlPrefix);
57     }
58
59     public EiProducerApiImpl(AsyncRestClient webClient) {
60         this.webClient = webClient;
61     }
62
63     @Override
64     public ResponseEntity<String> getAllEiTypeIds() {
65         return getResponseArray(EI_TYPES);
66     }
67
68     @Override
69     public ResponseEntity<String> getEiType(String eiTypeId) {
70         return getResponseObject(EI_TYPES + "/" + eiTypeId);
71     }
72
73     @Override
74     public ResponseEntity<String> getAllEiProducerIds() {
75         return getResponseArray(EI_PRODUCERS);
76     }
77
78     @Override
79     public ResponseEntity<String> getEiProducer(String eiProducerId) {
80         return getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
81     }
82
83     @Override
84     public ResponseEntity<String> getEiJobsForOneEiProducer(String eiProducerId) {
85         return getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
86     }
87
88     @Override
89     public ResponseEntity<String> getEiProducerStatus(String eiProducerId) {
90         return getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
91     }
92
93     private ResponseEntity<String> getResponseArray(String url) {
94         try {
95             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
96             if (!rsp.getStatusCode().is2xxSuccessful()) {
97                 return rsp;
98             }
99             return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
100         } catch (Exception e) {
101             return handleException(e);
102         }
103     }
104
105     private ResponseEntity<String> getResponseObject(String url) {
106         try {
107             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
108             if (!rsp.getStatusCode().is2xxSuccessful()) {
109                 return rsp;
110             }
111             return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
112         } catch (Exception e) {
113             return handleException(e);
114         }
115     }
116
117     private ResponseEntity<String> handleException(Exception throwable) {
118         if (throwable instanceof HttpClientErrorException) {
119             HttpClientErrorException e = (HttpClientErrorException) throwable;
120             return new ResponseEntity<>(e.getMessage(), e.getStatusCode());
121         } else if (throwable instanceof HttpServerErrorException) {
122             HttpServerErrorException e = (HttpServerErrorException) throwable;
123             return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode());
124         } else if (throwable instanceof SSLException) {
125             SSLException e = (SSLException) throwable;
126             return new ResponseEntity<>("Could not create WebClient " + e.getMessage(),
127                 HttpStatus.INTERNAL_SERVER_ERROR);
128         }
129         return new ResponseEntity<>(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
130     }
131 }