Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / 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.WireFrameMessage.Companion.RESERVED_BYTE_COUNT
27 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
28 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
29 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.OTHER
30
31 import java.util.UUID.randomUUID
32
33
34 val allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT
35
36 private fun ByteBuf.writeValidWireFrameHeaders() {
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 fun vesWireFrameMessage(domain: VesEventDomain = OTHER,
45                         id: String = randomUUID().toString()): ByteBuf =
46         allocator.buffer().run {
47             writeValidWireFrameHeaders()
48
49             val gpb = vesEvent(domain, id).toByteString().asReadOnlyByteBuffer()
50             writeInt(gpb.limit())  // ves event size in bytes
51             writeBytes(gpb)  // ves event as GPB bytes
52         }
53
54 fun wireFrameMessageWithInvalidPayload(): ByteBuf = allocator.buffer().run {
55     writeValidWireFrameHeaders()
56
57     val invalidGpb = "some random data".toByteArray(Charsets.UTF_8)
58     writeInt(invalidGpb.size)  // ves event size in bytes
59     writeBytes(invalidGpb)
60 }
61
62 fun garbageFrame(): ByteBuf = allocator.buffer().run {
63     writeBytes("the meaning of life is &@)(*_!".toByteArray())
64 }
65
66 fun invalidWireFrame(): ByteBuf = allocator.buffer().run {
67     writeByte(0xAA)
68     writeByte(0x01)   // version major
69     writeByte(0x01)   // version minor
70 }
71
72 fun vesMessageWithPayloadOfSize(payloadSizeBytes: Int, domain: VesEventDomain = PERF3GPP): ByteBuf =
73         allocator.buffer().run {
74             writeValidWireFrameHeaders()
75
76             val gpb = vesEvent(
77                     domain = domain,
78                     eventFields = ByteString.copyFrom(ByteArray(payloadSizeBytes))
79             ).toByteString().asReadOnlyByteBuffer()
80
81             writeInt(gpb.limit())  // ves event size in bytes
82             writeBytes(gpb)  // ves event as GPB bytes
83         }
84
85