fecdd3b533dce5cbd0295b29cf693cbc0b887eeb
[ccsdk/features.git] /
1 /*******************************************************************************
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
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * 
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.toggleAlarmFilter;
19
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.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class NotificationDelayFilter<T> implements AutoCloseable {
29
30     private static final Logger LOG = LoggerFactory.getLogger(NotificationDelayFilter.class);
31
32     private final ConcurrentHashMap <String, NotificationWithServerTimeStamp<T>> problemItems;
33 //    private final HashMap<String, NotificationWithServerTimeStamp<T>> nonProblemItems;
34     private final NotificationDelayedListener<T> timeoutListener;
35
36     private static long delay;
37     private static boolean enabled;
38
39     public static void setDelay(long l) {
40         NotificationDelayFilter.delay = l;
41     }
42
43     public static long getDelay() {
44         return NotificationDelayFilter.delay;
45     }
46
47     public static boolean isEnabled() {
48         return NotificationDelayFilter.enabled;
49     }
50
51     public static void setEnabled(boolean enabled) {
52         NotificationDelayFilter.enabled = enabled;
53     }
54
55     private final ScheduledExecutorService scheduler;
56     private final Runnable timerRunner = () -> onTick();
57
58     private final String nodeName;
59
60     public NotificationDelayFilter(String nodeName, NotificationDelayedListener<T> timeoutListener) {
61         this.nodeName = nodeName;
62         this.timeoutListener = timeoutListener;
63         this.problemItems = new ConcurrentHashMap <>();
64         this.scheduler = Executors.newScheduledThreadPool(1);
65         this.startTimer();
66     }
67
68     /**
69      * Push notification with a specific severity (everything except non-alarmed)
70      * @param problemName key
71      * @param notification related notification
72      */
73     public void pushAlarmNotification(String problemName, T notification) {
74         synchronized (problemItems) {
75
76             boolean cp = this.problemItems.containsKey(problemName);
77             if (!cp) {
78                 // no alarm in entries => create entry and push the alarm currently
79                 NotificationWithServerTimeStamp<T> item = new NotificationWithServerTimeStamp<>(
80                         notification);
81                 LOG.debug("add event into list for node " + this.nodeName + " for alarm " + problemName + ": "
82                         + item.toString());
83                 this.problemItems.put(problemName, item);
84                 if (this.timeoutListener != null) {
85                     this.timeoutListener.onNotificationDelay(notification);
86                 }
87             } else {
88                 LOG.debug("clear contra event for node " + this.nodeName + " for alarm " + problemName);
89                 this.problemItems.get(problemName).clrContraEvent();
90             }
91
92         }
93     }
94
95     /**
96      * Push notification with severity non-alarmed
97      * @param problemName key
98      * @param notification related notification
99      */
100     public void clearAlarmNotification(String problemName, T notification) {
101         synchronized (problemItems) {
102
103             boolean cp = this.problemItems.containsKey(problemName);
104             if (cp) {
105                 LOG.debug("set contra event for alarm " + problemName);
106                 this.problemItems.get(problemName).setContraEvent(notification);
107             } else {
108                 // not in list => push directly through
109                 if (this.timeoutListener != null) {
110                     this.timeoutListener.onNotificationDelay(notification);
111                 }
112             }
113         }
114     }
115
116     private void startTimer() {
117         scheduler.scheduleAtFixedRate(timerRunner, 0, 1, TimeUnit.SECONDS);
118     }
119
120     private void stopTimer() {
121         scheduler.shutdown();
122     }
123
124     /**
125      * check for clearing item out of the list
126      */
127     private void onTick() {
128         long now = System.currentTimeMillis();
129         try {
130
131             synchronized (problemItems) {
132
133                 for (Entry<String, NotificationWithServerTimeStamp<T>> entry : problemItems
134                         .entrySet()) {
135                     NotificationWithServerTimeStamp<T> value = entry.getValue();
136                     if (value.isStable(now)) {
137                         // send contra Alarm if exists
138                         if (value.getContraAlarmNotification() != null) {
139                             if (this.timeoutListener != null) {
140                                 this.timeoutListener.onNotificationDelay(value.getContraAlarmNotification());
141                             }
142                         }
143                         problemItems.remove(entry.getKey());
144                         LOG.debug("removing entry for "+this.nodeName+" for alarm " + entry.getKey());
145                     } else {
146                         LOG.trace("currently state is still unstable for alarm " + entry.getKey());
147                     }
148                 }
149
150             }
151         } catch (Exception e) {
152             //Prevent stopping the task
153             LOG.warn("Exception during NotificationDelayFilter Task", e);
154         }
155     }
156
157     @Override
158     public void close() throws Exception {
159         this.stopTimer();
160     }
161
162 }