Merge "Added support for using oauth token for Kafka"
[nonrtric/plt/ranpm.git] / datafilecollector / src / main / java / org / onap / dcaegen2 / collectors / datafile / tasks / KafkaTopicListener.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.onap.dcaegen2.collectors.datafile.tasks;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import lombok.ToString;
28
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;
33
34 import reactor.core.publisher.Flux;
35 import reactor.kafka.receiver.KafkaReceiver;
36 import reactor.kafka.receiver.ReceiverOptions;
37
38 /**
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.
41  */
42 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
43 public class KafkaTopicListener {
44
45     @ToString
46     public static class DataFromTopic {
47         public final String key;
48         public final String value;
49
50         public DataFromTopic(String key, String value) {
51             this.key = key;
52             this.value = value;
53         }
54     }
55
56     private static final Logger logger = LoggerFactory.getLogger(KafkaTopicListener.class);
57
58     private final String inputTopic;
59     private final String kafkaBoostrapServers;
60     private final String kafkaClientId;
61     private Flux<DataFromTopic> dataFromTopic;
62
63     public KafkaTopicListener(String kafkaBoostrapServers, String clientId, String topic) {
64         this.kafkaClientId = clientId;
65         this.kafkaBoostrapServers = kafkaBoostrapServers;
66         this.inputTopic = topic;
67     }
68
69     public Flux<DataFromTopic> getFlux() {
70         if (this.dataFromTopic == null) {
71             this.dataFromTopic = startReceiveFromTopic();
72         }
73         return this.dataFromTopic;
74     }
75
76     private Flux<DataFromTopic> startReceiveFromTopic() {
77         logger.debug("Listening to kafka topic: {}, client id: {}", this.inputTopic, this.kafkaClientId);
78         return KafkaReceiver.create(kafkaInputProperties()) //
79             .receive() //
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())) //
86             .publish() //
87             .autoConnect();
88     }
89
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");
94         }
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);
101
102         return ReceiverOptions.<String, String>create(consumerProps)
103             .subscription(Collections.singleton(this.inputTopic));
104     }
105
106 }