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