aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/mountpoint-registrar/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/mountpointregistrar/kafka/VESMsgKafkaConsumer.java
blob: 80e232a158db83e3fbd20b4fe84fe8a6df7dfed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.kafka;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.errors.InvalidGroupIdException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class that provides a KafkaConsumer to communicate with a kafka cluster
 */
public class VESMsgKafkaConsumer {

    private static final Logger log = LoggerFactory.getLogger(VESMsgKafkaConsumer.class);
    final KafkaConsumer<String, String> consumer;
    private final int pollTimeout;
    private String topicName;
    private static final String DESERIALIZER_CLASS = "org.apache.kafka.common.serialization.StringDeserializer";

    /**
     *
     * @param consumerProperties
     * @param configuration The config provided to the client
     */
    public VESMsgKafkaConsumer(Properties strimziKafkaProperties, Properties consumerProperties) {
        Properties props = new Properties();
        props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, strimziKafkaProperties.getProperty("bootstrapServers"));
        props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, strimziKafkaProperties.getProperty("securityProtocol"));
        props.put(SaslConfigs.SASL_MECHANISM, strimziKafkaProperties.getProperty("saslMechanism"));
        props.put(SaslConfigs.SASL_JAAS_CONFIG, strimziKafkaProperties.getProperty("saslJaasConfig"));
        props.put(ConsumerConfig.GROUP_ID_CONFIG,
                consumerProperties.getProperty("consumerGroup") + "-" + consumerProperties.getProperty("topic"));
        props.put(ConsumerConfig.CLIENT_ID_CONFIG,
                consumerProperties.getProperty("topic") + "-" + consumerProperties.getProperty("consumerID"));
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
        props.put(ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG, false);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, DESERIALIZER_CLASS);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, DESERIALIZER_CLASS);
        consumer = new KafkaConsumer<>(props);
        pollTimeout = Integer.parseInt(consumerProperties.getProperty("timeout"));
    }

    /**
     *
     * @param topic The kafka topic to subscribe to
     */
    public void subscribe(String topic) {
        try {
            consumer.subscribe(Collections.singleton(topic));
            this.topicName = topic;
        } catch (InvalidGroupIdException e) {
            log.error("Invalid Group {}", e.getMessage());
        }
    }

    /**
     *
     * @return The list of records returned from the poll
     */
    public List<String> poll() {
        List<String> msgs = new ArrayList<>();
        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(pollTimeout));
        for (ConsumerRecord<String, String> rec : records) {
            msgs.add(rec.value());
        }
        return msgs;
    }

    public String getTopicName() {
        return topicName;
    }
}