[SO] Remove DMaap Dependency in SO-bpmn-infra
[so.git] / common / src / main / java / org / onap / so / client / kafka / KafkaConsumerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.so.client.kafka;
22
23 import org.apache.kafka.clients.CommonClientConfigs;
24 import org.apache.kafka.clients.consumer.Consumer;
25 import org.apache.kafka.clients.consumer.ConsumerRecord;
26 import org.apache.kafka.clients.consumer.ConsumerRecords;
27 import org.apache.kafka.clients.consumer.KafkaConsumer;
28 import org.apache.kafka.common.config.SaslConfigs;
29 import org.apache.kafka.common.security.auth.SecurityProtocol;
30 import org.apache.kafka.common.security.scram.internals.ScramMechanism;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import java.time.Duration;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Properties;
38
39 public class KafkaConsumerImpl extends KafkaClient {
40
41     protected static Logger logger = LoggerFactory.getLogger(KafkaConsumerImpl.class);
42     private static final String kafkaBootstrapServers = "kafkaBootstrapServers";
43     private Consumer<String, String> consumer;
44
45     public KafkaConsumerImpl(String bootstrapServers) throws Exception {
46         super("kafka/default-consumer.properties");
47         setProperties(bootstrapServers);
48     }
49
50
51     public List<String> get(String topic, String consumerGroup, String consumerId) {
52         logger.info("consuming message from kafka topic : " + topic);
53         this.properties.put("group.id", consumerGroup);
54         this.properties.put("client.id", consumerId);
55         if (consumer == null) {
56             consumer = getKafkaConsumer(properties);
57             consumer.subscribe(Arrays.asList(topic));
58         }
59         ArrayList<String> msgs = new ArrayList<>();
60         ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
61         for (ConsumerRecord<String, String> rec : records) {
62             msgs.add(rec.value());
63         }
64         logger.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<READING THE CONSUMED MESSAGES<<<<<<<<<<<<<<<<<<<<<<<<<<<");
65         msgs.forEach(msg -> logger.info("MESSAGE CONSUMED FROM KAFKA : <<<<<" + msg + ">>>>>"));
66         return msgs;
67     }
68
69     private void setProperties(String bootstrapServers) throws Exception {
70         if (bootstrapServers == null) {
71             logger.error("Environment Variable " + kafkaBootstrapServers + " is missing");
72             throw new Exception("Environment Variable " + kafkaBootstrapServers + " is missing");
73         } else {
74             this.properties.put("bootstrap.servers", bootstrapServers);
75         }
76
77         if (System.getenv("JAAS_CONFIG") == null) {
78             logger.info("Not using any authentication for kafka interaction");
79         } else {
80             logger.info("Using {} authentication provided for kafka interaction",
81                     ScramMechanism.SCRAM_SHA_512.mechanismName());
82             this.properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, SecurityProtocol.SASL_PLAINTEXT.name);
83             this.properties.put(SaslConfigs.SASL_MECHANISM, ScramMechanism.SCRAM_SHA_512.mechanismName());
84             this.properties.put(SaslConfigs.SASL_JAAS_CONFIG, System.getenv("JAAS_CONFIG"));
85         }
86     }
87
88     public static KafkaConsumer<String, String> getKafkaConsumer(Properties properties) {
89         return new KafkaConsumer<>(properties);
90     }
91
92     public void setConsumer(Consumer<String, String> kafkaConsumer) {
93         this.consumer = kafkaConsumer;
94     }
95
96     public void close() {
97         if (consumer != null) {
98             logger.info("Closing the Kafka Consumer");
99             consumer.close();
100             consumer = null;
101         }
102     }
103
104 }