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