Support INDEX in node filter tosca functions
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / kafka / KafkaCommonConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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 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;
33
34 public class KafkaCommonConfig {
35
36     private static final Logger log = LoggerFactory.getLogger(KafkaCommonConfig.class.getName());
37
38     private final DistributionEngineConfiguration deConfiguration;
39
40     public KafkaCommonConfig(DistributionEngineConfiguration config){
41         this.deConfiguration = config;
42     }
43
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");
52         return props;
53     }
54
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());
61
62         return props;
63     }
64
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());
69
70         if("SSL".equals(securityProtocolConfig)) {
71             props.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, deConfiguration.getSSLConfig().getKeystorePath());
72             props.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, deConfiguration.getSSLConfig().getKeystorePass());
73             props.put(SslConfigs.SSL_KEY_PASSWORD_CONFIG, deConfiguration.getSSLConfig().getKeyManagerPassword());
74             props.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "");
75             props.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, deConfiguration.getSSLConfig().getTruststorePath());
76             props.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, deConfiguration.getSSLConfig().getTruststorePass());
77         } else {
78             props.put(SaslConfigs.SASL_JAAS_CONFIG, getKafkaSaslJaasConfig());
79             props.put(SaslConfigs.SASL_MECHANISM, "SCRAM-SHA-512");
80         }
81     }
82
83     /**
84      * @return The Sasl Jaas Configuration
85      */
86     private String getKafkaSaslJaasConfig() throws KafkaException {
87         String saslJaasConfFromEnv = System.getenv("SASL_JAAS_CONFIG");
88         if(saslJaasConfFromEnv != null) {
89             return saslJaasConfFromEnv;
90         } else {
91             throw new KafkaException("sasl.jaas.config not set for Kafka Consumer");
92         }
93     }
94
95 }