2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018-2019 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.Option
23 import arrow.core.getOrElse
24 import io.netty.handler.ssl.SslContext
25 import org.onap.dcae.collectors.veshv.api.Server
26 import org.onap.dcae.collectors.veshv.boundary.Collector
27 import org.onap.dcae.collectors.veshv.boundary.CollectorFactory
28 import org.onap.dcae.collectors.veshv.boundary.Metrics
29 import org.onap.dcae.collectors.veshv.config.api.model.ServerConfiguration
30 import org.onap.dcae.collectors.veshv.domain.logging.ClientContext
31 import org.onap.dcae.collectors.veshv.domain.logging.ServiceContext
32 import org.onap.dcae.collectors.veshv.utils.NettyServerHandle
33 import org.onap.dcae.collectors.veshv.utils.ServerHandle
34 import org.onap.dcae.collectors.veshv.utils.logging.Logger
35 import org.onap.dcae.collectors.veshv.domain.logging.Marker
36 import org.onap.dcae.collectors.veshv.domain.logging.MarkerLogging.debug
37 import org.onap.dcae.collectors.veshv.domain.logging.MarkerLogging.info
38 import org.onap.dcae.collectors.veshv.domain.logging.MarkerLogging.warn
39 import org.onap.dcae.collectors.veshv.domain.logging.ClientContextLogging.debug
40 import org.onap.dcae.collectors.veshv.domain.logging.ClientContextLogging.info
41 import reactor.core.publisher.Mono
42 import reactor.netty.Connection
43 import reactor.netty.NettyInbound
44 import reactor.netty.NettyOutbound
45 import reactor.netty.tcp.TcpServer
46 import java.net.InetAddress
47 import java.net.InetSocketAddress
48 import java.time.Duration
52 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
55 internal class NettyTcpServer(private val serverConfiguration: ServerConfiguration,
56 private val sslContext: Option<SslContext>,
57 private val collectorFactory: CollectorFactory,
58 private val metrics: Metrics) : Server {
60 override fun start(): Mono<ServerHandle> =
63 .addressSupplier { InetSocketAddress(serverConfiguration.listenPort) }
65 .handle(this::handleConnection)
68 NettyServerHandle(it, closeAction())
72 private fun closeAction(): Mono<Void> =
73 collectorFactory.close().doOnSuccess {
74 logger.info(ServiceContext::mdc) { "Netty TCP Server closed" }
78 private fun TcpServer.configureSsl() =
80 .map { serverContext ->
81 logger.info { "Collector configured with SSL enabled" }
82 this.secure { it.sslContext(serverContext) }
84 logger.info { "Collector configured with SSL disabled" }
88 private fun handleConnection(nettyInbound: NettyInbound, nettyOutbound: NettyOutbound): Mono<Void> =
89 messageHandlingStream(nettyInbound, nettyOutbound).run {
91 nettyOutbound.neverComplete()
94 private fun messageHandlingStream(nettyInbound: NettyInbound, nettyOutbound: NettyOutbound): Mono<Void> =
95 withNewClientContextFrom(nettyInbound, nettyOutbound)
97 logger.debug(clientContext::fullMdc) { "Client connection request received" }
99 clientContext.clientAddress
100 .map { acceptIfNotLocalConnection(it, clientContext, nettyInbound) }
102 logger.warn(clientContext::fullMdc) {
103 "Client address could not be resolved. Discarding connection"
105 nettyInbound.closeConnectionAndReturn(Mono.empty())
109 private fun acceptIfNotLocalConnection(address: InetAddress,
110 clientContext: ClientContext,
111 nettyInbound: NettyInbound): Mono<Void> =
112 if (address.isLocalClientAddress()) {
113 logger.debug(clientContext) {
114 "Client address resolved to localhost. Discarding connection as suspected healthcheck"
116 nettyInbound.closeConnectionAndReturn(Mono.empty<Void>())
118 acceptClientConnection(clientContext, nettyInbound)
121 private fun acceptClientConnection(clientContext: ClientContext, nettyInbound: NettyInbound): Mono<Void> {
122 metrics.notifyClientConnected()
123 logger.info(clientContext::fullMdc, Marker.Entry) { "Handling new client connection" }
124 val collector = collectorFactory(clientContext)
125 return collector.handleClient(clientContext, nettyInbound)
128 private fun Collector.handleClient(clientContext: ClientContext,
129 nettyInbound: NettyInbound) =
130 withConnectionFrom(nettyInbound) { connection ->
132 .configureIdleTimeout(clientContext, serverConfiguration.idleTimeout)
133 .logConnectionClosed(clientContext)
135 handleConnection(nettyInbound.createDataStream())
138 private fun Connection.configureIdleTimeout(ctx: ClientContext, timeout: Duration): Connection =
139 onReadIdle(timeout.toMillis()) {
141 "Idle timeout of ${timeout.seconds} s reached. Closing connection from ${address()}..."
143 disconnectClient(ctx)
146 private fun Connection.disconnectClient(ctx: ClientContext) =
147 closeChannelAndThen {
149 logger.debug(ctx::fullMdc, Marker.Exit) { "Channel closed successfully." }
151 logger.warn(ctx::fullMdc, Marker.Exit, { "Channel close failed" }, it.cause())
154 private fun Connection.logConnectionClosed(ctx: ClientContext): Connection =
156 metrics.notifyClientDisconnected()
157 logger.info(ctx::fullMdc, Marker.Exit) { "Connection has been closed" }
161 private val logger = Logger(NettyTcpServer::class)