174a01fd1176ef76dba045d315874dba287a460a
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-2019 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 arrow.core.Either
23 import arrow.core.Option
24 import arrow.core.Try
25 import arrow.core.identity
26 import org.onap.dcae.collectors.veshv.utils.logging.Logger
27 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters
28 import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser
29 import org.onap.dcae.collectors.veshv.ves.message.generator.api.ParsingError
30 import org.onap.dcae.collectors.veshv.ves.message.generator.api.VesEventParameters
31 import org.onap.dcae.collectors.veshv.ves.message.generator.api.VesEventType
32 import org.onap.dcae.collectors.veshv.ves.message.generator.api.VesEventType.Companion.isVesEventType
33 import org.onap.dcae.collectors.veshv.ves.message.generator.api.WireFrameParameters
34 import org.onap.dcae.collectors.veshv.ves.message.generator.api.WireFrameType
35 import org.onap.dcae.collectors.veshv.ves.message.generator.api.WireFrameType.Companion.isWireFrameType
36 import org.onap.dcae.collectors.veshv.ves.message.generator.impl.vesevent.CommonEventHeaderParser
37 import javax.json.JsonArray
38 import javax.json.JsonObject
39 import javax.json.JsonValue
40
41 /**
42  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
43  * @since July 2018
44  */
45 internal class MessageParametersParserImpl(
46         private val commonEventHeaderParser: CommonEventHeaderParser = CommonEventHeaderParser()
47 ) : MessageParametersParser {
48
49     override fun parse(request: JsonArray): Either<ParsingError, List<MessageParameters>> =
50             Try { parseArray(request) }
51                     .toEither()
52                     .mapLeft { ex ->
53                         ParsingError(
54                                 ex.message ?: "Unable to parse message parameters",
55                                 Option.fromNullable(ex))
56                     }
57
58     private fun parseArray(array: JsonArray) = array
59             .map(JsonValue::asJsonObject)
60             .onEach { logger.info { "Parsing MessageParameters body: $it" } }
61             .map(::parseParameters)
62
63     private fun parseParameters(json: JsonObject): MessageParameters {
64         val messagesAmount = json.getJsonNumber("messagesAmount")?.longValue()
65                 ?: throw ParsingException("\"messagesAmount\" could not be parsed.")
66
67         val messageType = json.getString("messageType")
68
69         return when {
70             isVesEventType(messageType) ->
71                 constructVesEventParams(json, messageType, messagesAmount)
72             isWireFrameType(messageType) ->
73                 WireFrameParameters(WireFrameType.valueOf(messageType), messagesAmount)
74             else -> throw ParsingException("Invalid message type")
75         }
76     }
77
78     private fun constructVesEventParams(json: JsonObject,
79                                         messageType: String,
80                                         messagesAmount: Long): VesEventParameters =
81             commonEventHeaderParser
82                     .parse(json.getJsonObject("commonEventHeader"))
83                     .fold({ throw ParsingException("Invalid common header") }, ::identity)
84                     .let { VesEventParameters(it, VesEventType.valueOf(messageType), messagesAmount) }
85
86
87     private class ParsingException(message: String) : Exception(message)
88
89     companion object {
90         private val logger = Logger(MessageParametersParserImpl::class)
91     }
92 }
93
94