cc1d16fe7130faa4e5d2a8b31ddd66ebfcfa03db
[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(
46         private val payloadGenerator: PayloadGenerator,
47         private val maxPayloadSizeBytes: Int
48 ) : MessageGenerator {
49
50     override fun createMessageFlux(messageParameters: List<MessageParameters>): Flux<WireFrameMessage> = Flux
51             .fromIterable(messageParameters)
52             .flatMap { createMessageFlux(it) }
53
54     private fun createMessageFlux(parameters: MessageParameters): Flux<WireFrameMessage> =
55             Mono.fromCallable { createMessage(parameters.commonEventHeader, parameters.messageType) }
56                     .let {
57                         if (parameters.amount < 0)
58                             it.repeat()
59                         else
60                             it.repeat(parameters.amount)
61                     }
62
63     private fun createMessage(commonEventHeader: CommonEventHeader, messageType: MessageType): WireFrameMessage =
64             when (messageType) {
65                 VALID ->
66                     WireFrameMessage(vesEvent(commonEventHeader, payloadGenerator.generatePayload()))
67                 TOO_BIG_PAYLOAD ->
68                     WireFrameMessage(vesEvent(commonEventHeader, oversizedPayload()))
69                 FIXED_PAYLOAD ->
70                     WireFrameMessage(vesEvent(commonEventHeader, fixedPayload()))
71                 INVALID_WIRE_FRAME -> {
72                     val payload = ByteData(vesEvent(commonEventHeader, payloadGenerator.generatePayload()))
73                     WireFrameMessage(
74                             payload,
75                             UNSUPPORTED_VERSION,
76                             UNSUPPORTED_VERSION,
77                             PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
78                             payload.size())
79                 }
80                 INVALID_GPB_DATA ->
81                     WireFrameMessage("invalid vesEvent".toByteArray(Charset.defaultCharset()))
82             }
83
84     private fun vesEvent(commonEventHeader: CommonEventHeader, eventFields: ByteString): ByteArray {
85         return createVesEvent(commonEventHeader, eventFields).toByteArray()
86     }
87
88     private fun createVesEvent(commonEventHeader: CommonEventHeader, payload: ByteString): VesEvent =
89             VesEvent.newBuilder()
90                     .setCommonEventHeader(commonEventHeader)
91                     .setEventFields(payload)
92                     .build()
93
94     private fun oversizedPayload() =
95             payloadGenerator.generateRawPayload(maxPayloadSizeBytes + 1)
96
97     private fun fixedPayload() =
98             payloadGenerator.generateRawPayload(MessageGenerator.FIXED_PAYLOAD_SIZE)
99
100     companion object {
101         private const val UNSUPPORTED_VERSION: Short = 2
102     }
103 }