2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2023 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.components.kafka;
22 import java.util.Properties;
23 import java.util.UUID;
24 import org.apache.kafka.clients.CommonClientConfigs;
25 import org.apache.kafka.clients.consumer.ConsumerConfig;
26 import org.apache.kafka.clients.producer.ProducerConfig;
27 import org.apache.kafka.common.KafkaException;
28 import org.apache.kafka.common.config.SaslConfigs;
29 import org.apache.kafka.common.config.SslConfigs;
30 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class KafkaCommonConfig {
36 private static final Logger log = LoggerFactory.getLogger(KafkaCommonConfig.class.getName());
38 private final DistributionEngineConfiguration deConfiguration;
40 public KafkaCommonConfig(DistributionEngineConfiguration config){
41 this.deConfiguration = config;
44 public Properties getConsumerProperties(){
45 Properties props = new Properties();
46 setCommonProperties(props);
47 props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
48 props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
49 props.put(ConsumerConfig.CLIENT_ID_CONFIG, deConfiguration.getDistributionStatusTopic().getConsumerId() + "-consumer-" + UUID.randomUUID());
50 props.put(ConsumerConfig.GROUP_ID_CONFIG, deConfiguration.getDistributionStatusTopic().getConsumerGroup());
51 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
55 public Properties getProducerProperties(){
56 Properties props = new Properties();
57 setCommonProperties(props);
58 props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
59 props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
60 props.put(ProducerConfig.CLIENT_ID_CONFIG, deConfiguration.getDistributionStatusTopic().getConsumerId() + "-producer-" + UUID.randomUUID());
65 private void setCommonProperties(Properties props) {
66 String securityProtocolConfig = System.getenv().getOrDefault("SECURITY_PROTOCOL", "SASL_PLAINTEXT");
67 props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, securityProtocolConfig);
68 props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, deConfiguration.getKafkaBootStrapServers());
70 if("SSL".equals(securityProtocolConfig)) {
71 log.error("Kafka over SSL has not been implemented yet");
74 props.put(SaslConfigs.SASL_JAAS_CONFIG, getKafkaSaslJaasConfig());
75 props.put(SaslConfigs.SASL_MECHANISM, "SCRAM-SHA-512");
80 * @return The Sasl Jaas Configuration
82 private String getKafkaSaslJaasConfig() throws KafkaException {
83 String saslJaasConfFromEnv = System.getenv("SASL_JAAS_CONFIG");
84 if(saslJaasConfFromEnv != null) {
85 return saslJaasConfFromEnv;
87 throw new KafkaException("sasl.jaas.config not set for Kafka Consumer");