98758c25bee8fee060764849bf38fed1604420c4
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
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.main.impl
21
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
32
33
34 /**
35  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
36  * @since June 2018
37  */
38 class VesHvClient(configuration: ClientConfiguration) {
39
40     private val logger = Logger(VesHvClient::class)
41     private val client: TcpClient = TcpClient.create(configuration.vesHost, configuration.vesPort)
42
43     fun send(messages: Flux<WireFrame>) {
44         client.start(BiFunction { i, o -> handler(i, o, messages) })
45     }
46
47     // sending flux with multiple WireFrames not supported yet
48     private fun handler(nettyInbound: NettyInbound,
49                         nettyOutbound: NettyOutbound,
50                         messages: Flux<WireFrame>): Publisher<Void> {
51
52         nettyInbound
53                 .receive()
54                 .asString(Charsets.UTF_8)
55                 .subscribe { str -> logger.info("Server response: $str") }
56
57         return nettyOutbound
58                 .options { it.flushOnEach() }
59                 .send(messages.map { it.encode(ByteBufAllocator.DEFAULT) })
60     }
61 }