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 java.lang.invoke.MethodHandles;
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;
34 @Component("EiProducerApi")
35 public class EiProducerApiImpl implements EiProducerApi {
36 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38 private static final String EI_PRODUCERS = "/eiproducers";
39 private static final String EI_JOBS = "/eijobs";
40 private static final String STATUS = "/status";
42 private final AsyncRestClient webClient;
45 public EiProducerApiImpl(
46 @org.springframework.beans.factory.annotation.Value("${enrichmentcontroller.url.prefix}") final String urlPrefix) {
47 this(new AsyncRestClient(urlPrefix));
48 logger.debug("enrichment controller url prefix '{}'", urlPrefix);
51 public EiProducerApiImpl(AsyncRestClient webClient) {
52 this.webClient = webClient;
56 public ResponseEntity<String> getAllEiProducerIds() {
57 return getResponseArray(EI_PRODUCERS);
61 public ResponseEntity<String> getEiProducer(String eiProducerId) {
62 return getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
66 public ResponseEntity<String> getEiJobsForOneEiProducer(String eiProducerId) {
67 return getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
71 public ResponseEntity<String> getEiProducerStatus(String eiProducerId) {
72 return getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
75 private ResponseEntity<String> getResponseArray(String url) {
77 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
78 if (!rsp.getStatusCode().is2xxSuccessful()) {
81 return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
82 } catch (Exception e) {
83 return ErrorResponseHandler.handleException(e);
87 private ResponseEntity<String> getResponseObject(String url) {
89 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
90 if (!rsp.getStatusCode().is2xxSuccessful()) {
93 return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
94 } catch (Exception e) {
95 return ErrorResponseHandler.handleException(e);