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.onap.dcaegen2.collectors.datafile.tasks;
23 import java.util.Collections;
24 import java.util.HashMap;
27 import lombok.ToString;
29 import org.apache.kafka.clients.consumer.ConsumerConfig;
30 import org.apache.kafka.common.serialization.StringDeserializer;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import reactor.core.publisher.Flux;
35 import reactor.kafka.receiver.KafkaReceiver;
36 import reactor.kafka.receiver.ReceiverOptions;
39 * The class streams incoming requests from a Kafka topic and sends them further
40 * to a multi cast sink, which several other streams can connect to.
42 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
43 public class KafkaTopicListener {
46 public static class DataFromTopic {
47 public final String key;
48 public final String value;
50 public DataFromTopic(String key, String value) {
56 private static final Logger logger = LoggerFactory.getLogger(KafkaTopicListener.class);
58 private final String inputTopic;
59 private final String kafkaBoostrapServers;
60 private final String kafkaClientId;
61 private Flux<DataFromTopic> dataFromTopic;
63 public KafkaTopicListener(String kafkaBoostrapServers, String clientId, String topic) {
64 this.kafkaClientId = clientId;
65 this.kafkaBoostrapServers = kafkaBoostrapServers;
66 this.inputTopic = topic;
69 public Flux<DataFromTopic> getFlux() {
70 if (this.dataFromTopic == null) {
71 this.dataFromTopic = startReceiveFromTopic();
73 return this.dataFromTopic;
76 private Flux<DataFromTopic> startReceiveFromTopic() {
77 logger.debug("Listening to kafka topic: {}, client id: {}", this.inputTopic, this.kafkaClientId);
78 return KafkaReceiver.create(kafkaInputProperties()) //
80 .doOnNext(input -> logger.debug("Received from kafka topic: {} :{}", this.inputTopic, input.value())) //
81 .doOnError(t -> logger.error("KafkaTopicReceiver error: {}", t.getMessage())) //
82 .doFinally(sig -> logger.error("KafkaTopicReceiver stopped, reason: {}", sig)) //
83 .doFinally(sig -> this.dataFromTopic = null) //
84 .filter(t -> !t.value().isEmpty() || !t.key().isEmpty()) //
85 .map(input -> new DataFromTopic(input.key(), input.value())) //
90 private ReceiverOptions<String, String> kafkaInputProperties() {
91 Map<String, Object> consumerProps = new HashMap<>();
92 if (this.kafkaBoostrapServers.isEmpty()) {
93 logger.error("No kafka boostrap server is setup");
95 consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.kafkaBoostrapServers);
96 consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "osc-dmaap-adapter-" + inputTopic);
97 consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
98 consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
99 consumerProps.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
100 consumerProps.put(ConsumerConfig.CLIENT_ID_CONFIG, this.kafkaClientId);
102 return ReceiverOptions.<String, String>create(consumerProps)
103 .subscription(Collections.singleton(this.inputTopic));