b735138def550ebeacd95b2b2e0e7d239aa59915
[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.impl.wire
21
22 import arrow.effects.IO
23 import io.netty.buffer.ByteBuf
24 import org.onap.dcae.collectors.veshv.domain.InvalidWireFrame
25 import org.onap.dcae.collectors.veshv.domain.MissingWireFrameBytes
26 import org.onap.dcae.collectors.veshv.domain.WireFrameDecoder
27 import org.onap.dcae.collectors.veshv.domain.WireFrameDecodingError
28 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
29 import org.onap.dcae.collectors.veshv.model.ClientContext
30 import org.onap.dcae.collectors.veshv.utils.logging.Logger
31 import org.onap.dcae.collectors.veshv.impl.adapters.ClientContextLogging.handleReactiveStreamError
32 import org.onap.dcae.collectors.veshv.impl.adapters.ClientContextLogging.trace
33 import reactor.core.publisher.Flux
34 import reactor.core.publisher.Flux.defer
35 import reactor.core.publisher.SynchronousSink
36
37 /**
38  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
39  * @since May 2018
40  */
41 internal class WireChunkDecoder(
42         private val decoder: WireFrameDecoder,
43         private val ctx: ClientContext) {
44     private val streamBuffer = ctx.alloc.compositeBuffer()
45
46     fun release() {
47         streamBuffer.release()
48     }
49
50     fun decode(byteBuf: ByteBuf): Flux<WireFrameMessage> = defer {
51         logIncomingMessage(byteBuf)
52         if (byteBuf.readableBytes() == 0) {
53             byteBuf.release()
54             Flux.empty()
55         } else {
56             streamBuffer.addComponent(true, byteBuf)
57             generateFrames()
58                     .onErrorResume { logger.handleReactiveStreamError(ctx, it, Flux.error(it)) }
59                     .doFinally { streamBuffer.discardReadComponents() }
60         }
61     }
62
63     private fun generateFrames(): Flux<WireFrameMessage> = Flux.generate { next ->
64         decoder.decodeFirst(streamBuffer)
65                 .fold(onError(next), onSuccess(next))
66                 .unsafeRunSync()
67     }
68
69     private fun onError(next: SynchronousSink<WireFrameMessage>): (WireFrameDecodingError) -> IO<Unit> = { err ->
70         when (err) {
71             is InvalidWireFrame -> IO {
72                 next.error(WireFrameException(err))
73             }
74             is MissingWireFrameBytes -> IO {
75                 logEndOfData()
76                 next.complete()
77             }
78         }
79     }
80
81     private fun onSuccess(next: SynchronousSink<WireFrameMessage>): (WireFrameMessage) -> IO<Unit> = { frame ->
82         IO {
83             logDecodedWireMessage(frame)
84             next.next(frame)
85         }
86     }
87
88     private fun logIncomingMessage(wire: ByteBuf) {
89         logger.trace(ctx) { "Got message with total size of ${wire.readableBytes()} B" }
90     }
91
92     private fun logDecodedWireMessage(wire: WireFrameMessage) {
93         logger.trace(ctx) { "Wire payload size: ${wire.payloadSize} B" }
94     }
95
96     private fun logEndOfData() {
97         logger.trace(ctx) { "End of data in current TCP buffer" }
98     }
99
100     companion object {
101         val logger = Logger(WireChunkDecoder::class)
102     }
103 }