Get rid of arrow-effects usage
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-xnf-simulator / src / main / kotlin / org / onap / dcae / collectors / veshv / simulators / xnf / impl / XnfSimulator.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-2019 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 arrow.core.Either
23 import arrow.core.Some
24 import arrow.core.Try
25 import arrow.core.extensions.either.monad.monad
26 import arrow.core.fix
27 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.adapters.HvVesClient
28 import org.onap.dcae.collectors.veshv.simulators.xnf.impl.factory.ClientFactory
29 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters
30 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser
31 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
32 import org.onap.dcae.collectors.veshv.ves.message.generator.api.VesEventParameters
33 import org.onap.dcae.collectors.veshv.ves.message.generator.api.WireFrameParameters
34 import org.onap.dcae.collectors.veshv.ves.message.generator.factory.MessageGeneratorFactory
35 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.PayloadType
36 import org.onap.ves.VesEventOuterClass
37 import reactor.core.Disposable
38 import reactor.core.publisher.Flux
39 import reactor.core.publisher.Mono
40 import reactor.core.publisher.toFlux
41 import java.io.InputStream
42 import java.nio.ByteBuffer
43 import javax.json.Json
44 import javax.json.JsonArray
45
46 /**
47  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
48  * @since August 2018
49  */
50 class XnfSimulator(
51         private val clientFactory: ClientFactory,
52         private val generatorFactory: MessageGeneratorFactory,
53         private val messageParametersParser: MessageParametersParser = MessageParametersParser.INSTANCE) {
54
55     private val wireFrameGenerator by lazy { generatorFactory.createWireFrameGenerator() }
56     private val vesEventGenerator by lazy { generatorFactory.createVesEventGenerator() }
57
58     private val defaultHvVesClient by lazy { clientFactory.create() }
59
60     fun startSimulation(messageParameters: InputStream): Either<ParsingError, Mono<Void>> =
61             Either.monad<ParsingError>().binding {
62                 val json = parseJsonArray(messageParameters).bind()
63                 val parameters = messageParametersParser.parse(json).bind()
64                 simulationFrom(parameters)
65             }.fix()
66
67     private fun parseJsonArray(jsonStream: InputStream): Either<ParsingError, JsonArray> =
68             Try { Json.createReader(jsonStream).readArray() }
69                     .toEither()
70                     .mapLeft { ParsingError("Failed to parse JSON", Some(it)) }
71
72
73     private fun simulationFrom(parameters: List<MessageParameters>): Mono<Void> =
74             parameters
75                     .map(::asClientToMessages)
76                     .groupMessagesByClients()
77                     .flattenValuesToFlux()
78                     .toList()
79                     .toFlux()
80                     .map(::simulate)
81                     .then()
82
83     private fun <M> List<Pair<HvVesClient, M>>.groupMessagesByClients() =
84             groupBy({ it.first }, { it.second })
85
86     private fun <K> Map<K, List<Flux<ByteBuffer>>>.flattenValuesToFlux(): Map<K, Flux<ByteBuffer>> =
87             mapValues { Flux.concat(it.value) }
88
89     private fun asClientToMessages(parameters: MessageParameters) =
90             when (parameters) {
91                 is VesEventParameters -> {
92                     val messages = vesEventGenerator
93                             .createMessageFlux(parameters)
94                             .map(VesEventOuterClass.VesEvent::toByteBuffer)
95                     Pair(defaultHvVesClient, messages)
96                 }
97                 is WireFrameParameters -> {
98                     val messages = wireFrameGenerator.createMessageFlux(parameters)
99                     val client = clientFactory.create(parameters.wireFrameVersion)
100                     Pair(client, messages)
101                 }
102             }
103
104     private fun simulate(pair: Pair<HvVesClient, Flux<ByteBuffer>>): Disposable =
105             pair.first
106                     .sendRawPayload(pair.second, PayloadType.PROTOBUF)
107                     .subscribe()
108 }
109
110 internal fun VesEventOuterClass.VesEvent.toByteBuffer() = toByteString().asReadOnlyByteBuffer()