2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
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.main.impl
22 import io.netty.buffer.ByteBufAllocator
23 import org.onap.dcae.collectors.veshv.domain.WireFrame
24 import org.onap.dcae.collectors.veshv.main.config.ClientConfiguration
25 import org.onap.dcae.collectors.veshv.utils.logging.Logger
26 import org.reactivestreams.Publisher
27 import reactor.core.publisher.Flux
28 import reactor.ipc.netty.NettyInbound
29 import reactor.ipc.netty.NettyOutbound
30 import reactor.ipc.netty.tcp.TcpClient
31 import java.util.function.BiFunction
35 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
38 class VesHvClient(configuration: ClientConfiguration) {
40 private val logger = Logger(VesHvClient::class)
41 private val client: TcpClient = TcpClient.create(configuration.vesHost, configuration.vesPort)
43 fun send(messages: Flux<WireFrame>) {
44 client.start(BiFunction { i, o -> handler(i, o, messages) })
47 // sending flux with multiple WireFrames not supported yet
48 private fun handler(nettyInbound: NettyInbound,
49 nettyOutbound: NettyOutbound,
50 messages: Flux<WireFrame>): Publisher<Void> {
54 .asString(Charsets.UTF_8)
55 .subscribe { str -> logger.info("Server response: $str") }
58 .options { it.flushOnEach() }
59 .send(messages.map { it.encode(ByteBufAllocator.DEFAULT) })