Remove Alarm Info from DB when alarms are cleared
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / dmaap / DMaaPAlarmPolling.java
1 /*
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.engine.dmaap;
17
18 import lombok.extern.slf4j.Slf4j;
19 import org.onap.holmes.common.api.entity.AlarmInfo;
20 import org.onap.holmes.common.api.stat.VesAlarm;
21 import org.onap.holmes.common.exception.AlarmInfoException;
22 import org.onap.holmes.common.exception.CorrelationException;
23 import org.onap.holmes.dsa.dmaappolling.Subscriber;
24 import org.onap.holmes.engine.db.AlarmInfoDao;
25 import org.onap.holmes.engine.manager.DroolsEngine;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 @Slf4j
31 public class DMaaPAlarmPolling implements Runnable {
32
33     private Subscriber subscriber;
34     private DroolsEngine droolsEngine;
35     private volatile boolean isAlive = true;
36     private AlarmInfoDao alarmInfoDao;
37
38
39     public DMaaPAlarmPolling(Subscriber subscriber, DroolsEngine droolsEngine, AlarmInfoDao alarmInfoDao) {
40         this.subscriber = subscriber;
41         this.droolsEngine = droolsEngine;
42         this.alarmInfoDao = alarmInfoDao;
43     }
44
45     public void run() {
46         while (isAlive) {
47             List<VesAlarm> vesAlarmList = new ArrayList<>();
48             try {
49                 vesAlarmList = subscriber.subscribe();
50                 vesAlarmList.forEach(vesAlarm -> {
51                     try {
52                         AlarmInfo alarmInfo = getAlarmInfo(vesAlarm);
53                         if (alarmInfo.getAlarmIsCleared() != 1) {
54                             alarmInfoDao.saveAlarm(alarmInfo);
55                         } else {
56                             alarmInfoDao.deleteAlarm(alarmInfo);
57                         }
58                         droolsEngine.putRaisedIntoStream(vesAlarm);
59
60                     } catch (AlarmInfoException e) {
61                         log.error("Failed to save alarm to database", e);
62                     }
63                 });
64             } catch (CorrelationException e) {
65                 log.error("Failed to process alarms. Sleep for 60 seconds to restart.", e);
66                 try {
67                     Thread.sleep(60000);
68                 } catch (InterruptedException e1) {
69                     log.info("Thread is still active.", e);
70                     Thread.currentThread().interrupt();
71                 }
72             } catch (Exception e) {
73                 log.error("An error occurred while processing alarm. Sleep for 60 seconds to restart.", e);
74                 try {
75                     Thread.sleep(60000);
76                 } catch (InterruptedException e1) {
77                     log.info("Thread is still active.", e);
78                     Thread.currentThread().interrupt();
79                 }
80             }
81         }
82     }
83
84     private AlarmInfo getAlarmInfo(VesAlarm vesAlarm) {
85         AlarmInfo alarmInfo = new AlarmInfo();
86         alarmInfo.setAlarmIsCleared(vesAlarm.getAlarmIsCleared());
87         alarmInfo.setSourceName(vesAlarm.getSourceName());
88         alarmInfo.setSourceId(vesAlarm.getSourceId());
89         alarmInfo.setStartEpochMicroSec(vesAlarm.getStartEpochMicrosec());
90         alarmInfo.setLastEpochMicroSec(vesAlarm.getLastEpochMicrosec());
91         alarmInfo.setEventId(vesAlarm.getEventId());
92         alarmInfo.setEventName(vesAlarm.getEventName());
93         alarmInfo.setRootFlag(vesAlarm.getRootFlag());
94         return alarmInfo;
95     }
96
97     public void stopTask() {
98         isAlive = false;
99     }
100 }