Refactor babel-related code to not update parameter values
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / restclient / AaiRestClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.modelloader.restclient;
22
23 import java.util.Collections;
24 import org.onap.aai.cl.api.Logger;
25 import org.onap.aai.cl.eelf.LoggerFactory;
26 import org.onap.aai.modelloader.config.ModelLoaderConfig;
27 import org.onap.aai.modelloader.entity.AaiResourcesObject;
28 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
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.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Component;
36 import org.springframework.web.client.RestTemplate;
37
38 /**
39  * Wrapper around the standard A&AI Rest Client interface. This currently uses Jersey client 1.x
40  *
41  */
42 @Component
43 public class AaiRestClient {
44
45     private static Logger logger = LoggerFactory.getInstance().getLogger(AaiRestClient.class.getName());
46     public static final String HEADER_TRANS_ID = "X-TransactionId";
47     public static final String HEADER_FROM_APP_ID = "X-FromAppId";
48     public static final String ML_APP_NAME = "ModelLoader";
49     private static final String RESOURCE_VERSION_PARAM = "resource-version";
50     private final ModelLoaderConfig config;
51     private final RestTemplate restTemplate;
52
53     public AaiRestClient(ModelLoaderConfig config, RestTemplate restTemplate) {
54         this.config = config;
55         this.restTemplate = restTemplate;
56     }
57
58     /**
59      * Send a GET request to the A&AI for a resource.
60      * @param <T>
61      *
62      * @param url
63      * @param transId
64      * @param mediaType
65      * @return
66      */
67     public <T> ResponseEntity<T> getResource(String url, String transId, MediaType mediaType, Class<T> responseType) {
68         HttpHeaders headers = defaultHeaders(transId);
69         headers.setAccept(Collections.singletonList(mediaType));
70         HttpEntity<String> entity = new HttpEntity<>(headers);
71
72         return restTemplate.exchange(url, HttpMethod.GET, entity, responseType);
73     }
74
75     /**
76      * Send a PUT request to the A&AI.
77      *
78      * @param url - the url
79      * @param payload - the XML or JSON payload for the request
80      * @param transId - transaction ID
81      * @param mediaType - the content type (XML or JSON)
82      * @return operation result
83      */
84     public <T> ResponseEntity<T> putResource(String url, T payload, String transId, MediaType mediaType, Class<T> responseType) {
85         logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload.toString());
86         HttpHeaders headers = defaultHeaders(transId);
87         headers.setAccept(Collections.singletonList(mediaType));
88         headers.setContentType(mediaType);
89         HttpEntity<T> entity = new HttpEntity<>(payload, headers);
90
91         return restTemplate.exchange(url, HttpMethod.PUT, entity, responseType);
92     }
93
94     public <T> ResponseEntity<T> postResource(String url, T payload, String transId, MediaType mediaType, Class<T> responseType) {
95         logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload.toString());
96         HttpHeaders headers = defaultHeaders(transId);
97         headers.setAccept(Collections.singletonList(mediaType));
98         headers.setContentType(mediaType);
99         HttpEntity<T> entity = new HttpEntity<>(payload, headers);
100
101         return restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
102     }
103
104     /**
105      * Send a DELETE request to the A&AI.
106      *
107      * @param url - the url
108      * @param resourceVersion - the resource-version of the model to delete
109      * @param transId - transaction ID
110      * @return ClientResponse
111      */
112     public ResponseEntity<String> deleteResource(String url, String resourceVersion, String transId) {
113         HttpHeaders headers = defaultHeaders(transId);
114         String uri = url + "?" + RESOURCE_VERSION_PARAM + "=" + resourceVersion;
115         HttpEntity<String> entity = new HttpEntity<>(headers);
116         return restTemplate.exchange(uri, HttpMethod.DELETE, entity, String.class);
117     }
118
119     /**
120      * Does a GET on a resource to retrieve the resource version, and then DELETE that version.
121      *
122      * @param url - the url
123      * @param transId - transaction ID
124      * @return ClientResponse
125      */
126     public ResponseEntity<?> getAndDeleteResource(String url, String transId) {
127         ResponseEntity<AaiResourcesObject> response = getResource(url, transId, MediaType.APPLICATION_XML, AaiResourcesObject.class);
128         // First, GET the model
129         if (response == null || response.getStatusCode() != HttpStatus.OK) {
130             return response;
131         }
132
133         return deleteResource(url, response.getBody().getResourceVersion(), transId);
134     }
135
136
137     private boolean useBasicAuth() {
138         return config.getAaiAuthenticationUser() != null && config.getAaiAuthenticationPassword() != null;
139     }
140
141     /**
142      * Create the HTTP headers required for an A&AI operation (GET/POST/PUT/DELETE)
143      * 
144      * @param transId
145      * @return map of headers
146      */
147     private HttpHeaders defaultHeaders(String transId) {
148         HttpHeaders headers = new HttpHeaders();
149         headers.set(AaiRestClient.HEADER_TRANS_ID, transId);
150         headers.set(AaiRestClient.HEADER_FROM_APP_ID, AaiRestClient.ML_APP_NAME);
151         if (useBasicAuth()) {
152             headers.setBasicAuth(config.getAaiAuthenticationUser(), config.getAaiAuthenticationPassword());
153         }
154         return headers;
155     }
156 }