20c0f59233f19fab87492980dd3984e69e67a456
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-dcae-app-simulator / src / main / kotlin / org / onap / dcae / collectors / veshv / simulators / dcaeapp / impl / MessageStreamValidation.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.simulators.dcaeapp.impl
21
22 import arrow.core.getOrElse
23 import arrow.effects.IO
24 import arrow.effects.fix
25 import arrow.effects.instances.io.monadError.monadError
26 import arrow.instances.option.foldable.fold
27 import arrow.typeclasses.bindingCatch
28 import org.onap.dcae.collectors.veshv.domain.ByteData
29 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
30 import org.onap.dcae.collectors.veshv.utils.arrow.asIo
31 import org.onap.dcae.collectors.veshv.utils.logging.Logger
32 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageGenerator
33 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters
34 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser
35 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType
36 import org.onap.ves.VesEventOuterClass
37 import java.io.InputStream
38 import javax.json.Json
39
40 class MessageStreamValidation(
41         private val messageGenerator: MessageGenerator,
42         private val messageParametersParser: MessageParametersParser = MessageParametersParser.INSTANCE) {
43
44     fun validate(jsonDescription: InputStream, consumedMessages: List<ByteArray>): IO<Boolean> =
45             IO.monadError().bindingCatch {
46                 val messageParams = parseMessageParams(jsonDescription)
47                 logger.debug { "Parsed message parameters: $messageParams" }
48                 val expectedEvents = generateEvents(messageParams).bind()
49                 val actualEvents = decodeConsumedEvents(consumedMessages)
50                 if (shouldValidatePayloads(messageParams)) {
51                     expectedEvents == actualEvents
52                 } else {
53                     validateHeaders(actualEvents, expectedEvents)
54                 }
55             }.fix()
56
57     private fun parseMessageParams(input: InputStream): List<MessageParameters> {
58         val expectations = Json.createReader(input).readArray()
59         val messageParams = messageParametersParser.parse(expectations)
60
61         return messageParams.fold(
62                 {
63                     logger.warn { "Error while parsing message parameters: ${it::class.qualifiedName} : ${it.message}" }
64                     logger.debug { "Detailed stack trace: ${it}" }
65                     throw IllegalArgumentException("Parsing error: " + it.message)
66                 },
67                 {
68                     if (it.isEmpty()) {
69                         val message = "Message param list cannot be empty"
70                         logger.warn(message)
71                         throw IllegalArgumentException(message)
72                     }
73                     it
74                 }
75         )
76     }
77
78     private fun shouldValidatePayloads(parameters: List<MessageParameters>) =
79             parameters.all { it.messageType == MessageType.FIXED_PAYLOAD }
80
81     private fun validateHeaders(actual: List<VesEventOuterClass.VesEvent>,
82                                 expected: List<VesEventOuterClass.VesEvent>): Boolean {
83         val consumedHeaders = actual.map { it.commonEventHeader }
84         val generatedHeaders = expected.map { it.commonEventHeader }
85         return generatedHeaders == consumedHeaders
86     }
87
88     private fun generateEvents(parameters: List<MessageParameters>): IO<List<VesEventOuterClass.VesEvent>> =
89             messageGenerator.createMessageFlux(parameters)
90                     .map(WireFrameMessage::payload)
91                     .map(ByteData::unsafeAsArray)
92                     .map(VesEventOuterClass.VesEvent::parseFrom)
93                     .collectList()
94                     .asIo()
95
96     private fun decodeConsumedEvents(consumedMessages: List<ByteArray>) =
97             consumedMessages.map(VesEventOuterClass.VesEvent::parseFrom)
98
99     companion object {
100         private val logger = Logger(MessageStreamValidation::class)
101     }
102 }