8644dc9882a82626d6147a04858ce5f9e6da0d9c
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Modifications Copyright (C) 2025 OpenInfra Foundation Europe
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.oran.smo.ncmp_to_teiv_adapter;
21
22 import io.cloudevents.CloudEvent;
23 import lombok.RequiredArgsConstructor;
24 import org.apache.kafka.clients.admin.AdminClientConfig;
25 import org.apache.kafka.clients.producer.ProducerConfig;
26 import org.apache.kafka.common.serialization.StringSerializer;
27 import io.cloudevents.kafka.CloudEventSerializer;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.context.annotation.Bean;
30 import org.springframework.context.annotation.Configuration;
31 import org.springframework.kafka.core.DefaultKafkaProducerFactory;
32 import org.springframework.kafka.core.KafkaTemplate;
33 import org.springframework.kafka.core.ProducerFactory;
34
35 import java.util.HashMap;
36 import java.util.Map;
37
38 @Configuration
39 @RequiredArgsConstructor
40 public class KafkaProducerConfig {
41
42     private final KafkaSecurityConfig securityConfig;
43
44     @Value("${spring.kafka.bootstrap-servers}")
45     private String bootstrapServer;
46
47     @Value("${spring.kafka.security.enabled}")
48     private boolean securityEnabled;
49
50     @Bean
51     public ProducerFactory<String, CloudEvent> producerFactory() {
52         Map<String, Object> configProps = new HashMap<>();
53         configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
54         configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
55         configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, CloudEventSerializer.class);
56         if (securityConfig.isEnabled()) {
57             configProps.put(AdminClientConfig.SECURITY_PROTOCOL_CONFIG, securityConfig.getProtocol());
58             configProps.putAll(securityConfig.getProperties());
59         }
60         return new DefaultKafkaProducerFactory<>(configProps);
61     }
62
63     @Bean
64     public KafkaTemplate<String, CloudEvent> kafkaTemplate() {
65         return new KafkaTemplate<>(producerFactory());
66     }
67 }