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