c69c4ca524dd4ec6edcbfb7e4bac215a89c148ae
[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;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.UUID;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.function.Consumer;
28 import org.apache.commons.collections4.CollectionUtils;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.notification.config.ConfigurationManager;
32 import org.openecomp.sdc.notification.types.NotificationsStatusDto;
33
34 public class NotificationWorker {
35
36     private static final int DEFAULT_POLLING_INTERVAL = 2000;
37     private static final String POLLING_INTERVAL = "pollingIntervalMsec";
38     private static final int DEFAULT_SELECTION_LIMIT = 10;
39     private static final String SELECTION_SIZE = "selectionSize";
40     private static final Logger LOGGER = LoggerFactory.getLogger(NotificationWorker.class);
41     private static boolean stopRunning = false;
42     private static Map<String, NotificationReceiver> activeUsers = new ConcurrentHashMap<>();
43     private int selectionLimit = DEFAULT_SELECTION_LIMIT;
44     private int pollingSleepInterval = DEFAULT_POLLING_INTERVAL;
45     private NewNotificationsReader news = null;
46
47     public NotificationWorker(NewNotificationsReader news) {
48         ConfigurationManager cm = ConfigurationManager.getInstance();
49         pollingSleepInterval = cm.getConfigValue(POLLING_INTERVAL, DEFAULT_POLLING_INTERVAL);
50         selectionLimit = cm.getConfigValue(SELECTION_SIZE, DEFAULT_SELECTION_LIMIT);
51         Objects.requireNonNull(news, "NotificationNews object is not initialized.");
52         this.news = news;
53         NotificationWorker.Poller p = new Poller();
54         Thread thread = new Thread(p);
55         thread.start();
56     }
57
58     public Map<String, NotificationReceiver> getActiveUsers() {
59         return activeUsers;
60     }
61
62     public void register(String ownerId, UUID lastDelivered, Consumer<NotificationsStatusDto> notesProcessor) {
63         NotificationReceiver receiver = new NotificationReceiver(ownerId, lastDelivered, notesProcessor);
64         activeUsers.put(ownerId, receiver);
65         LOGGER.debug("User {} is registered with eventId: {}", ownerId, receiver.getLastEventId());
66     }
67
68     public void unregister(String ownerId) {
69         activeUsers.remove(ownerId);
70         LOGGER.debug("User {} is unregistered.", ownerId);
71     }
72
73     public void stopPolling() {
74         LOGGER.debug("Stop notification polling.");
75         stopRunning = true;
76     }
77
78     public class Poller extends Thread {
79
80         @Override
81         public void run() {
82             try {
83                 while (!stopRunning) {
84                     pollNotifications();
85                     Thread.sleep(pollingSleepInterval);
86                 }
87             } catch (InterruptedException e) {
88                 LOGGER.error("Interrupted Exception during Notification poller launch.", e);
89                 Thread.currentThread().interrupt();
90             }
91         }
92
93         private void pollNotifications() {
94             Map<String, NotificationReceiver> currUsers = new HashMap<>();
95             currUsers.putAll(getActiveUsers());
96             for (NotificationReceiver receiver : currUsers.values()) {
97                 String ownerId = receiver.getOwnerId();
98                 UUID eventId = receiver.getLastEventId();
99                 NotificationsStatusDto status = news.getNewNotifications(ownerId, eventId, selectionLimit);
100                 if (Objects.nonNull(status) && CollectionUtils.isNotEmpty(status.getNotifications())) {
101                     receiver.setLastEventId(status.getLastScanned());
102                     receiver.getNotesProcessor().accept(status);
103                 }
104             }
105         }
106     }
107 }