2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 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.simulators.dcaeapp.impl.adapters
22 import org.apache.kafka.clients.CommonClientConfigs
23 import org.apache.kafka.clients.consumer.ConsumerConfig
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.PLAIN_MECHANISM
27 import org.apache.kafka.common.serialization.ByteArrayDeserializer
28 import org.onap.dcae.collectors.veshv.utils.logging.Logger
29 import reactor.core.publisher.Flux
30 import reactor.kafka.receiver.KafkaReceiver
31 import reactor.kafka.receiver.ReceiverOptions
32 import reactor.kafka.receiver.ReceiverRecord
35 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
38 internal class KafkaSource(private val receiver: KafkaReceiver<ByteArray, ByteArray>) {
40 fun start(): Flux<ReceiverRecord<ByteArray, ByteArray>> =
42 .also { logger.info { "Started Kafka source" } }
45 private val logger = Logger(KafkaSource::class)
47 private const val LOGIN_MODULE_CLASS = "org.apache.kafka.common.security.plain.PlainLoginModule"
48 private const val USERNAME = "admin"
49 private const val PASSWORD = "admin_secret"
50 private const val JAAS_CONFIG = "$LOGIN_MODULE_CLASS required username=$USERNAME password=$PASSWORD;"
51 private val SASL_PLAINTEXT = (SecurityProtocol.SASL_PLAINTEXT as Enum<SecurityProtocol>).name
53 fun create(bootstrapServers: String, topics: Set<String>): KafkaSource {
54 return KafkaSource(KafkaReceiver.create(createReceiverOptions(bootstrapServers, topics)))
58 fun createReceiverOptions(bootstrapServers: String,
59 topics: Set<String>): ReceiverOptions<ByteArray, ByteArray>? {
60 val props = mapOf<String, Any>(
61 ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServers,
62 ConsumerConfig.CLIENT_ID_CONFIG to "hv-collector-dcae-app-simulator",
63 ConsumerConfig.GROUP_ID_CONFIG to "hv-collector-simulators",
64 ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to ByteArrayDeserializer::class.java,
65 ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to ByteArrayDeserializer::class.java,
66 ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to "earliest",
68 CommonClientConfigs.SECURITY_PROTOCOL_CONFIG to SASL_PLAINTEXT,
69 SaslConfigs.SASL_MECHANISM to PLAIN_MECHANISM,
70 SaslConfigs.SASL_JAAS_CONFIG to JAAS_CONFIG
72 return ReceiverOptions.create<ByteArray, ByteArray>(props)
73 .addAssignListener { partitions -> logger.debug { "Partitions assigned $partitions" } }
74 .addRevokeListener { partitions -> logger.debug { "Partitions revoked $partitions" } }