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.*
30 import org.onap.ves.HVRanMeasFieldsV5
31 import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields
32 import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload
33 import org.onap.ves.VesEventV5.VesEvent
34 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader
35 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain
36 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain.OTHER
37 import reactor.core.publisher.Flux
38 import reactor.core.publisher.Mono
39 import java.nio.charset.Charset
42 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
45 class MessageGeneratorImpl internal constructor(private val payloadGenerator: PayloadGenerator) : MessageGenerator {
47 override fun createMessageFlux(messageParameters: List<MessageParameters>): Flux<PayloadWireFrameMessage> = Flux
48 .fromIterable(messageParameters)
49 .flatMap { createMessageFlux(it) }
51 private fun createMessageFlux(parameters: MessageParameters): Flux<PayloadWireFrameMessage> =
52 Mono.fromCallable { createMessage(parameters.domain, parameters.messageType) }
54 if (parameters.amount < 0)
57 it.repeat(parameters.amount)
60 private fun createMessage(domain: Domain, messageType: MessageType): PayloadWireFrameMessage =
63 PayloadWireFrameMessage(vesEvent(domain, payloadGenerator.generatePayload()))
65 PayloadWireFrameMessage(vesEvent(domain, oversizedPayload()))
67 PayloadWireFrameMessage(vesEvent(OTHER, payloadGenerator.generatePayload()))
68 INVALID_WIRE_FRAME -> {
69 val payload = ByteData(vesEvent(domain, payloadGenerator.generatePayload()))
70 PayloadWireFrameMessage(
73 PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
77 PayloadWireFrameMessage("invalid vesEvent".toByteArray(Charset.defaultCharset()))
80 private fun vesEvent(domain: Domain, hvRanMeasPayload: HVRanMeasPayload): ByteArray {
81 return vesEvent(domain, hvRanMeasPayload.toByteString())
84 private fun vesEvent(domain: Domain, hvRanMeasPayload: ByteString): ByteArray {
85 return createVesEvent(createCommonHeader(domain), hvRanMeasPayload).toByteArray()
88 private fun createVesEvent(commonEventHeader: CommonEventHeader, payload: ByteString): VesEvent =
90 .setCommonEventHeader(commonEventHeader)
91 .setHvRanMeasFields(payload)
94 private fun oversizedPayload() =
95 payloadGenerator.generateRawPayload(PayloadWireFrameMessage.MAX_PAYLOAD_SIZE + 1)
98 private fun createCommonHeader(domain: Domain): CommonEventHeader = CommonEventHeader.newBuilder()
99 .setVersion("sample-version")
102 .setPriority(CommonEventHeader.Priority.NORMAL)
103 .setEventId("sample-event-id")
104 .setEventName("sample-event-name")
105 .setEventType("sample-event-type")
106 .setStartEpochMicrosec(SAMPLE_START_EPOCH)
107 .setLastEpochMicrosec(SAMPLE_LAST_EPOCH)
108 .setNfNamingCode("sample-nf-naming-code")
109 .setNfcNamingCode("sample-nfc-naming-code")
110 .setReportingEntityId("sample-reporting-entity-id")
111 .setReportingEntityName(ByteString.copyFromUtf8("sample-reporting-entity-name"))
112 .setSourceId(ByteString.copyFromUtf8("sample-source-id"))
113 .setSourceName("sample-source-name")
117 private const val UNSUPPORTED_VERSION: Short = 2
118 private const val SAMPLE_START_EPOCH = 120034455L
119 private const val SAMPLE_LAST_EPOCH = 120034455L