Switched from Dropwizard to Springboot
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / dmaap / SubscriberAction.java
1 /*
2  * Copyright 2017 - 2022 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 jakarta.annotation.PreDestroy;
19 import lombok.extern.slf4j.Slf4j;
20 import org.onap.holmes.common.database.DbDaoUtil;
21 import org.onap.holmes.dsa.dmaappolling.Subscriber;
22 import org.onap.holmes.engine.db.AlarmInfoDaoService;
23 import org.onap.holmes.engine.db.jdbi.AlarmInfoDao;
24 import org.onap.holmes.engine.manager.DroolsEngine;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Component;
27
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31
32 @Component
33 @Slf4j
34 public class SubscriberAction {
35
36     @Autowired
37     private DroolsEngine droolsEngine;
38     @Autowired
39     private DbDaoUtil daoUtil;
40     @Autowired
41     private AlarmInfoDaoService alarmInfoDaoService;
42
43     private HashMap<String, DMaaPAlarmPolling> pollingTasks = new HashMap<>();
44
45     public synchronized void addSubscriber(Subscriber subscriber) {
46         String topic = subscriber.getTopic();
47         if (topic != null) {
48             if (pollingTasks.containsKey(topic)) {
49                 removeSubscriber(subscriber);
50             }
51             AlarmInfoDao alarmInfoDao = daoUtil.getJdbiDaoByOnDemand(AlarmInfoDao.class);
52             DMaaPAlarmPolling pollingTask = new DMaaPAlarmPolling(subscriber, droolsEngine, alarmInfoDaoService);
53             Thread thread = new Thread(pollingTask);
54             thread.start();
55             pollingTasks.put(topic, pollingTask);
56             log.info("Subscribed to topic: " + subscriber.getUrl());
57         } else {
58             log.info("The topic is null. Operation aborted.");
59         }
60     }
61
62     public synchronized void removeSubscriber(Subscriber subscriber) {
63         String topic = subscriber.getTopic();
64         if (topic != null && pollingTasks.containsKey(topic)) {
65             pollingTasks.get(topic).stopTask();
66             pollingTasks.remove(topic);
67         }
68         log.info("Topic unsubscribed: " + subscriber.getUrl());
69     }
70
71     @PreDestroy
72     public void stopPollingTasks() {
73         Iterator iterator = pollingTasks.entrySet().iterator();
74         while (iterator.hasNext()) {
75             Map.Entry entry = (Map.Entry) iterator.next();
76             String key = (String) entry.getKey();
77             pollingTasks.get(key).stopTask();
78         }
79         pollingTasks.clear();
80     }
81 }