bbdcb00266c0464303117d4f512d473b3c9438e4
[nonrtric.git] / sdnc-a1-controller / northbound / nonrt-ric-api / provider / src / main / java / org / onap / sdnc / northbound / restadpter / RestAdapterImpl.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.sdnc.northbound.restadpter;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.http.HttpEntity;
26 import org.springframework.http.HttpMethod;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.web.client.RestTemplate;
30 import com.google.common.base.Optional;
31
32 /**
33  * This class provides the Generic Rest Adapter interface to the RestTemplate
34  * 
35  * @author lathishbabu.ganesan@est.tech
36  *
37  */
38
39 public class RestAdapterImpl implements RestAdapter {
40
41   private final Logger log = LoggerFactory.getLogger(RestAdapterImpl.class);
42
43   private RestTemplate restTemplate;
44
45   public RestAdapterImpl() {
46     restTemplate = new RestTemplate();
47   }
48
49   private HttpEntity<?> getHttpEntity(final Object object) {
50     return new HttpEntity<>(object);
51   }
52
53   @Override
54   public <T> Optional<T> get(String uri, Class<?> clazz) {
55     HttpEntity<?> entity = getHttpEntity(null);
56     final ResponseEntity<T> response = invokeHttpRequest(uri, HttpMethod.GET, clazz, entity);
57     return buildOptional(response);
58   }
59
60   @Override
61   public <T> Optional<T> put(String uri, Object object) {
62     HttpEntity<?> entity = getHttpEntity(object);
63     final ResponseEntity<T> response = invokeHttpRequest(uri, HttpMethod.PUT, null, entity);
64     return buildOptional(response);
65   }
66
67   @Override
68   public <T> Optional<T> delete(String uri) {
69     HttpEntity<?> entity = getHttpEntity(null);
70     final ResponseEntity<T> response = invokeHttpRequest(uri, HttpMethod.DELETE, null, entity);
71     return buildOptional(response);
72   }
73
74   @SuppressWarnings("unchecked")
75   private <T> ResponseEntity<T> invokeHttpRequest(String uri, HttpMethod httpMethod, Class<?> clazz,
76       HttpEntity<?> entity) {
77     return (ResponseEntity<T>) restTemplate.exchange(uri, httpMethod, entity, clazz);
78   }
79
80   private <T> Optional<T> buildOptional(ResponseEntity<T> response) {
81     if (!response.getStatusCode().equals(HttpStatus.OK)
82         | !response.getStatusCode().equals(HttpStatus.CREATED)
83         | !response.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
84       log.error("Failed to get the Response");
85       return Optional.absent();
86     }
87     if (response.hasBody()) {
88       return Optional.of(response.getBody());
89     }
90     return Optional.absent();
91   }
92 }