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