3 * Copyright (c) 2017 Orange. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.onap.nbi.apis.servicecatalog;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.util.LinkedHashMap;
23 import java.util.List;
25 import org.apache.commons.io.IOUtils;
26 import org.onap.nbi.OnapComponentsUrlPaths;
27 import org.onap.nbi.exceptions.BackendFunctionalException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.http.HttpEntity;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.HttpMethod;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Service;
38 import org.springframework.util.MultiValueMap;
39 import org.springframework.web.client.RestTemplate;
40 import org.springframework.web.util.UriComponentsBuilder;
47 public class SdcClient {
49 public static final String HTTP_CALL_SDC_ON = "HTTP call SDC on ";
51 private RestTemplate restTemplate;
54 private String sdcHost;
56 @Value("${sdc.header.ecompInstanceId}")
57 private String ecompInstanceId;
59 @Value("${sdc.header.authorization}")
60 private String sdcHeaderAuthorization;
62 private static final String HEADER_ECOMP_INSTANCE_ID = "x-ecomp-instanceid";
63 private static final String HEADER_AUTHORIZATION = "Authorization";
65 private static final Logger LOGGER = LoggerFactory.getLogger(SdcClient.class);
68 public LinkedHashMap callGet(String id) {
69 StringBuilder callURL = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL)
70 .append(id).append(OnapComponentsUrlPaths.SDC_GET_PATH);
72 ResponseEntity<Object> response = callSdc(callURL.toString());
73 return (LinkedHashMap) response.getBody();
77 public List<LinkedHashMap> callFind(MultiValueMap<String, String> parametersMap) {
79 UriComponentsBuilder callURL = 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 callURL.queryParam(key, stringStringMap.get(key));
89 ResponseEntity<Object> response = callSdc(callURL.build().encode().toUri().toString());
90 return (List<LinkedHashMap>) response.getBody();
95 public File callGetWithAttachment(String toscaModelUrl) {
96 StringBuilder callURL = new StringBuilder().append(sdcHost).append(toscaModelUrl);
98 String fileName = System.currentTimeMillis() + "tosca.csar";
99 ResponseEntity<byte[]> response = callSdcWithAttachment(callURL.toString());
100 File toscaFile = new File(fileName);
102 FileOutputStream toscaFileStream = new FileOutputStream(toscaFile);
103 if (response != null) {
104 IOUtils.write(response.getBody(), toscaFileStream);
106 toscaFileStream.close();
107 } catch (IOException e) {
108 LOGGER.error("cannot get TOSCA File for url " + toscaModelUrl);
114 private HttpEntity<String> buildRequestHeader() {
115 HttpHeaders httpHeaders = new HttpHeaders();
116 httpHeaders.add(HEADER_ECOMP_INSTANCE_ID, ecompInstanceId);
117 httpHeaders.add(HEADER_AUTHORIZATION, sdcHeaderAuthorization);
118 HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaders);
124 private ResponseEntity<Object> callSdc(String callURL) {
125 ResponseEntity<Object> response =
126 restTemplate.exchange(callURL, HttpMethod.GET, buildRequestHeader(), Object.class);
127 LOGGER.debug("response body : " + response.getBody().toString());
128 LOGGER.info("response status : " + response.getStatusCodeValue());
129 loggDebugIfResponseKo(callURL, response);
133 private void loggDebugIfResponseKo(String callURL, ResponseEntity<Object> response) {
134 if (!response.getStatusCode().equals(HttpStatus.OK)) {
135 LOGGER.warn(HTTP_CALL_SDC_ON + callURL + " returns " + response.getStatusCodeValue() + ", "
136 + response.getBody().toString());
140 private ResponseEntity<byte[]> callSdcWithAttachment(String callURL) {
142 ResponseEntity<byte[]> response =
143 restTemplate.exchange(callURL.toString(), HttpMethod.GET, buildRequestHeader(), byte[].class);
144 LOGGER.info("response status : " + response.getStatusCodeValue());
145 if (!response.getStatusCode().equals(HttpStatus.OK)) {
146 LOGGER.warn(HTTP_CALL_SDC_ON + callURL.toString() + " returns " + response.getStatusCodeValue() + ", "
147 + response.getBody().toString());
151 } catch (BackendFunctionalException e) {
152 LOGGER.error(HTTP_CALL_SDC_ON + callURL.toString() + " error " + e);