2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdcrests.item.rest.services.catalog.notification.http;
19 import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.DONE;
20 import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.RETRY;
22 import java.io.UnsupportedEncodingException;
23 import java.util.Collection;
24 import java.util.concurrent.Callable;
26 import lombok.NoArgsConstructor;
28 import lombok.ToString;
29 import org.apache.http.HttpEntity;
30 import org.apache.http.HttpHeaders;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.StatusLine;
33 import org.apache.http.client.methods.CloseableHttpResponse;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.entity.ContentType;
36 import org.apache.http.entity.StringEntity;
37 import org.apache.http.impl.client.CloseableHttpClient;
38 import org.apache.http.impl.client.HttpClients;
39 import org.openecomp.core.utilities.json.JsonUtil;
40 import org.openecomp.sdc.logging.api.Logger;
41 import org.openecomp.sdc.logging.api.LoggerFactory;
42 import org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier;
45 * HTTP client for notifying the Catalog of an action on items. The items are referenced by their IDs. The client can
46 * run multiple times, in which case only failed IDs will be re-attempted.
52 class HttpNotificationTask implements Callable<AsyncNotifier.NextAction> {
54 private static final Logger LOGGER = LoggerFactory.getLogger(HttpNotificationTask.class);
56 private static final String APPLICATION_JSON = ContentType.APPLICATION_JSON.getMimeType();
57 private static final String USER_ID_HEADER_PARAM = "USER_ID";
59 private final String endpoint;
60 private final String userId;
61 private volatile Collection<String> itemIds;
63 HttpNotificationTask(String endpoint, String userId, Collection<String> itemIds) {
64 this.endpoint = endpoint;
66 this.itemIds = itemIds;
70 public synchronized AsyncNotifier.NextAction call() {
72 try (CloseableHttpClient client = HttpClients.createDefault()) {
74 HttpPost request = createPostRequest(endpoint, itemIds, userId);
76 try (CloseableHttpResponse response = client.execute(request)) {
78 StatusLine status = response.getStatusLine();
80 LOGGER.debug("Catalog notification on VSP IDs: {}, endpoint: {}, response: {}",
81 itemIds, endpoint, status);
83 itemIds = getFailedIds(itemIds, response.getEntity());
85 if ((status.getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR)
86 && (itemIds != null) && !itemIds.isEmpty()) {
88 LOGGER.debug("Catalog notification on VSP IDs {} failed. Endpoint: {}. Retry", itemIds, endpoint);
95 } catch (Exception e) {
96 LOGGER.error("Catalog notification on VSP IDs {} failed. Endpoint: {}", itemIds, endpoint, e);
101 private HttpPost createPostRequest(String postUrl, Collection<String> itemIds, String userId)
102 throws UnsupportedEncodingException {
104 HttpPost request = new HttpPost(postUrl);
106 request.addHeader(HttpHeaders.ACCEPT, APPLICATION_JSON);
107 request.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
108 request.addHeader(USER_ID_HEADER_PARAM, userId);
110 HttpEntity entity = new StringEntity(JsonUtil.object2Json(itemIds));
111 request.setEntity(entity);
115 private Collection<String> getFailedIds(Collection<String> itemIds, HttpEntity responseBody) {
118 NotificationResponse response = JsonUtil.json2Object(responseBody.getContent(), NotificationResponse.class);
119 return response != null ? response.failedIds : null;
120 } catch (Exception e) {
121 LOGGER.error("Error getting failed IDs from response", e);
131 private static class NotificationResponse {
133 private Collection<String> failedIds;