2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2019 NOKIA
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.onap.dcae.collectors.veshv.impl.adapters.kafka
22 import org.apache.kafka.clients.CommonClientConfigs
23 import org.apache.kafka.clients.producer.ProducerConfig
24 import org.apache.kafka.common.config.SaslConfigs
25 import org.apache.kafka.common.security.auth.SecurityProtocol
26 import org.apache.kafka.common.security.plain.internals.PlainSaslServer
27 import org.jetbrains.annotations.Nullable
28 import org.onap.dcae.collectors.veshv.domain.VesMessage
29 import org.onap.dcae.collectors.veshv.utils.applyIf
30 import org.onap.dcaegen2.services.sdk.model.streams.AafCredentials
31 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.KafkaSink
32 import org.onap.ves.VesEventOuterClass.CommonEventHeader
33 import reactor.kafka.sender.SenderOptions
35 internal object KafkaSenderOptionsFactory {
37 private const val MAXIMUM_REQUEST_SIZE_MULTIPLIER = 1.2f
38 private const val BUFFER_MEMORY_MULTIPLIER = 32
39 private const val MINIMUM_BUFFER_MEMORY = 32 * 1024 * 1024
41 private const val LOGIN_MODULE_CLASS = "org.apache.kafka.common.security.plain.PlainLoginModule"
42 private val SASL_PLAINTEXT = (SecurityProtocol.SASL_PLAINTEXT as Enum<SecurityProtocol>).name
44 fun createSenderOptions(kafkaSink: KafkaSink): SenderOptions<CommonEventHeader, VesMessage> =
45 SenderOptions.create<CommonEventHeader, VesMessage>()
46 .producerProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaSink.bootstrapServers())
47 .producerProperty(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, maxRequestSize(kafkaSink))
48 .producerProperty(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory(kafkaSink))
49 .producerProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ProtobufSerializer::class.java)
50 .producerProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, VesMessageSerializer::class.java)
51 .producerProperty(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, 1)
52 .producerProperty(ProducerConfig.RETRIES_CONFIG, 1)
53 .producerProperty(ProducerConfig.ACKS_CONFIG, "1")
55 .applyIf(kafkaSink.aafCredentials() != null) {
56 producerProperty(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, SASL_PLAINTEXT)
57 .producerProperty(SaslConfigs.SASL_MECHANISM, PlainSaslServer.PLAIN_MECHANISM)
58 .producerProperty(SaslConfigs.SASL_JAAS_CONFIG, jaasConfig(kafkaSink.aafCredentials()!!))
61 private fun jaasConfig(aafCredentials: AafCredentials) =
62 """$LOGIN_MODULE_CLASS required username="${aafCredentials.username().jaasEscape()}" password="${aafCredentials.password().jaasEscape()}";"""
64 private fun String?.jaasEscape() = this?.replace("\"", "\\\"")
66 private fun maxRequestSize(kafkaSink: KafkaSink) =
67 (MAXIMUM_REQUEST_SIZE_MULTIPLIER * kafkaSink.maxPayloadSizeBytes()).toInt()
69 private fun bufferMemory(kafkaSink: KafkaSink) =
70 Integer.max(MINIMUM_BUFFER_MEMORY, BUFFER_MEMORY_MULTIPLIER * kafkaSink.maxPayloadSizeBytes())