Check for existing VNF in VNFM
[so.git] / common / src / main / java / org / onap / so / rest / service / HttpRestServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.rest.service;
22
23 import com.google.common.base.Optional;
24 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
25 import org.onap.so.configuration.rest.HttpHeadersProvider;
26 import org.onap.so.rest.exceptions.InvalidRestRequestException;
27 import org.onap.so.rest.exceptions.RestProcessingException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.http.HttpEntity;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpMethod;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.web.client.HttpClientErrorException;
36 import org.springframework.web.client.RestClientException;
37 import org.springframework.web.client.RestTemplate;
38
39 /**
40  * A Service to perform HTTP requests
41  *
42  * @author waqas.ikram@est.tech
43  */
44 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(HttpRestServiceProviderImpl.class);
47     private final RestTemplate restTemplate;
48     private final HttpHeadersProvider httpHeadersProvider;
49
50     public HttpRestServiceProviderImpl(final RestTemplate restTemplate) {
51         this.restTemplate = restTemplate;
52         this.httpHeadersProvider = new BasicHttpHeadersProvider();
53     }
54
55     public HttpRestServiceProviderImpl(final RestTemplate restTemplate, final HttpHeadersProvider httpHeadersProvider) {
56         this.restTemplate = restTemplate;
57         this.httpHeadersProvider = httpHeadersProvider;
58     }
59
60     @Override
61     public <T> Optional<T> get(final String url, final Class<T> clazz) {
62         final ResponseEntity<T> response = getHttpResponse(url, clazz);
63         return createOptional(response, url, HttpMethod.GET);
64     }
65
66
67     @Override
68     public <T> ResponseEntity<T> getHttpResponse(final String url, final Class<T> clazz) {
69         final HttpEntity<?> request = new HttpEntity<>(getHttpHeaders());
70         return invokeHttpRequest(request, HttpMethod.GET, url, clazz);
71     }
72
73     @Override
74     public <T> Optional<T> post(final Object object, final String url, final Class<T> clazz) {
75         final ResponseEntity<T> response = postHttpRequest(object, url, clazz);
76         return createOptional(response, url, HttpMethod.POST);
77     }
78
79     @Override
80     public <T> ResponseEntity<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
81         final HttpEntity<?> request = new HttpEntity<>(object, getHttpHeaders());
82         return invokeHttpRequest(request, HttpMethod.POST, url, clazz);
83     }
84
85     @Override
86     public <T> Optional<T> put(final Object object, final String url, final Class<T> clazz) {
87         final ResponseEntity<T> response = putHttpRequest(object, url, clazz);
88         return createOptional(response, url, HttpMethod.PUT);
89     }
90
91     @Override
92     public <T> ResponseEntity<T> putHttpRequest(final Object object, final String url, final Class<T> clazz) {
93         final HttpEntity<?> request = new HttpEntity<>(object, getHttpHeaders());
94         return invokeHttpRequest(request, HttpMethod.PUT, url, clazz);
95     }
96
97     private <T> Optional<T> createOptional(final ResponseEntity<T> response, final String url,
98             final HttpMethod httpMethod) {
99         if (!response.getStatusCode().equals(HttpStatus.OK)) {
100             final String message = "Unable to invoke HTTP " + httpMethod + " using URL: " + url + ", Response Code: "
101                     + response.getStatusCode();
102             LOGGER.error(message);
103             return Optional.absent();
104         }
105
106         if (response.hasBody()) {
107             return Optional.of(response.getBody());
108         }
109
110         return Optional.absent();
111     }
112
113     private <T> ResponseEntity<T> invokeHttpRequest(final HttpEntity<?> request, final HttpMethod httpMethod,
114             final String url, final Class<T> clazz) {
115         LOGGER.trace("Will invoke HTTP {} using URL: {}", httpMethod, url);
116         try {
117             return restTemplate.exchange(url, httpMethod, request, clazz);
118
119         } catch (final HttpClientErrorException httpClientErrorException) {
120             final String message = "Unable to invoke HTTP " + httpMethod + " using url: " + url + ", Response: "
121                     + httpClientErrorException.getRawStatusCode();
122             LOGGER.error(message, httpClientErrorException);
123             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
124             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
125                 throw new InvalidRestRequestException("No result found for given url: " + url);
126             }
127             throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url);
128
129         } catch (final RestClientException restClientException) {
130             LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException);
131             throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url,
132                     restClientException);
133         }
134     }
135
136     private HttpHeaders getHttpHeaders() {
137         return httpHeadersProvider.getHttpHeaders();
138     }
139 }