e9db716d2948140083fd9b4fa744f26127e77c72
[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.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.INVALID_GPB_DATA
30 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.INVALID_WIRE_FRAME
31 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.TOO_BIG_PAYLOAD
32 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType.VALID
33 import org.onap.ves.HVRanMeasFieldsV5.HVRanMeasFields.HVRanMeasPayload
34 import org.onap.ves.VesEventV5.VesEvent
35 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader
36 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain
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<PayloadWireFrameMessage> = Flux
48             .fromIterable(messageParameters)
49             .flatMap { createMessageFlux(it) }
50
51     private fun createMessageFlux(parameters: MessageParameters): Flux<PayloadWireFrameMessage> =
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): PayloadWireFrameMessage =
61             when (messageType) {
62                 VALID ->
63                     PayloadWireFrameMessage(vesEvent(commonEventHeader, payloadGenerator.generatePayload()))
64                 TOO_BIG_PAYLOAD ->
65                     PayloadWireFrameMessage(vesEvent(commonEventHeader, oversizedPayload()))
66                 INVALID_WIRE_FRAME -> {
67                     val payload = ByteData(vesEvent(commonEventHeader, payloadGenerator.generatePayload()))
68                     PayloadWireFrameMessage(
69                             payload,
70                             UNSUPPORTED_VERSION,
71                             PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
72                             payload.size())
73                 }
74                 INVALID_GPB_DATA ->
75                     PayloadWireFrameMessage("invalid vesEvent".toByteArray(Charset.defaultCharset()))
76             }
77
78     private fun vesEvent(commonEventHeader: CommonEventHeader, hvRanMeasPayload: HVRanMeasPayload): ByteArray {
79         return vesEvent(commonEventHeader, hvRanMeasPayload.toByteString())
80     }
81
82     private fun vesEvent(commonEventHeader: CommonEventHeader, hvRanMeasPayload: ByteString): ByteArray {
83         return createVesEvent(commonEventHeader, hvRanMeasPayload).toByteArray()
84     }
85
86     private fun createVesEvent(commonEventHeader: CommonEventHeader, payload: ByteString): VesEvent =
87             VesEvent.newBuilder()
88                     .setCommonEventHeader(commonEventHeader)
89                     .setHvRanMeasFields(payload)
90                     .build()
91
92     private fun oversizedPayload() =
93             payloadGenerator.generateRawPayload(PayloadWireFrameMessage.MAX_PAYLOAD_SIZE + 1)
94
95     companion object {
96         private const val UNSUPPORTED_VERSION: Short = 2
97     }
98 }