1 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.kafka;
3 import java.time.Duration;
4 import java.util.ArrayList;
5 import java.util.Collections;
7 import java.util.Properties;
8 import org.apache.kafka.clients.CommonClientConfigs;
9 import org.apache.kafka.clients.consumer.ConsumerConfig;
10 import org.apache.kafka.clients.consumer.ConsumerRecord;
11 import org.apache.kafka.clients.consumer.ConsumerRecords;
12 import org.apache.kafka.clients.consumer.KafkaConsumer;
13 import org.apache.kafka.common.config.SaslConfigs;
14 import org.apache.kafka.common.errors.InvalidGroupIdException;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Utility class that provides a KafkaConsumer to communicate with a kafka cluster
21 public class VESMsgKafkaConsumer {
23 private static final Logger log = LoggerFactory.getLogger(VESMsgKafkaConsumer.class);
24 final KafkaConsumer<String, String> consumer;
25 private final int pollTimeout;
26 private String topicName;
27 private static final String DESERIALIZER_CLASS = "org.apache.kafka.common.serialization.StringDeserializer";
31 * @param consumerProperties
32 * @param configuration The config provided to the client
34 public VESMsgKafkaConsumer(Properties strimziKafkaProperties, Properties consumerProperties) {
35 Properties props = new Properties();
36 props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, strimziKafkaProperties.getProperty("bootstrap.servers"));
37 props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, strimziKafkaProperties.getProperty("security.protocol"));
38 props.put(SaslConfigs.SASL_MECHANISM, strimziKafkaProperties.getProperty("sasl.mechanism"));
39 props.put(SaslConfigs.SASL_JAAS_CONFIG, strimziKafkaProperties.getProperty("sasl.jaas.config"));
40 props.put(ConsumerConfig.GROUP_ID_CONFIG,
41 consumerProperties.getProperty("consumerGroup") + "-" + consumerProperties.getProperty("topic"));
42 props.put(ConsumerConfig.CLIENT_ID_CONFIG,
43 consumerProperties.getProperty("topic") + "-" + consumerProperties.getProperty("consumerID"));
44 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
45 props.put(ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG, false);
46 props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, DESERIALIZER_CLASS);
47 props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, DESERIALIZER_CLASS);
48 consumer = new KafkaConsumer<>(props);
49 pollTimeout = Integer.parseInt(consumerProperties.getProperty("timeout"));
54 * @param topic The kafka topic to subscribe to
56 public void subscribe(String topic) {
58 consumer.subscribe(Collections.singleton(topic));
59 this.topicName = topic;
60 } catch (InvalidGroupIdException e) {
61 log.error("Invalid Group {}", e.getMessage());
67 * @return The list of records returned from the poll
69 public List<String> poll() {
70 List<String> msgs = new ArrayList<>();
71 ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(pollTimeout));
72 for (ConsumerRecord<String, String> rec : records) {
73 msgs.add(rec.value());
78 public String getTopicName() {
83 consumer.unsubscribe();