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.impl
22 import arrow.core.Either
23 import io.netty.buffer.ByteBuf
24 import io.netty.buffer.ByteBufAllocator
25 import org.onap.dcae.collectors.veshv.boundary.Collector
26 import org.onap.dcae.collectors.veshv.boundary.Metrics
27 import org.onap.dcae.collectors.veshv.boundary.Sink
28 import org.onap.dcae.collectors.veshv.domain.ByteData
29 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
30 import org.onap.dcae.collectors.veshv.impl.wire.WireChunkDecoder
31 import org.onap.dcae.collectors.veshv.model.RoutedMessage
32 import org.onap.dcae.collectors.veshv.model.VesMessage
33 import org.onap.dcae.collectors.veshv.utils.logging.Logger
34 import org.onap.dcae.collectors.veshv.utils.logging.filterFailedWithLog
35 import org.onap.dcae.collectors.veshv.utils.logging.filterEmptyWithLog
36 import org.onap.dcae.collectors.veshv.utils.logging.handleReactiveStreamError
37 import reactor.core.publisher.Flux
38 import reactor.core.publisher.Mono
41 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
44 internal class VesHvCollector(
45 private val wireChunkDecoderSupplier: (ByteBufAllocator) -> WireChunkDecoder,
46 private val protobufDecoder: VesDecoder,
47 private val router: Router,
48 private val sink: Sink,
49 private val metrics: Metrics) : Collector {
51 override fun handleConnection(alloc: ByteBufAllocator, dataStream: Flux<ByteBuf>): Mono<Void> =
52 wireChunkDecoderSupplier(alloc).let { wireDecoder ->
54 .transform { decodeWireFrame(it, wireDecoder) }
55 .transform(::filterInvalidWireFrame)
56 .transform(::decodeProtobufPayload)
57 .transform(::filterInvalidProtobufMessages)
58 .transform(::routeMessage)
59 .onErrorResume { logger.handleReactiveStreamError(it) }
60 .doFinally { releaseBuffersMemory(wireDecoder) }
64 private fun decodeWireFrame(flux: Flux<ByteBuf>, decoder: WireChunkDecoder): Flux<WireFrameMessage> = flux
65 .doOnNext { metrics.notifyBytesReceived(it.readableBytes()) }
66 .concatMap(decoder::decode)
67 .doOnNext { metrics.notifyMessageReceived(it.payloadSize) }
69 private fun filterInvalidWireFrame(flux: Flux<WireFrameMessage>): Flux<WireFrameMessage> = flux
70 .filterFailedWithLog(MessageValidator::validateFrameMessage)
72 private fun decodeProtobufPayload(flux: Flux<WireFrameMessage>): Flux<VesMessage> = flux
73 .map(WireFrameMessage::payload)
74 .flatMap(::decodePayload)
76 private fun decodePayload(rawPayload: ByteData): Flux<VesMessage> = protobufDecoder
78 .filterFailedWithLog(logger,
79 { "Ves event header decoded successfully" },
80 { "Failed to decode ves event header, reason: ${it.message}" })
82 private fun filterInvalidProtobufMessages(flux: Flux<VesMessage>): Flux<VesMessage> = flux
83 .filterFailedWithLog(MessageValidator::validateProtobufMessage)
85 private fun routeMessage(flux: Flux<VesMessage>): Flux<RoutedMessage> = flux
86 .flatMap(this::findRoute)
88 .doOnNext { metrics.notifyMessageSent(it.topic) }
90 private fun findRoute(msg: VesMessage) = router
92 .filterEmptyWithLog(logger,
93 { "Found route for message: ${it.topic}, partition: ${it.partition}" },
94 { "Could not find route for message" })
96 private fun releaseBuffersMemory(wireChunkDecoder: WireChunkDecoder) = wireChunkDecoder.release()
97 .also { logger.debug("Released buffer memory after handling message stream") }
99 fun <T> Flux<T>.filterFailedWithLog(predicate: (T) -> Either<() -> String, () -> String>) =
100 filterFailedWithLog(logger, predicate)
103 private val logger = Logger(VesHvCollector::class)