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_TYPES = "/eitypes";
39 private static final String EI_PRODUCERS = "/eiproducers";
40 private static final String EI_JOBS = "/eijobs";
41 private static final String STATUS = "/status";
43 private final AsyncRestClient webClient;
46 public EiProducerApiImpl(
47 @org.springframework.beans.factory.annotation.Value("${enrichmentcontroller.url.prefix}") final String urlPrefix) {
48 this(new AsyncRestClient(urlPrefix));
49 logger.debug("enrichment controller url prefix '{}'", urlPrefix);
52 public EiProducerApiImpl(AsyncRestClient webClient) {
53 this.webClient = webClient;
57 public ResponseEntity<String> getAllEiTypeIds() {
58 return getResponseArray(EI_TYPES);
62 public ResponseEntity<String> getEiType(String eiTypeId) {
63 return getResponseObject(EI_TYPES + "/" + eiTypeId);
67 public ResponseEntity<String> getAllEiProducerIds() {
68 return getResponseArray(EI_PRODUCERS);
72 public ResponseEntity<String> getEiProducer(String eiProducerId) {
73 return getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
77 public ResponseEntity<String> getEiJobsForOneEiProducer(String eiProducerId) {
78 return getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
82 public ResponseEntity<String> getEiProducerStatus(String eiProducerId) {
83 return getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
86 private ResponseEntity<String> getResponseArray(String url) {
88 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
89 if (!rsp.getStatusCode().is2xxSuccessful()) {
92 return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
93 } catch (Exception e) {
94 return ErrorResponseHandler.handleException(e);
98 private ResponseEntity<String> getResponseObject(String url) {
100 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
101 if (!rsp.getStatusCode().is2xxSuccessful()) {
104 return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
105 } catch (Exception e) {
106 return ErrorResponseHandler.handleException(e);