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