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