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