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
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.simulators.xnf.impl.adapters
22 import arrow.core.Option
23 import io.netty.handler.ssl.ClientAuth
24 import io.netty.handler.ssl.SslContext
25 import io.netty.handler.ssl.SslContextBuilder
26 import io.netty.handler.ssl.SslProvider
27 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
28 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
29 import org.onap.dcae.collectors.veshv.domain.WireFrameEncoder
30 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.config.SimulatorConfiguration
31 import org.onap.dcae.collectors.veshv.ssl.boundary.ClientSslContextFactory
32 import org.onap.dcae.collectors.veshv.utils.arrow.asIo
33 import org.onap.dcae.collectors.veshv.utils.logging.Logger
34 import org.reactivestreams.Publisher
35 import reactor.core.publisher.Flux
36 import reactor.core.publisher.Mono
37 import reactor.core.publisher.ReplayProcessor
38 import reactor.netty.NettyOutbound
39 import reactor.netty.tcp.TcpClient
42 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
45 class VesHvClient(private val configuration: SimulatorConfiguration) {
47 private val client: TcpClient = TcpClient.create()
48 .host(configuration.vesHost)
49 .port(configuration.vesPort)
51 createSslContext(configuration.security).fold({}, sslSpec::sslContext)
54 fun sendIo(messages: Flux<WireFrameMessage>) =
55 sendRx(messages).then(Mono.just(Unit)).asIo()
57 private fun sendRx(messages: Flux<WireFrameMessage>): Mono<Void> {
58 val complete = ReplayProcessor.create<Void>(1)
60 .handle { _, output -> handler(complete, messages, output) }
63 logger.info("Failed to connect to VesHvCollector on " +
64 "${configuration.vesHost}:${configuration.vesPort}")
67 logger.info("Connected to VesHvCollector on " +
68 "${configuration.vesHost}:${configuration.vesPort}")
70 return complete.then()
73 private fun handler(complete: ReplayProcessor<Void>,
74 messages: Flux<WireFrameMessage>,
75 nettyOutbound: NettyOutbound): Publisher<Void> {
77 val allocator = nettyOutbound.alloc()
78 val encoder = WireFrameEncoder(allocator)
81 .window(MAX_BATCH_SIZE)
84 .logConnectionClosed()
85 .options { it.flushOnBoundary() }
88 logger.info("Messages have been sent")
94 private fun createSslContext(config: SecurityConfiguration): Option<SslContext> =
95 ClientSslContextFactory().createSslContext(config)
97 private fun NettyOutbound.logConnectionClosed() =
98 withConnection { conn ->
99 conn.onTerminate().subscribe {
100 logger.info { "Connection to ${conn.address()} has been closed" }
105 private val logger = Logger(VesHvClient::class)
106 private const val MAX_BATCH_SIZE = 128