2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.portal.nonrtric.controlpanel.eiproducerapi;
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;
37 @Component("EiProducerApi")
38 public class EiProducerApiImpl implements EiProducerApi {
39 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
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";
46 private final AsyncRestClient webClient;
48 private static com.google.gson.Gson gson = new GsonBuilder() //
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);
59 public EiProducerApiImpl(AsyncRestClient webClient) {
60 this.webClient = webClient;
64 public ResponseEntity<String> getAllEiTypeIds() {
65 return getResponseArray(EI_TYPES);
69 public ResponseEntity<String> getEiType(String eiTypeId) {
70 return getResponseObject(EI_TYPES + "/" + eiTypeId);
74 public ResponseEntity<String> getAllEiProducerIds() {
75 return getResponseArray(EI_PRODUCERS);
79 public ResponseEntity<String> getEiProducer(String eiProducerId) {
80 return getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
84 public ResponseEntity<String> getEiJobsForOneEiProducer(String eiProducerId) {
85 return getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
89 public ResponseEntity<String> getEiProducerStatus(String eiProducerId) {
90 return getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
93 private ResponseEntity<String> getResponseArray(String url) {
95 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
96 if (!rsp.getStatusCode().is2xxSuccessful()) {
99 return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
100 } catch (Exception e) {
101 return handleException(e);
105 private ResponseEntity<String> getResponseObject(String url) {
107 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
108 if (!rsp.getStatusCode().is2xxSuccessful()) {
111 return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
112 } catch (Exception e) {
113 return handleException(e);
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);
129 return new ResponseEntity<>(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);