2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.nbi.apis.servicecatalog;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.util.LinkedHashMap;
20 import java.util.List;
22 import org.apache.commons.io.IOUtils;
23 import org.onap.nbi.OnapComponentsUrlPaths;
24 import org.onap.nbi.exceptions.BackendFunctionalException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.http.HttpEntity;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpMethod;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Service;
35 import org.springframework.util.MultiValueMap;
36 import org.springframework.web.client.RestTemplate;
37 import org.springframework.web.util.UriComponentsBuilder;
44 public class SdcClient {
46 public static final String HTTP_CALL_SDC_ON = "HTTP call SDC on ";
48 private RestTemplate restTemplate;
51 private String sdcHost;
53 @Value("${sdc.header.ecompInstanceId}")
54 private String ecompInstanceId;
56 @Value("${sdc.header.authorization}")
57 private String sdcHeaderAuthorization;
59 private static final String HEADER_ECOMP_INSTANCE_ID = "x-ecomp-instanceid";
60 private static final String HEADER_AUTHORIZATION = "Authorization";
62 private static final Logger LOGGER = LoggerFactory.getLogger(SdcClient.class);
65 public LinkedHashMap callGet(String id) {
66 StringBuilder callURL = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL)
67 .append(id).append(OnapComponentsUrlPaths.SDC_GET_PATH);
69 ResponseEntity<Object> response = callSdc(callURL.toString());
70 return (LinkedHashMap) response.getBody();
74 public List<LinkedHashMap> callFind(MultiValueMap<String, String> parametersMap) {
76 UriComponentsBuilder callURL = UriComponentsBuilder.fromHttpUrl(sdcHost + OnapComponentsUrlPaths.SDC_ROOT_URL);
77 if (parametersMap != null) {
78 Map<String, String> stringStringMap = parametersMap.toSingleValueMap();
79 for (String key : stringStringMap.keySet()) {
80 if (!key.equals("fields")) {
81 callURL.queryParam(key, stringStringMap.get(key));
86 ResponseEntity<Object> response = callSdc(callURL.build().encode().toUri().toString());
87 return (List<LinkedHashMap>) response.getBody();
92 public File callGetWithAttachment(String toscaModelUrl) {
93 StringBuilder callURL = new StringBuilder().append(sdcHost).append(toscaModelUrl);
95 String fileName = System.currentTimeMillis() + "tosca.csar";
96 ResponseEntity<byte[]> response = callSdcWithAttachment(callURL.toString());
97 File toscaFile = new File(fileName);
99 FileOutputStream toscaFileStream = new FileOutputStream(toscaFile);
100 if (response != null) {
101 IOUtils.write(response.getBody(), toscaFileStream);
103 toscaFileStream.close();
104 } catch (IOException e) {
105 LOGGER.error("cannot get TOSCA File for url " + toscaModelUrl, e);
111 private HttpEntity<String> buildRequestHeader() {
112 HttpHeaders httpHeaders = new HttpHeaders();
113 httpHeaders.add(HEADER_ECOMP_INSTANCE_ID, ecompInstanceId);
114 httpHeaders.add(HEADER_AUTHORIZATION, sdcHeaderAuthorization);
115 HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaders);
121 private ResponseEntity<Object> callSdc(String callURL) {
122 ResponseEntity<Object> response =
123 restTemplate.exchange(callURL, HttpMethod.GET, buildRequestHeader(), Object.class);
124 LOGGER.debug("response body : " + response.getBody().toString());
125 LOGGER.info("response status : " + response.getStatusCodeValue());
126 loggDebugIfResponseKo(callURL, response);
130 private void loggDebugIfResponseKo(String callURL, ResponseEntity<Object> response) {
131 if (!response.getStatusCode().equals(HttpStatus.OK)) {
132 LOGGER.warn(HTTP_CALL_SDC_ON + callURL + " returns " + response.getStatusCodeValue() + ", "
133 + response.getBody().toString());
137 private ResponseEntity<byte[]> callSdcWithAttachment(String callURL) {
139 ResponseEntity<byte[]> response =
140 restTemplate.exchange(callURL.toString(), HttpMethod.GET, buildRequestHeader(), byte[].class);
141 LOGGER.info("response status : " + response.getStatusCodeValue());
142 if (!response.getStatusCode().equals(HttpStatus.OK)) {
143 LOGGER.warn(HTTP_CALL_SDC_ON + callURL.toString() + " returns " + response.getStatusCodeValue() + ", "
144 + response.getBody().toString());
148 } catch (BackendFunctionalException e) {
149 LOGGER.error(HTTP_CALL_SDC_ON + callURL.toString() + " error " + e);