update link to upper-constraints.txt
[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.AlarmInfoDaoService;
25 import org.onap.holmes.engine.manager.DroolsEngine;
26
27 import java.util.List;
28
29 @Slf4j
30 public class DMaaPAlarmPolling implements Runnable {
31
32     private Subscriber subscriber;
33     private DroolsEngine droolsEngine;
34     private volatile boolean isAlive = true;
35     private AlarmInfoDaoService alarmInfoDaoService;
36
37
38     public DMaaPAlarmPolling(Subscriber subscriber, DroolsEngine droolsEngine, AlarmInfoDaoService alarmInfoDaoService) {
39         this.subscriber = subscriber;
40         this.droolsEngine = droolsEngine;
41         this.alarmInfoDaoService = alarmInfoDaoService;
42     }
43
44     public void run() {
45         while (isAlive) {
46             List<VesAlarm> vesAlarmList;
47             try {
48                 vesAlarmList = subscriber.subscribe();
49                 vesAlarmList.forEach(vesAlarm -> {
50                     try {
51                         AlarmInfo alarmInfo = getAlarmInfo(vesAlarm);
52                         if (alarmInfo.getAlarmIsCleared() != 1) {
53                             alarmInfoDaoService.saveAlarm(alarmInfo);
54                         } else {
55                             alarmInfoDaoService.deleteAlarm(alarmInfo);
56                         }
57                         droolsEngine.putRaisedIntoStream(vesAlarm);
58
59                     } catch (AlarmInfoException e) {
60                         log.error("Failed to save alarm to database", e);
61                     }
62                 });
63             } catch (CorrelationException e) {
64                 log.error("Failed to process alarms. Sleep for 60 seconds to restart.", e);
65                 try {
66                     Thread.sleep(60000);
67                 } catch (InterruptedException e1) {
68                     log.info("Thread is still active.", e);
69                     Thread.currentThread().interrupt();
70                 }
71             } catch (Exception e) {
72                 log.error("An error occurred while processing alarm. Sleep for 60 seconds to restart.", e);
73                 try {
74                     Thread.sleep(60000);
75                 } catch (InterruptedException e1) {
76                     log.info("Thread is still active.", e);
77                     Thread.currentThread().interrupt();
78                 }
79             }
80         }
81     }
82
83     private AlarmInfo getAlarmInfo(VesAlarm vesAlarm) {
84         AlarmInfo alarmInfo = new AlarmInfo();
85         alarmInfo.setAlarmIsCleared(vesAlarm.getAlarmIsCleared());
86         alarmInfo.setSourceName(vesAlarm.getSourceName());
87         alarmInfo.setSourceId(vesAlarm.getSourceId());
88         alarmInfo.setStartEpochMicroSec(vesAlarm.getStartEpochMicrosec());
89         alarmInfo.setLastEpochMicroSec(vesAlarm.getLastEpochMicrosec());
90         alarmInfo.setEventId(vesAlarm.getEventId());
91         alarmInfo.setEventName(vesAlarm.getEventName());
92         alarmInfo.setRootFlag(vesAlarm.getRootFlag());
93         return alarmInfo;
94     }
95
96     public void stopTask() {
97         isAlive = false;
98     }
99 }