862895baf08a0a1219c44f5abb240de19c8ae517
[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 static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import lombok.extern.slf4j.Slf4j;
23 import org.onap.holmes.common.api.stat.VesAlarm;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.dsa.dmaappolling.Subscriber;
26 import org.onap.holmes.engine.manager.DroolsEngine;
27
28 @Slf4j
29 public class DMaaPAlarmPolling implements Runnable {
30
31     private Subscriber subscriber;
32     private DroolsEngine droolsEngine;
33     private volatile boolean isAlive = true;
34
35     public DMaaPAlarmPolling(Subscriber subscriber, DroolsEngine droolsEngine) {
36         this.subscriber = subscriber;
37         this.droolsEngine = droolsEngine;
38     }
39
40     public void run() {
41         while (isAlive) {
42             List<VesAlarm> vesAlarmList = new ArrayList<>();
43             try {
44                 vesAlarmList = subscriber.subscribe();
45                 vesAlarmList.forEach(vesAlarm -> droolsEngine.putRaisedIntoStream(vesAlarm));
46             } catch (CorrelationException e) {
47                 log.error("Failed to process alarms. Sleep for 60 seconds to restart.", e);
48                 try {
49                     Thread.sleep(60000);
50                 } catch (InterruptedException e1) {
51                     log.info("Thread is still active.", e);
52                     Thread.currentThread().interrupt();
53                 }
54             } catch (Exception e) {
55                 log.error("An error occurred while processing alarm. Sleep for 60 seconds to restart.", e);
56                 try {
57                     Thread.sleep(60000);
58                 } catch (InterruptedException e1) {
59                     log.info("Thread is still active.", e);
60                     Thread.currentThread().interrupt();
61                 }
62             }
63         }
64     }
65
66     public void stopTask() {
67         isAlive = false;
68     }
69 }