01e11665bb6512f7ef860dac3cd97d5d11ef887d
[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.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 import org.onap.ves.VesEventOuterClass.VesEvent
31
32 import java.util.UUID.randomUUID
33
34
35 val allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT
36
37 private fun ByteBuf.writeValidWireFrameHeaders() {
38     writeByte(0xAA)          // always 0xAA
39     writeByte(0x01)          // major version
40     writeByte(0x00)          // minor version
41     writeZero(RESERVED_BYTE_COUNT)  // reserved
42     writeShort(0x0001)       // content type = GPB
43 }
44
45 fun vesWireFrameMessage(domain: VesEventDomain = OTHER,
46                         id: String = randomUUID().toString(),
47                         eventFields: ByteString = ByteString.EMPTY): ByteBuf =
48         vesWireFrameMessage(vesEvent(domain, id, eventFields))
49
50 fun vesWireFrameMessage(vesEvent: VesEvent) =
51         allocator.buffer().run {
52             writeValidWireFrameHeaders()
53
54             val gpb = vesEvent.toByteString().asReadOnlyByteBuffer()
55             writeInt(gpb.limit())  // ves event size in bytes
56             writeBytes(gpb)  // ves event as GPB bytes
57         }
58
59 fun wireFrameMessageWithInvalidPayload(): ByteBuf = allocator.buffer().run {
60     writeValidWireFrameHeaders()
61
62     val invalidGpb = "some random data".toByteArray(Charsets.UTF_8)
63     writeInt(invalidGpb.size)  // ves event size in bytes
64     writeBytes(invalidGpb)
65 }
66
67 fun garbageFrame(): ByteBuf = allocator.buffer().run {
68     writeBytes("the meaning of life is &@)(*_!".toByteArray())
69 }
70
71 fun invalidWireFrame(): ByteBuf = allocator.buffer().run {
72     writeByte(0xAA)
73     writeByte(0x01)   // version major
74     writeByte(0x01)   // version minor
75 }
76
77 fun vesMessageWithPayloadOfSize(payloadSizeBytes: Int, domain: VesEventDomain = PERF3GPP): ByteBuf =
78         vesWireFrameMessage(
79                 domain = domain,
80                 eventFields = ByteString.copyFrom(ByteArray(payloadSizeBytes))
81         )
82
83