715835779959e75f375561c850ee944c4a601ba4
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / kafka / SdcKafkaProducer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2022 Nordix Foundation. 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 package org.openecomp.sdc.be.components.kafka;
21
22 import com.google.common.annotations.VisibleForTesting;
23 import java.util.Properties;
24 import java.util.UUID;
25 import org.apache.kafka.clients.CommonClientConfigs;
26 import org.apache.kafka.clients.producer.KafkaProducer;
27 import org.apache.kafka.clients.producer.ProducerConfig;
28 import org.apache.kafka.clients.producer.ProducerRecord;
29 import org.apache.kafka.common.KafkaException;
30 import org.apache.kafka.common.config.SaslConfigs;
31 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Utility class that provides a KafkaProducer to communicate with a kafka cluster
37  */
38 public class SdcKafkaProducer {
39     private static final Logger log = LoggerFactory.getLogger(SdcKafkaProducer.class.getName());
40     private static final String DEFAULT_SASL_MECHANISM = "SCRAM-SHA-512";
41
42     private KafkaProducer<String, String> kafkaProducer;
43
44     /**
45      * Constructor setting up the KafkaProducer from a predefined set of configurations
46      */
47     public SdcKafkaProducer(DistributionEngineConfiguration deConfiguration) {
48         log.info("Create SdcKafkaProducer via constructor");
49         Properties properties = new Properties();
50
51         properties.put(ProducerConfig.CLIENT_ID_CONFIG, deConfiguration.getDistributionStatusTopic().getConsumerId() + "-producer-" + UUID.randomUUID());
52         properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
53         properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,  "org.apache.kafka.common.serialization.StringSerializer");
54         properties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, deConfiguration.getKafkaBootStrapServers());
55         properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
56         properties.put(SaslConfigs.SASL_JAAS_CONFIG, getKafkaSaslJaasConfig());
57         properties.put(SaslConfigs.SASL_MECHANISM, getKafkaSaslMechanism());
58         kafkaProducer = new KafkaProducer<>(properties);
59     }
60
61     /**
62      *
63      * @param kafkaProducer Setting a KafkaProducer to use within the sdcKafkaProducer class
64      */
65     @VisibleForTesting
66     SdcKafkaProducer(KafkaProducer kafkaProducer) {
67         this.kafkaProducer = kafkaProducer;
68     }
69
70     /**
71      * @return The Sasl Jaas Configuration
72      */
73     private static String getKafkaSaslJaasConfig() throws KafkaException {
74         String saslJaasConfFromEnv = System.getenv("SASL_JAAS_CONFIG");
75         if(saslJaasConfFromEnv != null) {
76             return saslJaasConfFromEnv;
77         } else {
78             throw new KafkaException("sasl.jaas.config not set for Kafka Consumer");
79         }
80     }
81     
82     private static String getKafkaSaslMechanism() throws KafkaException {
83         String saslMechanism = System.getenv("SASL_MECHANISM");
84         if(saslMechanism != null) {
85             return saslMechanism;
86         } else {
87             return DEFAULT_SASL_MECHANISM;
88         }
89     }
90     
91     /**
92      * @param message A message to Send
93      * @param topicName The name of the topic to publish to
94      * @return The status of the send request
95      */
96     public void send(String message, String topicName) throws KafkaException {
97         ProducerRecord<String, String> kafkaMessagePayload = new ProducerRecord<>(topicName, "PartitionKey", message);
98         kafkaProducer.send(kafkaMessagePayload);
99     }
100
101     /**
102      * Kafka FLush operation
103      */
104     public void flush() throws KafkaException {
105         log.info("SdcKafkaProducer - flush");
106         kafkaProducer.flush();
107     }
108 }