Add metrics for dropped messages
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-core / src / main / kotlin / org / onap / dcae / collectors / veshv / impl / VesHvCollector.kt
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.impl
21
22 import io.netty.buffer.ByteBuf
23 import org.onap.dcae.collectors.veshv.boundary.Collector
24 import org.onap.dcae.collectors.veshv.boundary.Metrics
25 import org.onap.dcae.collectors.veshv.boundary.Sink
26 import org.onap.dcae.collectors.veshv.domain.ByteData
27 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
28 import org.onap.dcae.collectors.veshv.impl.adapters.ClientContextLogging.handleReactiveStreamError
29 import org.onap.dcae.collectors.veshv.impl.wire.WireChunkDecoder
30 import org.onap.dcae.collectors.veshv.model.ClientContext
31 import org.onap.dcae.collectors.veshv.model.MessageDropCause.INVALID_MESSAGE
32 import org.onap.dcae.collectors.veshv.model.MessageDropCause.ROUTE_NOT_FOUND
33 import org.onap.dcae.collectors.veshv.model.RoutedMessage
34 import org.onap.dcae.collectors.veshv.model.VesMessage
35 import org.onap.dcae.collectors.veshv.utils.arrow.doOnEmpty
36 import org.onap.dcae.collectors.veshv.utils.arrow.doOnFailure
37 import org.onap.dcae.collectors.veshv.utils.arrow.doOnLeft
38 import org.onap.dcae.collectors.veshv.utils.logging.Logger
39 import org.onap.dcae.collectors.veshv.utils.logging.MessageEither
40 import org.onap.dcae.collectors.veshv.utils.logging.filterEmptyWithLog
41 import org.onap.dcae.collectors.veshv.utils.logging.filterFailedWithLog
42 import reactor.core.publisher.Flux
43 import reactor.core.publisher.Mono
44
45 /**
46  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
47  * @since May 2018
48  */
49 internal class VesHvCollector(
50         private val clientContext: ClientContext,
51         private val wireChunkDecoder: WireChunkDecoder,
52         private val protobufDecoder: VesDecoder,
53         private val router: Router,
54         private val sink: Sink,
55         private val metrics: Metrics) : Collector {
56
57     override fun handleConnection(dataStream: Flux<ByteBuf>): Mono<Void> =
58             dataStream
59                     .transform { decodeWireFrame(it) }
60                     .transform(::filterInvalidWireFrame)
61                     .transform(::decodeProtobufPayload)
62                     .transform(::filterInvalidProtobufMessages)
63                     .transform(::routeMessage)
64                     .onErrorResume { logger.handleReactiveStreamError(clientContext, it) }
65                     .doFinally { releaseBuffersMemory() }
66                     .then()
67
68     private fun decodeWireFrame(flux: Flux<ByteBuf>): Flux<WireFrameMessage> = flux
69             .doOnNext { metrics.notifyBytesReceived(it.readableBytes()) }
70             .concatMap(wireChunkDecoder::decode)
71             .doOnNext { metrics.notifyMessageReceived(it.payloadSize) }
72
73     private fun filterInvalidWireFrame(flux: Flux<WireFrameMessage>): Flux<WireFrameMessage> = flux
74             .filterFailedWithLog {
75                 MessageValidator
76                         .validateFrameMessage(it)
77                         .doOnLeft { metrics.notifyMessageDropped(INVALID_MESSAGE) }
78             }
79
80     private fun decodeProtobufPayload(flux: Flux<WireFrameMessage>): Flux<VesMessage> = flux
81             .map(WireFrameMessage::payload)
82             .flatMap(::decodePayload)
83
84     private fun decodePayload(rawPayload: ByteData): Flux<VesMessage> = protobufDecoder
85             .decode(rawPayload)
86             .doOnFailure { metrics.notifyMessageDropped(INVALID_MESSAGE) }
87             .filterFailedWithLog(logger, clientContext::fullMdc,
88                     { "Ves event header decoded successfully" },
89                     { "Failed to decode ves event header, reason: ${it.message}" })
90
91     private fun filterInvalidProtobufMessages(flux: Flux<VesMessage>): Flux<VesMessage> = flux
92             .filterFailedWithLog {
93                 MessageValidator
94                         .validateProtobufMessage(it)
95                         .doOnLeft { metrics.notifyMessageDropped(INVALID_MESSAGE) }
96             }
97
98     private fun routeMessage(flux: Flux<VesMessage>): Flux<RoutedMessage> = flux
99             .flatMap(this::findRoute)
100             .compose(sink::send)
101             .doOnNext { metrics.notifyMessageSent(it.topic) }
102
103     private fun findRoute(msg: VesMessage) = router
104             .findDestination(msg)
105             .doOnEmpty { metrics.notifyMessageDropped(ROUTE_NOT_FOUND) }
106             .filterEmptyWithLog(logger, clientContext::fullMdc,
107                     { "Found route for message: ${it.topic}, partition: ${it.partition}" },
108                     { "Could not find route for message" })
109
110     private fun releaseBuffersMemory() = wireChunkDecoder.release()
111             .also { logger.debug { "Released buffer memory after handling message stream" } }
112
113     private fun <T> Flux<T>.filterFailedWithLog(predicate: (T) -> MessageEither): Flux<T> =
114             filterFailedWithLog(logger, clientContext::fullMdc, predicate)
115
116     companion object {
117         private val logger = Logger(VesHvCollector::class)
118     }
119 }