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