NONRTRIC - Implement DMaaP mediator producer service in Java
[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 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;
35
36 /**
37  * The class fetches incoming requests from DMAAP and sends them further to the
38  * consumers that has a job for this InformationType.
39  */
40 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
41 @Component
42 public class KafkaTopicConsumers {
43     private static final Logger logger = LoggerFactory.getLogger(KafkaTopicConsumers.class);
44
45     private final Map<String, KafkaTopicConsumer> topicConsumers = new HashMap<>();
46     private final Map<String, Disposable> activeSubscriptions = new HashMap<>();
47     private final ApplicationConfig appConfig;
48
49     public KafkaTopicConsumers(@Autowired ApplicationConfig appConfig) {
50         this.appConfig = appConfig;
51     }
52
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);
58             }
59         }
60     }
61
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);
68         }
69     }
70
71     public synchronized void removeJob(Job job) {
72         Disposable d = activeSubscriptions.remove(job.getId());
73         if (d != null) {
74             logger.debug("Kafka job removed {}", job.getId());
75             d.dispose();
76         }
77     }
78
79 }