Merge "ICS tests with istio and JWTs"
[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.oran.dmaapadapter.repository.MultiMap;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.scheduling.annotation.EnableScheduling;
38 import org.springframework.scheduling.annotation.Scheduled;
39 import org.springframework.stereotype.Component;
40
41 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
42 @Component
43 @EnableScheduling
44 public class KafkaTopicConsumers {
45     private static final Logger logger = LoggerFactory.getLogger(KafkaTopicConsumers.class);
46
47     private final Map<String, KafkaTopicListener> topicListeners = new HashMap<>(); // Key is typeId
48
49     @Getter
50     private final MultiMap<KafkaJobDataConsumer> consumers = new MultiMap<>(); // Key is typeId, jobId
51
52     private static final int CONSUMER_SUPERVISION_INTERVAL_MS = 1000 * 60 * 3;
53
54     public KafkaTopicConsumers(@Autowired ApplicationConfig appConfig, @Autowired InfoTypes types,
55             @Autowired Jobs jobs) {
56
57         for (InfoType type : types.getAll()) {
58             if (type.isKafkaTopicDefined()) {
59                 KafkaTopicListener topicConsumer = new KafkaTopicListener(appConfig, type);
60                 topicListeners.put(type.getId(), topicConsumer);
61             }
62         }
63
64         jobs.addObserver(new Jobs.Observer() {
65             @Override
66             public void onJobbAdded(Job job) {
67                 addJob(job);
68             }
69
70             @Override
71             public void onJobRemoved(Job job) {
72                 removeJob(job);
73             }
74         });
75     }
76
77     public synchronized void addJob(Job job) {
78         if (job.getType().isKafkaTopicDefined()) {
79             removeJob(job);
80             logger.debug("Kafka job added {}", job.getId());
81             KafkaTopicListener topicConsumer = topicListeners.get(job.getType().getId());
82             if (consumers.get(job.getType().getId()).isEmpty()) {
83                 topicConsumer.start();
84             }
85             KafkaJobDataConsumer subscription = new KafkaJobDataConsumer(job);
86             subscription.start(topicConsumer.getOutput().asFlux());
87             consumers.put(job.getType().getId(), job.getId(), subscription);
88         }
89     }
90
91     public synchronized void removeJob(Job job) {
92         KafkaJobDataConsumer d = consumers.remove(job.getType().getId(), job.getId());
93         if (d != null) {
94             logger.debug("Kafka job removed {}", job.getId());
95             d.stop();
96         }
97     }
98
99     @Scheduled(fixedRate = CONSUMER_SUPERVISION_INTERVAL_MS)
100     public synchronized void restartNonRunningTopics() {
101         for (String typeId : this.consumers.keySet()) {
102             for (KafkaJobDataConsumer consumer : this.consumers.get(typeId)) {
103                 if (!consumer.isRunning()) {
104                     restartTopic(consumer);
105                 }
106             }
107         }
108     }
109
110     private void restartTopic(KafkaJobDataConsumer consumer) {
111         InfoType type = consumer.getJob().getType();
112         KafkaTopicListener topic = this.topicListeners.get(type.getId());
113         topic.start();
114         restartConsumersOfType(topic, type);
115     }
116
117     private void restartConsumersOfType(KafkaTopicListener topic, InfoType type) {
118         this.consumers.get(type.getId()).forEach(consumer -> consumer.start(topic.getOutput().asFlux()));
119     }
120 }