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=========================================================
20 package org.openecomp.sdc.notification.workers;
22 import java.util.HashMap;
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;
34 public class NotificationWorker {
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;
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.");
53 NotificationWorker.Poller p = new Poller();
54 Thread thread = new Thread(p);
58 public Map<String, NotificationReceiver> getActiveUsers() {
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());
68 public void unregister(String ownerId) {
69 activeUsers.remove(ownerId);
70 LOGGER.debug("User {} is unregistered.", ownerId);
73 public void stopPolling() {
74 LOGGER.debug("Stop notification polling.");
78 public class Poller extends Thread {
83 while (!stopRunning) {
85 Thread.sleep(pollingSleepInterval);
87 } catch (InterruptedException e) {
88 LOGGER.error("Interrupted Exception during Notification poller launch.", e);
89 Thread.currentThread().interrupt();
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);