Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-notification-lib / openecomp-sdc-notification-worker / src / main / java / org / openecomp / sdc / notification / workers / NotificationWorker.java
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                 public void run() {
72                         try {
73                                 while (!stopRunning) {
74                                         pollNotifications();
75                                         Thread.sleep(pollingSleepInterval);
76                                 }
77                         }
78                         catch (InterruptedException e) {
79                                 LOGGER.error("Interrupted Exception during Notification poller launch.", e);
80                         }
81                 }
82
83                 private void pollNotifications() {
84
85                         Map<String, NotificationReceiver> currUsers = new HashMap<>();
86                         currUsers.putAll(getActiveUsers());
87
88                         for (NotificationReceiver receiver : currUsers.values()) {
89                                 String ownerId = receiver.getOwnerId();
90                                 UUID eventId = receiver.getlastEventId();
91                                 NotificationsStatusDto status = news.getNewNotifications(ownerId, eventId, selectionLimit);
92                                 if(Objects.nonNull(status) && CollectionUtils.isNotEmpty(status.getNotifications())) {
93                                         receiver.setLastEventId(status.getLastScanned());
94                                         receiver.getNotesProcessor().accept(status);
95                                 }
96                         }
97                 }
98
99         }
100
101         public void register(String ownerId, UUID lastDelivered, Consumer<NotificationsStatusDto> notesProcessor) {
102                 NotificationReceiver receiver = new NotificationReceiver(ownerId, lastDelivered, notesProcessor);
103                 activeUsers.put(ownerId, receiver);
104                 LOGGER.debug("User {} is registered with eventId: {}", ownerId, receiver.getlastEventId());
105         }
106
107         public void unregister(String ownerId) {
108                 activeUsers.remove(ownerId);
109                 LOGGER.debug("User {} is unregistered.", ownerId);
110         }
111
112         public void stopPolling() {
113                 LOGGER.debug("Stop notification polling.");
114                 stopRunning = true;
115         }
116
117 }