1 package org.onap.nbi.apis.servicecatalog;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.LinkedHashMap;
9 import org.apache.commons.io.IOUtils;
10 import org.onap.nbi.OnapComponentsUrlPaths;
11 import org.onap.nbi.exceptions.BackendFunctionalException;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.beans.factory.annotation.Value;
16 import org.springframework.http.HttpEntity;
17 import org.springframework.http.HttpHeaders;
18 import org.springframework.http.HttpMethod;
19 import org.springframework.http.HttpStatus;
20 import org.springframework.http.ResponseEntity;
21 import org.springframework.stereotype.Service;
22 import org.springframework.util.MultiValueMap;
23 import org.springframework.web.client.RestTemplate;
24 import org.springframework.web.util.UriComponentsBuilder;
31 public class SdcClient {
33 public static final String HTTP_CALL_SDC_ON = "HTTP call SDC on ";
35 private RestTemplate restTemplate;
38 private String sdcHost;
40 @Value("${sdc.header.ecompInstanceId}")
41 private String ecompInstanceId;
43 @Value("${sdc.header.authorization}")
44 private String sdcHeaderAuthorization;
46 private static final String HEADER_ECOMP_INSTANCE_ID = "x-ecomp-instanceid";
47 private static final String HEADER_AUTHORIZATION = "Authorization";
49 private static final Logger LOGGER = LoggerFactory.getLogger(SdcClient.class);
52 public LinkedHashMap callGet(String id) {
53 StringBuilder callURL = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL)
54 .append(id).append(OnapComponentsUrlPaths.SDC_GET_PATH);
56 ResponseEntity<Object> response = callSdc(callURL.toString());
57 return (LinkedHashMap) response.getBody();
61 public List<LinkedHashMap> callFind(MultiValueMap<String, String> parametersMap) {
63 UriComponentsBuilder callURL = UriComponentsBuilder.fromHttpUrl(sdcHost + OnapComponentsUrlPaths.SDC_ROOT_URL);
64 if (parametersMap != null) {
65 Map<String, String> stringStringMap = parametersMap.toSingleValueMap();
66 for (String key : stringStringMap.keySet()) {
67 if (!key.equals("fields")) {
68 callURL.queryParam(key, stringStringMap.get(key));
73 ResponseEntity<Object> response = callSdc(callURL.build().encode().toUri().toString());
74 return (List<LinkedHashMap>) response.getBody();
79 public File callGetWithAttachment(String toscaModelUrl) {
80 StringBuilder callURL = new StringBuilder().append(sdcHost).append(toscaModelUrl);
82 String fileName = System.currentTimeMillis() + "tosca.csar";
83 ResponseEntity<byte[]> response = callSdcWithAttachment(callURL.toString());
84 File toscaFile = new File(fileName);
86 FileOutputStream toscaFileStream = new FileOutputStream(toscaFile);
87 if (response != null) {
88 IOUtils.write(response.getBody(), toscaFileStream);
90 toscaFileStream.close();
91 } catch (IOException e) {
92 LOGGER.error("cannot get TOSCA File for url " + toscaModelUrl);
98 private HttpEntity<String> buildRequestHeader() {
99 HttpHeaders httpHeaders = new HttpHeaders();
100 httpHeaders.add(HEADER_ECOMP_INSTANCE_ID, ecompInstanceId);
101 httpHeaders.add(HEADER_AUTHORIZATION, sdcHeaderAuthorization);
102 HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaders);
108 private ResponseEntity<Object> callSdc(String callURL) {
109 ResponseEntity<Object> response =
110 restTemplate.exchange(callURL, HttpMethod.GET, buildRequestHeader(), Object.class);
111 LOGGER.debug("response body : " + response.getBody().toString());
112 LOGGER.info("response status : " + response.getStatusCodeValue());
113 loggDebugIfResponseKo(callURL, response);
117 private void loggDebugIfResponseKo(String callURL, ResponseEntity<Object> response) {
118 if (!response.getStatusCode().equals(HttpStatus.OK)) {
119 LOGGER.warn(HTTP_CALL_SDC_ON + callURL + " returns " + response.getStatusCodeValue() + ", "
120 + response.getBody().toString());
124 private ResponseEntity<byte[]> callSdcWithAttachment(String callURL) {
126 ResponseEntity<byte[]> response =
127 restTemplate.exchange(callURL.toString(), HttpMethod.GET, buildRequestHeader(), byte[].class);
128 LOGGER.info("response status : " + response.getStatusCodeValue());
129 if (!response.getStatusCode().equals(HttpStatus.OK)) {
130 LOGGER.warn(HTTP_CALL_SDC_ON + callURL.toString() + " returns " + response.getStatusCodeValue() + ", "
131 + response.getBody().toString());
135 } catch (BackendFunctionalException e) {
136 LOGGER.error(HTTP_CALL_SDC_ON + callURL.toString() + " error " + e);