Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / hv-collector-ves-message-generator / src / main / kotlin / org / onap / dcae / collectors / veshv / ves / message / generator / impl / MessageGeneratorImpl.kt
1 /*
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
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.ves.message.generator.impl
21
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.WireFrameMessage
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.VesEventOuterClass.CommonEventHeader
35 import org.onap.ves.VesEventOuterClass.VesEvent
36 import reactor.core.publisher.Flux
37 import reactor.core.publisher.Mono
38 import java.nio.charset.Charset
39
40 /**
41  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
42  * @since June 2018
43  */
44 class MessageGeneratorImpl internal constructor(
45         private val payloadGenerator: PayloadGenerator,
46         private val maxPayloadSizeBytes: Int
47 ) : MessageGenerator {
48
49     override fun createMessageFlux(messageParameters: List<MessageParameters>): Flux<WireFrameMessage> = Flux
50             .fromIterable(messageParameters)
51             .flatMap { createMessageFlux(it) }
52
53     private fun createMessageFlux(parameters: MessageParameters): Flux<WireFrameMessage> =
54             Mono.fromCallable { createMessage(parameters.commonEventHeader, parameters.messageType) }
55                     .let {
56                         when {
57                             parameters.amount < 0 ->
58                                 // repeat forever
59                                 it.repeat()
60                             parameters.amount == 0L ->
61                                 // do not generate any message
62                                 Flux.empty()
63                             else ->
64                                 // send original message and additional amount-1 messages
65                                 it.repeat(parameters.amount - 1)
66                         }
67                     }
68
69     private fun createMessage(commonEventHeader: CommonEventHeader, messageType: MessageType): WireFrameMessage =
70             when (messageType) {
71                 VALID ->
72                     WireFrameMessage(vesEvent(commonEventHeader, payloadGenerator.generatePayload()))
73                 TOO_BIG_PAYLOAD ->
74                     WireFrameMessage(vesEvent(commonEventHeader, oversizedPayload()))
75                 FIXED_PAYLOAD ->
76                     WireFrameMessage(vesEvent(commonEventHeader, fixedPayload()))
77                 INVALID_WIRE_FRAME -> {
78                     val payload = ByteData(vesEvent(commonEventHeader, payloadGenerator.generatePayload()))
79                     WireFrameMessage(
80                             payload,
81                             UNSUPPORTED_VERSION,
82                             UNSUPPORTED_VERSION,
83                             PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
84                             payload.size())
85                 }
86                 INVALID_GPB_DATA ->
87                     WireFrameMessage("invalid vesEvent".toByteArray(Charset.defaultCharset()))
88             }
89
90     private fun vesEvent(commonEventHeader: CommonEventHeader, eventFields: ByteString): ByteArray {
91         return createVesEvent(commonEventHeader, eventFields).toByteArray()
92     }
93
94     private fun createVesEvent(commonEventHeader: CommonEventHeader, payload: ByteString): VesEvent =
95             VesEvent.newBuilder()
96                     .setCommonEventHeader(commonEventHeader)
97                     .setEventFields(payload)
98                     .build()
99
100     private fun oversizedPayload() =
101             payloadGenerator.generateRawPayload(maxPayloadSizeBytes + 1)
102
103     private fun fixedPayload() =
104             payloadGenerator.generateRawPayload(MessageGenerator.FIXED_PAYLOAD_SIZE)
105
106     companion object {
107         private const val UNSUPPORTED_VERSION: Short = 2
108     }
109 }