2  * ============LICENSE_START=======================================================
 
   3  * dcaegen2-collectors-veshv
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018,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.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                     .doOnNext { it.receiverOffset().acknowledge() }
 
  43                     .also { logger.info { "Started Kafka source" } }
 
  46         private val logger = Logger(KafkaSource::class)
 
  48         private const val LOGIN_MODULE_CLASS = "org.apache.kafka.common.security.plain.PlainLoginModule"
 
  49         private const val USERNAME = "admin"
 
  50         private const val PASSWORD = "admin_secret"
 
  51         private const val JAAS_CONFIG = "$LOGIN_MODULE_CLASS required username=$USERNAME password=$PASSWORD;"
 
  52         private val SASL_PLAINTEXT = (SecurityProtocol.SASL_PLAINTEXT as Enum<SecurityProtocol>).name
 
  54         fun create(bootstrapServers: String, topics: Set<String>) =
 
  55                 KafkaSource(KafkaReceiver.create(createReceiverOptions(bootstrapServers, topics)))
 
  57         fun createReceiverOptions(bootstrapServers: String,
 
  58                                   topics: Set<String>): ReceiverOptions<ByteArray, ByteArray>? {
 
  59             val props = mapOf<String, Any>(
 
  60                     ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServers,
 
  61                     ConsumerConfig.CLIENT_ID_CONFIG to "hv-collector-dcae-app-simulator",
 
  62                     ConsumerConfig.GROUP_ID_CONFIG to "hv-collector-simulators",
 
  63                     ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to ByteArrayDeserializer::class.java,
 
  64                     ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to ByteArrayDeserializer::class.java,
 
  65                     ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to "earliest",
 
  66                     ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG to "3000",
 
  69                     CommonClientConfigs.SECURITY_PROTOCOL_CONFIG to SASL_PLAINTEXT,
 
  70                     SaslConfigs.SASL_MECHANISM to PLAIN_MECHANISM,
 
  71                     SaslConfigs.SASL_JAAS_CONFIG to JAAS_CONFIG
 
  73             return ReceiverOptions.create<ByteArray, ByteArray>(props)
 
  74                     .addAssignListener { partitions -> logger.debug { "Partitions assigned $partitions" } }
 
  75                     .addRevokeListener { partitions -> logger.debug { "Partitions revoked $partitions" } }