Merge "Add INFO.yaml file"
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / SdcClient.java
1 package org.onap.nbi.apis.servicecatalog;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
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;
25
26 /**
27  * @author user
28  *
29  */
30 @Service
31 public class SdcClient {
32
33     public static final String HTTP_CALL_SDC_ON = "HTTP call SDC on ";
34     @Autowired
35     private RestTemplate restTemplate;
36
37     @Value("${sdc.host}")
38     private String sdcHost;
39
40     @Value("${sdc.header.ecompInstanceId}")
41     private String ecompInstanceId;
42
43     @Value("${sdc.header.authorization}")
44     private String sdcHeaderAuthorization;
45
46     private static final String HEADER_ECOMP_INSTANCE_ID = "x-ecomp-instanceid";
47     private static final String HEADER_AUTHORIZATION = "Authorization";
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(SdcClient.class);
50
51
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);
55
56         ResponseEntity<Object> response = callSdc(callURL.toString());
57         return (LinkedHashMap) response.getBody();
58
59     }
60
61     public List<LinkedHashMap> callFind(MultiValueMap<String, String> parametersMap) {
62
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));
69                 }
70             }
71         }
72
73         ResponseEntity<Object> response = callSdc(callURL.build().encode().toUri().toString());
74         return (List<LinkedHashMap>) response.getBody();
75
76     }
77
78
79     public File callGetWithAttachment(String toscaModelUrl) {
80         StringBuilder callURL = new StringBuilder().append(sdcHost).append(toscaModelUrl);
81
82         String fileName = System.currentTimeMillis() + "tosca.csar";
83         ResponseEntity<byte[]> response = callSdcWithAttachment(callURL.toString());
84         File toscaFile = new File(fileName);
85         try {
86             FileOutputStream toscaFileStream = new FileOutputStream(toscaFile);
87             if (response != null) {
88                 IOUtils.write(response.getBody(), toscaFileStream);
89             }
90             toscaFileStream.close();
91         } catch (IOException e) {
92             LOGGER.error("cannot get TOSCA File for url " + toscaModelUrl);
93         }
94         return toscaFile;
95
96     }
97
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);
103
104         return entity;
105     }
106
107
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);
114         return response;
115     }
116
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());
121         }
122     }
123
124     private ResponseEntity<byte[]> callSdcWithAttachment(String callURL) {
125         try {
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());
132             }
133             return response;
134
135         } catch (BackendFunctionalException e) {
136             LOGGER.error(HTTP_CALL_SDC_ON + callURL.toString() + " error " + e);
137             return null;
138         }
139     }
140
141
142 }
143
144