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 com.google.gson.JsonSyntaxException;
25 import java.lang.invoke.MethodHandles;
26 import java.util.List;
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;
42 @Component("EiProducerApi")
43 public class EiProducerApiImpl implements EiProducerApi {
44 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46 private static final String EI_PRODUCERS = "/eiproducers";
47 private static final String EI_JOBS = "/eijobs";
48 private static final String STATUS = "/status";
50 private final AsyncRestClient webClient;
51 private static com.google.gson.Gson gson = new GsonBuilder().create();
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);
60 public EiProducerApiImpl(AsyncRestClient webClient) {
61 this.webClient = webClient;
65 public ResponseEntity<String> getAllEiProducerIds() {
66 return getResponseArray(EI_PRODUCERS);
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());
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());
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());
91 private ResponseEntity<String> getResponseArray(String url) {
93 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
94 if (!rsp.getStatusCode().is2xxSuccessful()) {
97 return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
98 } catch (Exception e) {
99 return ErrorResponseHandler.handleException(e);
103 private ResponseEntity<String> getResponseObject(String url) {
105 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
106 if (!rsp.getStatusCode().is2xxSuccessful()) {
109 return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
110 } catch (Exception e) {
111 return ErrorResponseHandler.handleException(e);