7f2390b38ad83d189d8c0a53d5103fea3b3dbd3d
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.notification.workers.impl;
21
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.io.InputStreamReader;
24 import java.util.UUID;
25 import javax.ws.rs.core.HttpHeaders;
26 import javax.ws.rs.core.MediaType;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.client.HttpClient;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.impl.client.HttpClientBuilder;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.notification.config.ConfigurationManager;
34 import org.openecomp.sdc.notification.types.NotificationsStatusDto;
35 import org.openecomp.sdc.notification.workers.NewNotificationsReader;
36
37 public class NewNotificationsReaderRestImpl implements NewNotificationsReader {
38
39     private static final String USER_ID_HEADER_PARAM = "USER_ID";
40     private static final String LAST_DELIVERED_QUERY_PARAM = "LAST_DELIVERED_EVENT_ID";
41     private static final String LIMIT_QUERY_PARAM = "NOTIFICATION_ROWS_LIMIT";
42     private static final String BE_HOST = "beHost";
43     private static final String BE_PORT = "beHttpPort";
44     private static final String DEFAULT_BE_HOST = "localhost";
45     private static final int DEFAULT_BE_PORT = 8080;
46     private static final String URL = "http://%s:%d/onboarding-api/v1.0/notifications/worker?";
47     private static final ObjectMapper mapper = new ObjectMapper();
48     private static final Logger LOGGER = LoggerFactory.getLogger(NewNotificationsReaderRestImpl.class);
49     private static String beHost;
50     private static int bePort;
51
52     public NewNotificationsReaderRestImpl() {
53         ConfigurationManager cm = ConfigurationManager.getInstance();
54         bePort = cm.getConfigValue(BE_PORT, DEFAULT_BE_PORT);
55         beHost = cm.getConfigValue(BE_HOST, DEFAULT_BE_HOST);
56     }
57
58     public NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit) {
59         HttpClient client = HttpClientBuilder.create().build();
60         String url = String.format(URL, beHost, bePort);
61         url = url + LIMIT_QUERY_PARAM + "=" + limit;
62         if (eventId != null) {
63             url = url + "&" + LAST_DELIVERED_QUERY_PARAM + "=" + eventId;
64         }
65         HttpGet request = new HttpGet(url);
66         request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
67         request.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
68         request.addHeader(USER_ID_HEADER_PARAM, ownerId);
69         try {
70             HttpResponse response = client.execute(request);
71             return mapper.readValue(new InputStreamReader(response.getEntity().getContent()), NotificationsStatusDto.class);
72         } catch (Exception e) {
73             LOGGER.error("Failed to execute the request {}", url, e);
74             return null;
75         }
76     }
77 }