0d3b8bcb8f8fe1d090f49bf4d9c74c4c97b96c68
[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.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.eclipse.jdt.annotation.NonNull;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NotificationDelayFilter<T extends ToggleAlarmFilterable> implements AutoCloseable {
30
31     private static final Logger LOG = LoggerFactory.getLogger(NotificationDelayFilter.class);
32
33     private static long delay;
34     private static boolean enabled;
35
36     private final ConcurrentHashMap <String, NotificationWithServerTimeStamp<T>> problemItems;
37 //    private final HashMap<String, NotificationWithServerTimeStamp<T>> nonProblemItems;
38     private final NotificationDelayedListener<T> timeoutListener;
39
40     private final ScheduledExecutorService scheduler;
41     private final Runnable timerRunner = () -> onTick();
42     private final String nodeName;
43
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);
49         this.startTimer();
50     }
51
52     public static void setDelay(long l) {
53         NotificationDelayFilter.delay = l;
54     }
55
56     public static long getDelay() {
57         return NotificationDelayFilter.delay;
58     }
59
60     public static boolean isEnabled() {
61         return NotificationDelayFilter.enabled;
62     }
63
64     public static void setEnabled(boolean enabled) {
65         NotificationDelayFilter.enabled = enabled;
66     }
67
68     /**
69      * If process the notification
70      * @return true if other processing is required, false if not
71      */
72     public boolean processNotification(@NonNull T notificationXml) {
73         // ToggleAlarmFilter functionality
74         if (NotificationDelayFilter.isEnabled()) {
75             if (notificationXml.isCleared()) {
76                 clearAlarmNotification(notificationXml);
77             } else {
78                 pushAlarmNotification(notificationXml);
79             }
80             return false;
81         } else {
82             return true;
83         }
84         // end of ToggleAlarmFilter
85     }
86
87     /**
88      * Push notification with a specific severity (everything except non-alarmed)
89      * @param notification related notification
90      */
91     public void pushAlarmNotification(@NonNull T notification) {
92         synchronized (problemItems) {
93             String problemName = notification.getUuidForMountpoint();
94             boolean cp = this.problemItems.containsKey(problemName);
95             if (!cp) {
96                 // no alarm in entries => create entry and push the alarm currently
97                 NotificationWithServerTimeStamp<T> item = new NotificationWithServerTimeStamp<>(
98                         notification);
99                 LOG.debug("add event into list for node " + this.nodeName + " for alarm " + problemName + ": "
100                         + item.toString());
101                 this.problemItems.put(problemName, item);
102                 if (this.timeoutListener != null) {
103                     this.timeoutListener.onNotificationDelay(this.nodeName,notification);
104                 }
105             } else {
106                 LOG.debug("clear contra event for node " + this.nodeName + " for alarm " + problemName);
107                 this.problemItems.get(problemName).clrContraEvent();
108             }
109
110         }
111     }
112
113     /**
114      * Push notification with severity non-alarmed
115      * @param notification related notification
116      */
117     public void clearAlarmNotification(@NonNull T notification) {
118         synchronized (problemItems) {
119             String problemName = notification.getUuidForMountpoint();
120             boolean cp = this.problemItems.containsKey(problemName);
121             if (cp) {
122                 LOG.debug("set contra event for alarm " + problemName);
123                 this.problemItems.get(problemName).setContraEvent(notification);
124             } else {
125                 // not in list => push directly through
126                 if (this.timeoutListener != null) {
127                     this.timeoutListener.onNotificationDelay(this.nodeName,notification);
128                 }
129             }
130         }
131     }
132
133     private void startTimer() {
134         scheduler.scheduleAtFixedRate(timerRunner, 0, 1, TimeUnit.SECONDS);
135     }
136
137     private void stopTimer() {
138         scheduler.shutdown();
139     }
140
141     /**
142      * check for clearing item out of the list
143      */
144     private void onTick() {
145         long now = System.currentTimeMillis();
146         try {
147
148             synchronized (problemItems) {
149
150                 for (Entry<String, NotificationWithServerTimeStamp<T>> entry : problemItems
151                         .entrySet()) {
152                     NotificationWithServerTimeStamp<T> value = entry.getValue();
153                     if (value.isStable(now)) {
154                         // send contra Alarm if exists
155                         if (value.getContraAlarmNotification() != null) {
156                             if (this.timeoutListener != null) {
157                                 this.timeoutListener.onNotificationDelay(this.nodeName,value.getContraAlarmNotification());
158                             }
159                         }
160                         problemItems.remove(entry.getKey());
161                         LOG.debug("removing entry for "+this.nodeName+" for alarm " + entry.getKey());
162                     } else {
163                         LOG.trace("currently state is still unstable for alarm " + entry.getKey());
164                     }
165                 }
166
167             }
168         } catch (Exception e) {
169             //Prevent stopping the task
170             LOG.warn("Exception during NotificationDelayFilter Task", e);
171         }
172     }
173
174     @Override
175     public void close() throws Exception {
176         this.stopTimer();
177     }
178
179 }