Add stndDefined domain to HV-VES
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-test-utils / src / main / kotlin / org / onap / dcae / collectors / veshv / tests / utils / vesEvents.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-2021 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.tests.utils
21
22
23 import com.google.protobuf.ByteString
24 import com.google.protobuf.MessageLite
25 import org.onap.dcae.collectors.veshv.domain.ByteData
26 import org.onap.dcae.collectors.veshv.domain.PayloadContentType
27 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
28 import org.onap.dcae.collectors.veshv.domain.VesEventStndDefinedNamespace
29 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
30 import org.onap.ves.VesEventOuterClass
31 import org.onap.ves.VesEventOuterClass.CommonEventHeader
32 import org.onap.ves.VesEventOuterClass.CommonEventHeader.Priority
33 import java.time.Instant
34 import java.time.temporal.Temporal
35 import java.util.UUID.randomUUID
36
37 fun vesEvent(domain: VesEventDomain = VesEventDomain.PERF3GPP,
38              id: String = randomUUID().toString(),
39              eventFields: ByteString = ByteString.EMPTY,
40              vesEventListenerVersion: String = "7.0.2"
41 ): VesEventOuterClass.VesEvent = vesEvent(
42         commonHeader(domain, id, vesEventListenerVersion, lastEpochMicrosec = 100000005),
43         eventFields
44 )
45
46 fun vesEvent(commonEventHeader: CommonEventHeader,
47              eventFields: ByteString = ByteString.EMPTY): VesEventOuterClass.VesEvent =
48         VesEventOuterClass.VesEvent.newBuilder()
49                 .setCommonEventHeader(commonEventHeader)
50                 .setEventFields(eventFields)
51                 .build()
52
53 fun commonHeader(domain: VesEventDomain = VesEventDomain.PERF3GPP,
54                  id: String = randomUUID().toString(),
55                  vesEventListenerVersion: String = "7.0.2",
56                  priority: Priority = Priority.NORMAL,
57                  lastEpochMicrosec: Long = 100000005,
58                  stndDefinedNamespace: VesEventStndDefinedNamespace? = null): CommonEventHeader {
59     val builder = CommonEventHeader.newBuilder().setVersion("sample-version")
60             .setDomain(domain.domainName)
61             .setSequence(1)
62             .setPriority(priority)
63             .setEventId(id)
64             .setEventName("sample-event-name")
65             .setEventType("sample-event-type")
66             .setStartEpochMicrosec(100000000)
67             .setLastEpochMicrosec(lastEpochMicrosec)
68             .setNfNamingCode("sample-nf-naming-code")
69             .setNfcNamingCode("sample-nfc-naming-code")
70             .setNfVendorName("vendor-name")
71             .setReportingEntityId(ByteString.copyFromUtf8("sample-reporting-entity-id"))
72             .setReportingEntityName("sample-reporting-entity-name")
73             .setSourceId(ByteString.copyFromUtf8("sample-source-id"))
74             .setSourceName("sample-source-name")
75             .setTimeZoneOffset("+1")
76             .setVesEventListenerVersion(vesEventListenerVersion)
77
78     stndDefinedNamespace?.let {
79         builder.setStndDefinedNamespace(stndDefinedNamespace.stndDefinedNamespace)
80     }
81     return builder.build()
82 }
83
84
85 fun emptyWireProtocolFrame(): WireFrameMessage = wireProtocolFrameWithPayloadSize(0)
86
87
88 fun wireProtocolFrameWithPayloadSize(size: Int): WireFrameMessage = WireFrameMessage(
89         payload = ByteData(ByteArray(size)),
90         versionMajor = WireFrameMessage.SUPPORTED_VERSION_MAJOR,
91         versionMinor = WireFrameMessage.SUPPORTED_VERSION_MINOR,
92         payloadSize = size,
93         payloadType = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue
94 )
95
96 fun wireProtocolFrame(commonHeader: CommonEventHeader,
97                       eventFields: ByteString = ByteString.EMPTY,
98                       receivedAt: Temporal = Instant.now()): WireFrameMessage =
99         vesEventBytes(commonHeader, eventFields).let { payload ->
100             WireFrameMessage(
101                     payload = payload,
102                     versionMajor = WireFrameMessage.SUPPORTED_VERSION_MAJOR,
103                     versionMinor = WireFrameMessage.SUPPORTED_VERSION_MINOR,
104                     payloadSize = payload.size(),
105                     payloadType = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
106                     receivedAt = receivedAt
107             )
108         }
109
110 fun wireProtocolFrame(evt: VesEventOuterClass.VesEvent) =
111         WireFrameMessage(
112                 payload = ByteData(evt.toByteArray()),
113                 payloadSize = evt.serializedSize,
114                 payloadType = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
115                 versionMajor = WireFrameMessage.SUPPORTED_VERSION_MAJOR,
116                 versionMinor = WireFrameMessage.SUPPORTED_VERSION_MINOR
117         )
118
119 fun vesEventBytes(commonHeader: CommonEventHeader, eventFields: ByteString = ByteString.EMPTY): ByteData =
120         vesEvent(commonHeader, eventFields).toByteData()
121
122 fun MessageLite.toByteData(): ByteData = ByteData(toByteArray())