Add metrics for dropped messages
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-test-utils / src / main / kotlin / org / onap / dcae / collectors / veshv / tests / utils / messages.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.tests.utils
21
22 import com.google.protobuf.ByteString
23 import io.netty.buffer.ByteBuf
24 import io.netty.buffer.ByteBufAllocator
25 import io.netty.buffer.PooledByteBufAllocator
26 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
27 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.OTHER
28 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
29 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage.Companion.RESERVED_BYTE_COUNT
30 import org.onap.ves.VesEventOuterClass.VesEvent
31 import java.util.UUID.randomUUID
32
33
34 val allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT
35
36 private fun validWireFrame() = allocator.buffer().run {
37     writeByte(0xAA)          // always 0xAA
38     writeByte(0x01)          // major version
39     writeByte(0x00)          // minor version
40     writeZero(RESERVED_BYTE_COUNT)  // reserved
41     writeShort(0x0001)  // content type = GPB
42 }
43
44 private fun invalidWireFrame() = allocator.buffer().run {
45     writeByte(0xAA)          // always 0xAA
46     writeByte(0x00)          // invalid major version
47     writeByte(0x00)          // minor version
48     writeZero(RESERVED_BYTE_COUNT)  // reserved
49     writeShort(0x0001)  // content type = GPB
50 }
51
52 fun garbageFrame(): ByteBuf = allocator.buffer().run {
53     writeBytes("the meaning of life is &@)(*_!".toByteArray())
54 }
55
56 fun vesWireFrameMessage(domain: VesEventDomain = OTHER,
57                         id: String = randomUUID().toString(),
58                         eventFields: ByteString = ByteString.EMPTY,
59                         vesEventListenerVersion: String = "7.0.2"): ByteBuf =
60         vesWireFrameMessage(vesEvent(domain, id, eventFields, vesEventListenerVersion))
61
62 fun vesWireFrameMessage(vesEvent: VesEvent): ByteBuf =
63         validWireFrame().run {
64             val gpb = vesEvent.toByteString().asReadOnlyByteBuffer()
65             writeInt(gpb.limit())  // ves event size in bytes
66             writeBytes(gpb)   // ves event as GPB bytes
67         }
68
69 fun messageWithInvalidWireFrameHeader(vesEvent: VesEvent = vesEvent()): ByteBuf =
70         invalidWireFrame().run {
71             val gpb = vesEvent.toByteString().asReadOnlyByteBuffer()
72             writeInt(gpb.limit())           // ves event size in bytes
73             writeBytes(gpb)            // ves event as GPB bytes
74         }
75
76 fun wireFrameMessageWithInvalidPayload(): ByteBuf =
77         validWireFrame().run {
78             val invalidGpb = "some random data".toByteArray(Charsets.UTF_8)
79             writeInt(invalidGpb.size)  // ves event size in bytes
80             writeBytes(invalidGpb)
81         }
82
83 fun messageWithPayloadOfSize(payloadSizeBytes: Int, domain: VesEventDomain = PERF3GPP): ByteBuf =
84         vesWireFrameMessage(
85                 domain = domain,
86                 eventFields = ByteString.copyFrom(ByteArray(payloadSizeBytes))
87         )
88
89 fun messageWithInvalidListenerVersion() = vesWireFrameMessage(vesEventListenerVersion = "invalid")