785f98bf41cc14d7a008ab5241221ef60e65a918
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / tasks / KafkaTopicConsumers.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oran.dmaapadapter.tasks;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import lombok.Getter;
27
28 import org.oran.dmaapadapter.configuration.ApplicationConfig;
29 import org.oran.dmaapadapter.repository.InfoType;
30 import org.oran.dmaapadapter.repository.InfoTypes;
31 import org.oran.dmaapadapter.repository.Job;
32 import org.oran.dmaapadapter.repository.Jobs;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.scheduling.annotation.EnableScheduling;
37 import org.springframework.scheduling.annotation.Scheduled;
38 import org.springframework.stereotype.Component;
39
40 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
41 @Component
42 @EnableScheduling
43 public class KafkaTopicConsumers {
44     private static final Logger logger = LoggerFactory.getLogger(KafkaTopicConsumers.class);
45
46     private final Map<String, KafkaTopicListener> topicListeners = new HashMap<>();
47     @Getter
48     private final Map<String, KafkaJobDataConsumer> activeSubscriptions = new HashMap<>();
49
50     private static final int CONSUMER_SUPERVISION_INTERVAL_MS = 1000 * 60 * 3;
51
52     public KafkaTopicConsumers(@Autowired ApplicationConfig appConfig, @Autowired InfoTypes types,
53             @Autowired Jobs jobs) {
54
55         for (InfoType type : types.getAll()) {
56             if (type.isKafkaTopicDefined()) {
57                 KafkaTopicListener topicConsumer = new KafkaTopicListener(appConfig, type);
58                 topicListeners.put(type.getId(), topicConsumer);
59             }
60         }
61
62         jobs.addObserver(new Jobs.Observer() {
63             @Override
64             public void onJobbAdded(Job job) {
65                 addJob(job);
66             }
67
68             @Override
69             public void onJobRemoved(Job job) {
70                 removeJob(job);
71             }
72
73         });
74     }
75
76     public synchronized void addJob(Job job) {
77         if (this.activeSubscriptions.get(job.getId()) == null && job.getType().isKafkaTopicDefined()) {
78             logger.debug("Kafka job added {}", job.getId());
79             KafkaTopicListener topicConsumer = topicListeners.get(job.getType().getId());
80             KafkaJobDataConsumer subscription = new KafkaJobDataConsumer(topicConsumer.getOutput(), job);
81             subscription.start();
82             activeSubscriptions.put(job.getId(), subscription);
83         }
84     }
85
86     public synchronized void removeJob(Job job) {
87         KafkaJobDataConsumer d = activeSubscriptions.remove(job.getId());
88         if (d != null) {
89             logger.debug("Kafka job removed {}", job.getId());
90             d.stop();
91         }
92     }
93
94     @Scheduled(fixedRate = CONSUMER_SUPERVISION_INTERVAL_MS)
95     public synchronized void restartNonRunningTasks() {
96         for (KafkaJobDataConsumer consumer : activeSubscriptions.values()) {
97             if (!consumer.isRunning()) {
98                 consumer.start();
99             }
100         }
101     }
102
103 }