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;
20 import java.util.LinkedHashMap;
21 import java.util.List;
23 import org.apache.commons.io.IOUtils;
24 import org.onap.nbi.OnapComponentsUrlPaths;
25 import org.onap.nbi.exceptions.BackendFunctionalException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.http.HttpEntity;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpMethod;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Service;
36 import org.springframework.util.MultiValueMap;
37 import org.springframework.web.client.RestTemplate;
38 import org.springframework.web.util.UriComponentsBuilder;
45 public class SdcClient {
47 public static final String HTTP_CALL_SDC_ON = "HTTP call SDC on ";
49 private RestTemplate restTemplate;
52 private String sdcHost;
54 @Value("${sdc.header.ecompInstanceId}")
55 private String ecompInstanceId;
57 @Value("${sdc.header.authorization}")
58 private String sdcHeaderAuthorization;
60 private static final String HEADER_ECOMP_INSTANCE_ID = "x-ecomp-instanceid";
61 private static final String HEADER_AUTHORIZATION = "Authorization";
63 private static final Logger LOGGER = LoggerFactory.getLogger(SdcClient.class);
66 public LinkedHashMap callGet(String id) {
67 StringBuilder urlBuilder = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL)
68 .append("/").append(id).append(OnapComponentsUrlPaths.SDC_GET_PATH);
70 UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(urlBuilder.toString());
72 ResponseEntity<Object> response = callSdc(callURI.build().encode().toUri());
73 return (LinkedHashMap) response.getBody();
77 public List<LinkedHashMap> callFind(MultiValueMap<String, String> parametersMap) {
79 UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(sdcHost + OnapComponentsUrlPaths.SDC_ROOT_URL);
80 if (parametersMap != null) {
81 Map<String, String> stringStringMap = parametersMap.toSingleValueMap();
82 for (String key : stringStringMap.keySet()) {
83 if (!key.equals("fields")) {
84 callURI.queryParam(key, stringStringMap.get(key));
89 ResponseEntity<Object> response = callSdc(callURI.build().encode().toUri());
90 return (List<LinkedHashMap>) response.getBody();
95 public File callGetWithAttachment(String toscaModelUrl) {
96 StringBuilder urlBuilder = new StringBuilder().append(sdcHost).append(toscaModelUrl);
98 UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(urlBuilder.toString());
101 String fileName = System.currentTimeMillis() + "tosca.csar";
102 ResponseEntity<byte[]> response = callSdcWithAttachment(callURI.build().encode().toUri());
103 File toscaFile = new File(fileName);
105 FileOutputStream toscaFileStream = new FileOutputStream(toscaFile);
106 if (response != null) {
107 IOUtils.write(response.getBody(), toscaFileStream);
109 toscaFileStream.close();
110 } catch (IOException e) {
111 LOGGER.error("cannot get TOSCA File for url " + toscaModelUrl, e);
117 private HttpEntity<String> buildRequestHeader() {
118 HttpHeaders httpHeaders = new HttpHeaders();
119 httpHeaders.add(HEADER_ECOMP_INSTANCE_ID, ecompInstanceId);
120 httpHeaders.add(HEADER_AUTHORIZATION, sdcHeaderAuthorization);
121 HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaders);
127 private ResponseEntity<Object> callSdc(URI callURI) {
128 ResponseEntity<Object> response =
129 restTemplate.exchange(callURI, HttpMethod.GET, buildRequestHeader(), Object.class);
130 LOGGER.debug("response body : " + response.getBody().toString());
131 LOGGER.info("response status : " + response.getStatusCodeValue());
132 loggDebugIfResponseKo(callURI.toString(), response);
137 private ResponseEntity<byte[]> callSdcWithAttachment(URI callURI) {
139 ResponseEntity<byte[]> response =
140 restTemplate.exchange(callURI, HttpMethod.GET, buildRequestHeader(), byte[].class);
141 LOGGER.info("response status : " + response.getStatusCodeValue());
142 if (!response.getStatusCode().equals(HttpStatus.OK)) {
143 LOGGER.error(HTTP_CALL_SDC_ON + callURI.toString() + " returns " + response.getStatusCodeValue() + ", "
144 + response.getBody().toString());
148 } catch (BackendFunctionalException e) {
149 LOGGER.error(HTTP_CALL_SDC_ON + callURI.toString() + " error " + e);
155 private void loggDebugIfResponseKo(String callURI, ResponseEntity<Object> response) {
156 if (!response.getStatusCode().equals(HttpStatus.OK)) {
157 LOGGER.warn(HTTP_CALL_SDC_ON + callURI + " returns " + response.getStatusCodeValue() + ", "
158 + response.getBody().toString());