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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.simulators.xnf.impl
22 import arrow.core.Either
23 import arrow.core.Some
25 import arrow.core.extensions.either.monad.monad
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
47 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
51 private val clientFactory: ClientFactory,
52 private val generatorFactory: MessageGeneratorFactory,
53 private val messageParametersParser: MessageParametersParser = MessageParametersParser.INSTANCE) {
55 private val wireFrameGenerator by lazy { generatorFactory.createWireFrameGenerator() }
56 private val vesEventGenerator by lazy { generatorFactory.createVesEventGenerator() }
58 private val defaultHvVesClient by lazy { clientFactory.create() }
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)
67 private fun parseJsonArray(jsonStream: InputStream): Either<ParsingError, JsonArray> =
68 Try { Json.createReader(jsonStream).readArray() }
70 .mapLeft { ParsingError("Failed to parse JSON", Some(it)) }
73 private fun simulationFrom(parameters: List<MessageParameters>): Mono<Void> =
75 .map(::asClientToMessages)
76 .groupMessagesByClients()
77 .flattenValuesToFlux()
83 private fun <M> List<Pair<HvVesClient, M>>.groupMessagesByClients() =
84 groupBy({ it.first }, { it.second })
86 private fun <K> Map<K, List<Flux<ByteBuffer>>>.flattenValuesToFlux(): Map<K, Flux<ByteBuffer>> =
87 mapValues { Flux.concat(it.value) }
89 private fun asClientToMessages(parameters: MessageParameters) =
91 is VesEventParameters -> {
92 val messages = vesEventGenerator
93 .createMessageFlux(parameters)
94 .map(VesEventOuterClass.VesEvent::toByteBuffer)
95 Pair(defaultHvVesClient, messages)
97 is WireFrameParameters -> {
98 val messages = wireFrameGenerator.createMessageFlux(parameters)
99 val client = clientFactory.create(parameters.wireFrameVersion)
100 Pair(client, messages)
104 private fun simulate(pair: Pair<HvVesClient, Flux<ByteBuffer>>): Disposable =
106 .sendRawPayload(pair.second, PayloadType.PROTOBUF)
110 internal fun VesEventOuterClass.VesEvent.toByteBuffer() = toByteString().asReadOnlyByteBuffer()