Sonar clean code
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / SdcClient.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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
12  * the License.
13  */
14 package org.onap.nbi.apis.servicecatalog;
15
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.util.LinkedHashMap;
20 import java.util.List;
21 import java.util.Map;
22 import org.apache.commons.io.IOUtils;
23 import org.onap.nbi.OnapComponentsUrlPaths;
24 import org.onap.nbi.exceptions.BackendFunctionalException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.http.HttpEntity;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpMethod;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Service;
35 import org.springframework.util.MultiValueMap;
36 import org.springframework.web.client.RestTemplate;
37 import org.springframework.web.util.UriComponentsBuilder;
38
39 /**
40  * @author user
41  *
42  */
43 @Service
44 public class SdcClient {
45
46     public static final String HTTP_CALL_SDC_ON = "HTTP call SDC on ";
47     @Autowired
48     private RestTemplate restTemplate;
49
50     @Value("${sdc.host}")
51     private String sdcHost;
52
53     @Value("${sdc.header.ecompInstanceId}")
54     private String ecompInstanceId;
55
56     @Value("${sdc.header.authorization}")
57     private String sdcHeaderAuthorization;
58
59     private static final String HEADER_ECOMP_INSTANCE_ID = "x-ecomp-instanceid";
60     private static final String HEADER_AUTHORIZATION = "Authorization";
61
62     private static final Logger LOGGER = LoggerFactory.getLogger(SdcClient.class);
63
64
65     public LinkedHashMap callGet(String id) {
66         StringBuilder callURL = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL)
67                 .append(id).append(OnapComponentsUrlPaths.SDC_GET_PATH);
68
69         ResponseEntity<Object> response = callSdc(callURL.toString());
70         return (LinkedHashMap) response.getBody();
71
72     }
73
74     public List<LinkedHashMap> callFind(MultiValueMap<String, String> parametersMap) {
75
76         UriComponentsBuilder callURL = UriComponentsBuilder.fromHttpUrl(sdcHost + OnapComponentsUrlPaths.SDC_ROOT_URL);
77         if (parametersMap != null) {
78             Map<String, String> stringStringMap = parametersMap.toSingleValueMap();
79             for (String key : stringStringMap.keySet()) {
80                 if (!key.equals("fields")) {
81                     callURL.queryParam(key, stringStringMap.get(key));
82                 }
83             }
84         }
85
86         ResponseEntity<Object> response = callSdc(callURL.build().encode().toUri().toString());
87         return (List<LinkedHashMap>) response.getBody();
88
89     }
90
91
92     public File callGetWithAttachment(String toscaModelUrl) {
93         StringBuilder callURL = new StringBuilder().append(sdcHost).append(toscaModelUrl);
94
95         String fileName = System.currentTimeMillis() + "tosca.csar";
96         ResponseEntity<byte[]> response = callSdcWithAttachment(callURL.toString());
97         File toscaFile = new File(fileName);
98         try {
99             FileOutputStream toscaFileStream = new FileOutputStream(toscaFile);
100             if (response != null) {
101                 IOUtils.write(response.getBody(), toscaFileStream);
102             }
103             toscaFileStream.close();
104         } catch (IOException e) {
105             LOGGER.error("cannot get TOSCA File for url " + toscaModelUrl, e);
106         }
107         return toscaFile;
108
109     }
110
111     private HttpEntity<String> buildRequestHeader() {
112         HttpHeaders httpHeaders = new HttpHeaders();
113         httpHeaders.add(HEADER_ECOMP_INSTANCE_ID, ecompInstanceId);
114         httpHeaders.add(HEADER_AUTHORIZATION, sdcHeaderAuthorization);
115         HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaders);
116
117         return entity;
118     }
119
120
121     private ResponseEntity<Object> callSdc(String callURL) {
122         ResponseEntity<Object> response =
123                 restTemplate.exchange(callURL, HttpMethod.GET, buildRequestHeader(), Object.class);
124         LOGGER.debug("response body : " + response.getBody().toString());
125         LOGGER.info("response status : " + response.getStatusCodeValue());
126         loggDebugIfResponseKo(callURL, response);
127         return response;
128     }
129
130     private void loggDebugIfResponseKo(String callURL, ResponseEntity<Object> response) {
131         if (!response.getStatusCode().equals(HttpStatus.OK)) {
132             LOGGER.warn(HTTP_CALL_SDC_ON + callURL + " returns " + response.getStatusCodeValue() + ", "
133                     + response.getBody().toString());
134         }
135     }
136
137     private ResponseEntity<byte[]> callSdcWithAttachment(String callURL) {
138         try {
139             ResponseEntity<byte[]> response =
140                     restTemplate.exchange(callURL.toString(), HttpMethod.GET, buildRequestHeader(), byte[].class);
141             LOGGER.info("response status : " + response.getStatusCodeValue());
142             if (!response.getStatusCode().equals(HttpStatus.OK)) {
143                 LOGGER.warn(HTTP_CALL_SDC_ON + callURL.toString() + " returns " + response.getStatusCodeValue() + ", "
144                         + response.getBody().toString());
145             }
146             return response;
147
148         } catch (BackendFunctionalException e) {
149             LOGGER.error(HTTP_CALL_SDC_ON + callURL.toString() + " error " + e);
150             return null;
151         }
152     }
153
154
155 }
156
157