summaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/NettyTcpServer.kt
blob: 2d29fe99ad055612453ca6966a91cb0b4f1729eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * ============LICENSE_START=======================================================
 * dcaegen2-collectors-veshv
 * ================================================================================
 * Copyright (C) 2018 NOKIA
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dcae.collectors.veshv.impl.socket

import arrow.core.getOrElse
import arrow.effects.IO
import org.onap.dcae.collectors.veshv.boundary.CollectorProvider
import org.onap.dcae.collectors.veshv.boundary.Server
import org.onap.dcae.collectors.veshv.model.ClientContext
import org.onap.dcae.collectors.veshv.model.ClientContextLogging.info
import org.onap.dcae.collectors.veshv.model.ClientContextLogging.debug
import org.onap.dcae.collectors.veshv.model.ClientContextLogging.withWarn
import org.onap.dcae.collectors.veshv.model.ServerConfiguration
import org.onap.dcae.collectors.veshv.ssl.boundary.ServerSslContextFactory
import org.onap.dcae.collectors.veshv.utils.NettyServerHandle
import org.onap.dcae.collectors.veshv.utils.ServerHandle
import org.onap.dcae.collectors.veshv.utils.logging.Logger
import reactor.core.publisher.Mono
import reactor.netty.ByteBufFlux
import reactor.netty.Connection
import reactor.netty.NettyInbound
import reactor.netty.NettyOutbound
import reactor.netty.tcp.TcpServer
import java.time.Duration

/**
 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
 * @since May 2018
 */
internal class NettyTcpServer(private val serverConfig: ServerConfiguration,
                              private val sslContextFactory: ServerSslContextFactory,
                              private val collectorProvider: CollectorProvider) : Server {

    override fun start(): IO<ServerHandle> = IO {
        val tcpServer = TcpServer.create()
                .addressSupplier { serverConfig.serverListenAddress }
                .configureSsl()
                .handle(this::handleConnection)

        NettyServerHandle(tcpServer.bindNow())
    }

    private fun TcpServer.configureSsl() =
            sslContextFactory
                    .createSslContext(serverConfig.securityConfiguration)
                    .map { sslContext ->
                        logger.info { "Collector configured with SSL enabled" }
                        this.secure { b -> b.sslContext(sslContext) }
                    }.getOrElse {
                        logger.info { "Collector configured with SSL disabled" }
                        this
                    }

    private fun handleConnection(nettyInbound: NettyInbound, nettyOutbound: NettyOutbound): Mono<Void> {
        val clientContext = ClientContext(nettyOutbound.alloc())
        nettyInbound.withConnection {
            clientContext.clientAddress = it.address()
        }

        return collectorProvider(clientContext).fold(
                {
                    logger.warn(clientContext::asMap) { "Collector not ready. Closing connection..." }
                    Mono.empty()
                },
                {
                    logger.info { "Handling new connection" }
                    nettyInbound.withConnection { conn ->
                        conn.configureIdleTimeout(clientContext, serverConfig.idleTimeout)
                                .logConnectionClosed(clientContext)
                    }
                    it.handleConnection(createDataStream(nettyInbound))
                }
        )
    }

    private fun createDataStream(nettyInbound: NettyInbound): ByteBufFlux = nettyInbound
            .receive()
            .retain()

    private fun Connection.configureIdleTimeout(ctx: ClientContext, timeout: Duration): Connection {
        onReadIdle(timeout.toMillis()) {
            logger.info(ctx) {
                "Idle timeout of ${timeout.seconds} s reached. Closing connection from ${address()}..."
            }
            disconnectClient(ctx)
        }
        return this
    }

    private fun Connection.disconnectClient(ctx: ClientContext) {
        channel().close().addListener {
            if (it.isSuccess)
                logger.debug(ctx) { "Channel closed successfully." }
            else
                logger.withWarn(ctx) { log("Channel close failed", it.cause()) }
        }
    }

    private fun Connection.logConnectionClosed(ctx: ClientContext): Connection {
        onTerminate().subscribe {
            logger.info(ctx) { "Connection has been closed" }
        }
        return this
    }

    companion object {
        private val logger = Logger(NettyTcpServer::class)
    }
}