d53609ca3a2041399ed6f30b50f04d769746e131
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
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.onap.dcae.collectors.veshv.simulators.dcaeapp.kafka
21
22 import arrow.effects.IO
23 import org.apache.kafka.clients.consumer.ConsumerConfig
24 import org.apache.kafka.common.serialization.ByteArrayDeserializer
25 import org.onap.dcae.collectors.veshv.utils.logging.Logger
26 import reactor.kafka.receiver.KafkaReceiver
27 import reactor.kafka.receiver.ReceiverOptions
28 import java.util.*
29
30 /**
31  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
32  * @since May 2018
33  */
34 class KafkaSource(private val receiver: KafkaReceiver<ByteArray, ByteArray>) {
35
36     fun start(): IO<Consumer> = IO {
37         val consumer = Consumer()
38         receiver.receive().subscribe(consumer::update)
39         consumer
40     }
41
42     companion object {
43         private val logger = Logger(KafkaSource::class)
44
45         fun create(bootstrapServers: String, topics: Set<String>): KafkaSource {
46             val props = HashMap<String, Any>()
47             props[ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG] = bootstrapServers
48             props[ConsumerConfig.CLIENT_ID_CONFIG] = "hv-collector-dcae-app-simulator"
49             props[ConsumerConfig.GROUP_ID_CONFIG] = "hv-collector-simulators"
50             props[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = ByteArrayDeserializer::class.java
51             props[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = ByteArrayDeserializer::class.java
52             props[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
53             val receiverOptions = ReceiverOptions.create<ByteArray, ByteArray>(props)
54                     .addAssignListener { partitions -> logger.debug { "Partitions assigned $partitions" } }
55                     .addRevokeListener { partitions -> logger.debug { "Partitions revoked $partitions" } }
56                     .subscription(topics)
57             return KafkaSource(KafkaReceiver.create(receiverOptions))
58         }
59     }
60 }