cb56db91cb11bb1978abe0f91751a74e06ef38d3
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
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.simulators.xnf.impl
21
22 import io.netty.buffer.Unpooled
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.WireFrame
28 import org.onap.dcae.collectors.veshv.domain.WireFrameEncoder
29 import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientConfiguration
30 import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientSecurityConfiguration
31 import org.onap.dcae.collectors.veshv.utils.logging.Logger
32 import org.reactivestreams.Publisher
33 import reactor.core.publisher.Flux
34 import reactor.core.publisher.Mono
35 import reactor.ipc.netty.NettyInbound
36 import reactor.ipc.netty.NettyOutbound
37 import reactor.ipc.netty.tcp.TcpClient
38 import java.util.function.BiFunction
39
40
41 /**
42  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
43  * @since June 2018
44  */
45 class VesHvClient(configuration: ClientConfiguration) {
46
47     private val client: TcpClient = TcpClient.builder()
48             .options { opts ->
49                 opts.host(configuration.vesHost)
50                         .port(configuration.vesPort)
51                         .sslContext(createSslContext(configuration.security))
52             }
53             .build()
54
55     fun send(messages: Flux<WireFrame>) {
56         client.startAndAwait(BiFunction { i, o -> handler(i, o, messages) })
57     }
58
59     private fun handler(nettyInbound: NettyInbound,
60                         nettyOutbound: NettyOutbound,
61                         messages: Flux<WireFrame>): Publisher<Void> {
62
63         nettyInbound
64                 .receive()
65                 .asString(Charsets.UTF_8)
66                 .subscribe { str -> logger.info("Server response: $str") }
67
68         val encoder = WireFrameEncoder(nettyOutbound.alloc())
69
70         val frames = messages
71                 .map(encoder::encode)
72                 .concatWith(Mono.just(Unpooled.EMPTY_BUFFER))
73
74         return nettyOutbound
75                 .options { it.flushOnEach() }
76                 .send(frames)
77     }
78
79     private fun createSslContext(config: ClientSecurityConfiguration): SslContext =
80             SslContextBuilder.forClient()
81                     .keyManager(config.cert.toFile(), config.privateKey.toFile())
82                     .trustManager(config.trustedCert.toFile())
83                     .sslProvider(SslProvider.OPENSSL)
84                     .clientAuth(ClientAuth.REQUIRE)
85                     .build()
86
87     companion object {
88         private val logger = Logger(VesHvClient::class)
89     }
90 }