2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.notification.workers;
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;
29 import java.util.HashMap;
31 import java.util.Objects;
32 import java.util.UUID;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.function.Consumer;
36 public class NotificationWorker {
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";
43 private static boolean stopRunning = false;
45 private int selectionLimit = DEFAULT_SELECTION_LIMIT;
46 private int pollingSleepInterval = DEFAULT_POLLING_INTERVAL;
48 private static final Logger LOGGER = LoggerFactory.getLogger(NotificationWorker.class);
50 private static Map<String, NotificationReceiver> activeUsers = new ConcurrentHashMap<>();
51 private NewNotificationsReader news = null;
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);
58 Objects.requireNonNull(news, "NotificationNews object is not initialized.");
61 NotificationWorker.Poller p = new Poller();
62 Thread thread = new Thread(p);
66 public Map<String, NotificationReceiver> getActiveUsers() {
70 public class Poller extends Thread {
73 while (!stopRunning) {
75 Thread.sleep(pollingSleepInterval);
78 catch (InterruptedException e) {
79 LOGGER.error("Interrupted Exception during Notification poller launch.", e);
83 private void pollNotifications() {
85 Map<String, NotificationReceiver> currUsers = new HashMap<>();
86 currUsers.putAll(getActiveUsers());
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);
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());
107 public void unregister(String ownerId) {
108 activeUsers.remove(ownerId);
109 LOGGER.debug("User {} is unregistered.", ownerId);
112 public void stopPolling() {
113 LOGGER.debug("Stop notification polling.");