78a72d937d7e731ca8e2e79a624e0a5245e58e45
[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.SecurityConfiguration
29 import org.onap.dcae.collectors.veshv.domain.WireFrameEncoder
30 import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientConfiguration
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.NettyOutbound
36 import reactor.ipc.netty.tcp.TcpClient
37
38
39 /**
40  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
41  * @since June 2018
42  */
43 class VesHvClient(private val configuration: ClientConfiguration) {
44
45     private val client: TcpClient = TcpClient.builder()
46             .options { opts ->
47                 opts.host(configuration.vesHost)
48                         .port(configuration.vesPort)
49                         .sslContext(createSslContext(configuration.security))
50             }
51             .build()
52
53     fun send(messages: Flux<WireFrame>) =
54             client
55                     .newHandler { _, out -> handler(out, messages) }
56                     .doOnError{logger.info("Failed to connect to VesHvCollector on " +
57                             "${configuration.vesHost}:${configuration.vesPort}")}
58                     .subscribe { logger.info("Connected to VesHvCollector on " +
59                             "${configuration.vesHost}:${configuration.vesPort}") }
60
61
62     private fun handler(nettyOutbound: NettyOutbound, messages: Flux<WireFrame>): Publisher<Void> {
63
64
65         val encoder = WireFrameEncoder(nettyOutbound.alloc())
66
67         val frames = messages
68                 .map(encoder::encode)
69                 .concatWith(Mono.just(Unpooled.EMPTY_BUFFER))
70
71         return nettyOutbound
72                 .options { it.flushOnEach() }
73                 .send(frames)
74                 .then { logger.info("Messages have been sent") }
75     }
76
77     private fun createSslContext(config: SecurityConfiguration): SslContext =
78             SslContextBuilder.forClient()
79                     .keyManager(config.cert.toFile(), config.privateKey.toFile())
80                     .trustManager(config.trustedCert.toFile())
81                     .sslProvider(SslProvider.OPENSSL)
82                     .clientAuth(ClientAuth.REQUIRE)
83                     .build()
84
85     companion object {
86         private val logger = Logger(VesHvClient::class)
87     }
88 }