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