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