701f01ed6fdb14355e5d034588903df32d57b56d
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / SdcClient.java
1 /**
2  *
3  *     Copyright (c) 2017 Orange.  All rights reserved.
4  *
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
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 package org.onap.nbi.apis.servicecatalog;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
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;
41
42 /**
43  * @author user
44  *
45  */
46 @Service
47 public class SdcClient {
48
49     public static final String HTTP_CALL_SDC_ON = "HTTP call SDC on ";
50     @Autowired
51     private RestTemplate restTemplate;
52
53     @Value("${sdc.host}")
54     private String sdcHost;
55
56     @Value("${sdc.header.ecompInstanceId}")
57     private String ecompInstanceId;
58
59     @Value("${sdc.header.authorization}")
60     private String sdcHeaderAuthorization;
61
62     private static final String HEADER_ECOMP_INSTANCE_ID = "x-ecomp-instanceid";
63     private static final String HEADER_AUTHORIZATION = "Authorization";
64
65     private static final Logger LOGGER = LoggerFactory.getLogger(SdcClient.class);
66
67
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);
71
72         ResponseEntity<Object> response = callSdc(callURL.toString());
73         return (LinkedHashMap) response.getBody();
74
75     }
76
77     public List<LinkedHashMap> callFind(MultiValueMap<String, String> parametersMap) {
78
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));
85                 }
86             }
87         }
88
89         ResponseEntity<Object> response = callSdc(callURL.build().encode().toUri().toString());
90         return (List<LinkedHashMap>) response.getBody();
91
92     }
93
94
95     public File callGetWithAttachment(String toscaModelUrl) {
96         StringBuilder callURL = new StringBuilder().append(sdcHost).append(toscaModelUrl);
97
98         String fileName = System.currentTimeMillis() + "tosca.csar";
99         ResponseEntity<byte[]> response = callSdcWithAttachment(callURL.toString());
100         File toscaFile = new File(fileName);
101         try {
102             FileOutputStream toscaFileStream = new FileOutputStream(toscaFile);
103             if (response != null) {
104                 IOUtils.write(response.getBody(), toscaFileStream);
105             }
106             toscaFileStream.close();
107         } catch (IOException e) {
108             LOGGER.error("cannot get TOSCA File for url " + toscaModelUrl);
109         }
110         return toscaFile;
111
112     }
113
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);
119
120         return entity;
121     }
122
123
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);
130         return response;
131     }
132
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());
137         }
138     }
139
140     private ResponseEntity<byte[]> callSdcWithAttachment(String callURL) {
141         try {
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());
148             }
149             return response;
150
151         } catch (BackendFunctionalException e) {
152             LOGGER.error(HTTP_CALL_SDC_ON + callURL.toString() + " error " + e);
153             return null;
154         }
155     }
156
157
158 }
159
160