sdc-BE TLS support
[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 org.apache.kafka.clients.producer.KafkaProducer;
25 import org.apache.kafka.clients.producer.ProducerRecord;
26 import org.apache.kafka.common.KafkaException;
27 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Utility class that provides a KafkaProducer to communicate with a kafka cluster
33  */
34 public class SdcKafkaProducer {
35     private static final Logger log = LoggerFactory.getLogger(SdcKafkaProducer.class.getName());
36
37     private final KafkaProducer<String, String> kafkaProducer;
38
39     /**
40      * Constructor setting up the KafkaProducer from a predefined set of configurations
41      */
42     public SdcKafkaProducer(DistributionEngineConfiguration deConfiguration) {
43         log.info("Create SdcKafkaProducer via constructor");
44         KafkaCommonConfig kafkaCommonConfig = new KafkaCommonConfig(deConfiguration);
45         Properties properties = kafkaCommonConfig.getProducerProperties();
46         kafkaProducer = new KafkaProducer<>(properties);
47     }
48
49     /**
50      *
51      * @param kafkaProducer Setting a KafkaProducer to use within the sdcKafkaProducer class
52      */
53     @VisibleForTesting
54     SdcKafkaProducer(KafkaProducer kafkaProducer) {
55         this.kafkaProducer = kafkaProducer;
56     }
57
58     /**
59      * @param message A message to Send
60      * @param topicName The name of the topic to publish to
61      */
62     public void send(String message, String topicName) throws KafkaException {
63         ProducerRecord<String, String> kafkaMessagePayload = new ProducerRecord<>(topicName, "PartitionKey", message);
64         kafkaProducer.send(kafkaMessagePayload);
65     }
66
67     /**
68      * Kafka FLush operation
69      */
70     public void flush() throws KafkaException {
71         log.info("SdcKafkaProducer - flush");
72         kafkaProducer.flush();
73     }
74 }