Merge "ICS tests with istio and JWTs"
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / 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.oran.dmaapadapter.tasks;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.kafka.clients.consumer.ConsumerConfig;
28 import org.apache.kafka.clients.consumer.ConsumerRecord;
29 import org.apache.kafka.common.serialization.StringDeserializer;
30 import org.oran.dmaapadapter.configuration.ApplicationConfig;
31 import org.oran.dmaapadapter.repository.InfoType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import reactor.core.Disposable;
36 import reactor.core.publisher.Sinks;
37 import reactor.core.publisher.Sinks.Many;
38 import reactor.kafka.receiver.KafkaReceiver;
39 import reactor.kafka.receiver.ReceiverOptions;
40
41 /**
42  * The class streams incoming requests from a Kafka topic and sends them further
43  * to a multi cast sink, which several other streams can connect to.
44  */
45 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
46 public class KafkaTopicListener {
47     private static final Logger logger = LoggerFactory.getLogger(KafkaTopicListener.class);
48     private final ApplicationConfig applicationConfig;
49     private final InfoType type;
50     private Many<String> output;
51     private Disposable topicReceiverTask;
52
53     public KafkaTopicListener(ApplicationConfig applicationConfig, InfoType type) {
54         this.applicationConfig = applicationConfig;
55         this.type = type;
56     }
57
58     public Many<String> getOutput() {
59         return this.output;
60     }
61
62     public void start() {
63         stop();
64         final int CONSUMER_BACKPRESSURE_BUFFER_SIZE = 1024 * 10;
65         this.output = Sinks.many().multicast().onBackpressureBuffer(CONSUMER_BACKPRESSURE_BUFFER_SIZE);
66         logger.debug("Listening to kafka topic: {} type :{}", this.type.getKafkaInputTopic(), type.getId());
67         topicReceiverTask = KafkaReceiver.create(kafkaInputProperties()) //
68                 .receive() //
69                 .doOnNext(this::onReceivedData) //
70                 .subscribe(null, //
71                         this::onReceivedError, //
72                         () -> logger.warn("KafkaTopicReceiver stopped"));
73     }
74
75     private void stop() {
76         if (topicReceiverTask != null) {
77             topicReceiverTask.dispose();
78             topicReceiverTask = null;
79         }
80     }
81
82     private void onReceivedData(ConsumerRecord<String, String> input) {
83         logger.debug("Received from kafka topic: {} :{}", this.type.getKafkaInputTopic(), input.value());
84         output.emitNext(input.value(), Sinks.EmitFailureHandler.FAIL_FAST);
85     }
86
87     private void onReceivedError(Throwable t) {
88         logger.error("KafkaTopicReceiver error: {}", t.getMessage());
89     }
90
91     private ReceiverOptions<String, String> kafkaInputProperties() {
92         Map<String, Object> consumerProps = new HashMap<>();
93         if (this.applicationConfig.getKafkaBootStrapServers().isEmpty()) {
94             logger.error("No kafka boostrap server is setup");
95         }
96         consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.applicationConfig.getKafkaBootStrapServers());
97         consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "osc-dmaap-adaptor");
98         consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
99         consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
100
101         return ReceiverOptions.<String, String>create(consumerProps)
102                 .subscription(Collections.singleton(this.type.getKafkaInputTopic()));
103     }
104
105 }