2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.modelloader.restclient;
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;
39 * Wrapper around the standard A&AI Rest Client interface. This currently uses Jersey client 1.x
43 public class AaiRestClient {
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;
53 public AaiRestClient(ModelLoaderConfig config, RestTemplate restTemplate) {
55 this.restTemplate = restTemplate;
59 * Send a GET request to the A&AI for a resource.
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);
72 return restTemplate.exchange(url, HttpMethod.GET, entity, responseType);
76 * Send a PUT request to the A&AI.
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
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);
91 return restTemplate.exchange(url, HttpMethod.PUT, entity, responseType);
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);
101 return restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
105 * Send a DELETE request to the A&AI.
107 * @param url - the url
108 * @param resourceVersion - the resource-version of the model to delete
109 * @param transId - transaction ID
110 * @return ClientResponse
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);
120 * Does a GET on a resource to retrieve the resource version, and then DELETE that version.
122 * @param url - the url
123 * @param transId - transaction ID
124 * @return ClientResponse
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) {
133 return deleteResource(url, response.getBody().getResourceVersion(), transId);
137 private boolean useBasicAuth() {
138 return config.getAaiAuthenticationUser() != null && config.getAaiAuthenticationPassword() != null;
142 * Create the HTTP headers required for an A&AI operation (GET/POST/PUT/DELETE)
145 * @return map of headers
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());