2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.toggleAlarmFilter;
20 import java.util.Map.Entry;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.TimeUnit;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class NotificationDelayFilter<T extends ToggleAlarmFilterable> implements AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(NotificationDelayFilter.class);
33 private static long delay;
34 private static boolean enabled;
36 private final ConcurrentHashMap<String, NotificationWithServerTimeStamp<T>> problemItems;
37 // private final HashMap<String, NotificationWithServerTimeStamp<T>> nonProblemItems;
38 private final NotificationDelayedListener<T> timeoutListener;
40 private final ScheduledExecutorService scheduler;
41 private final Runnable timerRunner = () -> onTick();
42 private final String nodeName;
44 public NotificationDelayFilter(String nodeName, NotificationDelayedListener<T> timeoutListener) {
45 this.nodeName = nodeName;
46 this.timeoutListener = timeoutListener;
47 this.problemItems = new ConcurrentHashMap<>();
48 this.scheduler = Executors.newScheduledThreadPool(1);
52 public static void setDelay(long l) {
53 NotificationDelayFilter.delay = l;
56 public static long getDelay() {
57 return NotificationDelayFilter.delay;
60 public static boolean isEnabled() {
61 return NotificationDelayFilter.enabled;
64 public static void setEnabled(boolean enabled) {
65 NotificationDelayFilter.enabled = enabled;
69 * If process the notification
71 * @return true if other processing is required, false if not
73 public boolean processNotification(@NonNull T notificationXml) {
74 // ToggleAlarmFilter functionality
75 if (NotificationDelayFilter.isEnabled()) {
76 if (notificationXml.isCleared()) {
77 clearAlarmNotification(notificationXml);
79 pushAlarmNotification(notificationXml);
85 // end of ToggleAlarmFilter
89 * Push notification with a specific severity (everything except non-alarmed)
91 * @param notification related notification
93 public void pushAlarmNotification(@NonNull T notification) {
94 synchronized (problemItems) {
95 String problemName = notification.getUuidForMountpoint();
96 boolean cp = this.problemItems.containsKey(problemName);
98 // no alarm in entries => create entry and push the alarm currently
99 NotificationWithServerTimeStamp<T> item = new NotificationWithServerTimeStamp<>(notification);
100 LOG.debug("add event into list for node " + this.nodeName + " for alarm " + problemName + ": "
102 this.problemItems.put(problemName, item);
103 if (this.timeoutListener != null) {
104 this.timeoutListener.onNotificationDelay(this.nodeName, notification);
107 LOG.debug("clear contra event for node " + this.nodeName + " for alarm " + problemName);
108 this.problemItems.get(problemName).clrContraEvent();
115 * Push notification with severity non-alarmed
117 * @param notification related notification
119 public void clearAlarmNotification(@NonNull T notification) {
120 synchronized (problemItems) {
121 String problemName = notification.getUuidForMountpoint();
122 boolean cp = this.problemItems.containsKey(problemName);
124 LOG.debug("set contra event for alarm " + problemName);
125 this.problemItems.get(problemName).setContraEvent(notification);
127 // not in list => push directly through
128 if (this.timeoutListener != null) {
129 this.timeoutListener.onNotificationDelay(this.nodeName, notification);
135 private void startTimer() {
136 scheduler.scheduleAtFixedRate(timerRunner, 0, 1, TimeUnit.SECONDS);
139 private void stopTimer() {
140 scheduler.shutdown();
144 * check for clearing item out of the list
146 private void onTick() {
147 long now = System.currentTimeMillis();
150 synchronized (problemItems) {
152 for (Entry<String, NotificationWithServerTimeStamp<T>> entry : problemItems.entrySet()) {
153 NotificationWithServerTimeStamp<T> value = entry.getValue();
154 if (value.isStable(now)) {
155 // send contra Alarm if exists
156 if (value.getContraAlarmNotification() != null) {
157 if (this.timeoutListener != null) {
158 this.timeoutListener.onNotificationDelay(this.nodeName,
159 value.getContraAlarmNotification());
162 problemItems.remove(entry.getKey());
163 LOG.debug("removing entry for " + this.nodeName + " for alarm " + entry.getKey());
165 LOG.trace("currently state is still unstable for alarm " + entry.getKey());
170 } catch (Exception e) {
171 //Prevent stopping the task
172 LOG.warn("Exception during NotificationDelayFilter Task", e);
177 public void close() throws Exception {