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
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.ves.message.generator.impl
22 import com.google.protobuf.ByteString
23 import org.onap.dcae.collectors.veshv.domain.ByteData
24 import org.onap.dcae.collectors.veshv.domain.PayloadContentType
25 import org.onap.dcae.collectors.veshv.domain.PayloadWireFrameMessage
26 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageGenerator
27 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters
28 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType
29 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.FIXED_PAYLOAD
30 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.INVALID_GPB_DATA
31 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.INVALID_WIRE_FRAME
32 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.TOO_BIG_PAYLOAD
33 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.VALID
34 import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload
35 import org.onap.ves.VesEventV5.VesEvent
36 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader
37 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain
38 import reactor.core.publisher.Flux
39 import reactor.core.publisher.Mono
40 import java.nio.charset.Charset
43 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
46 class MessageGeneratorImpl internal constructor(private val payloadGenerator: PayloadGenerator) : MessageGenerator {
48 override fun createMessageFlux(messageParameters: List<MessageParameters>): Flux<PayloadWireFrameMessage> = Flux
49 .fromIterable(messageParameters)
50 .flatMap { createMessageFlux(it) }
52 private fun createMessageFlux(parameters: MessageParameters): Flux<PayloadWireFrameMessage> =
53 Mono.fromCallable { createMessage(parameters.commonEventHeader, parameters.messageType) }
55 if (parameters.amount < 0)
58 it.repeat(parameters.amount)
61 private fun createMessage(commonEventHeader: CommonEventHeader, messageType: MessageType): PayloadWireFrameMessage =
64 PayloadWireFrameMessage(vesEvent(commonEventHeader, payloadGenerator.generatePayload()))
66 PayloadWireFrameMessage(vesEvent(commonEventHeader, oversizedPayload()))
68 PayloadWireFrameMessage(vesEvent(commonEventHeader, fixedPayload()))
69 INVALID_WIRE_FRAME -> {
70 val payload = ByteData(vesEvent(commonEventHeader, payloadGenerator.generatePayload()))
71 PayloadWireFrameMessage(
74 PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
78 PayloadWireFrameMessage("invalid vesEvent".toByteArray(Charset.defaultCharset()))
81 private fun vesEvent(commonEventHeader: CommonEventHeader, hvRanMeasPayload: HVRanMeasPayload): ByteArray {
82 return vesEvent(commonEventHeader, hvRanMeasPayload.toByteString())
85 private fun vesEvent(commonEventHeader: CommonEventHeader, hvRanMeasPayload: ByteString): ByteArray {
86 return createVesEvent(commonEventHeader, hvRanMeasPayload).toByteArray()
89 private fun createVesEvent(commonEventHeader: CommonEventHeader, payload: ByteString): VesEvent =
91 .setCommonEventHeader(commonEventHeader)
92 .setHvRanMeasFields(payload)
95 private fun oversizedPayload() =
96 payloadGenerator.generateRawPayload(PayloadWireFrameMessage.MAX_PAYLOAD_SIZE + 1)
98 private fun fixedPayload() =
99 payloadGenerator.generateRawPayload(MessageGenerator.FIXED_PAYLOAD_SIZE)
102 private const val UNSUPPORTED_VERSION: Short = 2