2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oran.dmaapadapter.tasks;
23 import java.util.HashMap;
26 import org.oran.dmaapadapter.configuration.ApplicationConfig;
27 import org.oran.dmaapadapter.repository.InfoType;
28 import org.oran.dmaapadapter.repository.InfoTypes;
29 import org.oran.dmaapadapter.repository.Job;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34 import reactor.core.Disposable;
37 * The class fetches incoming requests from DMAAP and sends them further to the
38 * consumers that has a job for this InformationType.
40 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
42 public class KafkaTopicConsumers {
43 private static final Logger logger = LoggerFactory.getLogger(KafkaTopicConsumers.class);
45 private final Map<String, KafkaTopicConsumer> topicConsumers = new HashMap<>();
46 private final Map<String, Disposable> activeSubscriptions = new HashMap<>();
47 private final ApplicationConfig appConfig;
49 public KafkaTopicConsumers(@Autowired ApplicationConfig appConfig) {
50 this.appConfig = appConfig;
53 public void start(InfoTypes types) {
54 for (InfoType type : types.getAll()) {
55 if (type.isKafkaTopicDefined()) {
56 KafkaTopicConsumer topicConsumer = new KafkaTopicConsumer(appConfig, type);
57 topicConsumers.put(type.getId(), topicConsumer);
62 public synchronized void addJob(Job job) {
63 if (this.activeSubscriptions.get(job.getId()) == null && job.getType().isKafkaTopicDefined()) {
64 logger.debug("Kafka job added {}", job.getId());
65 KafkaTopicConsumer topicConsumer = topicConsumers.get(job.getType().getId());
66 Disposable subscription = topicConsumer.startDistributeToConsumer(job);
67 activeSubscriptions.put(job.getId(), subscription);
71 public synchronized void removeJob(Job job) {
72 Disposable d = activeSubscriptions.remove(job.getId());
74 logger.debug("Kafka job removed {}", job.getId());