b96920f064d68a3ca329083250bc1b6aa7cad29d
[integration/csit.git] / plans / usecases / pnf-sw-upgrade / so / simulator / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / 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 package org.onap.so.aaisimulator.service.providers;
21
22 import java.util.Optional;
23 import org.onap.so.aaisimulator.exception.InvalidRestRequestException;
24 import org.onap.so.aaisimulator.exception.RestProcessingException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.http.HttpEntity;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.HttpMethod;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Service;
34 import org.springframework.web.client.HttpClientErrorException;
35 import org.springframework.web.client.RestClientException;
36 import org.springframework.web.client.RestTemplate;
37
38 /**
39  * @author Waqas Ikram (waqas.ikram@est.tech)
40  *
41  */
42 @Service
43 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
44     private static final Logger LOGGER = LoggerFactory.getLogger(HttpRestServiceProviderImpl.class);
45
46     private final RestTemplate restTemplate;
47
48     @Autowired
49     public HttpRestServiceProviderImpl(final RestTemplate restTemplate) {
50         this.restTemplate = restTemplate;
51     }
52
53     @Override
54     public <T> ResponseEntity<T> invokeHttpPut(final HttpEntity<Object> httpEntity, final String url,
55             final Class<T> clazz) {
56
57         final HttpMethod httpMethod = HttpMethod.PUT;
58         LOGGER.trace("Will invoke HTTP {} using URL: {}", httpMethod, url);
59         try {
60             return restTemplate.exchange(url, httpMethod, httpEntity, clazz);
61
62         } catch (final HttpClientErrorException httpClientErrorException) {
63             final String message = "Unable to invoke HTTP " + httpMethod + " using url: " + url + ", Response: "
64                     + httpClientErrorException.getRawStatusCode();
65             LOGGER.error(message, httpClientErrorException);
66             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
67             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
68                 throw new InvalidRestRequestException("No result found for given url: " + url);
69             }
70             throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url);
71
72         } catch (final RestClientException restClientException) {
73             LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException);
74             throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url,
75                     restClientException);
76         }
77     }
78
79     @Override
80     public <T> Optional<T> put(final HttpHeaders headers, final Object object, final String url, final Class<T> clazz) {
81         final HttpEntity<Object> httpEntity = new HttpEntity<Object>(object, headers);
82         final ResponseEntity<T> response = invokeHttpPut(httpEntity, url, clazz);
83
84         if (!response.getStatusCode().equals(HttpStatus.OK) && !response.getStatusCode().equals(HttpStatus.CREATED)
85                 && !response.getStatusCode().equals(HttpStatus.ACCEPTED)) {
86             final String message = "Unable to invoke HTTP " + HttpMethod.PUT + " using URL: " + url
87                     + ", Response Code: " + response.getStatusCode();
88             LOGGER.error(message);
89             return Optional.empty();
90         }
91
92         if (response.hasBody()) {
93             return Optional.of(response.getBody());
94         }
95         LOGGER.error("Received response without body status code: {}", response.getStatusCode());
96         return Optional.empty();
97     }
98 }