f731e11c1a3e0f2bb28f59bdf89a0417f4845f56
[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.simulators.xnf.impl
21
22 import com.google.protobuf.ByteString
23 import org.onap.dcae.collectors.veshv.domain.WireFrame
24 import org.onap.dcae.collectors.veshv.simulators.xnf.config.MessageParameters
25 import org.onap.ves.VesEventV5.VesEvent
26 import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader
27 import reactor.core.publisher.Flux
28 import reactor.core.publisher.Mono
29 import javax.json.JsonObject
30
31 /**
32  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
33  * @since June 2018
34  */
35 class MessageFactory(private val payloadGenerator: PayloadGenerator) {
36
37     fun createMessageFlux(messageParameters: MessageParameters): Flux<WireFrame> =
38             Mono.fromCallable { createMessage(messageParameters.commonEventHeader) }.let {
39                 if (messageParameters.amount < 0)
40                     it.repeat()
41                 else
42                     it.repeat(messageParameters.amount)
43             }
44
45     fun parseCommonHeader(json: JsonObject): CommonEventHeader = CommonEventHeader.newBuilder()
46             .setVersion(json.getString("version"))
47             .setDomain(CommonEventHeader.Domain.forNumber(json.getInt("domain")))
48             .setSequence(json.getInt("sequence"))
49             .setPriority(CommonEventHeader.Priority.forNumber(json.getInt("priority")))
50             .setEventId(json.getString("eventId"))
51             .setEventName(json.getString("eventName"))
52             .setEventType(json.getString("eventType"))
53             .setStartEpochMicrosec(json.getJsonNumber("startEpochMicrosec").longValue())
54             .setLastEpochMicrosec(json.getJsonNumber("lastEpochMicrosec").longValue())
55             .setNfNamingCode(json.getString("nfNamingCode"))
56             .setNfcNamingCode(json.getString("nfcNamingCode"))
57             .setReportingEntityId(json.getString("reportingEntityId"))
58             .setReportingEntityName(ByteString.copyFromUtf8(json.getString("reportingEntityName")))
59             .setSourceId(ByteString.copyFromUtf8(json.getString("sourceId")))
60             .setSourceName(json.getString("sourceName"))
61             .build()
62
63
64     private fun createMessage(commonHeader: CommonEventHeader): WireFrame =
65             WireFrame(vesMessageBytes(commonHeader))
66
67
68     private fun vesMessageBytes(commonHeader: CommonEventHeader): ByteArray {
69         val msg = VesEvent.newBuilder()
70                 .setCommonEventHeader(commonHeader)
71                 .setHvRanMeasFields(PayloadGenerator().generatePayload().toByteString())
72                 .build()
73
74         return msg.toByteArray()
75     }
76
77     companion object {
78         val INSTANCE = MessageFactory(PayloadGenerator())
79     }
80 }