c88ac4ecce3206cd656fd5d26c48ed5e70dc6242
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdcrests.item.rest.services.catalog.notification.http;
18
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;
21
22 import java.io.UnsupportedEncodingException;
23 import java.util.Collection;
24 import java.util.concurrent.Callable;
25 import lombok.Getter;
26 import lombok.NoArgsConstructor;
27 import lombok.Setter;
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;
43
44 /**
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.
47  *
48  * @author evitaliy
49  * @since 21 Nov 2018
50  */
51 @ToString
52 class HttpNotificationTask implements Callable<AsyncNotifier.NextAction> {
53
54     private static final Logger LOGGER = LoggerFactory.getLogger(HttpNotificationTask.class);
55
56     private static final String APPLICATION_JSON = ContentType.APPLICATION_JSON.getMimeType();
57     private static final String USER_ID_HEADER_PARAM = "USER_ID";
58
59     private final String endpoint;
60     private final String userId;
61     private volatile Collection<String> itemIds;
62
63     HttpNotificationTask(String endpoint, String userId, Collection<String> itemIds) {
64         this.endpoint = endpoint;
65         this.userId = userId;
66         this.itemIds = itemIds;
67     }
68
69     @Override
70     public synchronized AsyncNotifier.NextAction call() {
71
72         try (CloseableHttpClient client = HttpClients.createDefault()) {
73
74             HttpPost request = createPostRequest(endpoint, itemIds, userId);
75
76             try (CloseableHttpResponse response = client.execute(request)) {
77
78                 StatusLine status = response.getStatusLine();
79
80                 LOGGER.debug("Catalog notification on VSP IDs: {}, endpoint: {}, response: {}",
81                         itemIds, endpoint, status);
82
83                 itemIds = getFailedIds(itemIds, response.getEntity());
84
85                 if ((status.getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR)
86                             && (itemIds != null) && !itemIds.isEmpty()) {
87
88                     LOGGER.debug("Catalog notification on VSP IDs {} failed. Endpoint: {}. Retry", itemIds, endpoint);
89                     return RETRY;
90                 }
91
92                 return DONE;
93             }
94
95         } catch (Exception e) {
96             LOGGER.error("Catalog notification on VSP IDs {} failed. Endpoint: {}", itemIds, endpoint, e);
97             return DONE;
98         }
99     }
100
101     private HttpPost createPostRequest(String postUrl, Collection<String> itemIds, String userId)
102             throws UnsupportedEncodingException {
103
104         HttpPost request = new HttpPost(postUrl);
105
106         request.addHeader(HttpHeaders.ACCEPT, APPLICATION_JSON);
107         request.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
108         request.addHeader(USER_ID_HEADER_PARAM, userId);
109
110         HttpEntity entity = new StringEntity(JsonUtil.object2Json(itemIds));
111         request.setEntity(entity);
112         return request;
113     }
114
115     private Collection<String> getFailedIds(Collection<String> itemIds, HttpEntity responseBody) {
116
117         try {
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);
122         }
123
124         return itemIds;
125     }
126
127     @Setter
128     @Getter
129     @ToString
130     @NoArgsConstructor
131     private static class NotificationResponse {
132
133         private Collection<String> failedIds;
134     }
135 }